Debian Codegraph @

gcc/testsuite/g++.dg/modules/test-p1689.py

raw
1 import json
2
3
4 # Parameters.
5 ALL_ERRORS = False
6 REPLACEMENTS = {}
7
8
9 def _print_path(path):
10 '''Format a JSON path for output.'''
11 return '/'.join(path)
12
13
14 def _report_error(msg):
15 '''Report an error.'''
16 full_msg = 'ERROR: ' + msg
17 if ALL_ERRORS:
18 print(full_msg)
19 else:
20 raise RuntimeError(full_msg)
21
22
23 def _error_type_mismatch(path, actual, expect):
24 '''Report that there is a type mismatch.'''
25 _report_error('type mismatch at %s: actual: "%s" expect: "%s"' % (_print_path(path), actual, expect))
26
27
28 def _error_unknown_type(path, typ):
29 '''Report that there is an unknown type in the JSON object.'''
30 _report_error('unknown type at %s: "%s"' % (_print_path(path), typ))
31
32
33 def _error_length_mismatch(path, actual, expect):
34 '''Report a length mismatch in an object or array.'''
35 _report_error('length mismatch at %s: actual: "%s" expect: "%s"' % (_print_path(path), actual, expect))
36
37
38 def _error_unexpect_value(path, actual, expect):
39 '''Report a value mismatch.'''
40 _report_error('value mismatch at %s: actual: "%s" expect: "%s"' % (_print_path(path), actual, expect))
41
42
43 def _error_extra_key(path, key):
44 '''Report on a key that is unexpected.'''
45 _report_error('extra key at %s: "%s"' % (_print_path(path), key))
46
47
48 def _error_missing_key(path, key):
49 '''Report on a key that is missing.'''
50 _report_error('extra key at %s: %s' % (_print_path(path), key))
51
52
53 def _compare_object(path, actual, expect):
54 '''Compare a JSON object.'''
55 is_ok = True
56
57 if not len(actual) == len(expect):
58 _error_length_mismatch(path, len(actual), len(expect))
59 is_ok = False
60
61 for key in actual:
62 if key not in expect:
63 _error_extra_key(path, key)
64 is_ok = False
65 else:
66 sub_error = compare_json(path + [key], actual[key], expect[key])
67 if sub_error:
68 is_ok = False
69
70 for key in expect:
71 if key not in actual:
72 _error_missing_key(path, key)
73 is_ok = False
74
75 return is_ok
76
77
78 def _compare_array(path, actual, expect):
79 '''Compare a JSON array.'''
80 is_ok = True
81
82 if not len(actual) == len(expect):
83 _error_length_mismatch(path, len(actual), len(expect))
84 is_ok = False
85
86 for (idx, (a, e)) in enumerate(zip(actual, expect)):
87 sub_error = compare_json(path + [str(idx)], a, e)
88 if sub_error:
89 is_ok = False
90
91 return is_ok
92
93
94 def _make_replacements(value):
95 for (old, new) in REPLACEMENTS.values():
96 value = value.replace(old, new)
97 return value
98
99
100 def _compare_string(path, actual, expect):
101 '''Compare a JSON string supporting replacements in the expected output.'''
102 expect = _make_replacements(expect)
103
104 if not actual == expect:
105 _error_unexpect_value(path, actual, expect)
106 return False
107 else:
108 print('%s is ok: %s' % (_print_path(path), actual))
109 return True
110
111
112 def _compare_number(path, actual, expect):
113 '''Compare a JSON integer.'''
114 if not actual == expect:
115 _error_unexpect_value(path, actual, expect)
116 return False
117 else:
118 print('%s is ok: %s' % (_print_path(path), actual))
119 return True
120
121
122 def _inspect_ordering(arr):
123 req_ordering = True
124
125 if not arr:
126 return arr, req_ordering
127
128 if arr[0] == '__P1689_unordered__':
129 arr.pop(0)
130 req_ordering = False
131
132 return arr, req_ordering
133
134 def _get_predictable_ordered_array(arr):
135 if not arr:
136 return arr
137
138 # Convert to list of string to get predictable order.
139 # Using set() here causes issue with complex arrays
140 arr = [json.dumps(x) for x in arr]
141 arr.sort()
142 return [json.loads(x) for x in arr]
143
144
145 def compare_json(path, actual, expect):
146 actual_type = type(actual)
147 expect_type = type(expect)
148
149 is_ok = True
150
151 if not actual_type == expect_type:
152 _error_type_mismatch(path, actual_type, expect_type)
153 is_ok = False
154 elif actual_type == dict:
155 is_ok = _compare_object(path, actual, expect)
156 elif actual_type == list:
157 expect, req_ordering = _inspect_ordering(expect)
158 if not req_ordering:
159 actual = _get_predictable_ordered_array(actual)
160 expect = _get_predictable_ordered_array(expect)
161 is_ok = _compare_array(path, actual, expect)
162 elif actual_type == str:
163 is_ok = _compare_string(path, actual, expect)
164 elif actual_type == float:
165 is_ok = _compare_number(path, actual, expect)
166 elif actual_type == int:
167 is_ok = _compare_number(path, actual, expect)
168 elif actual_type == bool:
169 is_ok = _compare_number(path, actual, expect)
170 elif actual_type == type(None):
171 pass
172 else:
173 _error_unknown_type(path, actual_type)
174 is_ok = False
175
176 return is_ok
177
178
179 def validate_p1689(actual, expect):
180 '''Validate a P1689 file against an expected output file.
181
182 Returns `False` if it fails, `True` if they are the same.
183 '''
184 with open(actual, 'r') as fin:
185 actual_content = fin.read()
186 with open(expect, 'r') as fin:
187 expect_content = fin.read()
188
189 actual_json = json.loads(actual_content)
190 expect_json = json.loads(expect_content)
191
192 return compare_json([], actual_json, expect_json)
193
194
195 if __name__ == '__main__':
196 import sys
197
198 actual = None
199 expect = None
200
201 # Parse arguments.
202 args = sys.argv[1:]
203 while args:
204 # Take an argument.
205 arg = args.pop(0)
206
207 # Parse out replacement expressions.
208 if arg == '-r' or arg == '--replace':
209 replacement = args.pop(0)
210 (key, value) = replacement.split('=', maxsplit=1)
211 REPLACEMENTS[key] = value
212 # Flag to change how errors are reported.
213 elif arg == '-A' or arg == '--all':
214 ALL_ERRORS = True
215 # Required arguments.
216 elif arg == '-a' or arg == '--actual':
217 actual = args.pop(0)
218 elif arg == '-e' or arg == '--expect':
219 expect = args.pop(0)
220
221 # Validate that we have the required arguments.
222 if actual is None:
223 raise RuntimeError('missing "actual" file')
224 if expect is None:
225 raise RuntimeError('missing "expect" file')
226
227 # Do the actual work.
228 is_ok = validate_p1689(actual, expect)
229
230 # Fail if errors are found.
231 if not is_ok:
232 sys.exit(1)