def test_getattribute(): sym = Symbol().foo expr = "symbol.foo" p_expr = ast.parse(expr, mode='eval') c_expr = compile(ast.fix_missing_locations(p_expr), '', 'eval') sym2 = copy_chainable(sym) c_sym = compile_sym(sym) c_sym2 = compile_sym(sym2) assert ast.dump(object.__getattribute__(sym, "ast")) == ast.dump(object.__getattribute__(sym2, "ast")) assert ast.dump(p_expr.body) == ast.dump(object.__getattribute__(sym, "ast"))
def test_setattr(): sym = Symbol() sym.foo = 5 expr = "symbol.foo = 5" p_expr = ast.parse(expr, mode='exec') c_expr = compile(ast.fix_missing_locations(p_expr), '', 'exec') c_sym = compile_sym(sym) assert ast.dump(p_expr) == ast.dump(ast.Module([sym_getattr(sym_getattr(sym, "prior")[0], "ast")]))
def test_div(): x = 4 symbol = 4 sym = Symbol() / 2 expr = "x / 2" p_expr = ast.parse(expr, mode='eval') c_expr = compile(ast.fix_missing_locations(p_expr), '', 'eval') c_sym = compile_sym(sym) x = eval(c_expr) symbol = eval(c_sym) assert symbol == 4 / 2 assert x == symbol
def test_lshift(): x = 1 symbol = 1 sym = Symbol() << 1 expr = "x << 1" p_expr = ast.parse(expr, mode='eval') c_expr = compile(ast.fix_missing_locations(p_expr), '', 'eval') c_sym = compile_sym(sym) x = eval(c_expr) symbol = eval(c_sym) assert symbol == 1 << 1 assert x == symbol
def test_or(): x = False symbol = False sym = Symbol() | True expr = "x | True" p_expr = ast.parse(expr, mode='eval') c_expr = compile(ast.fix_missing_locations(p_expr), '', 'eval') c_sym = compile_sym(sym) x = eval(c_expr) symbol = eval(c_sym) assert symbol == True assert x == symbol
def test_ge(): x = 1 symbol = 1 sym = Symbol() >= 1 expr = "x >= 1" p_expr = ast.parse(expr, mode='eval') c_expr = compile(ast.fix_missing_locations(p_expr), '', 'eval') c_sym = compile_sym(sym) x = eval(c_expr) symbol = eval(c_sym) assert symbol == True assert x == symbol
def test_pow(): x = 2 symbol = 2 sym = Symbol() ** 2 expr = "x ** 2" p_expr = ast.parse(expr, mode='eval') c_expr = compile(ast.fix_missing_locations(p_expr), '', 'eval') c_sym = compile_sym(sym) x = eval(c_expr) symbol = eval(c_sym) assert symbol == 4 assert x == symbol
def test_pos(): x = 1 symbol = 1 sym = +Symbol() expr = "+x" p_expr = ast.parse(expr, mode='eval') c_expr = compile(ast.fix_missing_locations(p_expr), '', 'eval') c_sym = compile_sym(sym) x = eval(c_expr) symbol = eval(c_sym) assert symbol == 1 assert x == symbol
def test_irshift(): x = 1 symbol = 1 sym = Symbol() sym >>= 1 expr = "x >>= 1" p_expr = ast.parse(expr, mode='exec') c_expr = compile(ast.fix_missing_locations(p_expr), '', 'exec') c_sym = compile_sym(sym) exec c_expr exec c_sym assert symbol == 1 >> 1 assert x == symbol
def test_ipow(): x = 2 symbol = 2 sym = Symbol() sym **= 2 expr = "x **= 2" p_expr = ast.parse(expr, mode='exec') c_expr = compile(ast.fix_missing_locations(p_expr), '', 'exec') c_sym = compile_sym(sym) exec c_expr exec c_sym assert symbol == 4 assert x == symbol
def test_imod(): x = 4 symbol = 4 sym = Symbol() sym %= 2 expr = "x %= 2" p_expr = ast.parse(expr, mode='exec') c_expr = compile(ast.fix_missing_locations(p_expr), '', 'exec') c_sym = compile_sym(sym) exec c_expr exec c_sym assert symbol == 0 assert x == symbol
def test_ifloordiv(): x = 4 symbol = 4 sym = Symbol() sym //= 3 expr = "x //= 3" p_expr = ast.parse(expr, mode='exec') c_expr = compile(ast.fix_missing_locations(p_expr), '', 'exec') c_sym = compile_sym(sym) exec c_expr exec c_sym assert symbol == 4 // 3 assert x == symbol
def test_add_other_sym(): x = 4 y = 5 symbol_x = 4 symbol_y = 3 sym = Symbol(name="symbol_x") + (Symbol(name="symbol_y") + 2) expr = "x + y" p_expr = ast.parse(expr, mode='eval') c_expr = compile(ast.fix_missing_locations(p_expr), '', 'eval') c_sym = compile_sym(sym) x = eval(c_expr) symbol = eval(c_sym) assert symbol == symbol_x + symbol_y + 2 assert x == symbol
def test_getitem(): x = [1, 2, 3, 4, 5] symbol = [1, 2, 3, 4, 5] sym1 = Symbol()[0] sym2 = Symbol()[1:-1] expr1 = "x[0]" expr2 = "x[1:-1]" p_expr1 = ast.parse(expr1, mode='eval') p_expr2 = ast.parse(expr2, mode='eval') c_expr1 = compile(ast.fix_missing_locations(p_expr1), '', 'eval') c_expr2 = compile(ast.fix_missing_locations(p_expr2), '', 'eval') c_sym1 = compile_sym(sym1) c_sym2 = compile_sym(sym2) x = eval(c_expr1) symbol = eval(c_sym1) assert symbol == 1 assert x == symbol x = [1, 2, 3, 4, 5] symbol = [1, 2, 3, 4, 5] x = eval(c_expr2) symbol = eval(c_sym2) assert symbol == [2, 3, 4] assert x == symbol
def test_add_other_sym_stmt(): x = 4 y = 5 symbol_x = 4 symbol_y = 3 sym_ = Symbol(name="symbol_y") sym_ += 2 sym = Symbol(name="symbol_x") + sym_ expr = "x + y" p_expr = ast.parse(expr, mode='eval') c_expr = compile(ast.fix_missing_locations(p_expr), '', 'eval') c_sym = compile_sym(sym) x = eval(c_expr) exec(c_sym) assert symbol_y == 5
def test_call(): def x(one, two, three): return one + two + three symbol = x one_ = "one" two_ = "two" three_ = [] four_ = {} sym = Symbol()(1, two=2, three=3) expr = "x(1, two=2, three=3)" p_expr = ast.parse(expr, mode='eval') c_expr = compile(ast.fix_missing_locations(p_expr), '', 'eval') c_sym = compile_sym(sym) x = eval(c_expr) symbol = eval(c_sym) assert x == symbol