def test_tuple():
    ir = transform("""
def test_tuple():
    assert (1, 2) == (3, 4, 5)
""")
    assert ir == [
        AutowrapTest("test_tuple", [
            Assertion(
                Sequence([NumLiteral(1), NumLiteral(2)]),
                Sequence([NumLiteral(3),
                          NumLiteral(4),
                          NumLiteral(5)]),
            )
        ])
    ]
def test_one_assertion_literals_0():
    ir = transform("""
def random_function():
    pass


def test_foo():
    assert 1 == 2

""")
    assert ir == [
        AutowrapTest("test_foo", [
            Assertion(NumLiteral(1), NumLiteral(2)),
        ])
    ]
def test_assertion_call_function():
    ir = transform("""
def test_func():
    from mod import func
    assert func(7) == 42
""")

    assert ir == [
        AutowrapTest("test_func", [
            Import('mod', ['func']),
            Assertion(
                FunctionCall("func", [NumLiteral(7)]),
                NumLiteral(42),
            ),
        ])
    ]
def test_assertion_call_method_on_lvalue():

    ir = transform("""
def test_func():
    obj.func(42, 3.3)
""")

    assert ir == [
        AutowrapTest("test_func", [
            FunctionCall(
                Attribute(
                    'obj',
                    'func',
                ),
                [NumLiteral(42), NumLiteral(3.3)],
            ),
        ])
    ]
def test_subscript():
    ir = transform("""
def test_subscript():
    arr = [1, 2, 3]
    fst = arr[1]
""")

    assert ir == [
        AutowrapTest("test_subscript", [
            Assignment(
                'arr', Sequence([
                    NumLiteral(1),
                    NumLiteral(2),
                    NumLiteral(3),
                ])),
            Assignment('fst', Index('arr', NumLiteral(1)))
        ])
    ]
def test_assign_to_var():
    ir = transform("""
def test_assign():
    foo = 42
""")

    assert ir == [
        AutowrapTest("test_assign", [Assignment('foo', NumLiteral(42))])
    ]
def test_assign_to_list():
    ir = transform("""
def test_assign_to_list():
    [foo, bar] = 42
""")

    assert ir == [
        AutowrapTest("test_assign_to_list",
                     [Assignment(Sequence(['foo', 'bar']), NumLiteral(42))])
    ]
def test_assertion_call_method_on_rvalue():

    ir = transform("""
def test_func():
    from mod import Klass
    Klass('ctor_arg').func(42, 3.3)
""")

    assert ir == [
        AutowrapTest("test_func", [
            Import('mod', ['Klass']),
            FunctionCall(
                Attribute(
                    FunctionCall('Klass', [StringLiteral('ctor_arg')]),
                    'func',
                ),
                [NumLiteral(42), NumLiteral(3.3)],
            ),
        ])
    ]
def test_attribute():
    ir = transform("""
def test_func():
    foo.value = 42
""")
    assert ir == [
        AutowrapTest("test_func",
                     [Assignment(
                         Attribute('foo', 'value'),
                         NumLiteral(42),
                     )])
    ]
Пример #10
0
    def visit_Constant(self, node):
        from ir import NumLiteral, StringLiteral, BytesLiteral

        value = node.value
        if type(value) == int or type(value) == float:
            self.value = NumLiteral(value)
        elif type(value) == str:
            self.value = StringLiteral(value)
        elif type(value) == bytes:
            self.value = BytesLiteral(value)
        else:
            raise Exception(
                f"Cannot handle constant of type {type(value)} {value}")
Пример #11
0
    def visit_Subscript(self, node):
        from ir import Index, NumLiteral
        import ast

        if type(node.slice) is ast.Index:
            name = _run_visitor(node.value, ExpressionVisitor)
            index = _run_visitor(node.slice.value, ExpressionVisitor)
            self.value = Index(name, index)
        elif type(node.slice) is ast.Constant:
            name = _run_visitor(node.value, ExpressionVisitor)
            self.value = Index(name, NumLiteral(node.slice.value))
        else:
            raise TypeError(f"Cannot handle index of type {type(node.slice)}")
def test_one_assertion_literals_1():
    ir = transform("""
def random_function():
    pass


def test_bar():
    assert 3 == 'foo'

""")
    assert ir == [
        AutowrapTest("test_bar", [
            Assertion(NumLiteral(3), StringLiteral("foo")),
        ])
    ]
def test_length():
    ir = transform("""
def test_length():
    lst = [1]
    len(lst)
""")

    assert ir == [
        AutowrapTest(
            "test_length",
            [
                Assignment('lst', Sequence([NumLiteral(1)])),
                FunctionCall(
                    Attribute(
                        'lst',
                        'length',
                    ),
                    [],  # args
                )
            ])
    ]