Beispiel #1
0
def test_none_in_fifo(log):
    ''' Check that a None entry in the reader FIFO buffer is handled
    correctly. '''
    log.reset()
    handle, filename = tempfile.mkstemp(suffix='.f90', text=True)
    os.close(handle)

    with io.open(filename, mode='w', encoding='UTF-8') as source_file:
        source_file.write(FULL_FREE_SOURCE)

    with io.open(filename, mode='r', encoding='UTF-8') as source_file:
        unit_under_test = FortranFileReader(source_file)
        while True:
            try:
                _ = unit_under_test.next(ignore_comments=False)
            except StopIteration:
                break
        # Erroneously push a None to the FIFO buffer
        unit_under_test.put_item(None)
        # Attempt to read the next item
        with pytest.raises(StopIteration):
            _ = unit_under_test.next(ignore_comments=False)
        # Check that nothing has been logged
        for log_level in ["debug", "info", "warning", "error", "critical"]:
            assert log.messages[log_level] == []
Beispiel #2
0
def check_include_works(fortran_filename,
                        fortran_code,
                        include_info,
                        expected,
                        tmpdir,
                        ignore_comments=True):
    '''Utility function used by a number of tests to check that include
    files work as expected.

    :param str fortran_filename: the name of the fortran file that is \
    going to be created in the 'tmpdir' directory.
    :param str fortran_code: the fortran code to put in the fortran \
    file specified by 'fortran_filename'.
    :param include_info: a list of 2-tuples each with an include \
    filename as a string followed by include code as a string.
    :type include_info: list of (str, str)
    :param str expected: the expected output after parsing the code.
    :param str tmpdir: the temporary directory in which to create and \
    process the Fortran files.
    :param bool ignore_comments: whether to ignore (skip) comments in \
    the Fortran code or not. Defaults to ignore them.

    '''

    try:
        oldpwd = tmpdir.chdir()
        cwd = str(tmpdir)

        # Create the program
        with open(os.path.join(cwd, fortran_filename), "w") as cfile:
            cfile.write(fortran_code)
        for include_filename in include_info.keys():
            with open(os.path.join(cwd, include_filename), "w") as cfile:
                cfile.write(include_info[include_filename])
        reader = FortranFileReader(fortran_filename,
                                   ignore_comments=ignore_comments)
        for orig_line in expected.split("\n"):
            new_line = reader.next().line
            assert new_line == orig_line
        with pytest.raises(StopIteration):
            reader.next()
    finally:
        oldpwd.chdir()