def test_repr(self): # Leaves assert repr(Atom(1)) == "Atom(1)" assert repr(Name("x")) == "Name('x')" # Operators assert (repr( BinOp("+", Name("x"), Name("y"))) == "BinOp(Op.ADD, Name('x'), Name('y'))")
def test_source(self): # Simple statements assert Return(Name("x")).source() == "return x" assert Del(Name("x")).source() == "del x" assert Cmd.Break().source() == "break" assert Cmd.Continue().source() == "continue" fn = Function(Name("fn"), Tree("args", [Name("x")]), Block([Return(Name("x"))])) assert fn.source() == "def fn(x):\n return x\n"
def test_binary_operators(self): expr = BinOp("+", Name("x"), Name("y")) assert expr.tag == expr.op == expr.operators.ADD assert expr.lhs == Name("x") assert expr.rhs == Name("y") assert expr.source() == "x + y" assert len(expr.children) == 2 assert expr.children == [Name("x"), Name("y")] assert expr == BinOp("+", Name("x"), Name("y"))
def test_source(self): # Leaves assert Atom(1).source() == "1" assert Name("x").source() == "x" # Containers and comprehensions assert List([Atom(1), Atom(2)]).source() == "[1, 2]" assert Tuple([Atom(1), Atom(2)]).source() == "(1, 2)" assert Tuple([Atom(1)]).source() == "(1,)" assert Tuple([]).source() == "()" assert Set([Atom(1), Atom(2)]).source() == "{1, 2}" assert Set([]).source() == "set()" assert Dict([Atom(1), Atom(2)]).source() == "{1: 2}" # Operators assert BinOp("+", Name("x"), Atom(1)).source() == "x + 1" assert (BinOp("*", BinOp("+", Name("x"), Atom(1)), BinOp("*", Name("y"), Atom(2))).source() == "(x + 1) * y * 2") assert And(Name("x"), Name("y")).source() == "x and y" assert Or(Name("x"), Name("y")).source() == "x or y" assert Compare(">", [Name("x"), Name("y")]).source() == "x > y" # Getters assert GetAttr(Name("x"), "y").source() == "x.y" assert GetAttr(Atom(42), "y").source() == "(42).y" assert GetItem(Name("x"), Name("i")).source() == "x[i]" assert GetItem(Name("x"), Slice()).source() == "x[:]" # Function call and others assert (Call.from_args(Name("fn"), Name("x"), y=Atom(42)).source() == "fn(x, y=42)") assert (Ternary(Name("cond"), Name("x"), Name("y")).source() == "x if cond else y") assert Lambda.from_args(Name("x"), x=Atom(1)).source() == "lambda x=1: x"
def test_equality(self): # Leaves assert to_expr(1) == Atom(1) assert Name("x") == Name("x") assert Name("x") != Name("y") assert Name("x").parent is None
def test_function_creation(self): fn = unwrap(py("def", py.func, [py.x], [py("return", (2 * py.x) + 1)])) assert fn.source() == "def func(x):\n return 2 * x + 1\n" assert fn.name.value == "func" assert isinstance(fn.args, Tree) assert fn.args.children[0] == Name("x")
def test_create_function(self): e = S("def", Name("fn"), [Name("x")], [S("return", Name("x"))]) assert e.source() == "def fn(x):\n return x\n"
def test_fcall_constructor(self): e = Call.from_args(Name("foo"), Atom("bar"), kw=Atom(42)) assert e.expr == Name("foo") assert e.source() == "foo('bar', kw=42)"
def test_getattr_constructor(self): e = GetAttr(Name("x"), "foo") assert e.expr == Name("x") assert e.attrs == {"attr": "foo"} assert e.attr == "foo" assert e.source() == "x.foo"