-
-
Notifications
You must be signed in to change notification settings - Fork 384
/
test_class_attributes.py
109 lines (90 loc) · 2.3 KB
/
test_class_attributes.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
import pytest
from wemake_python_styleguide.violations.naming import (
UpperCaseAttributeViolation,
)
from wemake_python_styleguide.visitors.ast.naming import WrongNameVisitor
static_attribute = """
class Test(object):
{0} = None
"""
static_typed_attribute = """
class Test(object):
{0}: int = None
"""
static_typed_condition_attribute = """
class Test(object):
if sys.version_info > (3, 8):
{0}: int = None
"""
regression423 = """
class MyClass(object):
def action_method(self, request, second):
...
action_method.label = 'Do action'
"""
@pytest.mark.parametrize('code', [
static_attribute,
static_typed_attribute,
static_typed_condition_attribute,
])
@pytest.mark.parametrize('non_snake_case_name', [
'Abc',
'A_CONSTANT',
'AAA',
'B2',
'CONST1_bc',
'camelCase',
'_A_c',
])
def test_upper_case_class_attributes(
assert_errors,
assert_error_text,
parse_ast_tree,
non_snake_case_name,
code,
default_options,
):
"""Testing that attribute cannot have too short names."""
tree = parse_ast_tree(code.format(non_snake_case_name))
visitor = WrongNameVisitor(default_options, tree=tree)
visitor.run()
assert_errors(visitor, [UpperCaseAttributeViolation])
assert_error_text(visitor, non_snake_case_name)
@pytest.mark.parametrize('code', [
static_attribute,
static_typed_attribute,
static_typed_condition_attribute,
])
@pytest.mark.parametrize('snake_case_name', [
'abc',
'a_variable',
'aaa',
'two_minutes_to_midnight',
'variable42_5',
'_a_c',
])
def test_snake_case_class_attributes(
assert_errors,
parse_ast_tree,
snake_case_name,
code,
default_options,
):
"""Testing that attribute cannot have too short names."""
tree = parse_ast_tree(code.format(snake_case_name))
visitor = WrongNameVisitor(default_options, tree=tree)
visitor.run()
assert_errors(visitor, [])
def test_regression423(
assert_errors,
parse_ast_tree,
default_options,
):
"""
Tests that this issue-423 won't happen again.
See: https://github.com/wemake-services/wemake-python-styleguide/issues/423
"""
tree = parse_ast_tree(regression423)
visitor = WrongNameVisitor(default_options, tree=tree)
visitor.run()
assert_errors(visitor, [])