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 magic_trailing_comma = filename != "skip_magic_trailing_comma"
37 magic_trailing_comma=magic_trailing_comma, is_pyi=filename.endswith("_pyi")
39 check_file("simple_cases", filename, mode)
42 @pytest.mark.parametrize("filename", all_data_cases("preview"))
43 def test_preview_format(filename: str) -> None:
44 check_file("preview", filename, black.Mode(preview=True))
47 def test_preview_context_managers_targeting_py38() -> None:
48 source, expected = read_data("preview_context_managers", "targeting_py38.py")
49 mode = black.Mode(preview=True, target_versions={black.TargetVersion.PY38})
50 assert_format(source, expected, mode, minimum_version=(3, 8))
53 def test_preview_context_managers_targeting_py39() -> None:
54 source, expected = read_data("preview_context_managers", "targeting_py39.py")
55 mode = black.Mode(preview=True, target_versions={black.TargetVersion.PY39})
56 assert_format(source, expected, mode, minimum_version=(3, 9))
59 @pytest.mark.parametrize(
60 "filename", all_data_cases("preview_context_managers/auto_detect")
62 def test_preview_context_managers_auto_detect(filename: str) -> None:
63 match = re.match(r"features_3_(\d+)", filename)
64 assert match is not None, "Unexpected filename format: %s" % filename
65 source, expected = read_data("preview_context_managers/auto_detect", filename)
66 mode = black.Mode(preview=True)
67 assert_format(source, expected, mode, minimum_version=(3, int(match.group(1))))
75 def test_empty() -> None:
76 source = expected = ""
77 assert_format(source, expected)
80 @pytest.mark.parametrize("filename", all_data_cases("py_36"))
81 def test_python_36(filename: str) -> None:
82 source, expected = read_data("py_36", filename)
83 mode = black.Mode(target_versions=PY36_VERSIONS)
84 assert_format(source, expected, mode, minimum_version=(3, 6))
87 @pytest.mark.parametrize("filename", all_data_cases("py_37"))
88 def test_python_37(filename: str) -> None:
89 source, expected = read_data("py_37", filename)
90 mode = black.Mode(target_versions={black.TargetVersion.PY37})
91 assert_format(source, expected, mode, minimum_version=(3, 7))
94 @pytest.mark.parametrize("filename", all_data_cases("py_38"))
95 def test_python_38(filename: str) -> None:
96 source, expected = read_data("py_38", filename)
97 mode = black.Mode(target_versions={black.TargetVersion.PY38})
98 assert_format(source, expected, mode, minimum_version=(3, 8))
101 @pytest.mark.parametrize("filename", all_data_cases("py_39"))
102 def test_python_39(filename: str) -> None:
103 source, expected = read_data("py_39", filename)
104 mode = black.Mode(target_versions={black.TargetVersion.PY39})
105 assert_format(source, expected, mode, minimum_version=(3, 9))
108 @pytest.mark.parametrize("filename", all_data_cases("py_310"))
109 def test_python_310(filename: str) -> None:
110 source, expected = read_data("py_310", filename)
111 mode = black.Mode(target_versions={black.TargetVersion.PY310})
112 assert_format(source, expected, mode, minimum_version=(3, 10))
115 @pytest.mark.parametrize("filename", all_data_cases("py_310"))
116 def test_python_310_without_target_version(filename: str) -> None:
117 source, expected = read_data("py_310", filename)
119 assert_format(source, expected, mode, minimum_version=(3, 10))
122 def test_patma_invalid() -> None:
123 source, expected = read_data("miscellaneous", "pattern_matching_invalid")
124 mode = black.Mode(target_versions={black.TargetVersion.PY310})
125 with pytest.raises(black.parsing.InvalidInput) as exc_info:
126 assert_format(source, expected, mode, minimum_version=(3, 10))
128 exc_info.match("Cannot parse: 10:11")
131 @pytest.mark.parametrize("filename", all_data_cases("py_311"))
132 def test_python_311(filename: str) -> None:
133 source, expected = read_data("py_311", filename)
134 mode = black.Mode(target_versions={black.TargetVersion.PY311})
135 assert_format(source, expected, mode, minimum_version=(3, 11))
138 @pytest.mark.parametrize("filename", all_data_cases("py_312"))
139 def test_python_312(filename: str) -> None:
140 source, expected = read_data("py_312", filename)
141 mode = black.Mode(target_versions={black.TargetVersion.PY312})
142 assert_format(source, expected, mode, minimum_version=(3, 12))
145 @pytest.mark.parametrize("filename", all_data_cases("fast"))
146 def test_fast_cases(filename: str) -> None:
147 source, expected = read_data("fast", filename)
148 assert_format(source, expected, fast=True)
151 def test_python_2_hint() -> None:
152 with pytest.raises(black.parsing.InvalidInput) as exc_info:
153 assert_format("print 'daylily'", "print 'daylily'")
154 exc_info.match(black.parsing.PY2_HINT)
157 @pytest.mark.filterwarnings("ignore:invalid escape sequence.*:DeprecationWarning")
158 def test_docstring_no_string_normalization() -> None:
159 """Like test_docstring but with string normalization off."""
160 source, expected = read_data("miscellaneous", "docstring_no_string_normalization")
161 mode = replace(DEFAULT_MODE, string_normalization=False)
162 assert_format(source, expected, mode)
165 def test_docstring_line_length_6() -> None:
166 """Like test_docstring but with line length set to 6."""
167 source, expected = read_data("miscellaneous", "linelength6")
168 mode = black.Mode(line_length=6)
169 assert_format(source, expected, mode)
172 def test_preview_docstring_no_string_normalization() -> None:
174 Like test_docstring but with string normalization off *and* the preview style
177 source, expected = read_data(
178 "miscellaneous", "docstring_preview_no_string_normalization"
180 mode = replace(DEFAULT_MODE, string_normalization=False, preview=True)
181 assert_format(source, expected, mode)
184 def test_long_strings_flag_disabled() -> None:
185 """Tests for turning off the string processing logic."""
186 source, expected = read_data("miscellaneous", "long_strings_flag_disabled")
187 mode = replace(DEFAULT_MODE, experimental_string_processing=False)
188 assert_format(source, expected, mode)
191 def test_stub() -> None:
192 mode = replace(DEFAULT_MODE, is_pyi=True)
193 source, expected = read_data("miscellaneous", "stub.pyi")
194 assert_format(source, expected, mode)
197 def test_nested_class_stub() -> None:
198 mode = replace(DEFAULT_MODE, is_pyi=True, preview=True)
199 source, expected = read_data("miscellaneous", "nested_class_stub.pyi")
200 assert_format(source, expected, mode)
203 def test_power_op_newline() -> None:
204 # requires line_length=0
205 source, expected = read_data("miscellaneous", "power_op_newline")
206 assert_format(source, expected, mode=black.Mode(line_length=0))
209 def test_type_comment_syntax_error() -> None:
210 """Test that black is able to format python code with type comment syntax errors."""
211 source, expected = read_data("type_comments", "type_comment_syntax_error")
212 assert_format(source, expected)
213 black.assert_equivalent(source, expected)