def test_methods_two_ctors_and_one_method(): def fun(): class B(object): def __init__(self): pass def method(self): "".lstrip() class A(object): def __init__(self): pass def method(self): "".rstrip() a = A() if None else B() a.method() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.A", "fun.B", "fun.method", "fun.method.rstrip", "fun.method", "fun.method.lstrip"] assert list(dfs_node_names(root)) == path
def test_methods_class_change(): def fun(): class B(object): def __init__(self): pass def method_b(self): pass class A(object): def __init__(self): pass def method_a(self): pass a = A() a.method_a() a = B() a.method_b() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.A", "fun.method_a", "fun.B", "fun.method_b"] assert list(dfs_node_names(root)) == path
def test_stmt_with_single(): class A(object): def __init__(self): pass def __enter__(self): b = "" b.find() return b def __exit__(self, t, v, tb): c = "" c.index() def fun(): with A() as a: a.strip() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.A", "fun.__enter__", "fun.__enter__.find", "fun.strip", "fun.__exit__", "fun.__exit__.index"] assert list(dfs_node_names(root)) == path
def test_decorators_class(): def fun(): def decorator(cls): def decorating(): return cls() return decorating @decorator class DecoratedClass(object): def __init__(self): pass def method(self): pass obj = DecoratedClass() obj.method() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.decorating", "fun.decorating.DecoratedClass", "fun.method"] assert list(dfs_node_names(root)) == path
def test_decorators_chain(): def kuku(function): @wraps(function) def decorating(): return function() return decorating def decorator1(function): @wraps(function) def decorating1(): return function() return decorating1 def decorator2(function): @wraps(function) def decorating2(): return function() return decorating2 @decorator1 @decorator2 def decorated(): pass builder = CallGraphBuilder() root = builder.build(decorated) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["decorating1", "decorating1.decorating2", "decorating1.decorating2.decorated"] assert list(dfs_node_names(root)) == path
def test_assigns_attr_chain_call(): class A(object): def __init__(self): pass def f(self): return B() class B(object): def __init__(self): pass def method(self): pass def fun(): A().f().method() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.A", "fun.f", "fun.f.B", "fun.method"] assert list(dfs_node_names(root)) == path
def test_stmt_with_tuple(): class A(): def __init__(self): pass def __enter__(self): return 1, "" def __exit__(self, t, v, tb): pass def fun(): with A() as (a, b): a.to_bytes(1, "big") a.strip() b.to_bytes(1, "big") b.strip() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.A", "fun.__enter__", "fun.to_bytes", "fun.strip", "fun.__exit__"] assert list(dfs_node_names(root)) == path
def test_methods_two_ctors_and_method_and_ctor(): class A(object): def __init__(self): pass def f(self): return "" class B(object): def __init__(self): pass def f(self): return C class C(object): def __init__(self): pass def f(self): return [] def fun(): a = A() if None else B() a.f().strip() a.f()().f().sort() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.A", "fun.B", "fun.f", "fun.f", "fun.strip", "fun.C", "fun.f", "fun.sort"] assert list(dfs_node_names(root)) == path
def test_classes_self_between_fun(): class A(object): def __init__(self): self.a = "str" def method(self): self.b = "str" def fun1(): a = A() a.method() return a def fun(): fun1().b.lstrip() b = A() b.a.rstrip() b.b.index() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.fun1", "fun.fun1.A", "fun.fun1.method", "fun.lstrip", "fun.A", "fun.rstrip"] assert list(dfs_node_names(root)) == path
def test_classes_method_super(): class A(object): def __init__(self): pass def method(self): "".rstrip() class B(A): def __init__(self): pass def method(self): "".lstrip() super().method() def fun(): b = B() b.method() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.B", "fun.method", "fun.method.lstrip", "fun.method.__super__", "fun.method.method", "fun.method.method.rstrip"] assert list(dfs_node_names(root)) == path
def test_generators_from_two_obj(): class B(object): def __init__(self): pass def __next__(self): return "" class A(object): def __init__(self): pass def __iter__(self): return B() def fun(): for entry in A(): entry.strip() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.A", "fun.__iter__", "fun.__iter__.B", "fun.__next__", "fun.strip"] assert list(dfs_node_names(root)) == path
def test_methods_self_class(): def fun(): class A(object): def __init__(self): pass def method(self): pass @classmethod def class_method(cls): self.method() @staticmethod def static_method(): self.method() a = A() a.class_method() a.static_method() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.A", "fun.class_method", "fun.static_method"] assert list(dfs_node_names(root)) == path
def test_functions_recur(): def recur(): recur() builder = CallGraphBuilder() root = builder.build(recur) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["recur"] assert list(dfs_node_names(root)) == path
def test_stmt_raise_simple(): def fun(): raise RuntimeError() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.RuntimeError"] assert list(dfs_node_names(root)) == path
def test_ops_bin_add(): def fun(): if "".strip() + "".find(): return builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.strip", "fun.find"] assert list(dfs_node_names(root)) == path
def test_stmt_lambda_returns(): def fun(): a = lambda x: x a("").strip() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.strip"] assert list(dfs_node_names(root)) == path
def test_stmt_call_none(): def fun(): a = None a() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun"] assert list(dfs_node_names(root)) == path
def test_stmt_for_dict(): def fun(): for key in {"k1": 1, "k2": 2}: key.strip() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.strip"] assert list(dfs_node_names(root)) == path
def test_ops_cmp(): def fun(): if "".find() < "".strip(): return (1 < 2).to_bytes(1, "big") builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.find", "fun.strip", "fun.to_bytes"] assert list(dfs_node_names(root)) == path
def test_assigns_set_explode(): def fun(): a, b = {"", 2} a.rstrip() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.rstrip"] assert list(dfs_node_names(root)) == path
def test_assigns_cond(): def fun(): a = 3 if None else "3" a.strip() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.strip"] assert list(dfs_node_names(root)) == path
def test_methods_str(): def fun(): a = str() a.strip() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.object", "fun.strip"] assert list(dfs_node_names(root)) == path
def test_stmt_with_empty(): def fun(): with open("f"): pass builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.open", "fun.__enter__", "fun.__exit__"] assert list(dfs_node_names(root)) == path
def test_assigns_nested_explode(): def fun(): (a, b), c = ("", []), 1 a.strip() b.append(None) builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.strip", "fun.append"] assert list(dfs_node_names(root)) == path
def test_stmt_lambda_closure(): def fun(): y = "" a = lambda x: x.rstrip() and y.lstrip() a("") builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.rstrip", "fun.lstrip"] assert list(dfs_node_names(root)) == path
def test_stmt_while(): def fun(): a = 1 while "".strip(): a.to_bytes(1, "big") builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.strip", "fun.to_bytes"] assert list(dfs_node_names(root)) == path
def test_stmt_try_two_types(): def fun(): try: "".strip() except (RuntimeError, ValueError) as e: print(e.with_traceback()) builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.strip", "fun.with_traceback", "fun.print"] assert list(dfs_node_names(root)) == path
def test_stmt_for_none_and_tuple(): def fun(): var_list = ["", ""] if None: var_list = None for var in var_list: var.strip() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.strip"] assert list(dfs_node_names(root)) == path
def test_stmt_try_empty(): def fun(): try: "".lstrip() except: "".rstrip() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.lstrip", "fun.rstrip"] assert list(dfs_node_names(root)) == path
def test_args_fun_default_simple(): def fun1(a=""): a.strip() def fun(): fun1() builder = CallGraphBuilder() root = builder.build(fun) from callgraph.indent_printer import dump_tree dump_tree(root, lambda x: x.children) path = ["fun", "fun.fun1", "fun.fun1.strip"] assert list(dfs_node_names(root)) == path