Debian Codegraph @

tests/unit/test_wrap_modes.py

raw
1 import pytest
2 from hypothesis import given, reject
3 from hypothesis import strategies as st
4
5 import isort
6 from isort import wrap_modes
7 from isort.wrap_modes import WrapModes, _wrap_modes
8
9
10 def test_wrap_mode_interface() -> None:
11 assert (
12 wrap_modes._wrap_mode_interface("statement", [], "", "", 80, [], "", "", True, True) == ""
13 )
14
15
16 def test_auto_saved() -> None:
17 """hypothesis_auto tests cases that have been saved to ensure they run each test cycle"""
18 assert (
19 wrap_modes.noqa(
20 comment_prefix="-\U000bf82c\x0c\U0004608f\x10%",
21 comments=[],
22 imports=[],
23 include_trailing_comma=False,
24 indent="0\x19",
25 line_length=-19659,
26 line_separator="\x15\x0b\U00086494\x1d\U000e00a2\U000ee216\U0006708a\x03\x1f",
27 remove_comments=False,
28 statement="\U00092452",
29 white_space="\U000a7322\U000c20e3-\U0010eae4\x07\x14\U0007d486",
30 )
31 == "\U00092452-\U000bf82c\x0c\U0004608f\x10% NOQA"
32 )
33 assert (
34 wrap_modes.noqa(
35 comment_prefix='\x12\x07\U0009e994๐Ÿฃ"\U000ae787\x0e',
36 comments=["\x00\U0001ae99\U0005c3e7\U0004d08e", "\x1e", "", ""],
37 imports=["*"],
38 include_trailing_comma=True,
39 indent="",
40 line_length=31492,
41 line_separator="\U00071610\U0005bfbc",
42 remove_comments=False,
43 statement="",
44 white_space="\x08\x01โท“\x16%\U0006cd8c",
45 )
46 == '*\x12\x07\U0009e994๐Ÿฃ"\U000ae787\x0e \x00\U0001ae99\U0005c3e7\U0004d08e \x1e '
47 )
48 assert (
49 wrap_modes.noqa(
50 comment_prefix=" #",
51 comments=["NOQA", "THERE"],
52 imports=[],
53 include_trailing_comma=False,
54 indent="0\x19",
55 line_length=-19659,
56 line_separator="\n",
57 remove_comments=False,
58 statement="hi",
59 white_space=" ",
60 )
61 == "hi # NOQA THERE"
62 )
63
64
65 def test_backslash_grid() -> None:
66 """Tests the backslash_grid grid wrap mode, ensuring it matches formatting expectations.
67 See: https://github.com/PyCQA/isort/issues/1434
68 """
69 assert (
70 isort.code(
71 """
72 from kopf.engines import loggers, posting
73 from kopf.reactor import causation, daemons, effects, handling, lifecycles, registries
74 from kopf.storage import finalizers, states
75 from kopf.structs import (bodies, configuration, containers, diffs,
76 handlers as handlers_, patches, resources)
77 """,
78 multi_line_output=11,
79 line_length=88,
80 combine_as_imports=True,
81 )
82 == """
83 from kopf.engines import loggers, posting
84 from kopf.reactor import causation, daemons, effects, handling, lifecycles, registries
85 from kopf.storage import finalizers, states
86 from kopf.structs import bodies, configuration, containers, diffs, \\
87 handlers as handlers_, patches, resources
88 """
89 )
90
91
92 @pytest.mark.parametrize("include_trailing_comma", [False, True])
93 @pytest.mark.parametrize("line_length", [18, 19])
94 @pytest.mark.parametrize("multi_line_output", [4, 5])
95 def test_vertical_grid_size_near_line_length(
96 multi_line_output: int,
97 line_length: int,
98 include_trailing_comma: bool,
99 ) -> None:
100 separator = " "
101 # Cases where the input should be wrapped:
102 if (
103 # Mode 4 always adds a closing ")", making the imports line 19 chars,
104 # if include_trailing_comma is True that becomes 20 chars.
105 (multi_line_output == 4 and line_length < 19 + int(include_trailing_comma))
106 # Modes 5 and 6 only add a comma, if include_trailing_comma is True,
107 # so their lines are 18 or 19 chars long.
108 or (multi_line_output != 4 and line_length < 18 + int(include_trailing_comma))
109 ):
110 separator = "\n "
111
112 test_input = f"from foo import (\n aaaa, bbb,{separator}ccc"
113 if include_trailing_comma:
114 test_input += ","
115 if multi_line_output != 4:
116 test_input += "\n"
117 test_input += ")\n"
118
119 assert (
120 isort.code(
121 test_input,
122 multi_line_output=multi_line_output,
123 line_length=line_length,
124 include_trailing_comma=include_trailing_comma,
125 )
126 == test_input
127 )
128
129
130 # This test code was written by the `hypothesis.extra.ghostwriter` module
131 # and is provided under the Creative Commons Zero public domain dedication.
132
133
134 @given(
135 statement=st.text(),
136 imports=st.lists(st.text()),
137 white_space=st.text(),
138 indent=st.text(),
139 line_length=st.integers(),
140 comments=st.lists(st.text()),
141 line_separator=st.text(),
142 comment_prefix=st.text(),
143 include_trailing_comma=st.booleans(),
144 remove_comments=st.booleans(),
145 )
146 def test_fuzz_backslash_grid(
147 statement: str,
148 imports: list[str],
149 white_space: str,
150 indent: str,
151 line_length: int,
152 comments: list[str],
153 line_separator: str,
154 comment_prefix: str,
155 include_trailing_comma: bool,
156 remove_comments: bool,
157 ) -> None:
158 try:
159 isort.wrap_modes.backslash_grid(
160 statement=statement,
161 imports=imports,
162 white_space=white_space,
163 indent=indent,
164 line_length=line_length,
165 comments=comments,
166 line_separator=line_separator,
167 comment_prefix=comment_prefix,
168 include_trailing_comma=include_trailing_comma,
169 remove_comments=remove_comments,
170 )
171 except ValueError:
172 reject()
173
174
175 @given(
176 statement=st.text(),
177 imports=st.lists(st.text()),
178 white_space=st.text(),
179 indent=st.text(),
180 line_length=st.integers(),
181 comments=st.lists(st.text()),
182 line_separator=st.text(),
183 comment_prefix=st.text(),
184 include_trailing_comma=st.booleans(),
185 remove_comments=st.booleans(),
186 )
187 def test_fuzz_grid(
188 statement: str,
189 imports: list[str],
190 white_space: str,
191 indent: str,
192 line_length: int,
193 comments: list[str],
194 line_separator: str,
195 comment_prefix: str,
196 include_trailing_comma: bool,
197 remove_comments: bool,
198 ) -> None:
199 try:
200 isort.wrap_modes.grid(
201 statement=statement,
202 imports=imports,
203 white_space=white_space,
204 indent=indent,
205 line_length=line_length,
206 comments=comments,
207 line_separator=line_separator,
208 comment_prefix=comment_prefix,
209 include_trailing_comma=include_trailing_comma,
210 remove_comments=remove_comments,
211 )
212 except ValueError:
213 reject()
214
215
216 @given(
217 statement=st.text(),
218 imports=st.lists(st.text()),
219 white_space=st.text(),
220 indent=st.text(),
221 line_length=st.integers(),
222 comments=st.lists(st.text()),
223 line_separator=st.text(),
224 comment_prefix=st.text(),
225 include_trailing_comma=st.booleans(),
226 remove_comments=st.booleans(),
227 )
228 def test_fuzz_hanging_indent(
229 statement: str,
230 imports: list[str],
231 white_space: str,
232 indent: str,
233 line_length: int,
234 comments: list[str],
235 line_separator: str,
236 comment_prefix: str,
237 include_trailing_comma: bool,
238 remove_comments: bool,
239 ) -> None:
240 try:
241 isort.wrap_modes.hanging_indent(
242 statement=statement,
243 imports=imports,
244 white_space=white_space,
245 indent=indent,
246 line_length=line_length,
247 comments=comments,
248 line_separator=line_separator,
249 comment_prefix=comment_prefix,
250 include_trailing_comma=include_trailing_comma,
251 remove_comments=remove_comments,
252 )
253 except ValueError:
254 reject()
255
256
257 @pytest.mark.parametrize("include_trailing_comma", [True, False])
258 def test_hanging_indent__with_include_trailing_comma__expect_same_result(
259 include_trailing_comma: bool,
260 ) -> None:
261 result = isort.wrap_modes.hanging_indent(
262 statement="from datetime import ",
263 imports=["datetime", "time", "timedelta", "timezone", "tzinfo"],
264 white_space=" ",
265 indent=" ",
266 line_length=50,
267 comments=[],
268 line_separator="\n",
269 comment_prefix=" #",
270 include_trailing_comma=include_trailing_comma,
271 remove_comments=False,
272 )
273
274 assert result == "from datetime import datetime, time, timedelta, \\\n timezone, tzinfo"
275
276
277 @given(
278 statement=st.text(),
279 imports=st.lists(st.text()),
280 white_space=st.text(),
281 indent=st.text(),
282 line_length=st.integers(),
283 comments=st.lists(st.text()),
284 line_separator=st.text(),
285 comment_prefix=st.text(),
286 include_trailing_comma=st.booleans(),
287 remove_comments=st.booleans(),
288 )
289 def test_fuzz_hanging_indent_with_parentheses(
290 statement: str,
291 imports: list[str],
292 white_space: str,
293 indent: str,
294 line_length: int,
295 comments: list[str],
296 line_separator: str,
297 comment_prefix: str,
298 include_trailing_comma: bool,
299 remove_comments: bool,
300 ) -> None:
301 try:
302 isort.wrap_modes.hanging_indent_with_parentheses(
303 statement=statement,
304 imports=imports,
305 white_space=white_space,
306 indent=indent,
307 line_length=line_length,
308 comments=comments,
309 line_separator=line_separator,
310 comment_prefix=comment_prefix,
311 include_trailing_comma=include_trailing_comma,
312 remove_comments=remove_comments,
313 )
314 except ValueError:
315 reject()
316
317
318 @given(
319 statement=st.text(),
320 imports=st.lists(st.text()),
321 white_space=st.text(),
322 indent=st.text(),
323 line_length=st.integers(),
324 comments=st.lists(st.text()),
325 line_separator=st.text(),
326 comment_prefix=st.text(),
327 include_trailing_comma=st.booleans(),
328 remove_comments=st.booleans(),
329 )
330 def test_fuzz_noqa(
331 statement: str,
332 imports: list[str],
333 white_space: str,
334 indent: str,
335 line_length: int,
336 comments: list[str],
337 line_separator: str,
338 comment_prefix: str,
339 include_trailing_comma: bool,
340 remove_comments: bool,
341 ) -> None:
342 try:
343 isort.wrap_modes.noqa(
344 statement=statement,
345 imports=imports,
346 white_space=white_space,
347 indent=indent,
348 line_length=line_length,
349 comments=comments,
350 line_separator=line_separator,
351 comment_prefix=comment_prefix,
352 include_trailing_comma=include_trailing_comma,
353 remove_comments=remove_comments,
354 )
355 except ValueError:
356 reject()
357
358
359 @given(
360 statement=st.text(),
361 imports=st.lists(st.text()),
362 white_space=st.text(),
363 indent=st.text(),
364 line_length=st.integers(),
365 comments=st.lists(st.text()),
366 line_separator=st.text(),
367 comment_prefix=st.text(),
368 include_trailing_comma=st.booleans(),
369 remove_comments=st.booleans(),
370 )
371 def test_fuzz_vertical(
372 statement: str,
373 imports: list[str],
374 white_space: str,
375 indent: str,
376 line_length: int,
377 comments: list[str],
378 line_separator: str,
379 comment_prefix: str,
380 include_trailing_comma: bool,
381 remove_comments: bool,
382 ) -> None:
383 try:
384 isort.wrap_modes.vertical(
385 statement=statement,
386 imports=imports,
387 white_space=white_space,
388 indent=indent,
389 line_length=line_length,
390 comments=comments,
391 line_separator=line_separator,
392 comment_prefix=comment_prefix,
393 include_trailing_comma=include_trailing_comma,
394 remove_comments=remove_comments,
395 )
396 except ValueError:
397 reject()
398
399
400 @given(
401 statement=st.text(),
402 imports=st.lists(st.text()),
403 white_space=st.text(),
404 indent=st.text(),
405 line_length=st.integers(),
406 comments=st.lists(st.text()),
407 line_separator=st.text(),
408 comment_prefix=st.text(),
409 include_trailing_comma=st.booleans(),
410 remove_comments=st.booleans(),
411 )
412 def test_fuzz_vertical_grid(
413 statement: str,
414 imports: list[str],
415 white_space: str,
416 indent: str,
417 line_length: int,
418 comments: list[str],
419 line_separator: str,
420 comment_prefix: str,
421 include_trailing_comma: bool,
422 remove_comments: bool,
423 ) -> None:
424 try:
425 isort.wrap_modes.vertical_grid(
426 statement=statement,
427 imports=imports,
428 white_space=white_space,
429 indent=indent,
430 line_length=line_length,
431 comments=comments,
432 line_separator=line_separator,
433 comment_prefix=comment_prefix,
434 include_trailing_comma=include_trailing_comma,
435 remove_comments=remove_comments,
436 )
437 except ValueError:
438 reject()
439
440
441 @given(
442 statement=st.text(),
443 imports=st.lists(st.text()),
444 white_space=st.text(),
445 indent=st.text(),
446 line_length=st.integers(),
447 comments=st.lists(st.text()),
448 line_separator=st.text(),
449 comment_prefix=st.text(),
450 include_trailing_comma=st.booleans(),
451 remove_comments=st.booleans(),
452 )
453 def test_fuzz_vertical_grid_grouped(
454 statement: str,
455 imports: list[str],
456 white_space: str,
457 indent: str,
458 line_length: int,
459 comments: list[str],
460 line_separator: str,
461 comment_prefix: str,
462 include_trailing_comma: bool,
463 remove_comments: bool,
464 ) -> None:
465 try:
466 isort.wrap_modes.vertical_grid_grouped(
467 statement=statement,
468 imports=imports,
469 white_space=white_space,
470 indent=indent,
471 line_length=line_length,
472 comments=comments,
473 line_separator=line_separator,
474 comment_prefix=comment_prefix,
475 include_trailing_comma=include_trailing_comma,
476 remove_comments=remove_comments,
477 )
478 except ValueError:
479 reject()
480
481
482 @given(
483 statement=st.text(),
484 imports=st.lists(st.text()),
485 white_space=st.text(),
486 indent=st.text(),
487 line_length=st.integers(),
488 comments=st.lists(st.text()),
489 line_separator=st.text(),
490 comment_prefix=st.text(),
491 include_trailing_comma=st.booleans(),
492 remove_comments=st.booleans(),
493 )
494 def test_fuzz_vertical_hanging_indent(
495 statement: str,
496 imports: list[str],
497 white_space: str,
498 indent: str,
499 line_length: int,
500 comments: list[str],
501 line_separator: str,
502 comment_prefix: str,
503 include_trailing_comma: bool,
504 remove_comments: bool,
505 ) -> None:
506 try:
507 isort.wrap_modes.vertical_hanging_indent(
508 statement=statement,
509 imports=imports,
510 white_space=white_space,
511 indent=indent,
512 line_length=line_length,
513 comments=comments,
514 line_separator=line_separator,
515 comment_prefix=comment_prefix,
516 include_trailing_comma=include_trailing_comma,
517 remove_comments=remove_comments,
518 )
519 except ValueError:
520 reject()
521
522
523 @given(
524 statement=st.text(),
525 imports=st.lists(st.text()),
526 white_space=st.text(),
527 indent=st.text(),
528 line_length=st.integers(),
529 comments=st.lists(st.text()),
530 line_separator=st.text(),
531 comment_prefix=st.text(),
532 include_trailing_comma=st.booleans(),
533 remove_comments=st.booleans(),
534 )
535 def test_fuzz_vertical_hanging_indent_bracket(
536 statement: str,
537 imports: list[str],
538 white_space: str,
539 indent: str,
540 line_length: int,
541 comments: list[str],
542 line_separator: str,
543 comment_prefix: str,
544 include_trailing_comma: bool,
545 remove_comments: bool,
546 ) -> None:
547 try:
548 isort.wrap_modes.vertical_hanging_indent_bracket(
549 statement=statement,
550 imports=imports,
551 white_space=white_space,
552 indent=indent,
553 line_length=line_length,
554 comments=comments,
555 line_separator=line_separator,
556 comment_prefix=comment_prefix,
557 include_trailing_comma=include_trailing_comma,
558 remove_comments=remove_comments,
559 )
560 except ValueError:
561 reject()
562
563
564 @given(
565 statement=st.text(),
566 imports=st.lists(st.text()),
567 white_space=st.text(),
568 indent=st.text(),
569 line_length=st.integers(),
570 comments=st.lists(st.text()),
571 line_separator=st.text(),
572 comment_prefix=st.text(),
573 include_trailing_comma=st.booleans(),
574 remove_comments=st.booleans(),
575 )
576 def test_fuzz_vertical_prefix_from_module_import(
577 statement: str,
578 imports: list[str],
579 white_space: str,
580 indent: str,
581 line_length: int,
582 comments: list[str],
583 line_separator: str,
584 comment_prefix: str,
585 include_trailing_comma: bool,
586 remove_comments: bool,
587 ) -> None:
588 try:
589 isort.wrap_modes.vertical_prefix_from_module_import(
590 statement=statement,
591 imports=imports,
592 white_space=white_space,
593 indent=indent,
594 line_length=line_length,
595 comments=comments,
596 line_separator=line_separator,
597 comment_prefix=comment_prefix,
598 include_trailing_comma=include_trailing_comma,
599 remove_comments=remove_comments,
600 )
601 except ValueError:
602 reject()
603
604
605 def test_enum_matches_order_of_definitions() -> None:
606 """Before isort 9.0.0 WrapModes was dynamically generated, this tests for breaking changes."""
607
608 old_enum_logic = {wrap_mode: index for index, wrap_mode in enumerate(_wrap_modes.keys())}
609 new_enum_logic = {wrap_mode.name: wrap_mode.value for wrap_mode in WrapModes}
610 assert old_enum_logic == new_enum_logic, (
611 "WrapModes enum order has changed, this is a breaking change."
612 )