Repository navigation
Expand file tree
/
Copy pathtest_inline_approvals.py
More file actions
367 lines (274 loc) · 8.61 KB
/
Copy pathtest_inline_approvals.py
File metadata and controls
367 lines (274 loc) · 8.61 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import sys
import unittest
import pytest
from approvaltests import (
ApprovalException,
Options,
verify,
verify_all,
verify_all_combinations_with_labeled_input,
verify_as_json,
)
from approvaltests.inline.inline_options import InlineOptions
from approvaltests.inline.markers import PRESERVE_LEADING_WHITESPACE_MARKER
from approvaltests.inline.parse_docstring import parse_docstring
from approvaltests.namer.inline_comparator import InlineComparator
from approvaltests.namer.inline_python_reporter import (
detect_trailing_whitespace,
escape_backslashes,
escape_characters_dict,
handle_preceeding_whitespace,
)
from approvaltests.reporters.diff_tools import ReportWithBeyondCompare
from approvaltests.reporters.report_quietly import ReportQuietly
# Todo:
# detect the actual tab
# detect if the quote type used in the docstring (i.e. " or ')
def verify_approval_failure(text: str) -> None:
with pytest.raises(ApprovalException):
verify(text, options=Options().with_reporter(ReportQuietly()).inline())
def fizz_buzz(param: int) -> str:
return_string = ""
for i in range(1, param + 1):
if i % 15 == 0:
return_string += "FizzBuzz\n"
elif i % 3 == 0:
return_string += "Fizz\n"
elif i % 5 == 0:
return_string += "Buzz\n"
else:
return_string += str(i) + "\n"
return return_string
def test_fizz_buzz() -> None:
"""
1
2
Fizz
4
Buzz
Fizz
7
8
"""
verify(fizz_buzz(8), options=Options().inline())
def test_docstrings() -> None:
"""
hello
world
"""
# verify_inline(greetting())
# verify(greetting(), options=Options().inline(show_code= False))
verify(greeting(), options=Options().inline())
def greeting() -> str:
return "hello\nworld"
def test_docstring_parsing() -> None:
"""
1
2 -> 2
Fizz
4
Buzz
Fizz
7
8
"""
verify_all("inputs", parse_docstring())
def test_uppercase() -> None:
"""
a -> A
b -> B
c -> C
"""
verify(
"\n".join([f"{a} -> {a.upper()}" for a in parse_docstring()]),
options=Options().inline(),
)
options = Options().inline()
def test_when_options_is_created_outside_of_test() -> None:
"""
hello
world
"""
verify(greeting(), options=options)
def test_exception_on_failure() -> None:
"""
this string should not match
"""
with pytest.raises(ApprovalException):
verify(greeting(), options=Options().with_reporter(ReportQuietly()).inline())
class InlineTests(unittest.TestCase):
def test_with_labeled_input_inline(self) -> None:
"""
(arg1: 1, arg2: 2) => 3
(arg1: 1, arg2: 4) => 5
(arg1: 3, arg2: 2) => 5
(arg1: 3, arg2: 4) => 7
"""
verify_all_combinations_with_labeled_input(
lambda a, b: a + b,
arg1=(1, 3),
arg2=(2, 4),
options=Options().inline(),
)
def get_preceding_whitespace() -> str:
return " 4 whitespaces"
# fmt: off
@unittest.skipIf(sys.version_info >= (3, 13), "__doc__ removes preceding whitespace in Python 3.13+")
def test_preceding_whitespace_before_python_3_13() -> None:
"""
4 whitespaces
"""
verify(get_preceding_whitespace(), options=Options().inline())
def test_preceding_whitespace() -> None:
"""
<<approvaltests:preserve-leading-whitespace>>
4 whitespaces
"""
verify(get_preceding_whitespace(), options=Options().inline())
def test_no_preceeding_whitespace() -> None:
"""
4 whitespaces
applesauce
"""
text = " 4 whitespaces\napplesauce"
verify(text, options=Options().inline())
def test_trailing_whitespace() -> None:
"""
4 trailing whitespaces
""" # Warning: Editors may remove trailing spaces, causing this test to fail
# Note: Pycharm will remove the trailing whitespaces, to disable this go to:
# File -> Settings -> Editor -> General -> On Save -> [ ] Remove trailing spaces
verify("4 trailing whitespaces ", options=Options().inline())
# fmt: on
def test_bug_blank_lines() -> None:
"""
test bug <br \\> with blank lines
"""
verify("\n\ntest bug <br \\> with blank lines\n\n\n\n", options=Options().inline())
def test_handle_preceeding_whitespace_all_lines_indented() -> None:
text = " indented with spaces\n\tindented with tab\n still indented"
result = handle_preceeding_whitespace(text)
assert result.startswith(PRESERVE_LEADING_WHITESPACE_MARKER)
def test_handle_preceeding_whitespace_one_line_not_indented() -> None:
text = " indented with spaces\nnot indented\n\tindented with tab"
result = handle_preceeding_whitespace(text)
assert not result.startswith(PRESERVE_LEADING_WHITESPACE_MARKER)
def test_inline_with_additional_reporter() -> None:
"""
hello
world
"""
verify(
"hello\nworld",
options=(Options().inline().add_reporter(ReportWithBeyondCompare())),
)
def test_inline_with_preserved_approved_text() -> None:
"""
42
***** DELETE ME TO APPROVE *****
vvvvv PREVIOUS RESULT vvvvv
41
"""
options = Options().inline(InlineOptions.semi_automatic_with_previous_approved())
try:
verify("42", options=options)
except ApprovalException:
pass
verify(InlineComparator.get_test_method_doc_string())
def test_inline_with_semi_automatic_inline() -> None:
"""
42
***** DELETE ME TO APPROVE *****
"""
options = Options().inline(InlineOptions.semi_automatic())
try:
verify("42", options=options)
except ApprovalException:
pass
verify(InlineComparator.get_test_method_doc_string())
def test_detect_trailing_whitespace_true() -> None:
text = "no trail\nwith trail \t\nnormal"
assert detect_trailing_whitespace(text)
def test_detect_trailing_whitespace_false() -> None:
text = "alpha\nbeta\ngamma"
assert not detect_trailing_whitespace(text)
def test_detect_trailing_whitespace_ignores_pure_empty_lines() -> None:
text = "line1\n\nline2"
assert not detect_trailing_whitespace(text)
def test_escape_backslashes_windows_path() -> None:
text = "C:\\Temp\\foo\\bar\\note.txt"
assert escape_backslashes(text) == "C:\\Temp\\foo\\bar\\note.txt".replace(
"\\", "\\\\"
)
def test_escape_backslashes_mixed_sequences() -> None:
text = "a\\b\\c\\n"
assert escape_backslashes(text) == "a\\b\\c\\n".replace("\\", "\\\\")
def test_unicode_and_special_characters__passing() -> None:
"""
é
"""
text = "e\u0301" # 'e' + COMBINING ACUTE (2 code points)
verify(text, options=Options().inline())
def test_unicode_and_special_characters__mismatch() -> None:
"""
é
"""
# 'e' + COMBINING ACUTE (2 code points)
verify_approval_failure("xe\u0301")
def test_unicode_and_special_characters__missing() -> None:
text = "e\u0301" # 'e' + COMBINING ACUTE (2 code points)
verify_approval_failure(text)
def test_unicode_and_special_characters__not_inline() -> None:
text = "e\u0301" # 'e' + COMBINING ACUTE (2 code points)
verify(text)
def test_unicode_null_character__passing() -> None:
"""
\x00
"""
text = "\x00" # Null character
verify(text, options=Options().inline())
def test_unicode_null_character__missing() -> None:
text = "hello\x00world" # Null character
verify_approval_failure(text)
def test_unicode_null_character__incorrect_test() -> None:
"""
hello\x00world
"""
verify_approval_failure("hello world")
def test_unicode_null_character__incorrect_approval() -> None:
"""
hello .
"""
verify_approval_failure("hello\x00.")
def test_unicode_right_to_left_mark__passing() -> None:
"""
hello!\u200fworld
"""
text = "hello!\u200fworld" # Right-to-left mark
verify(text, options=Options().inline())
def test_unicode_right_to_left_mark__missing() -> None:
text = "hello!\u200fworld" # Right-to-left mark
verify_approval_failure(text)
def test_unicode_right_to_left_mark__incorrect_approval() -> None:
"""
hello world
"""
verify_approval_failure("hello!\u200fworld")
def test_unicode_right_to_left_mark__incorrect_test() -> None:
"""
hello!\u200fworld
"""
verify_approval_failure("hello world")
def test_escape_characters_dict() -> None:
verify_all("", escape_characters_dict.items())
# begin-snippet: verify_dict_example
def test_verify_dict() -> None:
"""
{
"k1": "v1",
"k2": "v2"
}
"""
d = {"k1": "v1", "k2": "v2"}
verify_as_json(d, options=Options().inline())
# end-snippet