Пример #1
0
 def test_wrapped_expressions(self):
     x = py.x
     y = py.y
     fn = py.fn
     src = lambda x: unwrap(x).source()
     assert src(x.foo) == "x.foo"
     assert src(fn(x)) == "fn(x)"
     assert src((x + y).method()) == "(x + y).method()"
Пример #2
0
    def test_source(self):
        src = lambda x: unwrap(x).source()

        # Atoms
        assert src(py(42)) == "42"
        assert src(py(-42)) == "-42"

        # Containers
        assert src(py([])) == "[]"
        assert src(py([42])) == "[42]"
Пример #3
0
 def test_free_vars(self):
     assert unwrap(py.x).free_vars() == {"x"}
     assert unwrap(py.x + py.y + 2).free_vars() == {"x", "y"}
Пример #4
0
 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")
Пример #5
0
 def test_containers(self):
     xs = unwrap(py([1, 2, 3]))
     assert len(xs.children) == 3
     assert xs.children[0] == Atom(1)
Пример #6
0
    Function,
    Block,
    Lambda,
    GetItem,
    Slice,
    Compare,
    Tuple,
    List,
    Set,
    Dict,
    Del,
)
from ox.target.python import to_expr, py, unwrap
from ox.target.python.stmt_ast import Cmd

src = lambda x: unwrap(x).source()


class TestMetaClass:
    """
    Check if class hierarchies are correct and metaclasses initialized everything
    nicely
    """
    def test_mro(self):
        assert Atom._meta.root is Expr
        assert Return._meta.root is Stmt

    def test_expr_root(self):
        roles = Expr._meta.wrapper_roles
        sexpr = Expr._meta.sexpr_symbol_map
        assert {"getattr", "fcall"}.issubset(roles.keys())