Skip to content

Commit

Permalink
#320 Added more tests as requested in review.
Browse files Browse the repository at this point in the history
  • Loading branch information
hiker committed Oct 11, 2024
1 parent e0ab61e commit 442fc01
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 8 deletions.
38 changes: 38 additions & 0 deletions src/fparser/two/tests/fortran2008/test_stop_code_r857.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@

import pytest
from fparser.two.utils import NoMatchError
from fparser.two import Fortran2003, utils
from fparser.two.Fortran2008 import Stop_Code

from fparser.api import get_reader
from fparser.two.utils import NoMatchError, walk


@pytest.mark.usefixtures("f2008_create")
@pytest.mark.parametrize("string", ["1", "- 1", '"abc"', "'abc'", "'abc' // 'def'"])
Expand All @@ -57,3 +61,37 @@ def test_simple_stop_code_errors(string):
with pytest.raises(NoMatchError) as err:
Stop_Code(string)
assert f"Stop_Code: '{string}'" in str(err.value)


@pytest.mark.parametrize("string", ["1", "12345"])
def test_stop_stmt_2003_stop_code(f2008_parser, string):
"""Test that 'stop' parsing works in real code, and returns a 2003
StopCode. This is the case if the stop code is between one and
five digits only:
"""
code = f"""
subroutine dummy()
stop {string}
end subroutine dummy
"""
tree = f2008_parser(get_reader(code))
stop_code = walk(tree, Fortran2003.Stop_Code)[0]
assert str(stop_code) == string


@pytest.mark.parametrize("string", ["1234567", "12 .AND. 34"])
def test_stop_stmt_2008(f2008_parser, string, monkeypatch):
"""Test that stop parsing works in real code when using F2008
only (i.e. not F2003) statements. Note that '12 .and. 34' is a
level-5-expr, and as such would not be accepted by the F2003
"extended-stop-args" extension in fparser.
"""
monkeypatch.setattr(utils, "_EXTENSIONS", [])
code = f"""
subroutine dummy()
stop {string}
end subroutine dummy
"""
tree = f2008_parser(get_reader(code))
stop_stmt = walk(tree, Fortran2003.Stop_Stmt)[0]
assert str(stop_stmt.children[1]) == string
47 changes: 39 additions & 8 deletions src/fparser/two/tests/test_fortran2003.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Modified work Copyright (c) 2017-2023 Science and Technology
# Modified work Copyright (c) 2017-2024 Science and Technology
# Facilities Council.
# Original work Copyright (c) 1999-2008 Pearu Peterson
#
Expand Down Expand Up @@ -68,10 +68,12 @@
"""

import pytest

from fparser.two.Fortran2003 import *
from fparser.two import Fortran2003
from fparser.two.symbol_table import SYMBOL_TABLES
from fparser.two.utils import NoMatchError
from fparser.two import utils
from fparser.api import get_reader


Expand Down Expand Up @@ -2096,7 +2098,17 @@ def test_continue_stmt(): # R848
assert repr(obj) == "Continue_Stmt('CONTINUE')"


def test_stop_stmt(): # R849
@pytest.mark.parametrize("standard_only", [True, False])
def test_stop_stmt_standard_2003(standard_only, monkeypatch):
'''Test that stop statements are parsed correctly [R849].
It tests both pure 2003 standard compliance, but also
that negative numbers and string concatenations are accepted.
'''
if standard_only:
# Disable the stop-stmt extension for this test to verify
# that really only standard expressions are accepted
monkeypatch.setattr(utils, "_EXTENSIONS", [])

tcls = Stop_Stmt
obj = tcls("stop")
assert isinstance(obj, tcls), repr(obj)
Expand All @@ -2110,13 +2122,32 @@ def test_stop_stmt(): # R849
assert isinstance(obj, tcls), repr(obj)
assert str(obj) == "STOP 'hey you'"

obj = tcls('stop "123"//"456"')
assert isinstance(obj, tcls), repr(obj)
assert str(obj) == 'STOP "123" // "456"'
# This should not be accepted even with the extension enabled:
with pytest.raises(NoMatchError) as excinfo:
tcls("stop 12 .and. 34")
assert "Stop_Stmt: 'stop 12 .and. 34'" in str(excinfo.value)

if standard_only:
# This should not be accepted according to F2003
with pytest.raises(NoMatchError) as excinfo:
tcls('stop "123"//"456"')
assert "Stop_Stmt: 'stop \"123\"//\"456\"" in str(excinfo.value)

# This should not be accepted according to F2003
with pytest.raises(NoMatchError) as excinfo:
tcls("stop -321")
assert "Stop_Stmt: 'stop -321'" in str(excinfo.value)

else:
# Test the F2003 standard extensions, which should
# accept these expressions
obj = tcls('stop "123"//"456"')
assert isinstance(obj, tcls), repr(obj)
assert str(obj) == 'STOP "123" // "456"'

obj = tcls("stop -321")
assert isinstance(obj, tcls), repr(obj)
assert str(obj) == "STOP - 321"
obj = tcls("stop -321")
assert isinstance(obj, tcls), repr(obj)
assert str(obj) == "STOP - 321"


#
Expand Down

0 comments on commit 442fc01

Please sign in to comment.