示例#1
0
def test_get_source_info_wrong():
    '''
    Tests that get_source_info throws an exception if passed the wrong type
    of argument.
    '''
    with pytest.raises(ValueError):
        _source_info = get_source_info(42)  # Obviously wrong

    with pytest.raises(ValueError):
        _source_info = get_source_info(['one'])  # Less obviously wrong
示例#2
0
def test_get_source_info_filename(extension, header, content):
    #pylint: disable=redefined-outer-name
    '''
    Tests that source format is correctly identified when read from a file.
    '''
    full_source = ''
    if header[0] is not None:
        full_source += header[0] + '\n'
    if content[0] is not None:
        full_source += content[0]

    source_file, filename = tempfile.mkstemp(suffix=extension[0], text=True)
    os.close(source_file)

    with open(filename, 'w') as source_file:
        print(full_source, file=source_file)

    try:
        source_info = get_source_info(filename)
        if extension[1] is not None:
            assert source_info == extension[1]
        elif header[0] is not None:
            assert source_info == header[1]
        else:  # No header
            assert source_info == content[1]
    except Exception as ex:
        os.remove(filename)
        raise ex
示例#3
0
def test_get_source_info_utf8():
    '''
    Tests that Fortran code containing a unicode character can be read
    by the get_source_info method.

    '''
    encoding = dict(encoding='UTF-8') if six.PY3 else {}
    with tempfile.NamedTemporaryFile(mode='w', **encoding) as tmp_file:
        content = u'''
            ! A fortran comment with a unicode character "{0}"
        '''.format(u"\u2014")
        if six.PY2:
            content = content.encode('UTF-8')
        tmp_file.write(content)
        tmp_file.flush()

        source_info = get_source_info(tmp_file.name)
    assert source_info is not None
示例#4
0
def test_get_source_info_file(extension, header, content):
    #pylint: disable=redefined-outer-name
    '''
    Tests that source format is correctly identified when read from a file.
    '''
    full_source = ''
    if header[0] is not None:
        full_source += header[0] + '\n'
    if content[0] is not None:
        full_source += content[0]

    with tempfile.TemporaryFile(mode='w+', suffix=extension[0]) as source_file:
        print(full_source, file=source_file)
        source_file.seek(0)

        source_info = get_source_info(source_file)
        if header[0] is not None:
            assert source_info == header[1]
        else:  # No header
            assert source_info == content[1]