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
3 from unittest.mock import patch
8 from tests.util import (
18 @pytest.fixture(autouse=True)
19 def patch_dump_to_file(request: Any) -> Iterator[None]:
20 with patch("black.dump_to_file", dump_to_stderr):
25 subdir: str, filename: str, mode: black.Mode, *, data: bool = True
27 source, expected = read_data(subdir, filename, data=data)
28 assert_format(source, expected, mode, fast=False)
31 @pytest.mark.filterwarnings("ignore:invalid escape sequence.*:DeprecationWarning")
32 @pytest.mark.parametrize("filename", all_data_cases("simple_cases"))
33 def test_simple_format(filename: str) -> None:
34 check_file("simple_cases", filename, DEFAULT_MODE)
37 @pytest.mark.parametrize("filename", all_data_cases("preview"))
38 def test_preview_format(filename: str) -> None:
39 magic_trailing_comma = filename != "skip_magic_trailing_comma"
43 black.Mode(preview=True, magic_trailing_comma=magic_trailing_comma),
47 @pytest.mark.parametrize("filename", all_data_cases("preview_39"))
48 def test_preview_minimum_python_39_format(filename: str) -> None:
49 source, expected = read_data("preview_39", filename)
50 mode = black.Mode(preview=True)
51 assert_format(source, expected, mode, minimum_version=(3, 9))
54 @pytest.mark.parametrize("filename", all_data_cases("preview_310"))
55 def test_preview_minimum_python_310_format(filename: str) -> None:
56 source, expected = read_data("preview_310", filename)
57 mode = black.Mode(preview=True)
58 assert_format(source, expected, mode, minimum_version=(3, 10))
66 def test_empty() -> None:
67 source = expected = ""
68 assert_format(source, expected)
71 @pytest.mark.parametrize("filename", all_data_cases("py_36"))
72 def test_python_36(filename: str) -> None:
73 source, expected = read_data("py_36", filename)
74 mode = black.Mode(target_versions=PY36_VERSIONS)
75 assert_format(source, expected, mode, minimum_version=(3, 6))
78 @pytest.mark.parametrize("filename", all_data_cases("py_37"))
79 def test_python_37(filename: str) -> None:
80 source, expected = read_data("py_37", filename)
81 mode = black.Mode(target_versions={black.TargetVersion.PY37})
82 assert_format(source, expected, mode, minimum_version=(3, 7))
85 @pytest.mark.parametrize("filename", all_data_cases("py_38"))
86 def test_python_38(filename: str) -> None:
87 source, expected = read_data("py_38", filename)
88 mode = black.Mode(target_versions={black.TargetVersion.PY38})
89 assert_format(source, expected, mode, minimum_version=(3, 8))
92 @pytest.mark.parametrize("filename", all_data_cases("py_39"))
93 def test_python_39(filename: str) -> None:
94 source, expected = read_data("py_39", filename)
95 mode = black.Mode(target_versions={black.TargetVersion.PY39})
96 assert_format(source, expected, mode, minimum_version=(3, 9))
99 @pytest.mark.parametrize("filename", all_data_cases("py_310"))
100 def test_python_310(filename: str) -> None:
101 source, expected = read_data("py_310", filename)
102 mode = black.Mode(target_versions={black.TargetVersion.PY310})
103 assert_format(source, expected, mode, minimum_version=(3, 10))
106 @pytest.mark.parametrize("filename", all_data_cases("py_310"))
107 def test_python_310_without_target_version(filename: str) -> None:
108 source, expected = read_data("py_310", filename)
110 assert_format(source, expected, mode, minimum_version=(3, 10))
113 def test_patma_invalid() -> None:
114 source, expected = read_data("miscellaneous", "pattern_matching_invalid")
115 mode = black.Mode(target_versions={black.TargetVersion.PY310})
116 with pytest.raises(black.parsing.InvalidInput) as exc_info:
117 assert_format(source, expected, mode, minimum_version=(3, 10))
119 exc_info.match("Cannot parse: 10:11")
122 @pytest.mark.parametrize("filename", all_data_cases("py_311"))
123 def test_python_311(filename: str) -> None:
124 source, expected = read_data("py_311", filename)
125 mode = black.Mode(target_versions={black.TargetVersion.PY311})
126 assert_format(source, expected, mode, minimum_version=(3, 11))
129 @pytest.mark.parametrize("filename", all_data_cases("fast"))
130 def test_fast_cases(filename: str) -> None:
131 source, expected = read_data("fast", filename)
132 assert_format(source, expected, fast=True)
135 def test_python_2_hint() -> None:
136 with pytest.raises(black.parsing.InvalidInput) as exc_info:
137 assert_format("print 'daylily'", "print 'daylily'")
138 exc_info.match(black.parsing.PY2_HINT)
141 @pytest.mark.filterwarnings("ignore:invalid escape sequence.*:DeprecationWarning")
142 def test_docstring_no_string_normalization() -> None:
143 """Like test_docstring but with string normalization off."""
144 source, expected = read_data("miscellaneous", "docstring_no_string_normalization")
145 mode = replace(DEFAULT_MODE, string_normalization=False)
146 assert_format(source, expected, mode)
149 def test_docstring_line_length_6() -> None:
150 """Like test_docstring but with line length set to 6."""
151 source, expected = read_data("miscellaneous", "linelength6")
152 mode = black.Mode(line_length=6)
153 assert_format(source, expected, mode)
156 def test_preview_docstring_no_string_normalization() -> None:
158 Like test_docstring but with string normalization off *and* the preview style
161 source, expected = read_data(
162 "miscellaneous", "docstring_preview_no_string_normalization"
164 mode = replace(DEFAULT_MODE, string_normalization=False, preview=True)
165 assert_format(source, expected, mode)
168 def test_long_strings_flag_disabled() -> None:
169 """Tests for turning off the string processing logic."""
170 source, expected = read_data("miscellaneous", "long_strings_flag_disabled")
171 mode = replace(DEFAULT_MODE, experimental_string_processing=False)
172 assert_format(source, expected, mode)
175 def test_stub() -> None:
176 mode = replace(DEFAULT_MODE, is_pyi=True)
177 source, expected = read_data("miscellaneous", "stub.pyi")
178 assert_format(source, expected, mode)
181 def test_power_op_newline() -> None:
182 # requires line_length=0
183 source, expected = read_data("miscellaneous", "power_op_newline")
184 assert_format(source, expected, mode=black.Mode(line_length=0))