Skip to content
Permalink
Browse files

[processing] fix expressions handling in ParameterString (fix #14834)

Add initial set of tests for ParameterString
  • Loading branch information
alexbruy committed May 23, 2016
1 parent 9074b14 commit 2f5c3a570a071923e80874a3df5dc4aaeb5dbafc
@@ -709,9 +709,10 @@ class ParameterString(Parameter):
ESCAPED_NEWLINE = '\\n'

def __init__(self, name='', description='', default=None, multiline=False,
optional=False):
optional=False, evaluateExpressions=False):
Parameter.__init__(self, name, description, default, optional)
self.multiline = parseBool(multiline)
self.evaluateExpressions = parseBool(evaluateExpressions)

def setValue(self, obj):
if obj is None:
@@ -169,14 +169,16 @@ def setParamValue(self, param, widget, alg=None):
return param.setValue(widget.getValue())
elif isinstance(param, ParameterString):
if param.multiline:
return param.setValue(unicode(widget.toPlainText()))
text = unicode(widget.toPlainText())
else:
text = widget.text()

if param.evaluateExpressions:
try:
text = self.evaluateExpression(text)
except:
pass
return param.setValue(text)
return param.setValue(text)
elif isinstance(param, ParameterGeometryPredicate):
return param.setValue(widget.value())
else:
@@ -36,7 +36,8 @@
ParameterFixedTable,
ParameterMultipleInput,
ParameterNumber,
ParameterPoint)
ParameterPoint,
ParameterString)

from qgis.core import (QgsRasterLayer,
QgsVectorLayer)
@@ -394,5 +395,28 @@ def testOptional(self):
self.assertEqual(requiredParameter.value, 5)


class ParameterStringTest(unittest.TestCase):

def testSetValue(self):
parameter = ParameterString('myName', 'myDescription')
self.assertTrue(parameter.setValue('test'))
self.assertEqual(parameter.value, 'test')

def testOptional(self):
optionalParameter = ParameterString('myName', 'myDesc', default='test', optional=True)
self.assertEqual(optionalParameter.value, 'test')
optionalParameter.setValue('check')
self.assertEqual(optionalParameter.value, 'check')
self.assertTrue(optionalParameter.setValue(None))
self.assertEqual(optionalParameter.value, None)

requiredParameter = ParameterCrs('myName', 'myDesc', default='test', optional=False)
self.assertEqual(requiredParameter.value, 'test')
requiredParameter.setValue('check')
self.assertEqual(requiredParameter.value, 'check')
self.assertFalse(requiredParameter.setValue(None))
self.assertEqual(requiredParameter.value, 'check')


if __name__ == '__main__':
unittest.main()

0 comments on commit 2f5c3a5

Please sign in to comment.
You can’t perform that action at this time.