forked from MetOffice/SimSys_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_umdp3_rules_S3.py
More file actions
492 lines (451 loc) · 18.5 KB
/
Copy pathtest_umdp3_rules_S3.py
File metadata and controls
492 lines (451 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# -----------------------------------------------------------------------------
# (C) Crown copyright Met Office. All rights reserved.
# The file LICENCE, distributed with this code, contains details of the terms
# under which the code may be used.
# -----------------------------------------------------------------------------
# from pyparsing import remove_quotes
import pytest
import sys
from pathlib import Path
# Add the current directory to Python path
sys.path.insert(0, str(Path(__file__).parent.parent))
from umdp3_rules_S3 import (
remove_comments,
remove_quoted,
concatenate_lines,
r3_1_1_there_can_be_only_one,
r3_2_1_check_crown_copyright,
r3_3_2_line_too_long,
r3_4_1_capitalised_keywords,
r3_4_2_no_full_uppercase_variable_names,
)
# from umdp3_checker_rules import TestResult, UMDP3Checker
def modify_fortran_lines(lines_in: list[str], changes: list[list]) -> list[str]:
"""Return a copy of example_fortran_lines with changes applied.
``changes`` is a list of operation list, each with:
- ``[<operation>, <N>, [<new line(s)>]]``
- where : <operation> = "replace" would replace line N with the new line(s)
- <operation> = delete" would remove line N
- and <operation> ="add" would insert the new line(s) after line N.
Operations are applied in descending line order so that earlier
line numbers are not shifted by later mutations.
"""
lines = lines_in.copy()
for change in sorted(changes, key=lambda o: o[1], reverse=True):
idx = int(change[1]) - 1
if change[0] == "replace":
del lines[idx]
print(f"Line {idx} : deleting line.")
for new_line in change[2]:
print(f'Line {idx} : replacing with "{new_line}"')
lines.insert(idx, new_line)
idx += 1
elif change[0] == "delete":
print(f"Line {idx} : deleting line.")
del lines[idx]
elif change[0] == "add":
for new_line in change[2]:
print(f'Line {idx} : adding "{new_line}"')
lines.insert(idx, new_line)
idx += 1
else:
raise ValueError(f"Unknown operation: {change[0]}")
# for count, line in enumerate(lines, 1):
# print(f"line [{count}]: {line}")
return lines
# =================================================================
"""First : test the helper functions in umdp3_rules_S3.py."""
def test_modify_fortran_lines(example_fortran_lines):
"""TODO: Does this need to be more rigorous ?"""
changes_list = [
["replace", 12, ["This is a replacement line."]],
["delete", 22, None],
["add", 32, ["This is an added line."]],
]
modified_lines = modify_fortran_lines(example_fortran_lines, changes_list)
# Line no.s below have to be carefully calculated. Basic is line no of change -1 but then add one for every line added aobve in the file and -1 for every line deleted above in the file.
# for line_no, line in enumerate(modified_lines, 1):
# print(f"line [{line_no:04}]: {line}")
assert modified_lines[11] == "This is a replacement line."
assert len(modified_lines) == len(
example_fortran_lines
) # One line deleted, One added
# Added @ 32, so 31, but one line deleted above, so 30 - seeemples
assert modified_lines[30] == "This is an added line."
def test_remove_quoted(example_fortran_lines):
for line in example_fortran_lines.copy():
uncommented_line = remove_quoted(line)
assert '"' not in uncommented_line
assert "'" not in uncommented_line
def test_remove_comments(example_fortran_lines):
for line in example_fortran_lines.copy():
line = remove_quoted(line)
uncommented_line = remove_comments(line)
comment_location = line.find("!")
if comment_location != -1:
assert uncommented_line == line[:comment_location].rstrip()
assert "!" not in uncommented_line
def test_concatenate_lines(example_fortran_lines):
for line_no, line in enumerate(example_fortran_lines.copy(), 1):
if line.find("&") >= 0:
concatenated_line = concatenate_lines(example_fortran_lines.copy(), line_no)
assert "&" not in concatenated_line
# This bit has to be a bit hard-wired and is going to break every time the example Fortran file line numbers are changed. If you can think of a better method - go ahead.
if line_no == 31:
expected_line = (
"SUBROUTINE example (xlen,ylen,l_unscale,input1,"
+ "input2, output, l_loud_opt)"
)
assert concatenated_line == expected_line
elif line_no == 70:
# This will look odd as the quoted text has been removed.
expected_line = (
"my_char "
+ " = // "
)
assert concatenated_line == expected_line
elif line_no == 96:
expected_line = (
" field2(i, j) = (1.0*i) - (2.0*j) "
+ " + (3.0*i*j) + "
+ "(4.0*i**2) + field(i, j)*2"
)
assert concatenated_line == expected_line
# =================================================================
"""These examples hopefully demonstrate some of the complexity available...
Setting up a test, may involve multiple changes to the demo Fortran file, hence the complex entries in the parametrization.
Each error found is recorded with the line number(s) it was found on using the Error text as a dict key, and the line no(s) as a list, which means finding 'use' and 'Use' in the code would generate 2 different keys in the dict.
Then a single value recording the total number of failures is also included in the 'TestResult' object.
So for the dictionary of errors returned, we have to check they match, which at present involves checking it's 'len' but also that all the keys in one are in the other, i.e. youre not accidentally getting a matching count but different errors.
Then for each error(key) in the dict, you need to check how many lines it occured on, and that the line numbers match, i.e. the list of lines numbers is a match with the expected list of line numbers.
There has to be a better way.....
As a side note, might it be better to write a function to compare 2 'TestResult' objects, which would be more robust to changes in the structure of the 'TestResult' object, and also make the test code more readable? If so, would it's place be here in the testing, or as a method in the 'TestResult' class itself?
"""
# =================================================================
@pytest.mark.parametrize(
"changes_list, expected_passed, expected_failure_count, expected_errors",
[
# Invalid: No program unit found (delete MODULE line)
(
[["replace", 12, ["! No module declaration here"]]],
False,
1,
{
"First executable line doesn't define an accepted programming unit :"
" IMPLICIT NONE": [0]
},
),
# Invalid: Mismatched END statement
(
[["replace", 121, ["END MODULE wrong_mod_name"]]],
False,
1,
{
"END statement found for a different program unit: should be example_mod got wrong_mod_name.": [
0
]
},
),
# Invalid: No END statement found
(
[["replace", 121, ["! Missing END MODULE"]]],
False,
1,
{
"Last executable line not a matching END statement for the first program unit found.": [
0
]
},
),
# Pass ccp directives #if !defined(MCT)
(
[["replace", 1, ["#if !defined(MCT)"]], ["add", 121, ["#endif"]]],
True,
0,
{},
),
# Valid: MODULE example_mod ... END MODULE example_mod (no changes)
([], True, 0, {}),
],
ids=[
"No program unit found",
"Mismatched END statement",
"No END statement",
"Valid checking cpp directives pass",
"Valid module with matching END",
],
)
def test_r3_1_1_there_can_be_only_one(
example_fortran_lines,
changes_list,
expected_passed,
expected_failure_count,
expected_errors,
):
modified_fortran_lines = modify_fortran_lines(example_fortran_lines, changes_list)
result = r3_1_1_there_can_be_only_one(modified_fortran_lines)
errors = result.errors
for error, lines_list in errors.items():
assert error in expected_errors
for line_no in lines_list:
assert line_no in expected_errors[error]
assert len(lines_list) == len(expected_errors[error])
assert len(errors) == len(expected_errors)
assert result.failure_count == expected_failure_count
assert result.passed == expected_passed
# =================================================================
@pytest.mark.parametrize(
"changes_list, expected_result, expected_errors",
[
(
[
["delete", 3, None],
["delete", 4, None],
["delete", 5, None],
["delete", 6, None],
["delete", 7, None],
],
1,
{"missing copyright or crown copyright statement": [0]},
),
([], 0, {}), # No changes, expect no errors
],
ids=["Missing copyright statement", "copyright statement present"],
)
def test_r3_2_1_check_crown_copyright(
example_fortran_lines, changes_list, expected_result, expected_errors
):
# checker = UMDP3Checker()
modified_fortran_lines = modify_fortran_lines(example_fortran_lines, changes_list)
result = r3_2_1_check_crown_copyright(modified_fortran_lines)
failure_count = result.failure_count
errors = result.errors
for error, lines_list in errors.items():
assert error in expected_errors
for line_no in lines_list:
assert line_no in expected_errors[error]
assert len(lines_list) == len(expected_errors[error])
assert len(errors) == len(expected_errors)
assert failure_count == expected_result
# =================================================================
@pytest.mark.parametrize(
"changes_list, expected_result, expected_errors",
[
(
[
[
"replace",
73,
[
' = "This is a very very very very very very very "'
+ " &"
],
],
[
"replace",
117,
[
"IF (lhook) CALL dr_hook(ModuleName// "
+ '":" // RoutineName, zhook_out, zhook_handle) ! extra comment'
],
],
],
2,
{"line too long": [73, 117]},
),
([], 0, {}), # No changes, expect no errors
],
ids=["3 line too long Errors", "No line too long Errors"],
)
def test_r3_3_2_line_too_long(
example_fortran_lines, changes_list, expected_result, expected_errors
):
# checker = UMDP3Checker()
modified_fortran_lines = modify_fortran_lines(example_fortran_lines, changes_list)
result = r3_3_2_line_too_long(modified_fortran_lines)
failure_count = result.failure_count
errors = result.errors
for error, lines_list in errors.items():
assert error in expected_errors
for line_no in lines_list:
assert line_no in expected_errors[error]
assert len(lines_list) == len(expected_errors[error])
assert len(errors) == len(expected_errors)
assert failure_count == expected_result
# =================================================================
@pytest.mark.parametrize(
"changes_list, expected_result, expected_errors",
[
(
[
["replace", 12, ["Module example_mod"]],
["replace", 39, ["use parkind1, ONLY: jpim, jprb"]],
["replace", 42, ["use yomhook, ONLY: lhook, dr_hook"]],
],
3,
{"lowercase keyword: Module": [12], "lowercase keyword: use": [39, 42]},
),
(
[
["add", 44, ["#if defined(SOMETHING)"]],
["add", 47, ["#else", "INTEGER, INTENT(INOUT) :: xlen",
"INTEGER, INTENT(OUT) :: ylen",
"LOGICAL, INTENT(IN) :: l_WhoopSies = .FALSE.", "#endif"]],
],
0,
{},
),
([], 0, {}), # No changes, expect no errors
],
ids=["3 Lowercase Errors", "Pass cpp directives", "No Lowercase Errors"],
)
def test_r3_4_1_capitalised_keywords(
example_fortran_lines, changes_list, expected_result, expected_errors
):
modified_fortran_lines = modify_fortran_lines(example_fortran_lines, changes_list)
result = r3_4_1_capitalised_keywords(modified_fortran_lines)
failure_count = result.failure_count
errors = result.errors
for error, lines_list in errors.items():
assert error in expected_errors
for line_no in lines_list:
assert line_no in expected_errors[error]
assert len(lines_list) == len(expected_errors[error])
assert len(errors) == len(expected_errors)
assert failure_count == expected_result
# =================================================================
# See if you can spot why I hate ruff - it highlights my awful data structures really well.
@pytest.mark.parametrize(
"changes_list, expected_result, expected_errors",
[
(
[
[
"replace",
45,
["INTEGER, INTENT(IN) :: XLEN !Length of first dim of the arrays."],
],
["replace", 61, ["REAL :: var1, DAVE_2, HiPPo"]],
],
2,
{
"Found UPPERCASE variable name in declaration at line 45: \"XLEN\"": [45],
"Found UPPERCASE variable name in declaration at line 61: \"DAVE_2\"": [61],
},
),
(
[
[
"add",
59,
["LOGICAL :: l_whizz_bang = .FALSE. ! optimisation flag"],
],
],
0,
{},
),
(
[
[
"add",
61,
[
"REAL :: VARIaBLE_1, variable_2, &",
" VARIABLE_3, Hot_Potato, Baked Potato &",
" nice_var, good_var, camelCase, & !comment test",
" CAPS_VAR, CASPVAR, the_ghost",
],
],
[
"replace",
57,
[
"INTEGER :: j ! Loop counter &",
"INTEGER :: k ! Loop counter &",
"INTEGER :: IJ ! Loop counter",
],
],
[
"replace",
45,
[
"INTEGER, INTENT(IN) :: XLEN !Length of first dimension of the"
+ " arrays."
],
],
],
5,
{
"Found UPPERCASE variable name in declaration at line 45: \"XLEN\"": [45],
"Found UPPERCASE variable name in declaration at line 59: \"IJ\"": [59],
"Found UPPERCASE variable name in declaration at line 63: \"CASPVAR\"": [
63
],
"Found UPPERCASE variable name in declaration at line 63: \"VARIABLE_3\"": [
63
],
"Found UPPERCASE variable name in declaration at line 63: \"CAPS_VAR\"": [
63
],
},
),
(
[
["delete", 48, None],
[
"replace",
49,
[
"REAL, INTENT(IN) :: input1(xlen, ylen), & !First input array",
" input2(XLEN, ylen), !Second input array",
],
],
# [],
],
0,
{},
),
(
[
["delete", 48, None],
[
"replace",
49,
[
"REAL, INTENT(IN) :: input1(xlen, ylen), & !First input array",
" INPUT2(XLEN, ylen), !Second input array",
],
],
# [],
],
1,
{"Found UPPERCASE variable name in declaration at line 48: \"INPUT2\"" : [48]},
),
([], 0, {}), # No changes, expect no errors
],
ids=[
"2 UpperCase Var Errors",
"False FALSE error",
"5 UpperCase Var Errors on extended lines",
"Array dimnensions, twice on an extended line, but no failures",
"Array dimnensions, twice on an extended line, with one failure",
"No UpperCase Var Errors",
],
)
def test_r3_4_2_no_full_uppercase_variable_names(
example_fortran_lines, changes_list, expected_result, expected_errors
):
modified_fortran_lines = modify_fortran_lines(example_fortran_lines, changes_list)
result = r3_4_2_no_full_uppercase_variable_names(modified_fortran_lines)
failure_count = result.failure_count
errors = result.errors
for error, lines_list in errors.items():
assert error in expected_errors
for line_no in lines_list:
assert line_no in expected_errors[error]
assert len(lines_list) == len(expected_errors[error])
assert len(errors) == len(expected_errors)
assert failure_count == expected_result
"""TODO: When testing for 'unseparated keywords' remember to test/discard false +ves on
"#endif" or similar. Current (old) test flags these in the wild, but the example file
has no cpp directives..."""