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

Use is_number_token instead of assertion (#3069)
[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", all_data_cases("preview_310"))
90 def test_preview_minimum_python_310_format(filename: str) -> None:
91     source, expected = read_data("preview_310", filename)
92     mode = black.Mode(preview=True)
93     assert_format(source, expected, mode, minimum_version=(3, 10))
94
95
96 @pytest.mark.parametrize("filename", SOURCES)
97 def test_source_is_formatted(filename: str) -> None:
98     check_file("", filename, DEFAULT_MODE, data=False)
99
100
101 # =============== #
102 # Complex cases
103 # ============= #
104
105
106 def test_empty() -> None:
107     source = expected = ""
108     assert_format(source, expected)
109
110
111 @pytest.mark.parametrize("filename", all_data_cases("py_36"))
112 def test_python_36(filename: str) -> None:
113     source, expected = read_data("py_36", filename)
114     mode = black.Mode(target_versions=PY36_VERSIONS)
115     assert_format(source, expected, mode, minimum_version=(3, 6))
116
117
118 @pytest.mark.parametrize("filename", all_data_cases("py_37"))
119 def test_python_37(filename: str) -> None:
120     source, expected = read_data("py_37", filename)
121     mode = black.Mode(target_versions={black.TargetVersion.PY37})
122     assert_format(source, expected, mode, minimum_version=(3, 7))
123
124
125 @pytest.mark.parametrize("filename", all_data_cases("py_38"))
126 def test_python_38(filename: str) -> None:
127     source, expected = read_data("py_38", filename)
128     mode = black.Mode(target_versions={black.TargetVersion.PY38})
129     assert_format(source, expected, mode, minimum_version=(3, 8))
130
131
132 @pytest.mark.parametrize("filename", all_data_cases("py_39"))
133 def test_python_39(filename: str) -> None:
134     source, expected = read_data("py_39", filename)
135     mode = black.Mode(target_versions={black.TargetVersion.PY39})
136     assert_format(source, expected, mode, minimum_version=(3, 9))
137
138
139 @pytest.mark.parametrize("filename", all_data_cases("py_310"))
140 def test_python_310(filename: str) -> None:
141     source, expected = read_data("py_310", filename)
142     mode = black.Mode(target_versions={black.TargetVersion.PY310})
143     assert_format(source, expected, mode, minimum_version=(3, 10))
144
145
146 @pytest.mark.parametrize("filename", all_data_cases("py_310"))
147 def test_python_310_without_target_version(filename: str) -> None:
148     source, expected = read_data("py_310", filename)
149     mode = black.Mode()
150     assert_format(source, expected, mode, minimum_version=(3, 10))
151
152
153 def test_patma_invalid() -> None:
154     source, expected = read_data("miscellaneous", "pattern_matching_invalid")
155     mode = black.Mode(target_versions={black.TargetVersion.PY310})
156     with pytest.raises(black.parsing.InvalidInput) as exc_info:
157         assert_format(source, expected, mode, minimum_version=(3, 10))
158
159     exc_info.match("Cannot parse: 10:11")
160
161
162 @pytest.mark.parametrize("filename", all_data_cases("py_311"))
163 def test_python_311(filename: str) -> None:
164     source, expected = read_data("py_311", filename)
165     mode = black.Mode(target_versions={black.TargetVersion.PY311})
166     assert_format(source, expected, mode, minimum_version=(3, 11))
167
168
169 @pytest.mark.parametrize("filename", all_data_cases("fast"))
170 def test_fast_cases(filename: str) -> None:
171     source, expected = read_data("fast", filename)
172     assert_format(source, expected, fast=True)
173
174
175 def test_python_2_hint() -> None:
176     with pytest.raises(black.parsing.InvalidInput) as exc_info:
177         assert_format("print 'daylily'", "print 'daylily'")
178     exc_info.match(black.parsing.PY2_HINT)
179
180
181 def test_docstring_no_string_normalization() -> None:
182     """Like test_docstring but with string normalization off."""
183     source, expected = read_data("miscellaneous", "docstring_no_string_normalization")
184     mode = replace(DEFAULT_MODE, string_normalization=False)
185     assert_format(source, expected, mode)
186
187
188 def test_long_strings_flag_disabled() -> None:
189     """Tests for turning off the string processing logic."""
190     source, expected = read_data("miscellaneous", "long_strings_flag_disabled")
191     mode = replace(DEFAULT_MODE, experimental_string_processing=False)
192     assert_format(source, expected, mode)
193
194
195 def test_stub() -> None:
196     mode = replace(DEFAULT_MODE, is_pyi=True)
197     source, expected = read_data("miscellaneous", "stub.pyi")
198     assert_format(source, expected, mode)
199
200
201 def test_power_op_newline() -> None:
202     # requires line_length=0
203     source, expected = read_data("miscellaneous", "power_op_newline")
204     assert_format(source, expected, mode=black.Mode(line_length=0))