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

Fix type dependencies of mypy invocation (#2411)
[etc/vim.git] / tests / test_format.py
1 from unittest.mock import patch
2
3 import black
4 import pytest
5 from parameterized import parameterized
6
7 from tests.util import (
8     BlackBaseTestCase,
9     fs,
10     DEFAULT_MODE,
11     dump_to_stderr,
12     read_data,
13     THIS_DIR,
14 )
15
16 SIMPLE_CASES = [
17     "beginning_backslash",
18     "bracketmatch",
19     "class_blank_parentheses",
20     "class_methods_new_line",
21     "collections",
22     "comments",
23     "comments2",
24     "comments3",
25     "comments4",
26     "comments5",
27     "comments6",
28     "comments_non_breaking_space",
29     "comment_after_escaped_newline",
30     "composition",
31     "composition_no_trailing_comma",
32     "docstring",
33     "empty_lines",
34     "expression",
35     "fmtonoff",
36     "fmtonoff2",
37     "fmtonoff3",
38     "fmtonoff4",
39     "fmtskip",
40     "fmtskip2",
41     "fmtskip3",
42     "fmtskip4",
43     "fmtskip5",
44     "fmtskip6",
45     "fstring",
46     "function",
47     "function2",
48     "function_trailing_comma",
49     "import_spacing",
50     "remove_parens",
51     "slices",
52     "string_prefixes",
53     "tricky_unicode_symbols",
54     "tupleassign",
55 ]
56
57 SIMPLE_CASES_PY2 = [
58     "numeric_literals_py2",
59     "python2",
60     "python2_unicode_literals",
61 ]
62
63 EXPERIMENTAL_STRING_PROCESSING_CASES = [
64     "cantfit",
65     "comments7",
66     "long_strings",
67     "long_strings__edge_case",
68     "long_strings__regression",
69     "percent_precedence",
70 ]
71
72
73 SOURCES = [
74     "src/black/__init__.py",
75     "src/black/__main__.py",
76     "src/black/brackets.py",
77     "src/black/cache.py",
78     "src/black/comments.py",
79     "src/black/concurrency.py",
80     "src/black/const.py",
81     "src/black/debug.py",
82     "src/black/files.py",
83     "src/black/linegen.py",
84     "src/black/lines.py",
85     "src/black/mode.py",
86     "src/black/nodes.py",
87     "src/black/numerics.py",
88     "src/black/output.py",
89     "src/black/parsing.py",
90     "src/black/report.py",
91     "src/black/rusty.py",
92     "src/black/strings.py",
93     "src/black/trans.py",
94     "src/blackd/__init__.py",
95     "src/blib2to3/pygram.py",
96     "src/blib2to3/pytree.py",
97     "src/blib2to3/pgen2/conv.py",
98     "src/blib2to3/pgen2/driver.py",
99     "src/blib2to3/pgen2/grammar.py",
100     "src/blib2to3/pgen2/literals.py",
101     "src/blib2to3/pgen2/parse.py",
102     "src/blib2to3/pgen2/pgen.py",
103     "src/blib2to3/pgen2/tokenize.py",
104     "src/blib2to3/pgen2/token.py",
105     "setup.py",
106     "tests/test_black.py",
107     "tests/test_blackd.py",
108     "tests/test_format.py",
109     "tests/test_primer.py",
110     "tests/optional.py",
111     "tests/util.py",
112     "tests/conftest.py",
113 ]
114
115
116 class TestSimpleFormat(BlackBaseTestCase):
117     @parameterized.expand(SIMPLE_CASES_PY2)
118     @pytest.mark.python2
119     @patch("black.dump_to_file", dump_to_stderr)
120     def test_simple_format_py2(self, filename: str) -> None:
121         self.check_file(filename, DEFAULT_MODE)
122
123     @parameterized.expand(SIMPLE_CASES)
124     @patch("black.dump_to_file", dump_to_stderr)
125     def test_simple_format(self, filename: str) -> None:
126         self.check_file(filename, DEFAULT_MODE)
127
128     @parameterized.expand(EXPERIMENTAL_STRING_PROCESSING_CASES)
129     @patch("black.dump_to_file", dump_to_stderr)
130     def test_experimental_format(self, filename: str) -> None:
131         self.check_file(filename, black.Mode(experimental_string_processing=True))
132
133     @parameterized.expand(SOURCES)
134     @patch("black.dump_to_file", dump_to_stderr)
135     def test_source_is_formatted(self, filename: str) -> None:
136         path = THIS_DIR.parent / filename
137         self.check_file(str(path), DEFAULT_MODE, data=False)
138
139     def check_file(self, filename: str, mode: black.Mode, *, data: bool = True) -> None:
140         source, expected = read_data(filename, data=data)
141         actual = fs(source, mode=mode)
142         self.assertFormatEqual(expected, actual)
143         if source != actual:
144             black.assert_equivalent(source, actual)
145             black.assert_stable(source, actual, mode)