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 check_file("preview", filename, black.Mode(preview=True))
42 @pytest.mark.parametrize("filename", all_data_cases("preview_39"))
43 def test_preview_minimum_python_39_format(filename: str) -> None:
44 source, expected = read_data("preview_39", filename)
45 mode = black.Mode(preview=True)
46 assert_format(source, expected, mode, minimum_version=(3, 9))
49 @pytest.mark.parametrize("filename", all_data_cases("preview_310"))
50 def test_preview_minimum_python_310_format(filename: str) -> None:
51 source, expected = read_data("preview_310", filename)
52 mode = black.Mode(preview=True)
53 assert_format(source, expected, mode, minimum_version=(3, 10))
61 def test_empty() -> None:
62 source = expected = ""
63 assert_format(source, expected)
66 @pytest.mark.parametrize("filename", all_data_cases("py_36"))
67 def test_python_36(filename: str) -> None:
68 source, expected = read_data("py_36", filename)
69 mode = black.Mode(target_versions=PY36_VERSIONS)
70 assert_format(source, expected, mode, minimum_version=(3, 6))
73 @pytest.mark.parametrize("filename", all_data_cases("py_37"))
74 def test_python_37(filename: str) -> None:
75 source, expected = read_data("py_37", filename)
76 mode = black.Mode(target_versions={black.TargetVersion.PY37})
77 assert_format(source, expected, mode, minimum_version=(3, 7))
80 @pytest.mark.parametrize("filename", all_data_cases("py_38"))
81 def test_python_38(filename: str) -> None:
82 source, expected = read_data("py_38", filename)
83 mode = black.Mode(target_versions={black.TargetVersion.PY38})
84 assert_format(source, expected, mode, minimum_version=(3, 8))
87 @pytest.mark.parametrize("filename", all_data_cases("py_39"))
88 def test_python_39(filename: str) -> None:
89 source, expected = read_data("py_39", filename)
90 mode = black.Mode(target_versions={black.TargetVersion.PY39})
91 assert_format(source, expected, mode, minimum_version=(3, 9))
94 @pytest.mark.parametrize("filename", all_data_cases("py_310"))
95 def test_python_310(filename: str) -> None:
96 source, expected = read_data("py_310", filename)
97 mode = black.Mode(target_versions={black.TargetVersion.PY310})
98 assert_format(source, expected, mode, minimum_version=(3, 10))
101 @pytest.mark.parametrize("filename", all_data_cases("py_310"))
102 def test_python_310_without_target_version(filename: str) -> None:
103 source, expected = read_data("py_310", filename)
105 assert_format(source, expected, mode, minimum_version=(3, 10))
108 def test_patma_invalid() -> None:
109 source, expected = read_data("miscellaneous", "pattern_matching_invalid")
110 mode = black.Mode(target_versions={black.TargetVersion.PY310})
111 with pytest.raises(black.parsing.InvalidInput) as exc_info:
112 assert_format(source, expected, mode, minimum_version=(3, 10))
114 exc_info.match("Cannot parse: 10:11")
117 @pytest.mark.parametrize("filename", all_data_cases("py_311"))
118 def test_python_311(filename: str) -> None:
119 source, expected = read_data("py_311", filename)
120 mode = black.Mode(target_versions={black.TargetVersion.PY311})
121 assert_format(source, expected, mode, minimum_version=(3, 11))
124 @pytest.mark.parametrize("filename", all_data_cases("fast"))
125 def test_fast_cases(filename: str) -> None:
126 source, expected = read_data("fast", filename)
127 assert_format(source, expected, fast=True)
130 def test_python_2_hint() -> None:
131 with pytest.raises(black.parsing.InvalidInput) as exc_info:
132 assert_format("print 'daylily'", "print 'daylily'")
133 exc_info.match(black.parsing.PY2_HINT)
136 @pytest.mark.filterwarnings("ignore:invalid escape sequence.*:DeprecationWarning")
137 def test_docstring_no_string_normalization() -> None:
138 """Like test_docstring but with string normalization off."""
139 source, expected = read_data("miscellaneous", "docstring_no_string_normalization")
140 mode = replace(DEFAULT_MODE, string_normalization=False)
141 assert_format(source, expected, mode)
144 def test_preview_docstring_no_string_normalization() -> None:
146 Like test_docstring but with string normalization off *and* the preview style
149 source, expected = read_data(
150 "miscellaneous", "docstring_preview_no_string_normalization"
152 mode = replace(DEFAULT_MODE, string_normalization=False, preview=True)
153 assert_format(source, expected, mode)
156 def test_long_strings_flag_disabled() -> None:
157 """Tests for turning off the string processing logic."""
158 source, expected = read_data("miscellaneous", "long_strings_flag_disabled")
159 mode = replace(DEFAULT_MODE, experimental_string_processing=False)
160 assert_format(source, expected, mode)
163 def test_stub() -> None:
164 mode = replace(DEFAULT_MODE, is_pyi=True)
165 source, expected = read_data("miscellaneous", "stub.pyi")
166 assert_format(source, expected, mode)
169 def test_power_op_newline() -> None:
170 # requires line_length=0
171 source, expected = read_data("miscellaneous", "power_op_newline")
172 assert_format(source, expected, mode=black.Mode(line_length=0))