]> git.madduck.net Git - etc/vim.git/blob - tests/test_format.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Port & upstream mypyc wheel build workflow (#3197)
[etc/vim.git] / tests / test_format.py
1 from dataclasses import replace
2 from typing import Any, Iterator
3 from unittest.mock import patch
4
5 import pytest
6
7 import black
8 from tests.util import (
9     DEFAULT_MODE,
10     PY36_VERSIONS,
11     all_data_cases,
12     assert_format,
13     dump_to_stderr,
14     read_data,
15 )
16
17
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):
21         yield
22
23
24 def check_file(
25     subdir: str, filename: str, mode: black.Mode, *, data: bool = True
26 ) -> None:
27     source, expected = read_data(subdir, filename, data=data)
28     assert_format(source, expected, mode, fast=False)
29
30
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)
35
36
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))
40
41
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))
47
48
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))
54
55
56 # =============== #
57 # Complex cases
58 # ============= #
59
60
61 def test_empty() -> None:
62     source = expected = ""
63     assert_format(source, expected)
64
65
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))
71
72
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))
78
79
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))
85
86
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))
92
93
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))
99
100
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)
104     mode = black.Mode()
105     assert_format(source, expected, mode, minimum_version=(3, 10))
106
107
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))
113
114     exc_info.match("Cannot parse: 10:11")
115
116
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))
122
123
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)
128
129
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)
134
135
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)
142
143
144 def test_preview_docstring_no_string_normalization() -> None:
145     """
146     Like test_docstring but with string normalization off *and* the preview style
147     enabled.
148     """
149     source, expected = read_data(
150         "miscellaneous", "docstring_preview_no_string_normalization"
151     )
152     mode = replace(DEFAULT_MODE, string_normalization=False, preview=True)
153     assert_format(source, expected, mode)
154
155
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)
161
162
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)
167
168
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))