def test_expressions_with_fame(self): """Test expression evaluation in a frame""" c = Context() c["foo"] = dict(a=1, b=2, bar="apples") c["top"] = 10 c["r"] = list(range(10)) tests = [("a+b", 3), (".top", 10), ("a+.top", 11), (".r.4+.top", 14)] with c.frame("foo"): for expression, result in tests: self.assertEqual(c.eval(expression), result)
def test_expressions_with_fame(self): """Test expression evaluation in a frame""" c = Context() c['foo'] = dict(a=1, b=2, bar="apples") c['top'] = 10 c['r'] = list(range(10)) tests = [("a+b", 3), (".top", 10), ("a+.top", 11), (".r.4+.top", 14)] with c.frame('foo'): for expression, result in tests: self.assertEqual(c.eval(expression), result)
def test_expression_filter(self): """Test filter evaluation""" c = Context() c['filter'] = dict(double=lambda v: v * 2, square=lambda v: v * v) c['data'] = dict(a=1, b=10, c=123) tests = [("3|filter.double", 6), ("3|.filter.double", 6), ("data.a + data.b|filter.double", 22), ("(data.a + data.b)|filter.double", 22), ("3|filter.square", 9), ("3|filter.double|filter.square", 36), ] for expression, result in tests: print(expression) expression_result = c.eval(expression) self.assertEqual(expression_result, result)
def test_expression_index(self): """Test the index operator""" c = Context() c["foo"] = {} c["baz"] = 2 c["foo.a"] = 10 c["foo.b"] = 20 c["foo.c"] = dict(inception="three levels") c["word"] = "apples" c["word2"] = c["word"] c["lt"] = "less than" class Obj(object): def __init__(self): self.n = 123 self.foo = ["Hello", "World", "!"] c["o"] = Obj() tests = [ ('"apples"[0]', "a"), ('"apples"[1]', "p"), ('"apples"[1+2]', "l"), ('"apples"[-1]', "s"), ('foo["a"]', 10), ('foo["b"]', 20), ('foo["c"]', dict(inception="three levels")), ('foo["c"]["inception"]', "three levels"), ('foo.c["inception"]', "three levels"), ('foo.c["inception"][1]', "h"), ('o["n"]', 123), ('o["foo"][1]', "World"), ] for expression, result in tests: print(expression) expression_result = c.eval(expression) self.assertEqual(expression_result, result)
def test_expression_index(self): """Test the index operator""" c = Context() c['foo'] = {} c['baz'] = 2 c['foo.a'] = 10 c['foo.b'] = 20 c['foo.c'] = dict(inception="three levels") c['word'] = 'apples' c['word2'] = c['word'] c['lt'] = "less than" class Obj(object): def __init__(self): self.n = 123 self.foo = ["Hello", "World", "!"] c['o'] = Obj() tests = [('"apples"[0]', 'a'), ('"apples"[1]', 'p'), ('"apples"[1+2]', 'l'), ('"apples"[-1]', 's'), ('foo["a"]', 10), ('foo["b"]', 20), ('foo["c"]', dict(inception="three levels")), ('foo["c"]["inception"]', "three levels"), ('foo.c["inception"]', "three levels"), ('foo.c["inception"][1]', "h"), ('o["n"]', 123), ('o["foo"][1]', "World"), ] for expression, result in tests: print(expression) expression_result = c.eval(expression) self.assertEqual(expression_result, result)
def test_expressions(self): """Test expression evaluation""" c = Context() c['foo'] = {} c['baz'] = 2 c['foo.a'] = 10 c['foo.b'] = 20 c['foo.c'] = dict(inception="three levels") c['word'] = 'apples' c['word2'] = c['word'] c['lt'] = "less than" tests = [('1', 1), ('123', 123), ('"1"', "1"), ("'1'", "1"), ('"\\""', '"'), ("'''1'''", "1"), ('"""1"""', "1"), ('100-5', 95), ('7//2', 3), ('1+1', 2), ('1+2+3', 6), ('2+3*2', 8), ('(2+3)*2', 10), ('foo.a', 10), ('$foo.a', 10), ('$lt', "less than"), ('foo.c.inception', "three levels"), #('foo.c.inception.:5 + " "+"little pigs"', "three little pigs"), #('foo.c.inception.::-1', "slevel eerht"), ('foo.a+foo.b', 30), ('.foo.a+.foo.b', 30), ('foo.a/2', 5), ('foo.a/4', 2.5), ('word*3', 'applesapplesapples'), ('word.2*3', 'ppp'), ('word+str:2', 'apples2'), ('word^="a"', True), ('word^="app"', True), ('word^="ppa"', False), ('word$="les"', True), ('word$="s"', True), ('2!=3', True), ('2>1', True), ('1<2', True), ('1>2', False), ('3<1', False), ('1==1', True), ('10>=10', True), ('9.9<=10', True), ('foo.a==10', True), ('foo.a=="a"', False), ('foo.a==\'a\'', False), ('3*2>5', True), ('2 gt 1', True), ('1 lt 2', True), ('1 gt 2', False), ('3 lt 1', False), ('10 gte 10', True), ('9.9 lte 10', True), ('3*2 gt 5', True), ('None', None), ('True', True), ('False', False), ('yes', True), ('no', False), ('int:"3"', 3), ('str:50', "50"), ('float:"2.5"', 2.5), ('bool:"test"', True), ('bool:1', True), ('bool:""', False), ('isint:5', True), ('isint:"5"', False), ('isnumber:2', True), ('isnumber:2.5', True), ('isnumber:"a"', False), ('isfloat:1.0', True), ('isfloat:1', False), ('isstr:1', False), ('isstr:"a"', True), ('isbool:True', True), ('isbool:False', True), ('isbool:(2+1)', False), ('isbool:bool:1', True), ('isbool:bool:0', True), ('len:word', 6), ('True and True', True), ('False and False', False), ('True or False', True), ('False or False', False), #('2>1 and word.-1=="s"', True), ('word=="apples"', True), ('1==2 or word=="apples"', True), ("'a' in 'apples'", True), ("'ppl' in 'apples'", True), ("word.1==word.2", True), ('word is word2', True), ("'index.html' fnmatches '*.html'", True), ("'foo/index.html' fnmatches '*.html'", True), ("'index.html' fnmatches '*.py'", False), ("'index.html' fnmatches '*.h??l'", True), ("'hello, world' matches /.*world/", True), ("'hello, will' matches /.*world/", False), ("'hello, world' matches '.*world'", True), ("'hello, will' matches '.*world'", False), ("'inception' in foo['c']", True), ("'inception' in (foo['c'])", True), ("exists:foo", True), ("exists:baz", True), ("exists:asdfsadf", False), ("missing:foo", False), ("missing:nobodyherebutuschickens", True), ("missing:yesterday", True), ("missing:foo.bar.baz", True), ("missing:andrew", True), ("'1' instr [1,2,3,4]", True), ("'5' instr [1,2,3,4]", False), ("'1' not instr [1,2,3,4]", False), ("'5' not instr [1,2,3,4]", True), ("1 in None", False), ("1 instr None", False), ('a=1', {'a': 1}), ('{"a":1}', {'a': 1}), ('[1,2,3]', [1, 2, 3]), ('[1,2,3,[4,5,6]]', [1, 2, 3, [4, 5, 6]]), ('[1,2,3,[4,5,6,[7,8,9]]]', [1, 2, 3, [4, 5, 6, [7, 8, 9]]]), ('[1]', [1]), ('[]', []), ] for expression, result in tests: print(expression) expression_result = c.eval(expression) self.assertEqual(expression_result, result)
def test_expressions(self): """Test expression evaluation""" c = Context() c["foo"] = {} c["baz"] = 2 c["foo.a"] = 10 c["foo.b"] = 20 c["foo.c"] = dict(inception="three levels") c["word"] = "apples" c["word2"] = c["word"] c["lt"] = "less than" class ChoiceTest(object): def __init__(self): self.choices = [] c["choicetest"] = ChoiceTest() class Obj(object): def __init__(self, id): self.id = id c["objects"] = [Obj(1), Obj(2), Obj(3)] tests = [ ("1", 1), ("123", 123), ('"1"', "1"), ("'1'", "1"), ('"\\""', '"'), ("'''1'''", "1"), ('"""1"""', "1"), ("100-5", 95), ("7//2", 3), ("1+1", 2), ("1+2+3", 6), ("2+3*2", 8), ("(2+3)*2", 10), ("foo.a", 10), ("$foo.a", 10), ("$lt", "less than"), ("foo.c.inception", "three levels"), # ('foo.c.inception.:5 + " "+"little pigs"', "three little pigs"), # ('foo.c.inception.::-1', "slevel eerht"), ("foo.a+foo.b", 30), (".foo.a+.foo.b", 30), ("foo.a/2", 5), ("foo.a/4", 2.5), ("word*3", "applesapplesapples"), ("word.2*3", "ppp"), ("word+str:2", "apples2"), ('word^="a"', True), ('word^="app"', True), ('word^="ppa"', False), ('word$="les"', True), ('word$="s"', True), ("2!=3", True), ("2>1", True), ("1<2", True), ("1>2", False), ("3<1", False), ("1==1", True), ("10>=10", True), ("9.9<=10", True), ("foo.a==10", True), ('foo.a=="a"', False), ("foo.a=='a'", False), ("3*2>5", True), ("2 gt 1", True), ("1 lt 2", True), ("1 gt 2", False), ("3 lt 1", False), ("10 gte 10", True), ("9.9 lte 10", True), ("3*2 gt 5", True), ("None", None), ("True", True), ("False", False), ("yes", True), ("no", False), ('int:"3"', 3), ("str:50", "50"), ('float:"2.5"', 2.5), ('bool:"test"', True), ("bool:1", True), ('bool:""', False), ("isint:5", True), ('isint:"5"', False), ("isnumber:2", True), ("isnumber:2.5", True), ('isnumber:"a"', False), ("isfloat:1.0", True), ("isfloat:1", False), ("isstr:1", False), ('isstr:"a"', True), ("isbool:True", True), ("isbool:False", True), ("isbool:(2+1)", False), ("isbool:bool:1", True), ("isbool:bool:0", True), ("len:word", 6), ("True and True", True), ("False and False", False), ("True or False", True), ("False or False", False), # ('2>1 and word.-1=="s"', True), ('word=="apples"', True), ('1==2 or word=="apples"', True), ("'a' in 'apples'", True), ("'ppl' in 'apples'", True), ("word.1==word.2", True), ("word is word2", True), ("'index.html' fnmatches '*.html'", True), ("'foo/index.html' fnmatches '*.html'", True), ("'index.html' fnmatches '*.py'", False), ("'index.html' fnmatches '*.h??l'", True), ("'hello, world' matches /.*world/", True), ("'hello, will' matches /.*world/", False), ("'hello, world' matches '.*world'", True), ("'hello, will' matches '.*world'", False), ("'inception' in foo['c']", True), ("'inception' in (foo['c'])", True), ("exists:foo", True), ("exists:baz", True), ("exists:asdfsadf", False), ("missing:foo", False), ("missing:nobodyherebutuschickens", True), ("missing:yesterday", True), ("missing:foo.bar.baz", True), ("missing:andrew", True), ("'1' instr [1,2,3,4]", True), ("'5' instr [1,2,3,4]", False), ("'1' not instr [1,2,3,4]", False), ("'5' not instr [1,2,3,4]", True), ("1 in None", False), ("1 instr None", False), ("a=1", {"a": 1}), ('{"a":1}', {"a": 1}), ("[1,2,3]", [1, 2, 3]), ("[1,2,3,[4,5,6]]", [1, 2, 3, [4, 5, 6]]), ("[1,2,3,[4,5,6,[7,8,9]]]", [1, 2, 3, [4, 5, 6, [7, 8, 9]]]), ("[1]", [1]), ("[]", []), ("d:'5'", 5), ("d:'5' + 1", 6), ("d:'5' + d:'1'", 6), ("debug:d:5", "d:'5'"), ("filesize:1024", "1.0 KB"), ("abs:-3.14", 3.14), ('basename:"/foo/bar/baz"', "baz"), ('bool:""', False), ('capitalize:"hello"', "Hello"), ("ceil:3.14", 4), ("choices:choicetest", []), ("chain:[[1, 2], [3, 4]]", [1, 2, 3, 4]), ("chr:65", "A"), ("collect:[['hello', 'world'], 0]", ["h", "w"]), ( "sorted:items:collectmap:[['hello', 'world'], 0]", [("h", "hello"), ("w", "world")], ), ("collectids:objects", [1, 2, 3]), ("commalist:['hello', 'world']", "hello,world"), ("commaspacelist:['hello', 'world']", "hello, world"), ("'hello\\nworld'", "hello\nworld"), (r"'you can \"quote me\" on that'", 'you can "quote me" on that'), ("'\\\\'", "\\"), ("'helloworld'[1]", "e"), ("'helloworld'[-1]", "d"), ("'helloworld'[:2]", "he"), ("'helloworld'[2:4]", "ll"), ("'helloworld'[::-1]", "dlrowolleh"), ] for expression, result in tests: print(expression, result) expression_result = c.eval(expression) print("\t", expression_result) self.assertEqual(expression_result, result)
def test_expressions(self): """Test expression evaluation""" c = Context() c['foo'] = {} c['baz'] = 2 c['foo.a'] = 10 c['foo.b'] = 20 c['foo.c'] = dict(inception="three levels") c['word'] = 'apples' c['word2'] = c['word'] c['lt'] = "less than" class ChoiceTest(object): def __init__(self): self.choices = [] c['choicetest'] = ChoiceTest() class Obj(object): def __init__(self, id): self.id = id c['objects'] = [Obj(1), Obj(2), Obj(3)] tests = [ ('1', 1), ('123', 123), ('"1"', "1"), ("'1'", "1"), ('"\\""', '"'), ("'''1'''", "1"), ('"""1"""', "1"), ('100-5', 95), ('7//2', 3), ('1+1', 2), ('1+2+3', 6), ('2+3*2', 8), ('(2+3)*2', 10), ('foo.a', 10), ('$foo.a', 10), ('$lt', "less than"), ('foo.c.inception', "three levels"), #('foo.c.inception.:5 + " "+"little pigs"', "three little pigs"), #('foo.c.inception.::-1', "slevel eerht"), ('foo.a+foo.b', 30), ('.foo.a+.foo.b', 30), ('foo.a/2', 5), ('foo.a/4', 2.5), ('word*3', 'applesapplesapples'), ('word.2*3', 'ppp'), ('word+str:2', 'apples2'), ('word^="a"', True), ('word^="app"', True), ('word^="ppa"', False), ('word$="les"', True), ('word$="s"', True), ('2!=3', True), ('2>1', True), ('1<2', True), ('1>2', False), ('3<1', False), ('1==1', True), ('10>=10', True), ('9.9<=10', True), ('foo.a==10', True), ('foo.a=="a"', False), ('foo.a==\'a\'', False), ('3*2>5', True), ('2 gt 1', True), ('1 lt 2', True), ('1 gt 2', False), ('3 lt 1', False), ('10 gte 10', True), ('9.9 lte 10', True), ('3*2 gt 5', True), ('None', None), ('True', True), ('False', False), ('yes', True), ('no', False), ('int:"3"', 3), ('str:50', "50"), ('float:"2.5"', 2.5), ('bool:"test"', True), ('bool:1', True), ('bool:""', False), ('isint:5', True), ('isint:"5"', False), ('isnumber:2', True), ('isnumber:2.5', True), ('isnumber:"a"', False), ('isfloat:1.0', True), ('isfloat:1', False), ('isstr:1', False), ('isstr:"a"', True), ('isbool:True', True), ('isbool:False', True), ('isbool:(2+1)', False), ('isbool:bool:1', True), ('isbool:bool:0', True), ('len:word', 6), ('True and True', True), ('False and False', False), ('True or False', True), ('False or False', False), #('2>1 and word.-1=="s"', True), ('word=="apples"', True), ('1==2 or word=="apples"', True), ("'a' in 'apples'", True), ("'ppl' in 'apples'", True), ("word.1==word.2", True), ('word is word2', True), ("'index.html' fnmatches '*.html'", True), ("'foo/index.html' fnmatches '*.html'", True), ("'index.html' fnmatches '*.py'", False), ("'index.html' fnmatches '*.h??l'", True), ("'hello, world' matches /.*world/", True), ("'hello, will' matches /.*world/", False), ("'hello, world' matches '.*world'", True), ("'hello, will' matches '.*world'", False), ("'inception' in foo['c']", True), ("'inception' in (foo['c'])", True), ("exists:foo", True), ("exists:baz", True), ("exists:asdfsadf", False), ("missing:foo", False), ("missing:nobodyherebutuschickens", True), ("missing:yesterday", True), ("missing:foo.bar.baz", True), ("missing:andrew", True), ("'1' instr [1,2,3,4]", True), ("'5' instr [1,2,3,4]", False), ("'1' not instr [1,2,3,4]", False), ("'5' not instr [1,2,3,4]", True), ("1 in None", False), ("1 instr None", False), ('a=1', { 'a': 1 }), ('{"a":1}', { 'a': 1 }), ('[1,2,3]', [1, 2, 3]), ('[1,2,3,[4,5,6]]', [1, 2, 3, [4, 5, 6]]), ('[1,2,3,[4,5,6,[7,8,9]]]', [1, 2, 3, [4, 5, 6, [7, 8, 9]]]), ('[1]', [1]), ('[]', []), ("d:'5'", 5), ("d:'5' + 1", 6), ("d:'5' + d:'1'", 6), ('debug:d:5', "d:'5'"), ('filesize:1024', '1.0 KB'), ('abs:-3.14', 3.14), ('basename:"/foo/bar/baz"', "baz"), ('bool:""', False), ('capitalize:"hello"', 'Hello'), ('ceil:3.14', 4), ('choices:choicetest', []), ('chain:[[1, 2], [3, 4]]', [1, 2, 3, 4]), ('chr:65', 'A'), ("collect:[['hello', 'world'], 0]", ['h', 'w']), ("sorted:items:collectmap:[['hello', 'world'], 0]", [('h', 'hello'), ('w', 'world')]), ("collectids:objects", [1, 2, 3]), ("commalist:['hello', 'world']", "hello,world"), ("commaspacelist:['hello', 'world']", "hello, world"), ("'hello\\nworld'", "hello\nworld"), (r"'you can \"quote me\" on that'", 'you can "quote me" on that'), ("'\\\\'", "\\"), ("'helloworld'[1]", 'e'), ("'helloworld'[-1]", 'd'), ("'helloworld'[:2]", "he"), ("'helloworld'[2:4]", "ll"), ("'helloworld'[::-1]", "dlrowolleh") ] for expression, result in tests: print(expression, result) expression_result = c.eval(expression) print("\t", expression_result) self.assertEqual(expression_result, result)
def test_expressions(self): """Test expression evaluation""" c = Context() c['foo'] = {} c['baz'] = 2 c['foo.a'] = 10 c['foo.b'] = 20 c['foo.c'] = dict(inception="three levels") c['word'] = 'apples' c['word2'] = c['word'] c['lt'] = "less than" tests = [('1', 1), ('123', 123), ('"1"', "1"), ("'1'", "1"), ('"\\""', '"'), ("'''1'''", "1"), ('"""1"""', "1"), ('100-5', 95), ('7//2', 3), ('1+1', 2), ('1+2+3', 6), ('2+3*2', 8), ('(2+3)*2', 10), ('foo.a', 10), ('$foo.a', 10), ('$lt', "less than"), ('foo.c.inception', "three levels"), #('foo.c.inception.:5 + " "+"little pigs"', "three little pigs"), #('foo.c.inception.::-1', "slevel eerht"), ('foo.a+foo.b', 30), ('.foo.a+.foo.b', 30), ('foo.a/2', 5), ('foo.a/4', 2.5), ('word*3', 'applesapplesapples'), ('word.2*3', 'ppp'), ('word+str:2', 'apples2'), ('word^="a"', True), ('word^="app"', True), ('word^="ppa"', False), ('word$="les"', True), ('word$="s"', True), ('2!=3', True), ('2>1', True), ('1<2', True), ('1>2', False), ('3<1', False), ('1==1', True), ('10>=10', True), ('9.9<=10', True), ('foo.a==10', True), ('foo.a=="a"', False), ('foo.a==\'a\'', False), ('3*2>5', True), ('2 gt 1', True), ('1 lt 2', True), ('1 gt 2', False), ('3 lt 1', False), ('10 gte 10', True), ('9.9 lte 10', True), ('3*2 gt 5', True), ('None', None), ('True', True), ('False', False), ('yes', True), ('no', False), ('int:"3"', 3), ('str:50', "50"), ('float:"2.5"', 2.5), ('bool:"test"', True), ('bool:1', True), ('bool:""', False), ('isint:5', True), ('isint:"5"', False), ('isnumber:2', True), ('isnumber:2.5', True), ('isnumber:"a"', False), ('isfloat:1.0', True), ('isfloat:1', False), ('isstr:1', False), ('isstr:"a"', True), ('isbool:True', True), ('isbool:False', True), ('isbool:(2+1)', False), ('isbool:bool:1', True), ('isbool:bool:0', True), ('len:word', 6), ('True and True', True), ('False and False', False), ('True or False', True), ('False or False', False), #('2>1 and word.-1=="s"', True), ('word=="apples"', True), ('1==2 or word=="apples"', True), ("'a' in 'apples'", True), ("'ppl' in 'apples'", True), ("word.1==word.2", True), ('word is word2', True), ("'index.html' fnmatches '*.html'", True), ("'foo/index.html' fnmatches '*.html'", True), ("'index.html' fnmatches '*.py'", False), ("'index.html' fnmatches '*.h??l'", True), ("'hello, world' matches /.*world/", True), ("'hello, will' matches /.*world/", False), ("'hello, world' matches '.*world'", True), ("'hello, will' matches '.*world'", False), ("'inception' in foo['c']", True), ("'inception' in (foo['c'])", True), ("exists:foo", True), ("exists:baz", True), ("exists:asdfsadf", False), ("missing:foo", False), ("missing:nobodyherebutuschickens", True), ("missing:yesterday", True), ("missing:foo.bar.baz", True), ("missing:andrew", True), ("'1' instr [1,2,3,4]", True), ("'5' instr [1,2,3,4]", False), ("'1' not instr [1,2,3,4]", False), ("'5' not instr [1,2,3,4]", True), ("1 in None", False), ("1 instr None", False), ('a=1', {'a': 1}), ('{"a":1}', {'a': 1}), ('[1,2,3]', [1, 2, 3]), ('[1,2,3,[4,5,6]]', [1, 2, 3, [4, 5, 6]]), ('[1,2,3,[4,5,6,[7,8,9]]]', [1, 2, 3, [4, 5, 6, [7, 8, 9]]]), ('[1]', [1]), ('[]', []), ("d:'5'", 5), ("d:'5' + 1", 6), ("d:'5' + d:'1'", 6), ('debug:d:5', "d:'5'") ] for expression, result in tests: print(expression, result) expression_result = c.eval(expression) print("\t", expression_result) self.assertEqual(expression_result, result)