/
test_yield_magic_method.py
120 lines (105 loc) · 2.43 KB
/
test_yield_magic_method.py
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
import pytest
from wemake_python_styleguide.violations.oop import YieldMagicMethodViolation
from wemake_python_styleguide.visitors.ast.classes import WrongMethodVisitor
method_template = """
class Test(object):
def {0}(self, *args, **kwargs):
{1}
"""
classmethod_template = """
class Test(object):
@classmethod
def {0}(cls, *args, **kwargs):
{1}
"""
@pytest.mark.parametrize('code', [
method_template,
classmethod_template,
])
@pytest.mark.parametrize('method', [
'__init__',
'__new__',
'__str__',
'__aenter__',
'__exit__',
'__anext__',
'__next__',
])
@pytest.mark.parametrize('statement', [
'yield',
'yield 1',
'yield from some()',
])
def test_magic_generator(
assert_errors,
assert_error_text,
parse_ast_tree,
default_options,
code,
method,
statement,
):
"""Testing that magic method with `yield` is prohibited."""
tree = parse_ast_tree(code.format(method, statement))
visitor = WrongMethodVisitor(default_options, tree=tree)
visitor.run()
assert_errors(visitor, [YieldMagicMethodViolation])
assert_error_text(visitor, method)
@pytest.mark.parametrize('code', [
method_template,
classmethod_template,
])
@pytest.mark.parametrize('method', [
'__init__',
'__new__',
'__str__',
'__aenter__',
'__iter__',
'__exit__',
'__custom__',
])
@pytest.mark.parametrize('statement', [
'return 1',
'print()',
])
def test_magic_statement(
assert_errors,
parse_ast_tree,
default_options,
code,
method,
statement,
):
"""Testing that magic method with statement is allowed."""
tree = parse_ast_tree(code.format(method, statement))
visitor = WrongMethodVisitor(default_options, tree=tree)
visitor.run()
assert_errors(visitor, [])
@pytest.mark.parametrize('code', [
method_template,
classmethod_template,
])
@pytest.mark.parametrize('method', [
'__iter__',
'__aiter__',
'__call__',
'__custom__',
])
@pytest.mark.parametrize('statement', [
'yield',
'yield 1',
'yield from some()',
])
def test_iter_generator(
assert_errors,
parse_ast_tree,
default_options,
code,
method,
statement,
):
"""Testing that some magic methods with `yield` are allowed."""
tree = parse_ast_tree(code.format(method, statement))
visitor = WrongMethodVisitor(default_options, tree=tree)
visitor.run()
assert_errors(visitor, [])