def test_arithmetic_pow(): result = parse(['>', ['^', ['get', 'size'], 2], 100]) assert result == ast.GreaterThan( ast.Function( 'pow', [ast.Attribute('size'), 2], ), 100)
def test_function_attr_string_arg(): result = parse( { "op": "eq", "args": [ {"property": "attr"}, { "function": { "name": "myfunc", "arguments": [{"property": "other_attr"}, "abc"], } }, ], } ) assert result == ast.Equal( ast.Attribute("attr"), ast.Function( "myfunc", [ ast.Attribute("other_attr"), "abc", ], ), )
def test_function_single_arg(): result = parse('attr = myfunc(1)') assert result == ast.Equal( ast.Attribute('attr'), ast.Function('myfunc', [ 1, ]), )
def test_function(): def myadder(a, b): return a + b result = optimize(parse("attr = myadder(1, 2)"), {"myadder": myadder}) assert result == ast.Equal( ast.Attribute('attr'), 3, ) # can't optimize a function referencing an attribute result = optimize(parse("attr = myadder(other, 2)"), {"myadder": myadder}) assert result == ast.Equal( ast.Attribute('attr'), ast.Function( "myadder", [ ast.Attribute("other"), 2 ] ) ) # can't optimize a function with a nested reference to an attribute result = optimize( parse("attr = myadder(other + 2, 2)"), {"myadder": myadder} ) assert result == ast.Equal( ast.Attribute('attr'), ast.Function( "myadder", [ ast.Add(ast.Attribute("other"), 2), 2 ] ) ) # can't optimize an unknown functions result = optimize(parse("attr = unkown(1, 2)"), {"myadder": myadder}) assert result == ast.Equal( ast.Attribute('attr'), ast.Function( "unkown", [ 1, 2, ] ) )
def test_function_attr_string_arg(): result = parse('attr = myfunc(other_attr, \'abc\')') assert result == ast.Equal( ast.Attribute('attr'), ast.Function('myfunc', [ ast.Attribute('other_attr'), "abc", ]), )
def test_arithmetic_abs(): result = parse(['>', ['abs', ['get', 'delta']], 1]) assert result == ast.GreaterThan( ast.Function( 'abs', [ ast.Attribute('delta'), ], ), 1)
def test_arithmetic_ceil(): result = parse(['==', ['ceil', ['get', 'age']], 42]) assert result == ast.Equal( ast.Function( 'ceil', [ ast.Attribute('age'), ], ), 42)
def test_arithmetic_modulo(): result = parse(['==', ['get', 'attr'], ['%', 3, 7]]) assert result == ast.Equal( ast.Attribute('attr'), ast.Function( 'mod', [3, 7], ), )
def test_arithmetic_max(): result = parse(['>', ['max', ['get', 'wins'], ['get', 'ties']], 10]) assert result == ast.GreaterThan( ast.Function( 'max', [ ast.Attribute('wins'), ast.Attribute('ties'), ], ), 10)
def test_function_no_arg(): result = parse( { "op": "eq", "args": [ {"property": "attr"}, {"function": {"name": "myfunc", "arguments": []}}, ], } ) assert result == ast.Equal( ast.Attribute("attr"), ast.Function("myfunc", []), )
def test_function_single_arg(): result = parse({ "eq": [ {"property": "attr"}, { "function": { "name": "myfunc", "arguments": [1] } } ] }) assert result == ast.Equal( ast.Attribute('attr'), ast.Function( 'myfunc', [1], ), )
def test_function_attr_string_arg(): result = parse({ "eq": [ {"property": "attr"}, { "function": { "name": "myfunc", "arguments": [ {"property": "other_attr"}, "abc" ] } } ] }) assert result == ast.Equal( ast.Attribute('attr'), ast.Function( 'myfunc', [ ast.Attribute('other_attr'), "abc", ] ), )