]> 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:

Vim plugin docs improvements (#3468)
[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     magic_trailing_comma = filename != "skip_magic_trailing_comma"
40     check_file(
41         "preview",
42         filename,
43         black.Mode(preview=True, magic_trailing_comma=magic_trailing_comma),
44     )
45
46
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))
52
53
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))
59
60
61 # =============== #
62 # Complex cases
63 # ============= #
64
65
66 def test_empty() -> None:
67     source = expected = ""
68     assert_format(source, expected)
69
70
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))
76
77
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))
83
84
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))
90
91
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))
97
98
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))
104
105
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)
109     mode = black.Mode()
110     assert_format(source, expected, mode, minimum_version=(3, 10))
111
112
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))
118
119     exc_info.match("Cannot parse: 10:11")
120
121
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))
127
128
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)
133
134
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)
139
140
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)
147
148
149 def test_preview_docstring_no_string_normalization() -> None:
150     """
151     Like test_docstring but with string normalization off *and* the preview style
152     enabled.
153     """
154     source, expected = read_data(
155         "miscellaneous", "docstring_preview_no_string_normalization"
156     )
157     mode = replace(DEFAULT_MODE, string_normalization=False, preview=True)
158     assert_format(source, expected, mode)
159
160
161 def test_long_strings_flag_disabled() -> None:
162     """Tests for turning off the string processing logic."""
163     source, expected = read_data("miscellaneous", "long_strings_flag_disabled")
164     mode = replace(DEFAULT_MODE, experimental_string_processing=False)
165     assert_format(source, expected, mode)
166
167
168 def test_stub() -> None:
169     mode = replace(DEFAULT_MODE, is_pyi=True)
170     source, expected = read_data("miscellaneous", "stub.pyi")
171     assert_format(source, expected, mode)
172
173
174 def test_power_op_newline() -> None:
175     # requires line_length=0
176     source, expected = read_data("miscellaneous", "power_op_newline")
177     assert_format(source, expected, mode=black.Mode(line_length=0))