Skip to content

Commit

Permalink
MetOffice#20 LabelledExit unit test and remove plural
Browse files Browse the repository at this point in the history
  • Loading branch information
bilal-chughtai committed Feb 25, 2022
1 parent 440c78c commit 8d6c7f6
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 8 deletions.
2 changes: 1 addition & 1 deletion source/stylist/fortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def examine_fortran(self, subject: FortranSource) -> List[Issue]:
return issues


class LabelledExits(FortranRule):
class LabelledExit(FortranRule):
"""
Catches cases where a construct is exited but not explicitly named.
"""
Expand Down
4 changes: 2 additions & 2 deletions system-tests/fortran/configuration.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ rules = MissingPointerInit
[style.wrong_kind]
rules = KindPattern(integer=r'.*_jazz', real=r'.*_metal')

[style.labelled_exits]
rules = LabelledExits
[style.labelled_exit]
rules = LabelledExit

[style.multiple]
rules = FortranCharacterset, MissingImplicit(require_everywhere=True), MissingPointerInit
5 changes: 5 additions & 0 deletions system-tests/fortran/expected.labelled_exit.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
stdout
Found 2 issues
stderr
$$/labelled_exit.f90: 4: Usage of "exit" without label indicating which do construct is being exited from.
$$/labelled_exit.f90: 10: Usage of "exit" without label indicating which do construct is being exited from.
5 changes: 0 additions & 5 deletions system-tests/fortran/expected.labelled_exits.txt

This file was deleted.

File renamed without changes.
78 changes: 78 additions & 0 deletions unit-tests/fortran_labelled_exit_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env python
##############################################################################
# (c) Crown copyright 2018 Met Office. All rights reserved.
# The file LICENCE, distributed with this code, contains details of the terms
# under which the code may be used.
##############################################################################
"""
Test of the rule for missing exit labels.
"""
from typing import List

import pytest # type: ignore
# ToDo: Obviously we shouldn't be importing "private" modules but until pytest
# sorts out its type hinting we are stuck with it.
#
from _pytest.fixtures import FixtureRequest # type: ignore

import stylist.fortran
from stylist.source import FortranSource, SourceStringReader


class TestLabelledExit(object):
"""
Tests the checker of missing exit labels.
"""
@pytest.fixture(scope='class',
params=['', 'foo'])
def do_construct_name(self, request: FixtureRequest) -> str:
"""
Parameter fixture giving do construct names to exit statement
"""
return request.param

def test_exit_labels(self,
do_construct_name: str) -> None:
"""
Checks that the rule reports missing exit labels correctly.
"""
template = '''
program test
contains
function function1()
foo : do
exit {do_construct_name}
end do foo
end function function1
function function2()
foo : do
if (.true.) exit {do_construct_name}
end do foo
end function function2
end program test
'''

expectation: List[str] = []
message = '{line}: Usage of "exit" without label indicating which do construct is being exited from.'
if do_construct_name == '':
expectation.extend([
message.format(line=6),
message.format(line=12)
])
expectation.sort(key=lambda x: (int(x.split(':', 1)[0]),
x.split(':', 1)))

text = template.format(
do_construct_name=do_construct_name)
print(text) # Shows up in failure reports, for debugging
reader = SourceStringReader(text)
source = FortranSource(reader)
unit_under_test = stylist.fortran.LabelledExit()
issues = unit_under_test.examine(source)
issue_descriptions = [str(issue) for issue in issues]
issue_descriptions.sort(key=lambda x: (int(x.split(':', 1)[0]),
x.split(':', 1)))
print(issue_descriptions)
print(expectation)
assert issue_descriptions == expectation

0 comments on commit 8d6c7f6

Please # to comment.