-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_checker.py
247 lines (214 loc) · 7.52 KB
/
test_checker.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"""KolesChecker test module."""
from unittest import mock
from unittest.mock import Mock
import pytest
from flake8.options.manager import OptionManager, Option
from flake8_koles.checker import KolesChecker
@pytest.mark.parametrize(
"ignore_shorties, expected_result",
(
(0, ['Mike D', 'MCA', 'Ad-Rock']),
(1, ['Mike D', 'MCA', 'Ad-Rock']),
(3, ['Mike D', 'Ad-Rock']),
(6, ['Ad-Rock', ]),
(69, []),
),
)
@mock.patch('flake8_koles.checker.pkg_resources.resource_string')
def test_get_bad_words(
mock_resource_string,
ignore_shorties,
expected_result,
koles_checker
):
"""Test that the function returns proper set of bad words depending on ignore-shorties option."""
mock_resource_string.return_value = b'Mike D\nMCA\nAd-Rock\n'
koles_checker.options = Mock(ignore_shorties=ignore_shorties)
result = [*koles_checker._get_bad_words()]
assert result == expected_result
@pytest.mark.parametrize(
'pattern, string, expected_result',
(
# Case 1: Multiple overlapping patterns
('abcd|ab|abc|cd', 'abcdab', [(0, 'abcd'), (2, 'cd'), (4, 'ab')]),
# Case 2: Single non-overlapping pattern
('ab', 'abcdab', [(0, 'ab'), (4, 'ab')]),
# Case 3: Empty string
('(?=(ab))', '', []),
# Case 4: Empty pattern
('', 'abcdab', []),
# Case 6: Empty string and pattern
('', '', []),
# Case 7: Uppercase string
('abcd|ab|abc|cd', 'ABCDAB', [(0, 'ABCD'), (2, 'CD'), (4, 'AB')]),
),
)
def test_check_row(
pattern,
string,
expected_result,
koles_checker
):
"""Test that check_string returns appropriate value for given pattern and string."""
koles_checker._pattern = pattern
result = koles_checker._check_row(string)
assert [*result] == expected_result
@pytest.mark.parametrize(
'word, censor_msg, expected_result',
(
('Mike D', True, 'M*****'),
('Mike D', False, 'Mike D'),
('MCA', True, 'M**'),
('MCA', False, 'MCA'),
('Ad-Rock', True, 'A******'),
('Ad-Rock', False, 'Ad-Rock'),
),
)
def test_censor_word(
word,
censor_msg,
expected_result,
koles_checker
):
"""Test that the function returns proper set of bad words depending on ignore-shorties option."""
koles_checker.options = Mock(censor_msg=censor_msg)
result = koles_checker._censor_word(word)
assert result == expected_result
@pytest.mark.parametrize(
'filename,',
(
'stdin',
'-',
None,
),
)
@mock.patch('flake8_koles.checker.stdin_get_value')
def test_get_file_content_stdin(
mock_stdin_get_value,
filename,
koles_checker
):
"""Test that flake8 stdin_get_value is called for appropriate filename."""
koles_checker.filename = filename
koles_checker._get_file_content()
mock_stdin_get_value.assert_called_once()
@mock.patch('flake8_koles.checker.readlines')
def test_get_file_content_regular_filename(
mock_readlines, koles_checker
):
"""Test that flake8 pycodestyle readlines is called for appropriate filename."""
koles_checker.filename = 'test_filename'
koles_checker._get_file_content()
mock_readlines.assert_called_once()
@pytest.mark.parametrize(
'filename, check_row_value, censor_word_value, expected_result',
(
(
'test_filename', [], [], []
),
(
'ugly_name',
[(0, 'ugly')],
['ugly', ],
[(0, 0, 'KOL002 Filename contains bad language: ugly', KolesChecker)]
),
(
'bad_ugly_name',
[(0, 'bad'), (4, 'ugly')],
['bad', 'ugly'],
[
(0, 0, 'KOL002 Filename contains bad language: bad', KolesChecker),
(0, 4, 'KOL002 Filename contains bad language: ugly', KolesChecker)
]
)
),
)
@mock.patch('flake8_koles.checker.KolesChecker._check_row')
@mock.patch('flake8_koles.checker.KolesChecker._censor_word')
def test_get_filename_errors(
mock_censor_word,
mock_check_row,
filename,
check_row_value,
censor_word_value,
expected_result,
koles_checker
):
"""Test that appropriate error messages are returned."""
mock_check_row.return_value = check_row_value
mock_censor_word.side_effect = censor_word_value
koles_checker.filename = filename
result = [*koles_checker._get_filename_errors()]
assert result == expected_result
@pytest.mark.parametrize(
'content, check_row_value, censor_word_value, expected_result',
(
(
['nice_content'], [[]], [], []
),
(
['ugly_content'],
[[(0, 'ugly')]],
['ugly', ],
[(1, 0, 'KOL001 Bad language found: ugly', KolesChecker)]
),
(
['ugly_content', 'very_bad_content'],
[[(0, 'ugly')], [(5, 'bad')]],
['ugly', 'bad'],
[
(1, 0, 'KOL001 Bad language found: ugly', KolesChecker),
(2, 5, 'KOL001 Bad language found: bad', KolesChecker)
]
),
),
)
@mock.patch('flake8_koles.checker.KolesChecker._check_row')
@mock.patch('flake8_koles.checker.KolesChecker._censor_word')
def test_get_content_errors(
mock_censor_word,
mock_check_row,
content,
check_row_value,
censor_word_value,
expected_result,
koles_checker
):
"""Test that appropriate error messages are returned."""
mock_check_row.side_effect = check_row_value
mock_censor_word.side_effect = censor_word_value
result = [*koles_checker._get_content_errors(content)]
assert result == expected_result
@mock.patch('flake8_koles.checker.readlines')
@mock.patch('flake8_koles.checker.KolesChecker._get_bad_words')
def test_run(
mock_get_bad_words,
mock_readlines
):
"""Test that flake interface returns appropriate error messages."""
mock_get_bad_words.return_value = ['very', 'bad', 'words']
mock_readlines.return_value = ['Test very', 'nice', 'and bad words']
koles_checker = KolesChecker(tree='test_tree', filename='test_filename')
koles_checker.options = Mock(censor_msg=True)
result = [*koles_checker.run()]
assert result == [
(1, 5, 'KOL001 Bad language found: v***', KolesChecker),
(3, 4, 'KOL001 Bad language found: b**', KolesChecker),
(3, 8, 'KOL001 Bad language found: w****', KolesChecker)]
def test_add_options(koles_checker):
"""Test that all options are added to the parser."""
option_manager = OptionManager()
koles_checker.add_options(option_manager)
assert repr(option_manager.options) == repr([
Option(
long_option_name="--ignore-shorties", default=0, type="int", parse_from_config=True
),
Option(
long_option_name="--censor-msg", default=0, parse_from_config=True, action='store_true'
)
])
def test_parse_options(koles_checker):
"""Test that options are correctly assigned to the class."""
test_options = {'kick_it': True}
koles_checker.parse_options(test_options)
assert koles_checker.options == test_options