Exemplo n.º 1
0
def test_main_output_task_default(tmpdir, capsys, monkeypatch):
    '''Test that the script main() function outputs the code it has parsed
    by default.'''
    import sys
    # Create a temporary file containing Fortran code to pass into runner()
    my_file = tmpdir.mkdir("sub").join("hello.f90")
    my_file.write("program hello\nend program hello\n")
    # Use monkeypatch to spoof the command-line argument
    monkeypatch.setattr(sys, "argv", ["fparser2", my_file.strpath])
    # run the relevant script method (main())
    fparser2.main()
    # capture the output and check that the code has been output
    stdout, _ = capsys.readouterr()
    assert stdout == "PROGRAM hello\nEND PROGRAM hello\n"
Exemplo n.º 2
0
def test_main_output_std_invalid(capsys, monkeypatch):
    '''Test that the script main() function raises an exception when an
    invalid standard --std=invalid is provided.

    '''
    import sys
    # Use monkeypatch to spoof the command-line argument.
    monkeypatch.setattr(sys, "argv",
                        ["fparser2", "--std=invalid", "dummy.f90"])
    # Run the relevant script method (main()).
    with pytest.raises(SystemExit) as excinfo:
        fparser2.main()
    assert str(excinfo.value) == "2"
    # Capture the output and check that the code has been output.
    stdout, stderr = capsys.readouterr()
    assert stdout == ""
    assert ("fparser2: error: option --std: invalid choice: 'invalid' "
            "(choose from 'f2003', 'f2008')" in stderr)
Exemplo n.º 3
0
def test_main_output_task_none(tmpdir, capsys, monkeypatch):
    '''Test that the script main() function outputs nothing when
    --task=none.

    '''
    import sys
    # Create a temporary file containing Fortran code to pass into
    # runner().
    my_file = tmpdir.mkdir("sub").join("hello.f90")
    my_file.write("program hello\nend program hello\n")
    # Use monkeypatch to spoof the command-line argument.
    monkeypatch.setattr(sys, "argv",
                        ["fparser2", "--task=none", my_file.strpath])
    # Run the relevant script method (main()).
    fparser2.main()
    # Capture the output and check that the code has been output.
    stdout, _ = capsys.readouterr()
    assert stdout == ""
Exemplo n.º 4
0
def test_main_output_std_f2008(capsys, tmpdir, monkeypatch):
    '''Test that the script main() function parses using the Fortran2008
    standard when --std=f2008. This test attempts to use submodules
    which are supported in Fortran200 so should be parsed correctly.

    '''
    import sys
    # Create a temporary file containing Fortran code to pass into
    # runner().
    my_file = tmpdir.mkdir("sub").join("submodule.f90")
    my_file.write("submodule(my_mod) test\nend submodule\n")
    # Use monkeypatch to spoof the command-line argument.
    monkeypatch.setattr(sys, "argv",
                        ["fparser2", "--std=f2008", my_file.strpath])
    # Run the relevant script method (main()).
    fparser2.main()
    # Capture the output and check that the code has been output.
    stdout, stderr = capsys.readouterr()
    assert stdout == "SUBMODULE (my_mod) test\nEND SUBMODULE\n"
    assert "File: '" in stderr and "submodule.f90'" in stderr
Exemplo n.º 5
0
def test_main_output_task_repr(tmpdir, capsys, monkeypatch):
    '''Test that the script main() function outputs the 'repr' of the code
    it has parsed when --task=repr.

    '''
    import sys
    # Create a temporary file containing Fortran code to pass into
    # runner().
    my_file = tmpdir.mkdir("sub").join("hello.f90")
    my_file.write("program hello\nend program hello\n")
    # Use monkeypatch to spoof the command-line argument.
    monkeypatch.setattr(sys, "argv",
                        ["fparser2", "--task=repr", my_file.strpath])
    # Run the relevant script method (main()).
    fparser2.main()
    # Capture the output and check that the code has been output.
    stdout, _ = capsys.readouterr()
    assert stdout == ("Program(Main_Program(Program_Stmt('PROGRAM',"
                      " Name('hello')), End_Program_Stmt('PROGRAM',"
                      " Name('hello'))))\n")
Exemplo n.º 6
0
def test_main_output_task_invalid(tmpdir, capsys, monkeypatch):
    '''Test that the script main() function prints an error when an
    invalid task option is provided.

    '''
    import sys
    # Create a temporary file containing Fortran code to pass into
    # runner().
    my_file = tmpdir.mkdir("sub").join("hello.f90")
    my_file.write("program hello\nend program hello\n")
    # Use monkeypatch to spoof the command-line argument.
    monkeypatch.setattr(sys, "argv",
                        ["fparser2", "--task=invalid", my_file.strpath])
    with pytest.raises(SystemExit):
        # Run the relevant script method (main()).
        fparser2.main()
    # Capture the output and check that the appropriate error has been
    # written.
    _, stderr = capsys.readouterr()
    assert ("fparser2: error: option --task: invalid choice: 'invalid'"
            "" in stderr)