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

Add script to ease migration to black (#3038)
[etc/vim.git] / tests / test_format.py
1 from dataclasses import replace
2 from typing import Any, Iterator, List
3 from unittest.mock import patch
4
5 import pytest
6
7 import black
8 from tests.util import (
9     DEFAULT_MODE,
10     PY36_VERSIONS,
11     assert_format,
12     dump_to_stderr,
13     read_data,
14     all_data_cases,
15 )
16
17 SOURCES: List[str] = [
18     "src/black/__init__.py",
19     "src/black/__main__.py",
20     "src/black/brackets.py",
21     "src/black/cache.py",
22     "src/black/comments.py",
23     "src/black/concurrency.py",
24     "src/black/const.py",
25     "src/black/debug.py",
26     "src/black/files.py",
27     "src/black/linegen.py",
28     "src/black/lines.py",
29     "src/black/mode.py",
30     "src/black/nodes.py",
31     "src/black/numerics.py",
32     "src/black/output.py",
33     "src/black/parsing.py",
34     "src/black/report.py",
35     "src/black/rusty.py",
36     "src/black/strings.py",
37     "src/black/trans.py",
38     "src/blackd/__init__.py",
39     "src/blib2to3/pygram.py",
40     "src/blib2to3/pytree.py",
41     "src/blib2to3/pgen2/conv.py",
42     "src/blib2to3/pgen2/driver.py",
43     "src/blib2to3/pgen2/grammar.py",
44     "src/blib2to3/pgen2/literals.py",
45     "src/blib2to3/pgen2/parse.py",
46     "src/blib2to3/pgen2/pgen.py",
47     "src/blib2to3/pgen2/tokenize.py",
48     "src/blib2to3/pgen2/token.py",
49     "setup.py",
50     "tests/test_black.py",
51     "tests/test_blackd.py",
52     "tests/test_format.py",
53     "tests/optional.py",
54     "tests/util.py",
55     "tests/conftest.py",
56 ]
57
58
59 @pytest.fixture(autouse=True)
60 def patch_dump_to_file(request: Any) -> Iterator[None]:
61     with patch("black.dump_to_file", dump_to_stderr):
62         yield
63
64
65 def check_file(
66     subdir: str, filename: str, mode: black.Mode, *, data: bool = True
67 ) -> None:
68     source, expected = read_data(subdir, filename, data=data)
69     assert_format(source, expected, mode, fast=False)
70
71
72 @pytest.mark.parametrize("filename", all_data_cases("simple_cases"))
73 def test_simple_format(filename: str) -> None:
74     check_file("simple_cases", filename, DEFAULT_MODE)
75
76
77 @pytest.mark.parametrize("filename", all_data_cases("preview"))
78 def test_preview_format(filename: str) -> None:
79     check_file("preview", filename, black.Mode(preview=True))
80
81
82 @pytest.mark.parametrize("filename", all_data_cases("preview_39"))
83 def test_preview_minimum_python_39_format(filename: str) -> None:
84     source, expected = read_data("preview_39", filename)
85     mode = black.Mode(preview=True)
86     assert_format(source, expected, mode, minimum_version=(3, 9))
87
88
89 @pytest.mark.parametrize("filename", SOURCES)
90 def test_source_is_formatted(filename: str) -> None:
91     check_file("", filename, DEFAULT_MODE, data=False)
92
93
94 # =============== #
95 # Complex cases
96 # ============= #
97
98
99 def test_empty() -> None:
100     source = expected = ""
101     assert_format(source, expected)
102
103
104 @pytest.mark.parametrize("filename", all_data_cases("py_36"))
105 def test_python_36(filename: str) -> None:
106     source, expected = read_data("py_36", filename)
107     mode = black.Mode(target_versions=PY36_VERSIONS)
108     assert_format(source, expected, mode, minimum_version=(3, 6))
109
110
111 @pytest.mark.parametrize("filename", all_data_cases("py_37"))
112 def test_python_37(filename: str) -> None:
113     source, expected = read_data("py_37", filename)
114     mode = black.Mode(target_versions={black.TargetVersion.PY37})
115     assert_format(source, expected, mode, minimum_version=(3, 7))
116
117
118 @pytest.mark.parametrize("filename", all_data_cases("py_38"))
119 def test_python_38(filename: str) -> None:
120     source, expected = read_data("py_38", filename)
121     mode = black.Mode(target_versions={black.TargetVersion.PY38})
122     assert_format(source, expected, mode, minimum_version=(3, 8))
123
124
125 @pytest.mark.parametrize("filename", all_data_cases("py_39"))
126 def test_python_39(filename: str) -> None:
127     source, expected = read_data("py_39", filename)
128     mode = black.Mode(target_versions={black.TargetVersion.PY39})
129     assert_format(source, expected, mode, minimum_version=(3, 9))
130
131
132 @pytest.mark.parametrize("filename", all_data_cases("py_310"))
133 def test_python_310(filename: str) -> None:
134     source, expected = read_data("py_310", filename)
135     mode = black.Mode(target_versions={black.TargetVersion.PY310})
136     assert_format(source, expected, mode, minimum_version=(3, 10))
137
138
139 @pytest.mark.parametrize("filename", all_data_cases("py_310"))
140 def test_python_310_without_target_version(filename: str) -> None:
141     source, expected = read_data("py_310", filename)
142     mode = black.Mode()
143     assert_format(source, expected, mode, minimum_version=(3, 10))
144
145
146 def test_patma_invalid() -> None:
147     source, expected = read_data("miscellaneous", "pattern_matching_invalid")
148     mode = black.Mode(target_versions={black.TargetVersion.PY310})
149     with pytest.raises(black.parsing.InvalidInput) as exc_info:
150         assert_format(source, expected, mode, minimum_version=(3, 10))
151
152     exc_info.match("Cannot parse: 10:11")
153
154
155 @pytest.mark.parametrize("filename", all_data_cases("py_311"))
156 def test_python_311(filename: str) -> None:
157     source, expected = read_data("py_311", filename)
158     mode = black.Mode(target_versions={black.TargetVersion.PY311})
159     assert_format(source, expected, mode, minimum_version=(3, 11))
160
161
162 @pytest.mark.parametrize("filename", all_data_cases("fast"))
163 def test_fast_cases(filename: str) -> None:
164     source, expected = read_data("fast", filename)
165     assert_format(source, expected, fast=True)
166
167
168 def test_python_2_hint() -> None:
169     with pytest.raises(black.parsing.InvalidInput) as exc_info:
170         assert_format("print 'daylily'", "print 'daylily'")
171     exc_info.match(black.parsing.PY2_HINT)
172
173
174 def test_docstring_no_string_normalization() -> None:
175     """Like test_docstring but with string normalization off."""
176     source, expected = read_data("miscellaneous", "docstring_no_string_normalization")
177     mode = replace(DEFAULT_MODE, string_normalization=False)
178     assert_format(source, expected, mode)
179
180
181 def test_long_strings_flag_disabled() -> None:
182     """Tests for turning off the string processing logic."""
183     source, expected = read_data("miscellaneous", "long_strings_flag_disabled")
184     mode = replace(DEFAULT_MODE, experimental_string_processing=False)
185     assert_format(source, expected, mode)
186
187
188 def test_stub() -> None:
189     mode = replace(DEFAULT_MODE, is_pyi=True)
190     source, expected = read_data("miscellaneous", "stub.pyi")
191     assert_format(source, expected, mode)
192
193
194 def test_power_op_newline() -> None:
195     # requires line_length=0
196     source, expected = read_data("miscellaneous", "power_op_newline")
197     assert_format(source, expected, mode=black.Mode(line_length=0))