Skip to content

Commit 68fd12d

Browse files
author
John Nonweiler
committed
Use list class with is_not_subset_of
1 parent d3a33d0 commit 68fd12d

File tree

1 file changed

+52
-52
lines changed

1 file changed

+52
-52
lines changed

scripts/filter_expected_warnings.py

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,59 @@ def approx_equals(self, other):
2222
return False
2323
return self.otherlines == other.otherlines
2424

25-
def is_in_list(self, warnings_list):
26-
"""Check if this (or a similar warning) is in the list."""
27-
for other in warnings_list:
28-
if self.approx_equals(other):
29-
return True
30-
return False
31-
3225
def print_with_prefix(self, prefix):
3326
print(prefix + self.firstline)
3427
for line in self.otherlines:
3528
print(prefix + ' ' + line)
3629

3730

38-
def build_warnings_list(all_lines):
39-
"""Create a list of the warnings in this file."""
40-
warnings_list = []
41-
current = None
42-
43-
warning_start_expr = re.compile(r'[^:]+/([^:/]+):\d+: (warning.*$)')
44-
45-
for line in all_lines:
46-
if line.isspace():
47-
continue
48-
49-
# Allow comments in the list of expected warnings.
50-
if line.startswith('#'):
51-
continue
52-
53-
matched = warning_start_expr.match(line)
54-
if matched:
55-
filename = matched.group(1)
56-
warning = matched.group(2)
57-
current = DoxygenWarning(line.strip(), filename, warning)
58-
warnings_list.append(current)
59-
elif line.startswith(' '):
60-
current.otherlines.append(line.strip())
61-
else:
62-
# Assuming all warnings are of the form [path:line: warning:...]
63-
# (and the warnings about too many nodes have been filtered out).
64-
print('Error filtering warnings: Unexpected input format.')
65-
print(' Input:' + line)
31+
class WarningsList(object):
32+
"""List of Doxygen warnings."""
33+
34+
def __init__(self, all_lines):
35+
"""Create a list of the warnings in this file."""
36+
self.warnings_list = []
37+
current = None
38+
39+
warning_start_expr = re.compile(r'[^:]+/([^:/]+):\d+: (warning.*$)')
40+
41+
for line in all_lines:
42+
if line.isspace():
43+
continue
44+
45+
# Allow comments in the list of expected warnings.
46+
if line.startswith('#'):
47+
continue
48+
49+
matched = warning_start_expr.match(line)
50+
if matched:
51+
filename = matched.group(1)
52+
warning = matched.group(2)
53+
current = DoxygenWarning(line.strip(), filename, warning)
54+
self.warnings_list.append(current)
55+
elif line.startswith(' '):
56+
current.otherlines.append(line.strip())
57+
else:
58+
# Assuming all warnings are of the form [path:line: warning:...]
59+
# (and the warnings about too many nodes have been filtered out).
60+
print('Error filtering warnings: Unexpected input format.')
61+
print(' Input:' + line)
62+
63+
def contains(self, warning):
64+
"""Check if a similar warning is in this list."""
65+
for other in self.warnings_list:
66+
if warning.approx_equals(other):
67+
return True
68+
return False
6669

67-
return warnings_list
70+
def is_not_subset_of(self, superset, prefix):
71+
"""Check if this a subset of superset, and print anything missing."""
72+
missing_element_found = False
73+
for warning in self.warnings_list:
74+
if not superset.contains(warning):
75+
missing_element_found = True
76+
warning.print_with_prefix(prefix)
77+
return missing_element_found
6878

6979

7080
def ignore_too_many_nodes(all_lines):
@@ -75,30 +85,20 @@ def ignore_too_many_nodes(all_lines):
7585
return [x for x in all_lines if not too_many_nodes_expr.match(x)]
7686

7787

78-
def list1_is_in_list2(list1, list2, prefix):
79-
missing_element_found = False
80-
for warning in list1:
81-
if not warning.is_in_list(list2):
82-
missing_element_found = True
83-
warning.print_with_prefix(prefix)
84-
return missing_element_found
85-
86-
8788
def filter_expected_warnings(expected_warnings_path):
8889
"""Filter lines from stdin and print to stdout."""
8990
with open(expected_warnings_path, "r") as warnings_file:
90-
expected_warnings = build_warnings_list(warnings_file.readlines())
91+
expected_warnings = WarningsList(warnings_file.readlines())
9192

92-
new_warnings = build_warnings_list(
93-
ignore_too_many_nodes(sys.stdin.readlines()))
93+
new_warnings = WarningsList(ignore_too_many_nodes(sys.stdin.readlines()))
9494

9595
# print unexpected warnings
96-
unexpected_warning_found = list1_is_in_list2(
97-
new_warnings, expected_warnings, '')
96+
unexpected_warning_found = new_warnings.is_not_subset_of(
97+
expected_warnings, '')
9898

9999
# print expected warnings which aren't found
100-
expected_warning_not_found = list1_is_in_list2(
101-
expected_warnings, new_warnings, '-')
100+
expected_warning_not_found = expected_warnings.is_not_subset_of(
101+
new_warnings, '-')
102102

103103
if expected_warning_not_found:
104104
print('NOTE: Warnings prefixed with \'-\' are expected ' +

0 commit comments

Comments
 (0)