-
-
Notifications
You must be signed in to change notification settings - Fork 384
/
test_naming.py
166 lines (136 loc) · 4.15 KB
/
test_naming.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# -*- coding: utf-8 -*-
import pytest
from wemake_python_styleguide.constants import VARIABLE_NAMES_BLACKLIST
from wemake_python_styleguide.violations.naming import (
ConsecutiveUnderscoresInNameViolation,
PrivateNameViolation,
TooLongNameViolation,
TooShortNameViolation,
UnderscoredNumberNameViolation,
WrongVariableNameViolation,
)
from wemake_python_styleguide.visitors.ast.naming import WrongNameVisitor
@pytest.mark.parametrize('wrong_name', VARIABLE_NAMES_BLACKLIST)
def test_wrong_variable_name(
assert_errors,
assert_error_text,
parse_ast_tree,
naming_template,
default_options,
mode,
wrong_name,
):
"""Ensures that wrong names are not allowed."""
tree = parse_ast_tree(mode(naming_template.format(wrong_name)))
visitor = WrongNameVisitor(default_options, tree=tree)
visitor.run()
assert_errors(visitor, [WrongVariableNameViolation])
assert_error_text(visitor, wrong_name)
def test_short_variable_name(
assert_errors,
assert_error_text,
parse_ast_tree,
naming_template,
default_options,
mode,
):
"""Ensures that short names are not allowed."""
short_name = 'y'
tree = parse_ast_tree(mode(naming_template.format(short_name)))
visitor = WrongNameVisitor(default_options, tree=tree)
visitor.run()
assert_errors(visitor, [TooShortNameViolation])
assert_error_text(visitor, short_name)
def test_private_variable_name(
assert_errors,
assert_error_text,
parse_ast_tree,
naming_template,
default_options,
mode,
):
"""Ensures that private names are not allowed."""
private_name = '__private'
tree = parse_ast_tree(mode(naming_template.format(private_name)))
visitor = WrongNameVisitor(default_options, tree=tree)
visitor.run()
assert_errors(visitor, [PrivateNameViolation])
assert_error_text(visitor, private_name)
@pytest.mark.parametrize('underscored_name', [
'with__underscore',
'mutliple__under__score',
'triple___underscore',
])
def test_underscored_variable_name(
assert_errors,
assert_error_text,
parse_ast_tree,
naming_template,
default_options,
mode,
underscored_name,
):
"""Ensures that underscored names are not allowed."""
tree = parse_ast_tree(mode(naming_template.format(underscored_name)))
visitor = WrongNameVisitor(default_options, tree=tree)
visitor.run()
assert_errors(visitor, [ConsecutiveUnderscoresInNameViolation])
assert_error_text(visitor, underscored_name)
@pytest.mark.parametrize('number_suffix', [
'number_5',
'between_45_letters',
'with_multiple_groups_4_5',
])
def test_number_prefix_variable_name(
assert_errors,
assert_error_text,
parse_ast_tree,
naming_template,
default_options,
mode,
number_suffix,
):
"""Ensures that number suffix names are not allowed."""
tree = parse_ast_tree(mode(naming_template.format(number_suffix)))
visitor = WrongNameVisitor(default_options, tree=tree)
visitor.run()
assert_errors(visitor, [UnderscoredNumberNameViolation])
assert_error_text(visitor, number_suffix)
@pytest.mark.parametrize('correct_name', [
'snake_case',
'_protected_or_unused',
'with_number5',
'xy',
])
def test_naming_correct(
assert_errors,
parse_ast_tree,
naming_template,
default_options,
mode,
correct_name,
):
"""Ensures that correct names are allowed."""
tree = parse_ast_tree(mode(naming_template.format(correct_name)))
visitor = WrongNameVisitor(default_options, tree=tree)
visitor.run()
assert_errors(visitor, [])
"""
Removed for now until _check_name gets refactored to
incorporate the new test
def test_long_variable_name(
assert_errors,
assert_error_text,
parse_ast_tree,
naming_template,
default_options,
mode,
):
Ensures that long names are not allowed.
long_name = 'incredibly_and_very_long_name_that_will_definitely_not_work'
tree = parse_ast_tree(mode(naming_template.format(long_name)))
visitor = WrongNameVisitor(default_options, tree=tree)
visitor.run()
assert_errors(visitor, [TooLongNameViolation])
assert_error_text(visitor, long_name)
"""