Total Complexity | 59 |
Total Lines | 401 |
Duplicated Lines | 0 % |
Complex classes like didyoumean.RegexTests often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # -*- coding: utf-8 |
||
10 | class RegexTests(unittest2.TestCase): |
||
11 | """Tests to check that error messages match the regexps.""" |
||
12 | |||
13 | def re_matches(self, text, regexp, results): |
||
14 | """Check that text matches regexp and gives the right match groups. |
||
15 | |||
16 | result is a tuple containing the expected return values for groups() |
||
17 | and groupdict(). |
||
18 | """ |
||
19 | groups, named_groups = results |
||
20 | self.assertRegexpMatches(text, regexp) # does pretty printing |
||
21 | match = re.match(regexp, text) |
||
22 | self.assertTrue(match) |
||
23 | self.assertEqual(groups, match.groups()) |
||
24 | self.assertEqual(named_groups, match.groupdict()) |
||
25 | |||
26 | def test_unbound_assignment(self): |
||
27 | """Test VARREFBEFOREASSIGN_RE.""" |
||
28 | msgs = [ |
||
29 | # Python 2.6/2.7/3.2/3.3/3.4/3.5/PyPy/PyPy3 |
||
30 | "local variable 'some_var' referenced before assignment", |
||
31 | "free variable 'some_var' referenced before assignment " \ |
||
32 | "in enclosing scope", |
||
33 | ] |
||
34 | groups = ('some_var',) |
||
35 | named_groups = {'name': 'some_var'} |
||
36 | results = (groups, named_groups) |
||
37 | for msg in msgs: |
||
38 | self.re_matches(msg, re.VARREFBEFOREASSIGN_RE, results) |
||
39 | |||
40 | def test_name_not_defined(self): |
||
41 | """Test NAMENOTDEFINED_RE.""" |
||
42 | msgs = [ |
||
43 | # Python 2.6/2.7/3.2/3.3/3.4/3.5/PyPy3 |
||
44 | "name 'some_name' is not defined", |
||
45 | # Python 2.6/2.7/3.2/3.3/PyPy/PyPy3 |
||
46 | "global name 'some_name' is not defined", |
||
47 | ] |
||
48 | groups = ('some_name',) |
||
49 | named_groups = {'name': 'some_name'} |
||
50 | for msg in msgs: |
||
51 | self.re_matches(msg, re.NAMENOTDEFINED_RE, (groups, named_groups)) |
||
52 | |||
53 | def test_attribute_error(self): |
||
54 | """Test ATTRIBUTEERROR_RE.""" |
||
55 | group_msg = { |
||
56 | ('some.class', 'attri'): [ |
||
57 | # Python 2.6/2.7/3.2/3.3/3.4/3.5/PyPy/PyPy3 |
||
58 | "'some.class' object has no attribute 'attri'", |
||
59 | ], |
||
60 | ('SomeClass', 'attri'): [ |
||
61 | # Python 2.6/2.7/PyPy |
||
62 | "SomeClass instance has no attribute 'attri'", |
||
63 | # Python 2.6/2.7 |
||
64 | "class SomeClass has no attribute 'attri'", |
||
65 | # Python 3.2/3.3/3.4/3.5 |
||
66 | "type object 'SomeClass' has no attribute 'attri'", |
||
67 | ], |
||
68 | } |
||
69 | for group, msgs in group_msg.items(): |
||
70 | for msg in msgs: |
||
71 | self.re_matches(msg, re.ATTRIBUTEERROR_RE, (group, dict())) |
||
72 | |||
73 | def test_module_attribute_error(self): |
||
74 | """Test MODULEHASNOATTRIBUTE_RE.""" |
||
75 | # Python 3.5 |
||
76 | msg = "module 'some_module' has no attribute 'attri'" |
||
77 | group = ('some_module', 'attri') |
||
78 | self.re_matches(msg, re.MODULEHASNOATTRIBUTE_RE, (group, dict())) |
||
79 | |||
80 | def test_cannot_import(self): |
||
81 | """Test CANNOTIMPORT_RE.""" |
||
82 | msgs = [ |
||
83 | # Python 2.6/2.7/3.2/3.3 |
||
84 | "cannot import name pie", |
||
85 | # Python 3.4/3.5/PyPy/PyPy3 |
||
86 | "cannot import name 'pie'", |
||
87 | ] |
||
88 | groups = ('pie',) |
||
89 | for msg in msgs: |
||
90 | self.re_matches(msg, re.CANNOTIMPORT_RE, (groups, dict())) |
||
91 | |||
92 | def test_no_module_named(self): |
||
93 | """Test NOMODULE_RE.""" |
||
94 | msgs = [ |
||
95 | # Python 2.6/2.7/3.2/PyPy/PyPy3 |
||
96 | "No module named fake_module", |
||
97 | # Python 3.3/3.4/3.5 |
||
98 | "No module named 'fake_module'", |
||
99 | ] |
||
100 | groups = ('fake_module',) |
||
101 | for msg in msgs: |
||
102 | self.re_matches(msg, re.NOMODULE_RE, (groups, dict())) |
||
103 | |||
104 | def test_index_out_of_range(self): |
||
105 | """Test INDEXOUTOFRANGE_RE.""" |
||
106 | # Python 2.6/2.7/3.2/3.3/3.4/3.5/PyPy/PyPy3 |
||
107 | msg = "list index out of range" |
||
108 | self.re_matches(msg, re.INDEXOUTOFRANGE_RE, NO_GROUP) |
||
109 | |||
110 | def test_unsubscriptable(self): |
||
111 | """Test UNSUBSCRIBTABLE_RE.""" |
||
112 | msgs = [ |
||
113 | # Python 2.6 |
||
114 | "'function' object is unsubscriptable", |
||
115 | # Python 2.7 |
||
116 | "'function' object has no attribute '__getitem__'", |
||
117 | # Python 3.2/3.3/3.4/3.5/PyPy/PyPy3 |
||
118 | "'function' object is not subscriptable", |
||
119 | ] |
||
120 | groups = ('function',) |
||
121 | for msg in msgs: |
||
122 | self.re_matches(msg, re.UNSUBSCRIBTABLE_RE, (groups, dict())) |
||
123 | |||
124 | def test_unexpected_kw_arg(self): |
||
125 | """Test UNEXPECTED_KEYWORDARG_RE.""" |
||
126 | # Python 2.6/2.7/3.2/3.3/3.4/3.5/PyPy/PyPy3 |
||
127 | msg = "some_func() got an unexpected keyword argument 'a'" |
||
128 | groups = ('some_func', 'a') |
||
129 | self.re_matches(msg, re.UNEXPECTED_KEYWORDARG_RE, (groups, dict())) |
||
130 | |||
131 | def test_unexpected_kw_arg2(self): |
||
132 | """Test UNEXPECTED_KEYWORDARG2_RE.""" |
||
133 | # Python 2.6/2.7/3.2/3.3/3.4/3.5 |
||
134 | msg = "'this_doesnt_exist' is an invalid " \ |
||
135 | "keyword argument for this function" |
||
136 | groups = ('this_doesnt_exist', ) |
||
137 | self.re_matches(msg, re.UNEXPECTED_KEYWORDARG2_RE, (groups, dict())) |
||
138 | |||
139 | def test_unexpected_kw_arg3(self): |
||
140 | """Test UNEXPECTED_KEYWORDARG3_RE.""" |
||
141 | # PyPy/PyPy3 |
||
142 | msg = "invalid keyword arguments to print()" |
||
143 | groups = ('print', ) |
||
144 | self.re_matches(msg, re.UNEXPECTED_KEYWORDARG3_RE, (groups, dict())) |
||
145 | |||
146 | def test_zero_length_field(self): |
||
147 | """Test ZERO_LEN_FIELD_RE.""" |
||
148 | # Python 2.6 |
||
149 | msg = "zero length field name in format" |
||
150 | self.re_matches(msg, re.ZERO_LEN_FIELD_RE, NO_GROUP) |
||
151 | |||
152 | def test_math_domain_error(self): |
||
153 | """Test MATH_DOMAIN_ERROR_RE.""" |
||
154 | # Python 2.6/2.7/3.2/3.3/3.4/3.5/PyPy/PyPy3 |
||
155 | msg = "math domain error" |
||
156 | self.re_matches(msg, re.MATH_DOMAIN_ERROR_RE, NO_GROUP) |
||
157 | |||
158 | def test_too_many_values(self): |
||
159 | """Test TOO_MANY_VALUES_UNPACK_RE.""" |
||
160 | msgs = [ |
||
161 | # Python 2.6/2.7 |
||
162 | "too many values to unpack", |
||
163 | # Python 3.2/3.3/3.4/3.5/PyPy3 |
||
164 | "too many values to unpack (expected 3)", |
||
165 | ] |
||
166 | for msg in msgs: |
||
167 | self.re_matches(msg, re.TOO_MANY_VALUES_UNPACK_RE, NO_GROUP) |
||
168 | |||
169 | def test_unhashable_type(self): |
||
170 | """Test UNHASHABLE_RE.""" |
||
171 | msgs = [ |
||
172 | # Python 2.6/2.7/3.2/3.3/3.4/3.5 |
||
173 | "unhashable type: 'list'", |
||
174 | # PyPy/PyPy3 |
||
175 | "'list' objects are unhashable", |
||
176 | ] |
||
177 | groups = ('list',) |
||
178 | for msg in msgs: |
||
179 | self.re_matches(msg, re.UNHASHABLE_RE, (groups, dict())) |
||
180 | |||
181 | def test_outside_function(self): |
||
182 | """Test OUTSIDE_FUNCTION_RE.""" |
||
183 | msgs = [ |
||
184 | # Python 2.6/2.7/3.2/3.3/3.4/3.5/PyPy/PyPy3 |
||
185 | "'return' outside function", |
||
186 | # PyPy/PyPy3 |
||
187 | "return outside function", |
||
188 | ] |
||
189 | groups = ('return',) |
||
190 | for msg in msgs: |
||
191 | self.re_matches(msg, re.OUTSIDE_FUNCTION_RE, (groups, dict())) |
||
192 | |||
193 | def test_nb_positional_argument(self): |
||
194 | """Test NB_ARG_RE.""" |
||
195 | msgs = [ |
||
196 | # Python 2.6/2.7/PyPy/PyPy3 |
||
197 | "some_func() takes exactly 1 argument (2 given)", |
||
198 | "some_func() takes exactly 3 arguments (1 given)", |
||
199 | "some_func() takes no arguments (1 given)", |
||
200 | # Python 3.2 |
||
201 | "some_func() takes exactly 1 positional argument (2 given)", |
||
202 | # Python 3.3/3.4/3.5 |
||
203 | "some_func() takes 1 positional argument but 2 were given", |
||
204 | "some_func() takes 0 positional arguments but 1 was given", |
||
205 | ] |
||
206 | groups = ('some_func',) |
||
207 | for msg in msgs: |
||
208 | self.re_matches(msg, re.NB_ARG_RE, (groups, dict())) |
||
209 | |||
210 | def test_missing_positional_arg(self): |
||
211 | """Test MISSING_POS_ARG_RE.""" |
||
212 | msgs = [ |
||
213 | # Python 3.3/3.4/3.5 |
||
214 | "some_func() missing 2 required positional arguments: " |
||
215 | "'much' and 'args'", |
||
216 | "some_func() missing 1 required positional argument: " |
||
217 | "'much'", |
||
218 | ] |
||
219 | groups = ('some_func',) |
||
220 | for msg in msgs: |
||
221 | self.re_matches(msg, re.MISSING_POS_ARG_RE, (groups, dict())) |
||
222 | |||
223 | def test_need_more_values_to_unpack(self): |
||
224 | """Test NEED_MORE_VALUES_RE.""" |
||
225 | msgs = [ |
||
226 | # Python 2.6/2.7/3.2/3.3/3.4/3.5(?)/PyPy3 |
||
227 | "need more than 2 values to unpack", |
||
228 | # Python 3.5 |
||
229 | "not enough values to unpack (expected 3, got 2)", |
||
230 | ] |
||
231 | for msg in msgs: |
||
232 | self.re_matches(msg, re.NEED_MORE_VALUES_RE, NO_GROUP) |
||
233 | |||
234 | def test_missing_parentheses(self): |
||
235 | """Test MISSING_PARENT_RE.""" |
||
236 | # Python 3.4/3.5 |
||
237 | msg = "Missing parentheses in call to 'exec'" |
||
238 | groups = ('exec',) |
||
239 | self.re_matches(msg, re.MISSING_PARENT_RE, (groups, dict())) |
||
240 | |||
241 | def test_invalid_literal(self): |
||
242 | """Test INVALID_LITERAL_RE.""" |
||
243 | # Python 2.6/2.7/3.2/3.3/3.4/3.5/PyPy/PyPy3 |
||
244 | msg = "invalid literal for int() with base 10: 'toto'" |
||
245 | groups = ('int', 'toto') |
||
246 | self.re_matches(msg, re.INVALID_LITERAL_RE, (groups, dict())) |
||
247 | |||
248 | def test_invalid_syntax(self): |
||
249 | """Test INVALID_SYNTAX_RE.""" |
||
250 | # Python 2.6/2.7/3.2/3.3/3.4/3.5/PyPy3 |
||
251 | msg = "invalid syntax" |
||
252 | self.re_matches(msg, re.INVALID_SYNTAX_RE, NO_GROUP) |
||
253 | |||
254 | def test_invalid_comp(self): |
||
255 | """Test INVALID_COMP_RE.""" |
||
256 | # PyPy3 |
||
257 | msg = "invalid comparison" |
||
258 | self.re_matches(msg, re.INVALID_COMP_RE, NO_GROUP) |
||
259 | |||
260 | def test_expected_length(self): |
||
261 | """Test EXPECTED_LENGTH_RE.""" |
||
262 | # PyPy |
||
263 | msg = "expected length 3, got 2" |
||
264 | groups = ('3', '2') |
||
265 | self.re_matches(msg, re.EXPECTED_LENGTH_RE, (groups, dict())) |
||
266 | |||
267 | def test_future_first(self): |
||
268 | """Test FUTURE_FIRST_RE.""" |
||
269 | msgs = [ |
||
270 | # Python 2.6/2.7/3.2/3.3/3.4/3.5 |
||
271 | "from __future__ imports must occur at the beginning of the file", |
||
272 | # PyPy/PyPy3 |
||
273 | "__future__ statements must appear at beginning of file", |
||
274 | ] |
||
275 | for msg in msgs: |
||
276 | self.re_matches(msg, re.FUTURE_FIRST_RE, NO_GROUP) |
||
277 | |||
278 | def test_future_feature_not_def(self): |
||
279 | """Test FUTURE_FEATURE_NOT_DEF_RE.""" |
||
280 | # Python 2.6/2.7/3.2/3.3/3.4/3.5/PyPy/PyPy3 |
||
281 | msg = "future feature divisio is not defined" |
||
282 | groups = ('divisio',) |
||
283 | self.re_matches(msg, re.FUTURE_FEATURE_NOT_DEF_RE, (groups, dict())) |
||
284 | |||
285 | def test_result_has_too_many_items(self): |
||
286 | """Test RESULT_TOO_MANY_ITEMS_RE.""" |
||
287 | # Python 2.6 |
||
288 | msg = "range() result has too many items" |
||
289 | groups = ('range',) |
||
290 | self.re_matches(msg, re.RESULT_TOO_MANY_ITEMS_RE, (groups, dict())) |
||
291 | |||
292 | def test_unqualified_exec(self): |
||
293 | """Test UNQUALIFIED_EXEC_RE.""" |
||
294 | msgs = [ |
||
295 | # Python 2.6 |
||
296 | "unqualified exec is not allowed in function 'func_name' " |
||
297 | "it is a nested function", |
||
298 | # Python 2.7 |
||
299 | "unqualified exec is not allowed in function 'func_name' " |
||
300 | "because it is a nested function", |
||
301 | # Python 2.6 |
||
302 | "unqualified exec is not allowed in function 'func_name' " |
||
303 | "it contains a nested function with free variables", |
||
304 | # Python 2.7 |
||
305 | "unqualified exec is not allowed in function 'func_name' " |
||
306 | "because it contains a nested function with free variables", |
||
307 | ] |
||
308 | for msg in msgs: |
||
309 | self.re_matches(msg, re.UNQUALIFIED_EXEC_RE, NO_GROUP) |
||
310 | |||
311 | def test_import_star(self): |
||
312 | """Test IMPORTSTAR_RE.""" |
||
313 | msgs = [ |
||
314 | # Python 2.6 |
||
315 | "import * is not allowed in function 'func_name' because it " |
||
316 | "is contains a nested function with free variables", |
||
317 | # Python 2.7 |
||
318 | "import * is not allowed in function 'func_name' because it " |
||
319 | "contains a nested function with free variables", |
||
320 | # Python 2.6 |
||
321 | "import * is not allowed in function 'func_name' because it " |
||
322 | "is is a nested function", |
||
323 | # Python 2.7 |
||
324 | "import * is not allowed in function 'func_name' because it " |
||
325 | "is a nested function", |
||
326 | # Python 3 |
||
327 | "import * only allowed at module level" |
||
328 | ] |
||
329 | for msg in msgs: |
||
330 | self.re_matches(msg, re.IMPORTSTAR_RE, NO_GROUP) |
||
331 | |||
332 | def test_does_not_support(self): |
||
333 | """Test OBJ_DOES_NOT_SUPPORT_RE.""" |
||
334 | msg = "'range' object does not support item assignment" |
||
335 | groups = ('range',) |
||
336 | self.re_matches(msg, re.OBJ_DOES_NOT_SUPPORT_RE, (groups, dict())) |
||
337 | |||
338 | def test_cant_convert(self): |
||
339 | """Test CANT_CONVERT_RE.""" |
||
340 | msg = "Can't convert 'int' object to str implicitly" |
||
341 | groups = ('int', 'str') |
||
342 | self.re_matches(msg, re.CANT_CONVERT_RE, (groups, dict())) |
||
343 | |||
344 | def test_cannot_concat(self): |
||
345 | """Test CANNOT_CONCAT_RE.""" |
||
346 | msg = "cannot concatenate 'str' and 'int' objects" |
||
347 | groups = ('str', 'int') |
||
348 | self.re_matches(msg, re.CANNOT_CONCAT_RE, (groups, dict())) |
||
349 | |||
350 | def test_unsupported_operand(self): |
||
351 | """Test UNSUPPORTED_OP_RE.""" |
||
352 | msg = "unsupported operand type(s) for +: 'int' and 'str'" |
||
353 | groups = ('+', 'int', 'str') |
||
354 | self.re_matches(msg, re.UNSUPPORTED_OP_RE, (groups, dict())) |
||
355 | |||
356 | def test_not_callable(self): |
||
357 | """Test NOT_CALLABLE_RE.""" |
||
358 | msg = "'list' object is not callable" |
||
359 | groups = ('list',) |
||
360 | self.re_matches(msg, re.NOT_CALLABLE_RE, (groups, dict())) |
||
361 | |||
362 | def test_descriptor_requires(self): |
||
363 | """Test DESCRIPT_REQUIRES_TYPE_RE.""" |
||
364 | msg = "descriptor 'add' requires a 'set' object but received a 'int'" |
||
365 | groups = ('add', 'set', 'int') |
||
366 | self.re_matches( |
||
367 | msg, re.DESCRIPT_REQUIRES_TYPE_RE, (groups, dict())) |
||
368 | |||
369 | def test_argument_not_iterable(self): |
||
370 | """Test ARG_NOT_ITERABLE_RE.""" |
||
371 | msgs = [ |
||
372 | # Python 2.6/2.7/3.2/3.3/3.4/3.5 |
||
373 | "argument of type 'type' is not iterable", |
||
374 | # PyPy/PyPy3 |
||
375 | "'type' object is not iterable" |
||
376 | ] |
||
377 | groups = ('type',) |
||
378 | for msg in msgs: |
||
379 | self.re_matches(msg, re.ARG_NOT_ITERABLE_RE, (groups, dict())) |
||
380 | |||
381 | def test_must_be_called_with_instance(self): |
||
382 | """Test MUST_BE_CALLED_WITH_INST_RE.""" |
||
383 | msg = "unbound method add() must be called with set " \ |
||
384 | "instance as first argument (got int instance instead)" |
||
385 | groups = ('add', 'set', 'int') |
||
386 | self.re_matches( |
||
387 | msg, re.MUST_BE_CALLED_WITH_INST_RE, (groups, dict())) |
||
388 | |||
389 | def test_object_has_no(self): |
||
390 | """Test OBJECT_HAS_NO_FUNC_RE.""" |
||
391 | msgs = { |
||
392 | # Python 2.6/2.7/3.2/3.3/3.4/3.5 |
||
393 | 'len': "object of type 'generator' has no len()", |
||
394 | # PyPy/PyPy3 |
||
395 | 'length': "'generator' has no length", |
||
396 | } |
||
397 | for name, msg in msgs.items(): |
||
398 | groups = ('generator', name) |
||
399 | self.re_matches(msg, re.OBJECT_HAS_NO_FUNC_RE, (groups, dict())) |
||
400 | |||
401 | def test_nobinding_nonlocal(self): |
||
402 | """Test NO_BINDING_NONLOCAL_RE.""" |
||
403 | msg = "no binding for nonlocal 'foo' found" |
||
404 | groups = ('foo',) |
||
405 | self.re_matches(msg, re.NO_BINDING_NONLOCAL_RE, (groups, dict())) |
||
406 | |||
407 | def test_nosuchfile(self): |
||
408 | """Test NO_SUCH_FILE_RE.""" |
||
409 | msg = "No such file or directory" |
||
410 | self.re_matches(msg, re.NO_SUCH_FILE_RE, NO_GROUP) |
||
411 | |||
426 |