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", SOURCES)
90 def test_source_is_formatted(filename: str) -> None:
91 check_file("", filename, DEFAULT_MODE, data=False)
99 def test_empty() -> None:
100 source = expected = ""
101 assert_format(source, expected)
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))
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))
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))
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))
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))
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)
143 assert_format(source, expected, mode, minimum_version=(3, 10))
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))
152 exc_info.match("Cannot parse: 10:11")
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))
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)
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)
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)
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)
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)
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))