@@ -32,6 +32,9 @@ def test_parser(self):
32
32
33
33
# but '"a b"' can *not* be used as a variable
34
34
self .assertEqual (parser .parse ('"a b"*2' ).evaluate ({'"a b"' : 2 }), "a ba b" )
35
+ # unless parse configured to allow double quoted variables (i.e. allow multi-word vars)
36
+ parser2 = Parser (string_literal_quotes = ("'" )) # only single, not double!
37
+ self .assertEqual (parser2 .parse ('"a b"*2' ).evaluate ({'"a b"' :2 }),4 )
35
38
36
39
# evaluate
37
40
self .assertExactEqual (parser .parse ('1' ).evaluate ({}), 1 )
@@ -215,15 +218,31 @@ def test_custom_functions_with_inline_strings(self):
215
218
expr = parser .parse ("func(1, \" func(2, 4)\" )" )
216
219
self .assertEqual (expr .variables (), ['func' ])
217
220
221
+ expr = parser .parse ("func(1, 'func(2, 4)')" )
222
+ self .assertEqual (expr .variables (), ['func' ])
223
+
224
+ parser2 = Parser (string_literal_quotes = ("'" ))
225
+ expr = parser2 .parse ("func(1, \" func(2, 4)\" )" )
226
+ self .assertEqual (expr .variables (), ['func' , "\" func(2, 4)\" " ])
227
+
228
+ expr = parser2 .parse ("func(1, 'func(2, 4)')" )
229
+ self .assertEqual (expr .variables (), ['func' ])
230
+
218
231
def test_custom_functions_substitute_strings (self ):
219
232
def func (var , str ):
220
233
if str == "custom text" :
221
234
return 1
235
+ if str == "foo" :
236
+ return 2
222
237
return 0
223
238
224
239
parser = Parser ()
225
240
expr = parser .parse ("func(1, \" custom text\" )" )
226
241
self .assertEqual (expr .evaluate ({"func" : func }), 1 )
242
+
243
+ parser = Parser (string_literal_quotes = ("'" ))
244
+ expr = parser .parse ("func(1, \" custom text\" )" )
245
+ self .assertEqual (expr .evaluate ({"func" : func , "\" custom text\" " : "foo" }), 2 )
227
246
228
247
def test_decimals (self ):
229
248
parser = Parser ()
0 commit comments