Exemplo n.º 1
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
Exemplo n.º 2
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_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'
Exemplo n.º 4
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
Exemplo n.º 5
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'
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'
Exemplo n.º 7
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
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'
Exemplo n.º 9
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
Exemplo n.º 10
0
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
Exemplo n.º 11
0
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