]> git.madduck.net Git - etc/vim.git/blob - tests/test_format.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Remove Python version in the_basics.md (#3528)
[etc/vim.git] / tests / test_format.py
1 import re
2 from dataclasses import replace
3 from typing import Any, Iterator
4 from unittest.mock import patch
5
6 import pytest
7
8 import black
9 from tests.util import (
10     DEFAULT_MODE,
11     PY36_VERSIONS,
12     all_data_cases,
13     assert_format,
14     dump_to_stderr,
15     read_data,
16 )
17
18
19 @pytest.fixture(autouse=True)
20 def patch_dump_to_file(request: Any) -> Iterator[None]:
21     with patch("black.dump_to_file", dump_to_stderr):
22         yield
23
24
25 def check_file(
26     subdir: str, filename: str, mode: black.Mode, *, data: bool = True
27 ) -> None:
28     source, expected = read_data(subdir, filename, data=data)
29     assert_format(source, expected, mode, fast=False)
30
31
32 @pytest.mark.filterwarnings("ignore:invalid escape sequence.*:DeprecationWarning")
33 @pytest.mark.parametrize("filename", all_data_cases("simple_cases"))
34 def test_simple_format(filename: str) -> None:
35     check_file("simple_cases", filename, DEFAULT_MODE)
36
37
38 @pytest.mark.parametrize("filename", all_data_cases("preview"))
39 def test_preview_format(filename: str) -> None:
40     magic_trailing_comma = filename != "skip_magic_trailing_comma"
41     check_file(
42         "preview",
43         filename,
44         black.Mode(preview=True, magic_trailing_comma=magic_trailing_comma),
45     )
46
47
48 @pytest.mark.parametrize("filename", all_data_cases("preview_39"))
49 def test_preview_minimum_python_39_format(filename: str) -> None:
50     source, expected = read_data("preview_39", filename)
51     mode = black.Mode(preview=True)
52     assert_format(source, expected, mode, minimum_version=(3, 9))
53
54
55 @pytest.mark.parametrize("filename", all_data_cases("preview_310"))
56 def test_preview_minimum_python_310_format(filename: str) -> None:
57     source, expected = read_data("preview_310", filename)
58     mode = black.Mode(preview=True)
59     assert_format(source, expected, mode, minimum_version=(3, 10))
60
61
62 def test_preview_context_managers_targeting_py38() -> None:
63     source, expected = read_data("preview_context_managers", "targeting_py38.py")
64     mode = black.Mode(preview=True, target_versions={black.TargetVersion.PY38})
65     assert_format(source, expected, mode, minimum_version=(3, 8))
66
67
68 def test_preview_context_managers_targeting_py39() -> None:
69     source, expected = read_data("preview_context_managers", "targeting_py39.py")
70     mode = black.Mode(preview=True, target_versions={black.TargetVersion.PY39})
71     assert_format(source, expected, mode, minimum_version=(3, 9))
72
73
74 @pytest.mark.parametrize(
75     "filename", all_data_cases("preview_context_managers/auto_detect")
76 )
77 def test_preview_context_managers_auto_detect(filename: str) -> None:
78     match = re.match(r"features_3_(\d+)", filename)
79     assert match is not None, "Unexpected filename format: %s" % filename
80     source, expected = read_data("preview_context_managers/auto_detect", filename)
81     mode = black.Mode(preview=True)
82     assert_format(source, expected, mode, minimum_version=(3, int(match.group(1))))
83
84
85 # =============== #
86 # Complex cases
87 # ============= #
88
89
90 def test_empty() -> None:
91     source = expected = ""
92     assert_format(source, expected)
93
94
95 @pytest.mark.parametrize("filename", all_data_cases("py_36"))
96 def test_python_36(filename: str) -> None:
97     source, expected = read_data("py_36", filename)
98     mode = black.Mode(target_versions=PY36_VERSIONS)
99     assert_format(source, expected, mode, minimum_version=(3, 6))
100
101
102 @pytest.mark.parametrize("filename", all_data_cases("py_37"))
103 def test_python_37(filename: str) -> None:
104     source, expected = read_data("py_37", filename)
105     mode = black.Mode(target_versions={black.TargetVersion.PY37})
106     assert_format(source, expected, mode, minimum_version=(3, 7))
107
108
109 @pytest.mark.parametrize("filename", all_data_cases("py_38"))
110 def test_python_38(filename: str) -> None:
111     source, expected = read_data("py_38", filename)
112     mode = black.Mode(target_versions={black.TargetVersion.PY38})
113     assert_format(source, expected, mode, minimum_version=(3, 8))
114
115
116 @pytest.mark.parametrize("filename", all_data_cases("py_39"))
117 def test_python_39(filename: str) -> None:
118     source, expected = read_data("py_39", filename)
119     mode = black.Mode(target_versions={black.TargetVersion.PY39})
120     assert_format(source, expected, mode, minimum_version=(3, 9))
121
122
123 @pytest.mark.parametrize("filename", all_data_cases("py_310"))
124 def test_python_310(filename: str) -> None:
125     source, expected = read_data("py_310", filename)
126     mode = black.Mode(target_versions={black.TargetVersion.PY310})
127     assert_format(source, expected, mode, minimum_version=(3, 10))
128
129
130 @pytest.mark.parametrize("filename", all_data_cases("py_310"))
131 def test_python_310_without_target_version(filename: str) -> None:
132     source, expected = read_data("py_310", filename)
133     mode = black.Mode()
134     assert_format(source, expected, mode, minimum_version=(3, 10))
135
136
137 def test_patma_invalid() -> None:
138     source, expected = read_data("miscellaneous", "pattern_matching_invalid")
139     mode = black.Mode(target_versions={black.TargetVersion.PY310})
140     with pytest.raises(black.parsing.InvalidInput) as exc_info:
141         assert_format(source, expected, mode, minimum_version=(3, 10))
142
143     exc_info.match("Cannot parse: 10:11")
144
145
146 @pytest.mark.parametrize("filename", all_data_cases("py_311"))
147 def test_python_311(filename: str) -> None:
148     source, expected = read_data("py_311", filename)
149     mode = black.Mode(target_versions={black.TargetVersion.PY311})
150     assert_format(source, expected, mode, minimum_version=(3, 11))
151
152
153 @pytest.mark.parametrize("filename", all_data_cases("fast"))
154 def test_fast_cases(filename: str) -> None:
155     source, expected = read_data("fast", filename)
156     assert_format(source, expected, fast=True)
157
158
159 def test_python_2_hint() -> None:
160     with pytest.raises(black.parsing.InvalidInput) as exc_info:
161         assert_format("print 'daylily'", "print 'daylily'")
162     exc_info.match(black.parsing.PY2_HINT)
163
164
165 @pytest.mark.filterwarnings("ignore:invalid escape sequence.*:DeprecationWarning")
166 def test_docstring_no_string_normalization() -> None:
167     """Like test_docstring but with string normalization off."""
168     source, expected = read_data("miscellaneous", "docstring_no_string_normalization")
169     mode = replace(DEFAULT_MODE, string_normalization=False)
170     assert_format(source, expected, mode)
171
172
173 def test_docstring_line_length_6() -> None:
174     """Like test_docstring but with line length set to 6."""
175     source, expected = read_data("miscellaneous", "linelength6")
176     mode = black.Mode(line_length=6)
177     assert_format(source, expected, mode)
178
179
180 def test_preview_docstring_no_string_normalization() -> None:
181     """
182     Like test_docstring but with string normalization off *and* the preview style
183     enabled.
184     """
185     source, expected = read_data(
186         "miscellaneous", "docstring_preview_no_string_normalization"
187     )
188     mode = replace(DEFAULT_MODE, string_normalization=False, preview=True)
189     assert_format(source, expected, mode)
190
191
192 def test_long_strings_flag_disabled() -> None:
193     """Tests for turning off the string processing logic."""
194     source, expected = read_data("miscellaneous", "long_strings_flag_disabled")
195     mode = replace(DEFAULT_MODE, experimental_string_processing=False)
196     assert_format(source, expected, mode)
197
198
199 def test_stub() -> None:
200     mode = replace(DEFAULT_MODE, is_pyi=True)
201     source, expected = read_data("miscellaneous", "stub.pyi")
202     assert_format(source, expected, mode)
203
204
205 def test_power_op_newline() -> None:
206     # requires line_length=0
207     source, expected = read_data("miscellaneous", "power_op_newline")
208     assert_format(source, expected, mode=black.Mode(line_length=0))