Example #1
0
def test_get_child():
    ''' Test the get_child() utility. '''
    from fparser.two import Fortran2003
    from fparser.two.utils import walk, get_child
    reader = get_reader("program hello\n"
                        "write(*,*) 'hello'\n"
                        "write(*,*) 'goodbye'\n"
                        "end program hello\n")
    main = Fortran2003.Program(reader)
    prog = get_child(main, Fortran2003.Main_Program)
    exe = get_child(prog, Fortran2003.Execution_Part)
    assert isinstance(exe, Fortran2003.Execution_Part)
    write_stmt = get_child(exe, Fortran2003.Write_Stmt)
    # Check that we got the first write and not the second
    assert "goodbye" not in str(write_stmt)
    # The top level has no Io_Control_Spec children
    assert not get_child(main, Fortran2003.Io_Control_Spec)
    # Check functionality when node has children in `items` and
    # not in `content`
    io_nodes = walk(main.content, Fortran2003.Io_Control_Spec)
    assert not hasattr(io_nodes[0], "content")
    io_unit = get_child(io_nodes[0], Fortran2003.Io_Unit)
    assert isinstance(io_unit, Fortran2003.Io_Unit)
    missing = get_child(io_nodes[0], Fortran2003.Execution_Part)
    assert missing is None
Example #2
0
def test_walk_debug(capsys):
    ''' Test the debug output of the walk() utility. '''
    import six
    reader = get_reader("program just_a_test\n"
                        "if(.true.)then\n"
                        "  b = 1\n"
                        "end if\n"
                        "end program just_a_test\n")
    main = Fortran2003.Program(reader)
    _ = walk(main, debug=True)
    stdout, _ = capsys.readouterr()
    if six.PY2:
        # Output of capsys under Python 2 is not the same as under 3
        assert stdout.startswith("('child type = ")
    else:
        assert stdout.startswith("child type = ")
    assert "Main_Program" in stdout
    assert "If_Construct" in stdout
    assert "Assignment" in stdout
    assert "Int_Literal_Constant" in stdout
    assert "End_If_Stmt" in stdout
    # Walk only part of the tree and specify an indent
    if_constructs = walk(main, Fortran2003.If_Construct)
    _ = walk(if_constructs[0], indent=4, debug=True)
    stdout, _ = capsys.readouterr()
    if six.PY2:
        # Output of capsys under Python 2 is not the same as under 3
        assert stdout.startswith("('" + 8 * " " + "child type =")
    else:
        assert stdout.startswith(8 * " " + "child type =")
    assert "Program" not in stdout
Example #3
0
def test_children_property():
    ''' Test that the children property of Base returns the correct
    results for both statements and expressions. '''
    reader = get_reader(TEST_CODE)
    main = Fortran2003.Program(reader)
    # Check that children returns items when we have an expression
    writes = walk(main, Fortran2003.Write_Stmt)
    assert writes[0].children is writes[0].items
    assert len(writes[0].children) == 2
    # Check that it returns content when we have a statement
    do_stmts = walk(main, Fortran2003.Block_Nonlabel_Do_Construct)
    assert do_stmts[0].children is do_stmts[0].content
    assert len(do_stmts[0].children) == 3
Example #4
0
def runner(parser, options, args):
    from fparser.two import Fortran2003
    from fparser.common.readfortran import FortranFileReader
    for filename in args:
        reader = FortranFileReader(filename)
        if options.mode != 'auto':
            mode = fparser.common.sourceinfo\
                   .FortranFormat.from_mode(options.mode)
            reader.format.set_mode(mode)
        try:
            program = Fortran2003.Program(reader)
            print(program)
        except Fortran2003.NoMatchError as msg:
            print('parsing %r failed at %s' % (filename, reader.fifo_item[-1]))
            print('started at %s' % (reader.fifo_item[0]))
            print('quiting')
            return
Example #5
0
def test_parent_info():
    ''' Check that parent information is correctly set-up in the
    parse tree. '''
    from fparser.two.utils import Base
    reader = get_reader(TEST_CODE)
    main = Fortran2003.Program(reader)
    node_list = walk(main)

    # Root node in the parse tree has no parent
    parent_prog = node_list[0]
    assert parent_prog.parent is None

    # Check connectivity of all non-string nodes
    for node in node_list[1:]:
        if isinstance(node, Base):
            for child in node.children:
                if isinstance(child, Base):
                    assert child.parent is node
                    assert child.get_root() is parent_prog
Example #6
0
def test_walk():
    ''' Test the walk() utility. '''
    reader = get_reader("program hello\n"
                        "write(*,*) 'hello'\n"
                        "write(*,*) 'goodbye'\n"
                        "end program hello\n")
    main = Fortran2003.Program(reader)
    # Check that walk produces the same result whether or not the
    # starting node is in a list.
    all_nodes = walk(main)
    all_nodes2 = walk([main])
    assert all_nodes == all_nodes2
    # Check that we can pull out nodes of a particular type
    all_writes = walk(main, types=Fortran2003.Write_Stmt)
    assert len(all_writes) == 2
    assert "hello" in str(all_writes[0])
    assert "goodbye" in str(all_writes[1])
    # Walk from a list of sibling nodes
    io_units = walk(all_writes, types=Fortran2003.Io_Unit)
    assert len(io_units) == 2
    # Should get empty list if no matching nodes found
    node_list = walk(all_writes, Fortran2003.Execution_Part)
    assert node_list == []