Exemplo n.º 1
0
def test_self_graph():
    import flow.graph
    with open(flow.graph.__file__) as fd:
        module = fd.read()
    ast_root = ast_from_text(module, flow.graph.__name__)
    graph = FlowGraph.from_ast(ast_root)
    graph.to_graphviz().render('{}.gv'.format(test_func_name()))
Exemplo n.º 2
0
def test_if_conditional():
    ast_root = ast_from_text("""
    if 1:
        print(1)
    """)
    graph = FlowGraph.from_ast(ast_root)
    assert graph.statement_count == 2  # 1x in if, 1x in if body
    graph.to_graphviz().render('{}.gv'.format(test_func_name()))
Exemplo n.º 3
0
def test_stdlib_func(param):
    import inspect
    module_name, _, func_name = param.rpartition('.')
    module = importlib.import_module(module_name)
    function, _ = inspect.getsourcelines(getattr(module, func_name))
    ast_root = ast_from_text('\n'.join(function), module_name)
    graph = FlowGraph.from_ast(ast_root.body[0])
    graph.to_graphviz().render('{}_{}.gv'.format(test_func_name(), param))
Exemplo n.º 4
0
def test_if_elif_conditional():
    ast_root = ast_from_text("""
    if 1:
        print(1)
    elif 2:
        print(2)
    """)
    graph = FlowGraph.from_ast(ast_root)
    assert graph.statement_count == 4  # 2x if stmt, 2x in if bodies
    graph.to_graphviz().render('{}.gv'.format(test_func_name()))
Exemplo n.º 5
0
def test_returns_implicitly():
    ast_root = ast_from_text("""
    def f():
        if 1:
            return 1
    """)
    graph = FlowGraph.from_ast(ast_root.body[0])
    assert graph.statement_count == 2
    assert not graph.returns_implicitly
    graph.to_graphviz().render('{}.gv'.format(test_func_name()))
Exemplo n.º 6
0
def test_early_returns():
    ast_root = ast_from_text("""
    def f():
        if 1:
            print(1)
            return 1

        print(2)
        return 2

        print(3)  # dead code
    """)
    graph = FlowGraph.from_ast(ast_root.body[0])
    assert graph.statement_count == 6
    assert not graph.returns_implicitly
    graph.to_graphviz().render('{}.gv'.format(test_func_name()))
Exemplo n.º 7
0
def test_from_two_statement_module():
    ast_root = ast_from_text("A = 1; B = 2")
    graph = FlowGraph.from_ast(ast_root)
    assert graph.statement_count == 2
    graph.to_graphviz().render('{}.gv'.format(test_func_name()))
Exemplo n.º 8
0
def test_from_empty_module():
    ast_root = ast_from_text("")
    graph = FlowGraph.from_ast(ast_root)
    graph.to_graphviz().render('{}.gv'.format(test_func_name()))