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

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