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

Prepare docs for release 22.6.0 (#3139)
[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     assert_format,
12     dump_to_stderr,
13     read_data,
14     all_data_cases,
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.parametrize("filename", all_data_cases("simple_cases"))
32 def test_simple_format(filename: str) -> None:
33     check_file("simple_cases", filename, DEFAULT_MODE)
34
35
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))
39
40
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))
46
47
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))
53
54
55 # =============== #
56 # Complex cases
57 # ============= #
58
59
60 def test_empty() -> None:
61     source = expected = ""
62     assert_format(source, expected)
63
64
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))
70
71
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))
77
78
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))
84
85
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))
91
92
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))
98
99
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)
103     mode = black.Mode()
104     assert_format(source, expected, mode, minimum_version=(3, 10))
105
106
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))
112
113     exc_info.match("Cannot parse: 10:11")
114
115
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))
121
122
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)
127
128
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)
133
134
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)
140
141
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)
147
148
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)
153
154
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))