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_from_bytes(): ir = transform(""" def test_assign_from_bytes(): foo = b'abc' """) assert ir == [ AutowrapTest("test_assign_from_bytes", [Assignment('foo', BytesLiteral(b'abc'))]) ]
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 visit_Assign(self, node): from ir import Assignment assert len(node.targets), "Cannot handle multiple assignment targets" lhs_node = node.targets[0] rhs_node = node.value lhs = _run_visitor(lhs_node, ExpressionVisitor) rhs = _run_visitor(rhs_node, ExpressionVisitor) self.value.append(Assignment(lhs, rhs))
def test_attribute(): ir = transform(""" def test_func(): foo.value = 42 """) assert ir == [ AutowrapTest("test_func", [Assignment( Attribute('foo', 'value'), NumLiteral(42), )]) ]
def test_import(): ir = transform(""" def test_import(): import foo, bar a = foo.GLOBAL """) assert ir == [ AutowrapTest("test_import", [ Import("foo", []), Import("bar", []), Assignment( 'a', Attribute('foo', 'GLOBAL'), ) ]) ]
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 ) ]) ]