def test_adduse_invalid_location(location): '''Test that the expected exception is raised when the specified location is invalid. ''' name = "my_use" with pytest.raises(GenerationError) as excinfo: adduse(location, name) assert ("Location argument must be a sub-class of fparser.two.utils.Base " "but got: " in str(excinfo.value))
def test_adduse_noonly_names(parser): '''Test that the expected output is obtained when variable/function names are provided for a use statement and the value for the only argument is not provided. ''' parse_tree = get_parse_tree(CODE, parser) location = parse_tree.content[0].content[0] name = "my_use" adduse(location, name, funcnames=["a", "b", "c"]) assert ("PROGRAM test\n USE my_use, ONLY: a, b, c\n" " INTEGER :: i\n") in str(parse_tree)
def test_adduse_only_nonames(parser): '''Test that the expected output is obtained when no variable/function names are provided for a use statement and only is True. ''' parse_tree = get_parse_tree(CODE, parser) location = parse_tree.content[0].content[0] name = "my_use" adduse(location, name, only=True) assert "PROGRAM test\n USE my_use, ONLY:\n INTEGER :: i\n" \ in str(parse_tree)
def test_adduse_only_names1(parser): '''Test that the expected output is obtained in a Fortran program when variable/function names are provided for a use statement and only is True. ''' parse_tree = get_parse_tree(CODE, parser) location = parse_tree.content[0].content[0] name = "my_use" adduse(location, name, only=True, funcnames=["a", "b", "c"]) assert "PROGRAM test\n USE my_use, ONLY: a, b, c\n INTEGER :: i\n" \ in str(parse_tree)
def test_adduse_onlyfalse_names(parser): '''Test that an exception is raised when variable/function names are provided for a use statement and the value for the only argument is set to False. ''' parse_tree = get_parse_tree(CODE, parser) location = parse_tree.content[0].content[0] name = "my_use" with pytest.raises(GenerationError) as excinfo: adduse(location, name, only=False, funcnames=["a", "b", "c"]) assert ("If the 'funcnames' argument is provided and has content, " "then the 'only' argument must not be set to " "'False'.") in str(excinfo.value)
def test_adduse_unsupportedparent1(parser): '''Test that the expected exception is raised when the specified location has an ancestor that is a module. ''' parse_tree = get_parse_tree( "module test\n" " integer :: i\n" "end module test\n", parser) location = parse_tree.content[0].content[0] name = "my_use" with pytest.raises(NotImplementedError) as excinfo: adduse(location, name) assert ("Currently support is limited to program, subroutine and " "function.") in str(excinfo.value)
def test_adduse_nospec(parser): '''Test that the expected exception is raised when the ancestor (a program or a subroutine) does not have a specification part as its second child location has a parent that is a function. This is the case if a program has no content. This could be considered to be a bug but we'll treat it as a feature at the moment. ''' parse_tree = get_parse_tree("program test\n" "end program test\n", parser) location = parse_tree.content[0].content[0] name = "my_use" with pytest.raises(InternalError) as excinfo: adduse(location, name) assert ("The second child of the parent code (content[1]) is expected " "to be a specification part but found 'End_Program_Stmt" "('PROGRAM', Name('test'))'.") in str(excinfo.value)
def test_adduse_only_names3(parser): '''Test that the expected output is obtained in a Fortran function when variable/function names are provided for a use statement and only is True. ''' parse_tree = get_parse_tree( "integer function test()\n" " integer :: i\n" " return i\n" "end function test\n", parser) location = parse_tree.content[0].content[0] name = "my_use" adduse(location, name, only=True, funcnames=["a", "b", "c"]) assert ("INTEGER FUNCTION test()\n USE my_use, ONLY: a, b, c\n" " INTEGER :: i\n") in str(parse_tree)
def test_adduse_noprogparent(parser): '''Test that the expected exception is raised when the specified location has no parent that is one of main_program, module, subroutine or function. ''' parse_tree = get_parse_tree(CODE, parser) # Choose the Program_Stmt node and then patch it so that it has # no parent location = parse_tree.content[0].content[0] location.parent = None name = "my_use" with pytest.raises(GenerationError) as excinfo: adduse(location, name) assert ("The specified location is invalid as it has no parent in the " "parse tree that is a program, module, subroutine or " "function.") in str(excinfo.value)
def test_adduse_location_none(): '''Test that the expected exception is raised when the specified location is None. There is a check for this as the parse tree can contain nodes with the value None. ''' location = None with pytest.raises(GenerationError) as excinfo: _ = adduse(None, location, None) assert "Location argument must not be None." \ in str(excinfo.value)
def test_adduse_nolocation(parser): '''Test that the expected exception is raised when the specified location is not in the parse_tree ''' parse_tree = get_parse_tree(CODE, parser) location = "lilliput" name = "my_use" with pytest.raises(GenerationError) as excinfo: _ = adduse(parse_tree, location, name) assert "The specified location is not in the parse tree." \ in str(excinfo.value)
def test_adduse_noonly_nonames(parser): '''Test that the expected output is obtained when no variable/function names are provided for a use statement and only is not explicitly set. ''' parse_tree = get_parse_tree(CODE, parser) location = parse_tree.content[0].content[0] name = "my_use" new_parse_tree = adduse(parse_tree, location, name) assert "PROGRAM test\n USE my_use\n INTEGER :: i\n" \ in str(new_parse_tree)
def test_adduse_noprogparent(parser): '''Test that the expected exception is raised when the specified location has no parent that is one of main_program, module, subroutine or function. ''' parse_tree = get_parse_tree(CODE, parser) # location is main_program which is not a child of program. location = parse_tree.content[0] name = "my_use" with pytest.raises(GenerationError) as excinfo: _ = adduse(parse_tree, location, name) assert ("The specified location is invalid as it has no parent in the " "parse tree that is a program, module, subroutine or " "function.") in str(excinfo.value)
def test_adduse_only_names2(parser): '''Test that the expected output is obtained in a Fortran subroutine when variable/function names are provided for a use statement and only is True. ''' parse_tree = get_parse_tree( "subroutine test()\n" " integer :: i\n" " i=0\n" "end subroutine test\n", parser) location = parse_tree.content[0].content[0] name = "my_use" new_parse_tree = adduse(parse_tree, location, name, only=True, funcnames=["a", "b", "c"]) assert ("SUBROUTINE test\n USE my_use, ONLY: a, b, c\n" " INTEGER :: i\n") in str(new_parse_tree)