Completed
Push — master ( 6c104f...735829 )
by De
01:04
created

RegexTests.test_bad_operand_unary()   A

Complexity

Conditions 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
1
# -*- coding: utf-8
2
"""Unit tests for regexps from didyoumean_re.py."""
3
import unittest2
4
import didyoumean_re as re
5
import sys
6
7
NO_GROUP = ((), dict())
8
9
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 UNSUBSCRIPTABLE_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.UNSUBSCRIPTABLE_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
        msgs = [
128
            ("some_func() got an unexpected keyword argument 'a'",
129
                ('some_func', 'a')),
130
            ("<lambda>() got an unexpected keyword argument 'a'",
131
                ('<lambda>', 'a')),
132
        ]
133
        for msg, groups in msgs:
134
            self.re_matches(msg, re.UNEXPECTED_KEYWORDARG_RE, (groups, dict()))
135
136
    def test_unexpected_kw_arg2(self):
137
        """Test UNEXPECTED_KEYWORDARG2_RE."""
138
        # Python 2.6/2.7/3.2/3.3/3.4/3.5
139
        msg = "'this_doesnt_exist' is an invalid " \
140
            "keyword argument for this function"
141
        groups = ('this_doesnt_exist', )
142
        self.re_matches(msg, re.UNEXPECTED_KEYWORDARG2_RE, (groups, dict()))
143
144
    def test_unexpected_kw_arg3(self):
145
        """Test UNEXPECTED_KEYWORDARG3_RE."""
146
        # PyPy/PyPy3
147
        msg = "invalid keyword arguments to print()"
148
        groups = ('print', )
149
        self.re_matches(msg, re.UNEXPECTED_KEYWORDARG3_RE, (groups, dict()))
150
151
    def test_zero_length_field(self):
152
        """Test ZERO_LEN_FIELD_RE."""
153
        # Python 2.6
154
        msg = "zero length field name in format"
155
        self.re_matches(msg, re.ZERO_LEN_FIELD_RE, NO_GROUP)
156
157
    def test_math_domain_error(self):
158
        """Test MATH_DOMAIN_ERROR_RE."""
159
        # Python 2.6/2.7/3.2/3.3/3.4/3.5/PyPy/PyPy3
160
        msg = "math domain error"
161
        self.re_matches(msg, re.MATH_DOMAIN_ERROR_RE, NO_GROUP)
162
163
    def test_too_many_values(self):
164
        """Test TOO_MANY_VALUES_UNPACK_RE."""
165
        msgs = [
166
            # Python 2.6/2.7
167
            "too many values to unpack",
168
            # Python 3.2/3.3/3.4/3.5/PyPy3
169
            "too many values to unpack (expected 3)",
170
        ]
171
        for msg in msgs:
172
            self.re_matches(msg, re.TOO_MANY_VALUES_UNPACK_RE, NO_GROUP)
173
174
    def test_unhashable_type(self):
175
        """Test UNHASHABLE_RE."""
176
        msgs = [
177
            # Python 2.6/2.7/3.2/3.3/3.4/3.5
178
            "unhashable type: 'list'",
179
            # PyPy/PyPy3
180
            "'list' objects are unhashable",
181
        ]
182
        groups = ('list',)
183
        for msg in msgs:
184
            self.re_matches(msg, re.UNHASHABLE_RE, (groups, dict()))
185
186
    def test_outside_function(self):
187
        """Test OUTSIDE_FUNCTION_RE."""
188
        msgs = [
189
            # Python 2.6/2.7/3.2/3.3/3.4/3.5/PyPy/PyPy3
190
            "'return' outside function",
191
            # PyPy/PyPy3
192
            "return outside function",
193
        ]
194
        groups = ('return',)
195
        for msg in msgs:
196
            self.re_matches(msg, re.OUTSIDE_FUNCTION_RE, (groups, dict()))
197
198
    def test_nb_positional_argument(self):
199
        """Test NB_ARG_RE."""
200
        msgs = [
201
            # Python 2.6/2.7/PyPy/PyPy3
202
            ("some_func() takes exactly 1 argument (2 given)",
203
                '1', '2'),
204
            ("some_func() takes exactly 3 arguments (1 given)",
205
                '3', '1'),
206
            ("some_func() takes no arguments (1 given)",
207
                'no', '1'),
208
            ("some_func() takes at least 2 non-keyword arguments (0 given)",
209
                '2', '0'),
210
            # Python 3.2
211
            ("some_func() takes exactly 1 positional argument (2 given)",
212
                '1', '2'),
213
            # Python 3.3/3.4/3.5
214
            ("some_func() takes 1 positional argument but 2 were given",
215
                '1', '2'),
216
            ("some_func() takes 0 positional arguments but 1 was given",
217
                '0', '1'),
218
        ]
219
        for msg, exp, nb in msgs:
220
            groups = ('some_func', exp, nb)
221
            self.re_matches(msg, re.NB_ARG_RE, (groups, dict()))
222
223
    def test_missing_positional_arg(self):
224
        """Test MISSING_POS_ARG_RE."""
225
        msgs = [
226
            # Python 3.3/3.4/3.5
227
            "some_func() missing 2 required positional arguments: "
228
            "'much' and 'args'",
229
            "some_func() missing 1 required positional argument: "
230
            "'much'",
231
        ]
232
        groups = ('some_func',)
233
        for msg in msgs:
234
            self.re_matches(msg, re.MISSING_POS_ARG_RE, (groups, dict()))
235
236
    def test_need_more_values_to_unpack(self):
237
        """Test NEED_MORE_VALUES_RE."""
238
        msgs = [
239
            # Python 2.6/2.7/3.2/3.3/3.4/3.5(?)/PyPy3
240
            "need more than 2 values to unpack",
241
            # Python 3.5
242
            "not enough values to unpack (expected 3, got 2)",
243
        ]
244
        for msg in msgs:
245
            self.re_matches(msg, re.NEED_MORE_VALUES_RE, NO_GROUP)
246
247
    def test_missing_parentheses(self):
248
        """Test MISSING_PARENT_RE."""
249
        # Python 3.4/3.5
250
        msg = "Missing parentheses in call to 'exec'"
251
        groups = ('exec',)
252
        self.re_matches(msg, re.MISSING_PARENT_RE, (groups, dict()))
253
254
    def test_invalid_literal(self):
255
        """Test INVALID_LITERAL_RE."""
256
        # Python 2.6/2.7/3.2/3.3/3.4/3.5/PyPy/PyPy3
257
        msg = "invalid literal for int() with base 10: 'toto'"
258
        groups = ('int', 'toto')
259
        self.re_matches(msg, re.INVALID_LITERAL_RE, (groups, dict()))
260
261
    def test_invalid_syntax(self):
262
        """Test INVALID_SYNTAX_RE."""
263
        # Python 2.6/2.7/3.2/3.3/3.4/3.5/PyPy3
264
        msg = "invalid syntax"
265
        self.re_matches(msg, re.INVALID_SYNTAX_RE, NO_GROUP)
266
267
    def test_invalid_comp(self):
268
        """Test INVALID_COMP_RE."""
269
        # PyPy3
270
        msg = "invalid comparison"
271
        self.re_matches(msg, re.INVALID_COMP_RE, NO_GROUP)
272
273
    def test_expected_length(self):
274
        """Test EXPECTED_LENGTH_RE."""
275
        # PyPy
276
        msg = "expected length 3, got 2"
277
        groups = ('3', '2')
278
        self.re_matches(msg, re.EXPECTED_LENGTH_RE, (groups, dict()))
279
280
    def test_future_first(self):
281
        """Test FUTURE_FIRST_RE."""
282
        msgs = [
283
            # Python 2.6/2.7/3.2/3.3/3.4/3.5
284
            "from __future__ imports must occur at the beginning of the file",
285
            # PyPy/PyPy3
286
            "__future__ statements must appear at beginning of file",
287
        ]
288
        for msg in msgs:
289
            self.re_matches(msg, re.FUTURE_FIRST_RE, NO_GROUP)
290
291
    def test_future_feature_not_def(self):
292
        """Test FUTURE_FEATURE_NOT_DEF_RE."""
293
        # Python 2.6/2.7/3.2/3.3/3.4/3.5/PyPy/PyPy3
294
        msg = "future feature divisio is not defined"
295
        groups = ('divisio',)
296
        self.re_matches(msg, re.FUTURE_FEATURE_NOT_DEF_RE, (groups, dict()))
297
298
    def test_result_has_too_many_items(self):
299
        """Test RESULT_TOO_MANY_ITEMS_RE."""
300
        # Python 2.6
301
        msg = "range() result has too many items"
302
        groups = ('range',)
303
        self.re_matches(msg, re.RESULT_TOO_MANY_ITEMS_RE, (groups, dict()))
304
305
    def test_unqualified_exec(self):
306
        """Test UNQUALIFIED_EXEC_RE."""
307
        msgs = [
308
            # Python 2.6
309
            "unqualified exec is not allowed in function 'func_name' "
310
            "it is a nested function",
311
            # Python 2.7
312
            "unqualified exec is not allowed in function 'func_name' "
313
            "because it is a nested function",
314
            # Python 2.6
315
            "unqualified exec is not allowed in function 'func_name' "
316
            "it contains a nested function with free variables",
317
            # Python 2.7
318
            "unqualified exec is not allowed in function 'func_name' "
319
            "because it contains a nested function with free variables",
320
        ]
321
        for msg in msgs:
322
            self.re_matches(msg, re.UNQUALIFIED_EXEC_RE, NO_GROUP)
323
324
    def test_import_star(self):
325
        """Test IMPORTSTAR_RE."""
326
        msgs = [
327
            # Python 2.6
328
            "import * is not allowed in function 'func_name' because it "
329
            "is contains a nested function with free variables",
330
            # Python 2.7
331
            "import * is not allowed in function 'func_name' because it "
332
            "contains a nested function with free variables",
333
            # Python 2.6
334
            "import * is not allowed in function 'func_name' because it "
335
            "is is a nested function",
336
            # Python 2.7
337
            "import * is not allowed in function 'func_name' because it "
338
            "is a nested function",
339
            # Python 3
340
            "import * only allowed at module level"
341
        ]
342
        for msg in msgs:
343
            self.re_matches(msg, re.IMPORTSTAR_RE, NO_GROUP)
344
345
    def test_does_not_support(self):
346
        """Test OBJ_DOES_NOT_SUPPORT_RE."""
347
        msgs = [
348
            ("'range' object does not support item assignment",
349
                ("range", "item assignment")),
350
            ("'str' object doesn't support item deletion",
351
                ("str", "item deletion")),
352
            ("'set' object does not support indexing",
353
                ("set", "indexing")),
354
        ]
355
        for msg, groups in msgs:
356
            self.re_matches(msg, re.OBJ_DOES_NOT_SUPPORT_RE, (groups, dict()))
357
358
    def test_cant_convert(self):
359
        """Test CANT_CONVERT_RE."""
360
        msg = "Can't convert 'int' object to str implicitly"
361
        groups = ('int', 'str')
362
        self.re_matches(msg, re.CANT_CONVERT_RE, (groups, dict()))
363
364
    def test_must_be_type1_not_type2(self):
365
        """Test MUST_BE_TYPE1_NOT_TYPE2_RE."""
366
        msg = "must be str, not int"
367
        groups = ('str', 'int')
368
        self.re_matches(msg, re.MUST_BE_TYPE1_NOT_TYPE2_RE, (groups, dict()))
369
370
    def test_cannot_concat(self):
371
        """Test CANNOT_CONCAT_RE."""
372
        msg = "cannot concatenate 'str' and 'int' objects"
373
        groups = ('str', 'int')
374
        self.re_matches(msg, re.CANNOT_CONCAT_RE, (groups, dict()))
375
376
    def test_unsupported_operand(self):
377
        """Test UNSUPPORTED_OP_RE."""
378
        msg = "unsupported operand type(s) for +: 'int' and 'str'"
379
        groups = ('+', 'int', 'str')
380
        self.re_matches(msg, re.UNSUPPORTED_OP_RE, (groups, dict()))
381
382
    def test_bad_operand_unary(self):
383
        """Test BAD_OPERAND_UNARY_RE."""
384
        msgs = [
385
            ("bad operand type for unary ~: 'set'", ('~', 'set')),
386
            ("bad operand type for abs(): 'set'", ('abs()', 'set')),
387
        ]
388
        for msg, group in msgs:
389
            self.re_matches(msg, re.BAD_OPERAND_UNARY_RE, (group, dict()))
390
391
    def test_not_callable(self):
392
        """Test NOT_CALLABLE_RE."""
393
        msg = "'list' object is not callable"
394
        groups = ('list',)
395
        self.re_matches(msg, re.NOT_CALLABLE_RE, (groups, dict()))
396
397
    def test_descriptor_requires(self):
398
        """Test DESCRIPT_REQUIRES_TYPE_RE."""
399
        msg = "descriptor 'add' requires a 'set' object but received a 'int'"
400
        groups = ('add', 'set', 'int')
401
        self.re_matches(
402
            msg, re.DESCRIPT_REQUIRES_TYPE_RE, (groups, dict()))
403
404
    def test_argument_not_iterable(self):
405
        """Test ARG_NOT_ITERABLE_RE."""
406
        msgs = [
407
            # Python 2.6/2.7/3.2/3.3/3.4/3.5
408
            "argument of type 'type' is not iterable",
409
            # PyPy/PyPy3
410
            "'type' object is not iterable"
411
        ]
412
        groups = ('type',)
413
        for msg in msgs:
414
            self.re_matches(msg, re.ARG_NOT_ITERABLE_RE, (groups, dict()))
415
416
    def test_must_be_called_with_instance(self):
417
        """Test MUST_BE_CALLED_WITH_INST_RE."""
418
        msg = "unbound method add() must be called with set " \
419
              "instance as first argument (got int instance instead)"
420
        groups = ('add', 'set', 'int')
421
        self.re_matches(
422
            msg, re.MUST_BE_CALLED_WITH_INST_RE, (groups, dict()))
423
424
    def test_object_has_no(self):
425
        """Test OBJECT_HAS_NO_FUNC_RE."""
426
        msgs = {
427
            # Python 2.6/2.7/3.2/3.3/3.4/3.5
428
            'len': "object of type 'generator' has no len()",
429
            # PyPy/PyPy3
430
            'length': "'generator' has no length",
431
        }
432
        for name, msg in msgs.items():
433
            groups = ('generator', name)
434
            self.re_matches(msg, re.OBJECT_HAS_NO_FUNC_RE, (groups, dict()))
435
436
    def test_nobinding_nonlocal(self):
437
        """Test NO_BINDING_NONLOCAL_RE."""
438
        msg = "no binding for nonlocal 'foo' found"
439
        groups = ('foo',)
440
        self.re_matches(msg, re.NO_BINDING_NONLOCAL_RE, (groups, dict()))
441
442
    def test_nosuchfile(self):
443
        """Test NO_SUCH_FILE_RE."""
444
        msg = "No such file or directory"
445
        self.re_matches(msg, re.NO_SUCH_FILE_RE, NO_GROUP)
446
447
    def test_timedata_does_not_match_format(self):
448
        """Test TIME_DATA_DOES_NOT_MATCH_FORMAT_RE."""
449
        msg = "time data '%d %b %y' does not match format '30 Nov 00'"
450
        # 'time data "%d \'%b %y" does not match format \'30 Nov 00\''
451
        groups = ("'%d %b %y'", "'30 Nov 00'")
452
        named_groups = {'format': "'30 Nov 00'", 'timedata': "'%d %b %y'"}
453
        self.re_matches(msg,
454
                        re.TIME_DATA_DOES_NOT_MATCH_FORMAT_RE,
455
                        (groups, named_groups))
456
457
    def test_invalid_token(self):
458
        """Test INVALID_TOKEN_RE."""
459
        msg = 'invalid token'
460
        self.re_matches(msg, re.INVALID_TOKEN_RE, NO_GROUP)
461
462
if __name__ == '__main__':
463
    print(sys.version_info)
464
    unittest2.main()
465