Exemplo n.º 1
0
def test_fw_gen_use(fort_writer):
    '''Check the FortranWriter class gen_use method produces the expected
    declaration. Also check that an exception is raised if the symbol
    does not describe a use statement.

    '''
    symbol = Symbol("dummy1",
                    "deferred",
                    interface=Symbol.FortranGlobal("my_module"))
    result = fort_writer.gen_use(symbol)
    assert result == "use my_module, only : dummy1\n"

    symbol = Symbol("dummy1", "integer")
    with pytest.raises(VisitorError) as excinfo:
        _ = fort_writer.gen_use(symbol)
    assert ("gen_use() requires the symbol interface for symbol 'dummy1' to "
            "be a FortranGlobal instance but found 'NoneType'."
            in str(excinfo.value))
Exemplo n.º 2
0
def test_fw_gen_vardecl(fort_writer):
    '''Check the FortranWriter class gen_vardecl method produces the
    expected declarations. Also check that an exception is raised if
    the symbol does not describe a variable declaration statement.

    '''
    # Basic entry
    symbol = Symbol("dummy1", "integer")
    result = fort_writer.gen_vardecl(symbol)
    assert result == "integer :: dummy1\n"

    # Array with intent
    symbol = Symbol("dummy2",
                    "integer",
                    shape=[2, None, 2],
                    interface=Symbol.Argument(access=Symbol.Access.READ))
    result = fort_writer.gen_vardecl(symbol)
    assert result == "integer, dimension(2,:,2), intent(in) :: dummy2\n"

    # Array with unknown intent
    symbol = Symbol("dummy2",
                    "integer",
                    shape=[2, None, 2],
                    interface=Symbol.Argument(access=Symbol.Access.UNKNOWN))
    result = fort_writer.gen_vardecl(symbol)
    assert result == "integer, dimension(2,:,2) :: dummy2\n"

    # Constant
    symbol = Symbol("dummy3", "integer", constant_value=10)
    result = fort_writer.gen_vardecl(symbol)
    assert result == "integer, parameter :: dummy3 = 10\n"

    # Use statement
    symbol = Symbol("dummy1",
                    "deferred",
                    interface=Symbol.FortranGlobal("my_module"))
    with pytest.raises(VisitorError) as excinfo:
        _ = fort_writer.gen_vardecl(symbol)
    assert ("gen_vardecl requires the symbol 'dummy1' to be a local "
            "declaration or an argument declaration, but found scope "
            "'global' and interface 'FortranGlobal'." in str(excinfo.value))
Exemplo n.º 3
0
def test_gen_decls(fort_writer):
    '''Check the FortranWriter class gen_decls method produces the
    expected declarations. Also check that an exception is raised if
    an 'argument' symbol exists in the supplied symbol table and the
    optional argument 'args_allowed' is set to False.

    '''
    symbol_table = SymbolTable()
    use_statement = Symbol("my_use",
                           "deferred",
                           interface=Symbol.FortranGlobal("my_module"))
    symbol_table.add(use_statement)
    argument_variable = Symbol("arg", "integer", interface=Symbol.Argument())
    symbol_table.add(argument_variable)
    local_variable = Symbol("local", "integer")
    symbol_table.add(local_variable)
    result = fort_writer.gen_decls(symbol_table)
    assert (result == "use my_module, only : my_use\n"
            "integer :: arg\n"
            "integer :: local\n")
    with pytest.raises(VisitorError) as excinfo:
        _ = fort_writer.gen_decls(symbol_table, args_allowed=False)
    assert ("Arguments are not allowed in this context but this symbol table "
            "contains argument(s): '['arg']'." in str(excinfo.value))