Example #1
0
def test_sort_links():
    before_link = MetaLink(None, None, 'before', [])
    after_link = MetaLink(None, None, 'after', [])
    instead_link = MetaLink(None, None, 'instead', [])
    node = ReflectivityExample.sample_node()

    node.links.append(before_link)
    node.links.append(after_link)
    node.links.append(instead_link)

    wrapper = FlatWrapper(node)

    assert len(wrapper.before_links) == 0
    assert len(wrapper.after_links) == 0
    assert len(wrapper.instead_links) == 0

    wrapper.sort_links()

    assert len(wrapper.before_links) == 1
    assert wrapper.before_links[0] is before_link

    assert len(wrapper.after_links) == 1
    assert wrapper.after_links[0] is after_link

    assert len(wrapper.instead_links) == 1
    assert wrapper.instead_links[0] is instead_link
Example #2
0
def test_link_before_after_while():
    example = ReflectivityExample()
    before_link = MetaLink(example, 'tag_push', 'before', [1])
    after_link = MetaLink(example, 'tag_push', 'after', [2])
    rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_while')
    node = rf_ast.original_ast.body[0].body[1]

    reflectivipy.link(before_link, node)
    reflectivipy.link(after_link, node)

    assert len(example.tagged_reifications) == 0
    assert ReflectivityExample().example_while() == 10
    assert len(example.tagged_reifications) == 2
    assert example.tagged_reifications[0] == 1
    assert example.tagged_reifications[1] == 2
Example #3
0
def test_multiple_links_within_while():
    example = ReflectivityExample()
    link_1 = MetaLink(example, 'tag_push', 'after', [0])
    link_2 = MetaLink(example, 'tag_push', 'after', [1])
    rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_while')
    node_1 = rf_ast.original_ast.body[0].body[1].test.left
    node_2 = rf_ast.original_ast.body[0].body[1].body[0]

    reflectivipy.link(link_1, node_1)
    reflectivipy.link(link_2, node_2)

    assert len(example.tagged_reifications) == 0
    assert ReflectivityExample().example_while() == 10
    assert len(example.tagged_reifications) == 21

    for i, reif_value in enumerate(example.tagged_reifications):
        assert reif_value == i % 2
def test_call_receiver_flattening():
    node = call_with_complex_receiver_sample_node()
    link = MetaLink(ReflectivityExample(), 'tag_exec_', 'before', [])
    node.value.links.append(link)

    transformation = node.wrapper.flat_wrap()
    assert len(transformation) == 4
    assert transformation[1].value.func.value.id == 'self'
    assert transformation[3].value.func.value.id == transformation[1].targets[
        0].id
Example #5
0
def test_link_inside_while():
    example = ReflectivityExample()
    link = MetaLink(example, 'tag_exec_', 'after', [])
    rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_while')
    node = rf_ast.original_ast.body[0].body[1].body[0]

    reflectivipy.link(link, node)

    assert example.tag is None
    assert ReflectivityExample().example_while() == 10
    assert example.tag == 'tag'
def test_wrap_complex_expr_call():
    node = complex_expr_call_sample_node()

    link = MetaLink(ReflectivityExample(), 'tag_exec_', 'before', [])
    node.value.args[0].links.append(link)

    transformation = node.wrapper.flat_wrap()
    assert len(transformation) == 6
    assert type(transformation[3]) is ast.Assign
    assert transformation[3].value.rf_id is node.value.args[0].rf_id
    assert transformation[3] is not node
Example #7
0
def test_link_on_while_after_left_test():
    example = ReflectivityExample()
    link = MetaLink(example, 'tag_exec', 'after', ['node'])
    rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_while')
    node = rf_ast.original_ast.body[0].body[1].test.left

    reflectivipy.link(link, node)

    assert example.tag is None
    assert ReflectivityExample().example_while() == 10
    assert example.tag is node
def test_wrap_assign_with_children():
    example = ReflectivityExample()
    link = MetaLink(example, 'tag_exec_', 'before', [])
    rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_assign_call')
    node = rf_ast.original_ast.body[0].body[1]

    reflectivipy.link(link, node)

    assert example.tag is None
    assert ReflectivityExample().example_assign_call() == 2
    assert example.tag == 'tag'
def test_metalinks_count():
    example = ReflectivityExample()
    link = MetaLink(example, 'tag_exec_', 'before', [])
    rf_method = reflectivipy.reflective_method_for(ReflectivityExample, 'example_assign')
    node = rf_method.original_ast.body[0].body[0]

    assert len(reflectivipy.metalinks) == 0

    reflectivipy.link(link, node)

    len(reflectivipy.metalinks) == 1
    assert reflectivipy.metalinks.pop() is link
def test_wrap_method_node():
    example = ReflectivityExample()

    link = MetaLink(example, 'tag_exec_', 'before', [])
    ast = reflectivipy.reflective_ast_for_method(ReflectivityExample,
                                                 'example_method')

    reflectivipy.link(link, ast)

    assert example.tag is None
    assert ReflectivityExample().example_method() == 9
    assert example.tag == 'tag'
Example #11
0
def test_link_instead_if():
    example = ReflectivityExample()
    link = MetaLink(example, 'tag_exec_', 'instead', [])
    rf_ast = reflectivipy.reflective_method_for(ReflectivityExample,
                                                'example_multiple_return')
    node = rf_ast.original_ast.body[0].body[0]

    reflectivipy.link(link, node)

    assert example.tag is None
    assert ReflectivityExample().example_multiple_return(1) == 2
    assert example.tag == 'tag'
Example #12
0
def test_link_after_if_that_returns_before_link():
    example = ReflectivityExample()
    link = MetaLink(example, 'tag_exec_', 'after', [])
    rf_ast = reflectivipy.reflective_method_for(ReflectivityExample,
                                                'example_multiple_return')
    node = rf_ast.original_ast.body[0].body[0]

    reflectivipy.link(link, node)

    assert example.tag is None
    assert ReflectivityExample().example_multiple_return(0) == 42
    assert example.tag is None
def test_link_to_node():
    example = ReflectivityExample()

    link = MetaLink(example, 'tag_exec', 'before', [])
    rf_node = reflectivipy.reflective_ast_for_method(ReflectivityExample,
                                                     'example_method')

    reflectivipy.link(link, rf_node)

    assert rf_node.links
    assert rf_node.links.pop() is link

    assert link.nodes
    assert link.nodes.pop() is rf_node
def test_globals_metalink_registry():
    example = ReflectivityExample()

    link = MetaLink(example, 'tag_exec', 'before', [])
    rf_node = reflectivipy.reflective_ast_for_method(ReflectivityExample,
                                                     'example_method')

    reflectivipy.link(link, rf_node)

    rf_method = reflectivipy.reflective_method_for(ReflectivityExample,
                                                   'example_method')
    method_globals = ReflectivityExample.example_method.__globals__

    assert method_globals["__rf_method__"] is rf_method
    assert method_globals["__rf_method__"].lookup_link(id(link)) is link
def test_wrap_expr():
    node = expr_sample_node()

    assert type(node) is ast.Expr

    transformation = node.wrapper.flat_wrap()
    assert len(transformation) == 1
    assert transformation[0] is node

    link = MetaLink(ReflectivityExample(), 'tag_exec_', 'before', [])
    node.links.append(link)

    assert type(node) is ast.Expr
    transformation = node.wrapper.flat_wrap()
    assert len(transformation) == 1
    assert transformation[0] is node
def test_after_returns_wraps_in_method():
    example = ReflectivityExample()
    link = MetaLink(example, 'tag_exec_', 'after', [])
    rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_multiple_return')
    node = rf_ast.original_ast

    reflectivipy.link(link, node)

    assert example.tag is None
    assert ReflectivityExample().example_multiple_return(0) == 42
    assert example.tag == 'tag'

    example.tag = None
    assert example.tag is None
    assert ReflectivityExample().example_multiple_return(1) == 2
    assert example.tag == 'tag'
def test_restore_original():
    example = ReflectivityExample()
    link = MetaLink(example, 'tag_exec', 'after', ['node'])
    rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_while')
    original_body = rf_ast.original_ast.body[0].body
    node = original_body[1].test.left

    reflectivipy.link(link, node)

    example.tag = None
    ReflectivityExample().example_while()
    assert example.tag is node

    reflectivipy.uninstall_all()

    example.tag = None
    ReflectivityExample().example_while()
    assert example.tag is None
def test_wrap_assign():
    node = sample_node()

    assert type(node) is ast.Assign

    transformation = node.wrapper.flat_wrap()
    assert len(transformation) == 1
    assert transformation[0] is node

    link = MetaLink(ReflectivityExample(), 'tag_exec_', 'before', [])
    node.links.append(link)

    assert type(node) is ast.Assign

    transformation = node.wrapper.flat_wrap()
    assert len(transformation) == 3
    assert type(transformation[0]) is ast.Assign
    assert transformation[0].value is node.value
    assert transformation[0] is not node
def test_wrap_call_in_assign():
    node = method_with_args_sample_node().body[0].body[0]

    link = MetaLink(ReflectivityExample(), 'tag_exec_', 'before', [])
    node.value.links.append(link)

    assert type(node) is ast.Assign
    assert type(node.value) is ast.Call

    transformation = node.wrapper.flat_wrap()
    assert len(transformation) == 5
    assert type(transformation[0]) == ast.Assign
    assert transformation[0].value is node.value.args[0]
    assert type(transformation[1]) is ast.Assign
    assert transformation[1].value.id == 'self'
    assert transformation[0] is not node
    assert len(transformation[3].value.args) == 1
    assert type(transformation[3].value.args[0]) is ast.Name
    assert transformation[3].value.args[0].id is node.value.args[0].temp_name
    assert transformation[
        3].value.func.value.id is node.value.func.value.temp_name
def test_wrap_call():
    node = call_sample_node().value

    assert type(node) is ast.Call

    transformation = node.wrapper.flat_wrap()
    assert len(transformation) == 1
    assert transformation[0] is node

    link = MetaLink(ReflectivityExample(), 'tag_exec_', 'before', [])
    node.links.append(link)

    assert type(node) is ast.Call

    transformation = node.wrapper.flat_wrap()
    assert len(transformation) == 4
    assert type(transformation[0]) is ast.Assign
    assert transformation[0].value is node.args[0]
    assert transformation[0] is not node

    assert len(transformation[3].value.args) == 1
    assert type(transformation[3].value.args[0]) is ast.Name
    assert transformation[3].value.args[0].id is node.args[0].temp_name
    assert transformation[3].value.func.value.id is node.func.value.temp_name
def test_original_ast_preservation():
    example = ReflectivityExample()
    link = MetaLink(example, 'tag_exec', 'after', ['node'])
    rf_ast = reflectivipy.reflective_method_for(ReflectivityExample, 'example_while')
    original_body = rf_ast.original_ast.body[0].body
    node = original_body[1].test.left

    number_of_nodes = len(original_body)
    original_left_id = node.id

    reflectivipy.link(link, node)

    new_body = rf_ast.original_ast.body[0].body
    new_left = new_body[1].test.left
    reflective_ast_body = rf_ast.reflective_ast.body[0].body

    ReflectivityExample().example_while()
    assert example.tag is node
    assert new_body is original_body
    assert len(new_body) == number_of_nodes
    assert original_left_id == new_left.id

    assert reflective_ast_body[1] is not new_body[1]
    assert len(reflective_ast_body) > number_of_nodes