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.parametrize("filename", all_data_cases("simple_cases"))
32 def test_simple_format(filename: str) -> None:
33 check_file("simple_cases", filename, DEFAULT_MODE)
36 @pytest.mark.parametrize("filename", all_data_cases("preview"))
37 def test_preview_format(filename: str) -> None:
38 check_file("preview", filename, black.Mode(preview=True))
41 @pytest.mark.parametrize("filename", all_data_cases("preview_39"))
42 def test_preview_minimum_python_39_format(filename: str) -> None:
43 source, expected = read_data("preview_39", filename)
44 mode = black.Mode(preview=True)
45 assert_format(source, expected, mode, minimum_version=(3, 9))
48 @pytest.mark.parametrize("filename", all_data_cases("preview_310"))
49 def test_preview_minimum_python_310_format(filename: str) -> None:
50 source, expected = read_data("preview_310", filename)
51 mode = black.Mode(preview=True)
52 assert_format(source, expected, mode, minimum_version=(3, 10))
60 def test_empty() -> None:
61 source = expected = ""
62 assert_format(source, expected)
65 @pytest.mark.parametrize("filename", all_data_cases("py_36"))
66 def test_python_36(filename: str) -> None:
67 source, expected = read_data("py_36", filename)
68 mode = black.Mode(target_versions=PY36_VERSIONS)
69 assert_format(source, expected, mode, minimum_version=(3, 6))
72 @pytest.mark.parametrize("filename", all_data_cases("py_37"))
73 def test_python_37(filename: str) -> None:
74 source, expected = read_data("py_37", filename)
75 mode = black.Mode(target_versions={black.TargetVersion.PY37})
76 assert_format(source, expected, mode, minimum_version=(3, 7))
79 @pytest.mark.parametrize("filename", all_data_cases("py_38"))
80 def test_python_38(filename: str) -> None:
81 source, expected = read_data("py_38", filename)
82 mode = black.Mode(target_versions={black.TargetVersion.PY38})
83 assert_format(source, expected, mode, minimum_version=(3, 8))
86 @pytest.mark.parametrize("filename", all_data_cases("py_39"))
87 def test_python_39(filename: str) -> None:
88 source, expected = read_data("py_39", filename)
89 mode = black.Mode(target_versions={black.TargetVersion.PY39})
90 assert_format(source, expected, mode, minimum_version=(3, 9))
93 @pytest.mark.parametrize("filename", all_data_cases("py_310"))
94 def test_python_310(filename: str) -> None:
95 source, expected = read_data("py_310", filename)
96 mode = black.Mode(target_versions={black.TargetVersion.PY310})
97 assert_format(source, expected, mode, minimum_version=(3, 10))
100 @pytest.mark.parametrize("filename", all_data_cases("py_310"))
101 def test_python_310_without_target_version(filename: str) -> None:
102 source, expected = read_data("py_310", filename)
104 assert_format(source, expected, mode, minimum_version=(3, 10))
107 def test_patma_invalid() -> None:
108 source, expected = read_data("miscellaneous", "pattern_matching_invalid")
109 mode = black.Mode(target_versions={black.TargetVersion.PY310})
110 with pytest.raises(black.parsing.InvalidInput) as exc_info:
111 assert_format(source, expected, mode, minimum_version=(3, 10))
113 exc_info.match("Cannot parse: 10:11")
116 @pytest.mark.parametrize("filename", all_data_cases("py_311"))
117 def test_python_311(filename: str) -> None:
118 source, expected = read_data("py_311", filename)
119 mode = black.Mode(target_versions={black.TargetVersion.PY311})
120 assert_format(source, expected, mode, minimum_version=(3, 11))
123 @pytest.mark.parametrize("filename", all_data_cases("fast"))
124 def test_fast_cases(filename: str) -> None:
125 source, expected = read_data("fast", filename)
126 assert_format(source, expected, fast=True)
129 def test_python_2_hint() -> None:
130 with pytest.raises(black.parsing.InvalidInput) as exc_info:
131 assert_format("print 'daylily'", "print 'daylily'")
132 exc_info.match(black.parsing.PY2_HINT)
135 def test_docstring_no_string_normalization() -> None:
136 """Like test_docstring but with string normalization off."""
137 source, expected = read_data("miscellaneous", "docstring_no_string_normalization")
138 mode = replace(DEFAULT_MODE, string_normalization=False)
139 assert_format(source, expected, mode)
142 def test_long_strings_flag_disabled() -> None:
143 """Tests for turning off the string processing logic."""
144 source, expected = read_data("miscellaneous", "long_strings_flag_disabled")
145 mode = replace(DEFAULT_MODE, experimental_string_processing=False)
146 assert_format(source, expected, mode)
149 def test_stub() -> None:
150 mode = replace(DEFAULT_MODE, is_pyi=True)
151 source, expected = read_data("miscellaneous", "stub.pyi")
152 assert_format(source, expected, mode)
155 def test_power_op_newline() -> None:
156 # requires line_length=0
157 source, expected = read_data("miscellaneous", "power_op_newline")
158 assert_format(source, expected, mode=black.Mode(line_length=0))