def test_metalinks_with_wrappers(): class MetaBehavior(object): def __init__(self): self.called = 0 def meta(self): self.called += 1 class MyClass(object): @decorate_with(1) @decorate_with(2) @decorate_with(3) def da_call(self): return 1234 m = MyClass() metabehavior = MetaBehavior() link = reflectivipy.MetaLink(metabehavior, selector='meta', control='before') rf_ast = reflectivipy.reflective_ast_for_method(m, 'da_call') node = rf_ast.body[0].body[0] reflectivipy.link(link, node) result = m.da_call() assert result == 1234 assert decorator_counter == 6 assert metabehavior.called == 1
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_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_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_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_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'
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_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'
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_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_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