Skip to content

Commit

Permalink
[processing] fix expressions handling in ParameterString (fix #14834)
Browse files Browse the repository at this point in the history
Add initial set of tests for ParameterString
  • Loading branch information
alexbruy committed May 23, 2016
1 parent 9074b14 commit 2f5c3a5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
3 changes: 2 additions & 1 deletion python/plugins/processing/core/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions python/plugins/processing/gui/AlgorithmDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 25 additions & 1 deletion python/plugins/processing/tests/ParametersTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
ParameterFixedTable,
ParameterMultipleInput,
ParameterNumber,
ParameterPoint)
ParameterPoint,
ParameterString)

from qgis.core import (QgsRasterLayer,
QgsVectorLayer)
Expand Down Expand Up @@ -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.