def test_ignore_comments(): ''' Check that the parser does throw away comments when requested ''' tree = Program( get_reader('''\ PROGRAM a_prog ! A full line comment PRINT *, "Hello" ! This block gets executed END PROGRAM a_prog ''', ignore_comments=True)) gen = str(tree) assert ("PROGRAM a_prog\n" " PRINT *, \"Hello\"\n" "END PROGRAM a_prog" in gen) assert "full line comment" not in gen assert "block gets executed" not in gen # Check that the default behaviour is to ignore comments tree = Program( get_reader('''\ PROGRAM a_prog ! A full line comment PRINT *, "Hello" ! This block gets executed END PROGRAM a_prog ''')) gen = str(tree) assert "full line comment" not in gen assert "block gets executed" not in gen
def test_mix(f2003_create): '''Test that multiple program_units can be parsed successfully with a mix of includes and comments. ''' reader = get_reader(("include '1'\n" "! comment1\n" "include '2'\n" "subroutine test()\n" "end subroutine\n" "include '3'\n" "include '4'\n" "! comment2\n" "! comment3\n" "module example\n" "end module\n" "! comment4\n" "include '5'\n" "! comment5\n"), ignore_comments=False) ast = Program(reader) assert ("INCLUDE '1'\n" "! comment1\n" "INCLUDE '2'\n" "SUBROUTINE test\n" "END SUBROUTINE\n" "INCLUDE '3'\n" "INCLUDE '4'\n" "! comment2\n" "! comment3\n" "MODULE example\n" "END MODULE\n" "! comment4\n" "INCLUDE '5'\n" "! comment5") in str(ast)
def test_single_error2(f2003_create): '''Test that a single program_unit with an error in the final statement raises an appropriate exception ''' reader = get_reader("subroutine test()\n\n" "end subroutin\n\n\n") with pytest.raises(FortranSyntaxError) as excinfo: dummy = Program(reader) assert ("at line 3\n>>>end subroutin\n" in str(excinfo.value))
def test_single(f2003_create): '''Test that a single program_unit can be parsed successfully.''' reader = get_reader('''\ subroutine test() end subroutine ''') ast = Program(reader) assert "SUBROUTINE test\n" \ "END SUBROUTINE" in str(ast)
def test_empty_input(f2003_create): '''Test that empty input or input only containing white space can be parsed succesfully ''' for code in ["", " ", " \n \n\n"]: reader = get_reader(code) ast = Program(reader) assert str(ast) == ""
def test_intrinsic_inside_intrinsic(f2003_create): '''Test that when an intrinsic is within another instrinsic then both are recognised as intrinsics. ''' reader = get_reader("subroutine sub()\na = sin(cos(b))\nend " "subroutine sub\n") ast = Program(reader) assert "Intrinsic_Name('SIN')" in repr(ast) assert "Intrinsic_Name('COS')" in repr(ast)
def test_comment0(f2003_create): '''Test that a single program_unit without comments can be parsed successfully with comment processing switched on. ''' reader = get_reader(("subroutine test()\n" "end subroutine\n"), ignore_comments=False) ast = Program(reader) assert ("SUBROUTINE test\n" "END SUBROUTINE") in str(ast)
def test_one_main1(f2003_create): '''Test that multiple main programs raise an exception.''' reader = get_reader('''\ program first end program second end ''') with pytest.raises(FortranSyntaxError) as excinfo: dummy_ = Program(reader) assert ("XXX" in str(excinfo.value))
def test_single_error1(f2003_create): '''Test that a single program_unit with an error in the initial statement raises an appropriate exception ''' reader = get_reader('''\ subroutin test() end subroutine ''') with pytest.raises(FortranSyntaxError) as excinfo: dummy_ = Program(reader) assert ("at line 1\n>>> subroutin test()\n" in str(excinfo.value))
def test_missing_prog(f2003_create): '''Test that a main program program_unit without a program declaration can be parsed successfully. This should not really be a test here, but this case is currently treated separately by the match method in Program. ''' reader = get_reader('''\ end ''') ast = Program(reader) assert "END" in str(ast)
def test_intrinsic_error(f2003_create): '''Test that Program raises the expected exception when there is an intrinsic syntax error. ''' reader = get_reader("subroutine sub()\na = sin(b,c)\nend subroutine sub\n") with pytest.raises(FortranSyntaxError) as excinfo: _ = Program(reader) assert ("at line 2\n" ">>>a = sin(b,c)\n" "Intrinsic 'SIN' expects 1 arg(s) but found 2") in str( excinfo.value)
def test_single(): '''Test that a single program_unit can be parsed successfully.''' reader = get_reader('''\ subroutine test() end subroutine ''') ast = Program(reader) assert "SUBROUTINE test\n" \ "END SUBROUTINE" in str(ast) # Check that the Name of the subroutine has the correct parent assert ast.content[0].content[0].items[1].parent is \ ast.content[0].content[0]
def test_single_with_end_name(): '''Test that a single program_unit can be parsed successfully when it has a name specified on the end clause.''' reader = get_reader('''\ subroutine test() end subroutine test ''') ast = Program(reader) assert "SUBROUTINE test\n" \ "END SUBROUTINE test" in str(ast) # Check parent information has been set-up correctly end_sub = ast.content[0].content[-1] assert end_sub.items[1].parent is end_sub
def test_single2(f2003_create): '''Test that a single program_unit with 5 or more spaces at the start of the line reports an error on the correct (first) line ''' reader = get_reader('''\ subroutin test() end subroutine ''') with pytest.raises(FortranSyntaxError) as excinfo: dummy_ = Program(reader) assert ("at line 1\n>>> subroutin test()\n" in str(excinfo.value))
def test_comment2(f2003_create): '''Test that a single program_unit can be parsed successfully with comments being included.''' reader = get_reader(("! comment1\n" "subroutine test()\n" "end subroutine\n" "! comment2\n"), ignore_comments=False) ast = Program(reader) assert ("! comment1\n" "SUBROUTINE test\n" "END SUBROUTINE\n" "! comment2") in str(ast)
def test_comment1(f2003_create): '''Test that a single program_unit can be parsed successfully with comments being ignored.''' reader = get_reader('''\ ! comment1 subroutine test() end subroutine ! comment2 ''') ast = Program(reader) assert "SUBROUTINE test\n" \ "END SUBROUTINE" in str(ast) assert "! comment" not in str(ast)
def test_module_comments(): ''' Tests for comments in modules ''' source = '''! This is a module module my_mod implicit none private end module my_mod ''' # Test when the reader is explicitly set to free-form mode reader = get_reader(source, isfree=True, ignore_comments=False) prog_unit = Program(reader) assert type(prog_unit.content[0]) == Comment assert str(prog_unit.content[0]) == "! This is a module"
def test_multiple_error3(f2003_create): '''Test that multiple program_units with an error raises an appropriate exception ''' reader = get_reader('''\ subroutine test() end subroutine subroutine test() end subroutin''') with pytest.raises(FortranSyntaxError) as excinfo: dummy_ = Program(reader) assert ("at line 4\n>>> end subroutin\n" in str(excinfo.value))
def test_include0(f2003_create): '''Test that a single program_unit with includes can be parsed succesfully. ''' reader = get_reader(("include '1'\n" "subroutine test()\n" "end subroutine\n" "include '2'\n")) ast = Program(reader) assert ("INCLUDE '1'\n" "SUBROUTINE test\n" "END SUBROUTINE\n" "INCLUDE '2'") in str(ast)
def test_multiple_error1(f2003_create): '''Test that multiple program_units with the same name raise an exception ''' reader = get_reader('''\ subroutine test() end subroutine subroutine test() end subroutine ''') with pytest.raises(FortranSyntaxError) as excinfo: dummy_ = Program(reader) assert ("XXX" in str(excinfo.value))
def test_missing_prog_multi(f2003_create): '''Test that a main program program_unit without a program declaration can be parsed successfully when it is not the first program_unit. ''' reader = get_reader('''\ subroutine first end end ''') ast = Program(reader) assert "SUBROUTINE first\n" \ "END SUBROUTINE" in str(ast) assert "END PROGRAM" in str(ast)
def test_simple_prog(): ''' Tests simplest case of comments in a program unit ''' tree = Program( get_reader('''\ PROGRAM a_prog ! A full line comment PRINT *, "Hello" ! This block gets executed END PROGRAM a_prog ''', ignore_comments=False)) assert (str(tree) == "PROGRAM a_prog\n" " ! A full line comment\n" " PRINT *, \"Hello\"\n" " ! This block gets executed\n" "END PROGRAM a_prog\n")
def test_to_be_moved(f2003_create): '''Show that an example with a space before a kind specification fails. This is not the fault of this class but I'm including it here for the moment until the reason for the error is fixed. ''' reader = get_reader('''\ subroutine test() integer( x) y end subroutine ''') ast = Program(reader) assert ("SUBROUTINE test\n" " integer( x) y\n" "END SUBROUTINE") in str(ast)
def test_comment3(f2003_create): '''Test that multiple program_units can be parsed successfully with comments being ignored.''' reader = get_reader('''\ ! comment1 subroutine test() end subroutine ! comment2 module example end module ! comment3 ''', ignore_comments=True) ast = Program(reader) assert ("SUBROUTINE test\n" "END SUBROUTINE\n" "MODULE example\n" "END MODULE") in str(ast) assert "! comment" not in str(ast)
def test_comment4(f2003_create): '''Test that multiple program_units can be parsed successfully with comments being included.''' reader = get_reader(("! comment1\n" "subroutine test()\n" "end subroutine\n" "! comment2\n" "module example\n" "end module\n" "! comment3\n"), ignore_comments=False) ast = Program(reader) assert ("! comment1\n" "SUBROUTINE test\n" "END SUBROUTINE\n" "! comment2\n" "MODULE example\n" "END MODULE\n" "! comment3") in str(ast)
def test_blockbase_tofortran_non_ascii(): ''' Check that the tofortran() method works when we have a program containing non-ascii characters within a sub-class of BlockBase. We use a Case Construct for this purpose. ''' from fparser.common.readfortran import FortranStringReader from fparser.two.utils import BlockBase, walk from fparser.two.Fortran2003 import Program, Case_Construct code = (u"program my_test\n" u"! A comment outside the select block\n" u"SELECT CASE(iflag)\n" u"CASE( 30 )\n" u" IF(lwp) WRITE(*,*) ' for e1=1\xb0'\n" u"END SELECT\n" u"end program\n") reader = FortranStringReader(code, ignore_comments=False) obj = Program(reader) bbase = walk(obj.content, Case_Construct)[0] # Explicitly call tofortran() on the BlockBase class. out_str = BlockBase.tofortran(bbase) assert "for e1=1" in out_str
def test_inline_ifthen(): ''' Tests for in-line comments within an if-then block ''' tree = Program( get_reader('''\ PROGRAM a_prog IF(.TRUE.)THEN PRINT *, "Hello" ! An in-line comment here END IF ! A comment after a block END PROGRAM a_prog ''', ignore_comments=False)) assert isinstance(tree, Program) assert (str(tree) == "PROGRAM a_prog\n" " IF (.TRUE.) THEN\n" " PRINT *, \"Hello\"\n" " ! An in-line comment here\n" " END IF\n" " ! A comment after a block\n" "END PROGRAM a_prog\n")
def test_include1(f2003_create): '''Test that multiple program_units with includes can be parsed successfully. ''' reader = get_reader( "include '1'\n" "subroutine test()\n" "end subroutine\n" "include '2'\n" "module example\n" "end module\n" "include '3'\n", ignore_comments=True) ast = Program(reader) assert ("INCLUDE '1'\n" "SUBROUTINE test\n" "END SUBROUTINE\n" "INCLUDE '2'\n" "MODULE example\n" "END MODULE\n" "INCLUDE '3'") in str(ast) assert "! comment" not in str(ast)
def test_intrinsic_recognised(): '''Test that an intrinsic is picked up when used in a program.''' reader = get_reader("subroutine sub()\na = sin(b)\nend subroutine sub\n") ast = Program(reader) assert walk(ast, Intrinsic_Function_Reference)