]> 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:

Bump docker/login-action from 2 to 3 (#3891)
[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     magic_trailing_comma = filename != "skip_magic_trailing_comma"
36     mode = black.Mode(
37         magic_trailing_comma=magic_trailing_comma, is_pyi=filename.endswith("_pyi")
38     )
39     check_file("simple_cases", filename, mode)
40
41
42 @pytest.mark.parametrize("filename", all_data_cases("preview"))
43 def test_preview_format(filename: str) -> None:
44     check_file("preview", filename, black.Mode(preview=True))
45
46
47 def test_preview_context_managers_targeting_py38() -> None:
48     source, expected = read_data("preview_context_managers", "targeting_py38.py")
49     mode = black.Mode(preview=True, target_versions={black.TargetVersion.PY38})
50     assert_format(source, expected, mode, minimum_version=(3, 8))
51
52
53 def test_preview_context_managers_targeting_py39() -> None:
54     source, expected = read_data("preview_context_managers", "targeting_py39.py")
55     mode = black.Mode(preview=True, target_versions={black.TargetVersion.PY39})
56     assert_format(source, expected, mode, minimum_version=(3, 9))
57
58
59 @pytest.mark.parametrize("filename", all_data_cases("preview_py_310"))
60 def test_preview_python_310(filename: str) -> None:
61     source, expected = read_data("preview_py_310", filename)
62     mode = black.Mode(target_versions={black.TargetVersion.PY310}, preview=True)
63     assert_format(source, expected, mode, minimum_version=(3, 10))
64
65
66 @pytest.mark.parametrize(
67     "filename", all_data_cases("preview_context_managers/auto_detect")
68 )
69 def test_preview_context_managers_auto_detect(filename: str) -> None:
70     match = re.match(r"features_3_(\d+)", filename)
71     assert match is not None, "Unexpected filename format: %s" % filename
72     source, expected = read_data("preview_context_managers/auto_detect", filename)
73     mode = black.Mode(preview=True)
74     assert_format(source, expected, mode, minimum_version=(3, int(match.group(1))))
75
76
77 # =============== #
78 # Complex cases
79 # ============= #
80
81
82 def test_empty() -> None:
83     source = expected = ""
84     assert_format(source, expected)
85
86
87 @pytest.mark.parametrize("filename", all_data_cases("py_36"))
88 def test_python_36(filename: str) -> None:
89     source, expected = read_data("py_36", filename)
90     mode = black.Mode(target_versions=PY36_VERSIONS)
91     assert_format(source, expected, mode, minimum_version=(3, 6))
92
93
94 @pytest.mark.parametrize("filename", all_data_cases("py_37"))
95 def test_python_37(filename: str) -> None:
96     source, expected = read_data("py_37", filename)
97     mode = black.Mode(target_versions={black.TargetVersion.PY37})
98     assert_format(source, expected, mode, minimum_version=(3, 7))
99
100
101 @pytest.mark.parametrize("filename", all_data_cases("py_38"))
102 def test_python_38(filename: str) -> None:
103     source, expected = read_data("py_38", filename)
104     mode = black.Mode(target_versions={black.TargetVersion.PY38})
105     assert_format(source, expected, mode, minimum_version=(3, 8))
106
107
108 @pytest.mark.parametrize("filename", all_data_cases("py_39"))
109 def test_python_39(filename: str) -> None:
110     source, expected = read_data("py_39", filename)
111     mode = black.Mode(target_versions={black.TargetVersion.PY39})
112     assert_format(source, expected, mode, minimum_version=(3, 9))
113
114
115 @pytest.mark.parametrize("filename", all_data_cases("py_310"))
116 def test_python_310(filename: str) -> None:
117     source, expected = read_data("py_310", filename)
118     mode = black.Mode(target_versions={black.TargetVersion.PY310})
119     assert_format(source, expected, mode, minimum_version=(3, 10))
120
121
122 @pytest.mark.parametrize("filename", all_data_cases("py_310"))
123 def test_python_310_without_target_version(filename: str) -> None:
124     source, expected = read_data("py_310", filename)
125     mode = black.Mode()
126     assert_format(source, expected, mode, minimum_version=(3, 10))
127
128
129 def test_patma_invalid() -> None:
130     source, expected = read_data("miscellaneous", "pattern_matching_invalid")
131     mode = black.Mode(target_versions={black.TargetVersion.PY310})
132     with pytest.raises(black.parsing.InvalidInput) as exc_info:
133         assert_format(source, expected, mode, minimum_version=(3, 10))
134
135     exc_info.match("Cannot parse: 10:11")
136
137
138 @pytest.mark.parametrize("filename", all_data_cases("py_311"))
139 def test_python_311(filename: str) -> None:
140     source, expected = read_data("py_311", filename)
141     mode = black.Mode(target_versions={black.TargetVersion.PY311})
142     assert_format(source, expected, mode, minimum_version=(3, 11))
143
144
145 @pytest.mark.parametrize("filename", all_data_cases("py_312"))
146 def test_python_312(filename: str) -> None:
147     source, expected = read_data("py_312", filename)
148     mode = black.Mode(target_versions={black.TargetVersion.PY312})
149     assert_format(source, expected, mode, minimum_version=(3, 12))
150
151
152 @pytest.mark.parametrize("filename", all_data_cases("fast"))
153 def test_fast_cases(filename: str) -> None:
154     source, expected = read_data("fast", filename)
155     assert_format(source, expected, fast=True)
156
157
158 def test_python_2_hint() -> None:
159     with pytest.raises(black.parsing.InvalidInput) as exc_info:
160         assert_format("print 'daylily'", "print 'daylily'")
161     exc_info.match(black.parsing.PY2_HINT)
162
163
164 @pytest.mark.filterwarnings("ignore:invalid escape sequence.*:DeprecationWarning")
165 def test_docstring_no_string_normalization() -> None:
166     """Like test_docstring but with string normalization off."""
167     source, expected = read_data("miscellaneous", "docstring_no_string_normalization")
168     mode = replace(DEFAULT_MODE, string_normalization=False)
169     assert_format(source, expected, mode)
170
171
172 def test_docstring_line_length_6() -> None:
173     """Like test_docstring but with line length set to 6."""
174     source, expected = read_data("miscellaneous", "linelength6")
175     mode = black.Mode(line_length=6)
176     assert_format(source, expected, mode)
177
178
179 def test_preview_docstring_no_string_normalization() -> None:
180     """
181     Like test_docstring but with string normalization off *and* the preview style
182     enabled.
183     """
184     source, expected = read_data(
185         "miscellaneous", "docstring_preview_no_string_normalization"
186     )
187     mode = replace(DEFAULT_MODE, string_normalization=False, preview=True)
188     assert_format(source, expected, mode)
189
190
191 def test_long_strings_flag_disabled() -> None:
192     """Tests for turning off the string processing logic."""
193     source, expected = read_data("miscellaneous", "long_strings_flag_disabled")
194     mode = replace(DEFAULT_MODE, experimental_string_processing=False)
195     assert_format(source, expected, mode)
196
197
198 def test_stub() -> None:
199     mode = replace(DEFAULT_MODE, is_pyi=True)
200     source, expected = read_data("miscellaneous", "stub.pyi")
201     assert_format(source, expected, mode)
202
203
204 def test_nested_stub() -> None:
205     mode = replace(DEFAULT_MODE, is_pyi=True, preview=True)
206     source, expected = read_data("miscellaneous", "nested_stub.pyi")
207     assert_format(source, expected, mode)
208
209
210 def test_power_op_newline() -> None:
211     # requires line_length=0
212     source, expected = read_data("miscellaneous", "power_op_newline")
213     assert_format(source, expected, mode=black.Mode(line_length=0))
214
215
216 def test_type_comment_syntax_error() -> None:
217     """Test that black is able to format python code with type comment syntax errors."""
218     source, expected = read_data("type_comments", "type_comment_syntax_error")
219     assert_format(source, expected)
220     black.assert_equivalent(source, expected)