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.
2 from dataclasses import replace
3 from typing import Any, Iterator
4 from unittest.mock import patch
9 from tests.util import (
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):
26 subdir: str, filename: str, mode: black.Mode, *, data: bool = True
28 source, expected = read_data(subdir, filename, data=data)
29 assert_format(source, expected, mode, fast=False)
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 check_file("simple_cases", filename, DEFAULT_MODE)
38 @pytest.mark.parametrize("filename", all_data_cases("preview"))
39 def test_preview_format(filename: str) -> None:
40 magic_trailing_comma = filename != "skip_magic_trailing_comma"
44 black.Mode(preview=True, magic_trailing_comma=magic_trailing_comma),
48 @pytest.mark.parametrize("filename", all_data_cases("preview_39"))
49 def test_preview_minimum_python_39_format(filename: str) -> None:
50 source, expected = read_data("preview_39", filename)
51 mode = black.Mode(preview=True)
52 assert_format(source, expected, mode, minimum_version=(3, 9))
55 @pytest.mark.parametrize("filename", all_data_cases("preview_310"))
56 def test_preview_minimum_python_310_format(filename: str) -> None:
57 source, expected = read_data("preview_310", filename)
58 mode = black.Mode(preview=True)
59 assert_format(source, expected, mode, minimum_version=(3, 10))
62 def test_preview_context_managers_targeting_py38() -> None:
63 source, expected = read_data("preview_context_managers", "targeting_py38.py")
64 mode = black.Mode(preview=True, target_versions={black.TargetVersion.PY38})
65 assert_format(source, expected, mode, minimum_version=(3, 8))
68 def test_preview_context_managers_targeting_py39() -> None:
69 source, expected = read_data("preview_context_managers", "targeting_py39.py")
70 mode = black.Mode(preview=True, target_versions={black.TargetVersion.PY39})
71 assert_format(source, expected, mode, minimum_version=(3, 9))
74 @pytest.mark.parametrize(
75 "filename", all_data_cases("preview_context_managers/auto_detect")
77 def test_preview_context_managers_auto_detect(filename: str) -> None:
78 match = re.match(r"features_3_(\d+)", filename)
79 assert match is not None, "Unexpected filename format: %s" % filename
80 source, expected = read_data("preview_context_managers/auto_detect", filename)
81 mode = black.Mode(preview=True)
82 assert_format(source, expected, mode, minimum_version=(3, int(match.group(1))))
90 def test_empty() -> None:
91 source = expected = ""
92 assert_format(source, expected)
95 @pytest.mark.parametrize("filename", all_data_cases("py_36"))
96 def test_python_36(filename: str) -> None:
97 source, expected = read_data("py_36", filename)
98 mode = black.Mode(target_versions=PY36_VERSIONS)
99 assert_format(source, expected, mode, minimum_version=(3, 6))
102 @pytest.mark.parametrize("filename", all_data_cases("py_37"))
103 def test_python_37(filename: str) -> None:
104 source, expected = read_data("py_37", filename)
105 mode = black.Mode(target_versions={black.TargetVersion.PY37})
106 assert_format(source, expected, mode, minimum_version=(3, 7))
109 @pytest.mark.parametrize("filename", all_data_cases("py_38"))
110 def test_python_38(filename: str) -> None:
111 source, expected = read_data("py_38", filename)
112 mode = black.Mode(target_versions={black.TargetVersion.PY38})
113 assert_format(source, expected, mode, minimum_version=(3, 8))
116 @pytest.mark.parametrize("filename", all_data_cases("py_39"))
117 def test_python_39(filename: str) -> None:
118 source, expected = read_data("py_39", filename)
119 mode = black.Mode(target_versions={black.TargetVersion.PY39})
120 assert_format(source, expected, mode, minimum_version=(3, 9))
123 @pytest.mark.parametrize("filename", all_data_cases("py_310"))
124 def test_python_310(filename: str) -> None:
125 source, expected = read_data("py_310", filename)
126 mode = black.Mode(target_versions={black.TargetVersion.PY310})
127 assert_format(source, expected, mode, minimum_version=(3, 10))
130 @pytest.mark.parametrize("filename", all_data_cases("py_310"))
131 def test_python_310_without_target_version(filename: str) -> None:
132 source, expected = read_data("py_310", filename)
134 assert_format(source, expected, mode, minimum_version=(3, 10))
137 def test_patma_invalid() -> None:
138 source, expected = read_data("miscellaneous", "pattern_matching_invalid")
139 mode = black.Mode(target_versions={black.TargetVersion.PY310})
140 with pytest.raises(black.parsing.InvalidInput) as exc_info:
141 assert_format(source, expected, mode, minimum_version=(3, 10))
143 exc_info.match("Cannot parse: 10:11")
146 @pytest.mark.parametrize("filename", all_data_cases("py_311"))
147 def test_python_311(filename: str) -> None:
148 source, expected = read_data("py_311", filename)
149 mode = black.Mode(target_versions={black.TargetVersion.PY311})
150 assert_format(source, expected, mode, minimum_version=(3, 11))
153 @pytest.mark.parametrize("filename", all_data_cases("fast"))
154 def test_fast_cases(filename: str) -> None:
155 source, expected = read_data("fast", filename)
156 assert_format(source, expected, fast=True)
159 def test_python_2_hint() -> None:
160 with pytest.raises(black.parsing.InvalidInput) as exc_info:
161 assert_format("print 'daylily'", "print 'daylily'")
162 exc_info.match(black.parsing.PY2_HINT)
165 @pytest.mark.filterwarnings("ignore:invalid escape sequence.*:DeprecationWarning")
166 def test_docstring_no_string_normalization() -> None:
167 """Like test_docstring but with string normalization off."""
168 source, expected = read_data("miscellaneous", "docstring_no_string_normalization")
169 mode = replace(DEFAULT_MODE, string_normalization=False)
170 assert_format(source, expected, mode)
173 def test_docstring_line_length_6() -> None:
174 """Like test_docstring but with line length set to 6."""
175 source, expected = read_data("miscellaneous", "linelength6")
176 mode = black.Mode(line_length=6)
177 assert_format(source, expected, mode)
180 def test_preview_docstring_no_string_normalization() -> None:
182 Like test_docstring but with string normalization off *and* the preview style
185 source, expected = read_data(
186 "miscellaneous", "docstring_preview_no_string_normalization"
188 mode = replace(DEFAULT_MODE, string_normalization=False, preview=True)
189 assert_format(source, expected, mode)
192 def test_long_strings_flag_disabled() -> None:
193 """Tests for turning off the string processing logic."""
194 source, expected = read_data("miscellaneous", "long_strings_flag_disabled")
195 mode = replace(DEFAULT_MODE, experimental_string_processing=False)
196 assert_format(source, expected, mode)
199 def test_stub() -> None:
200 mode = replace(DEFAULT_MODE, is_pyi=True)
201 source, expected = read_data("miscellaneous", "stub.pyi")
202 assert_format(source, expected, mode)
205 def test_power_op_newline() -> None:
206 # requires line_length=0
207 source, expected = read_data("miscellaneous", "power_op_newline")
208 assert_format(source, expected, mode=black.Mode(line_length=0))