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.
1 from dataclasses import replace
2 from typing import Any, Iterator, List
3 from unittest.mock import patch
8 from tests.util import (
17 SOURCES: List[str] = [
18 "src/black/__init__.py",
19 "src/black/__main__.py",
20 "src/black/brackets.py",
22 "src/black/comments.py",
23 "src/black/concurrency.py",
27 "src/black/linegen.py",
31 "src/black/numerics.py",
32 "src/black/output.py",
33 "src/black/parsing.py",
34 "src/black/report.py",
36 "src/black/strings.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",
50 "tests/test_black.py",
51 "tests/test_blackd.py",
52 "tests/test_format.py",
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):
66 subdir: str, filename: str, mode: black.Mode, *, data: bool = True
68 source, expected = read_data(subdir, filename, data=data)
69 assert_format(source, expected, mode, fast=False)
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)
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))
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))
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))
96 @pytest.mark.parametrize("filename", SOURCES)
97 def test_source_is_formatted(filename: str) -> None:
98 check_file("", filename, DEFAULT_MODE, data=False)
106 def test_empty() -> None:
107 source = expected = ""
108 assert_format(source, expected)
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))
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))
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))
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))
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))
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)
150 assert_format(source, expected, mode, minimum_version=(3, 10))
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))
159 exc_info.match("Cannot parse: 10:11")
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))
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)
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)
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)
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)
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)
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))