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

Use optional tests for "no_python2" to simplify local testing (#2203)
[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     ff,
11     DEFAULT_MODE,
12     dump_to_stderr,
13     read_data,
14     THIS_DIR,
15 )
16
17 SIMPLE_CASES = [
18     "beginning_backslash",
19     "bracketmatch",
20     "class_blank_parentheses",
21     "class_methods_new_line",
22     "collections",
23     "comments",
24     "comments2",
25     "comments3",
26     "comments4",
27     "comments5",
28     "comments6",
29     "comments_non_breaking_space",
30     "comment_after_escaped_newline",
31     "composition",
32     "composition_no_trailing_comma",
33     "docstring",
34     "empty_lines",
35     "expression",
36     "fmtonoff",
37     "fmtonoff2",
38     "fmtonoff3",
39     "fmtonoff4",
40     "fmtskip",
41     "fmtskip2",
42     "fmtskip3",
43     "fmtskip4",
44     "fmtskip5",
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     "tests/test_black.py",
75     "tests/test_format.py",
76     "tests/test_blackd.py",
77     "src/black/__init__.py",
78     "src/blib2to3/pygram.py",
79     "src/blib2to3/pytree.py",
80     "src/blib2to3/pgen2/conv.py",
81     "src/blib2to3/pgen2/driver.py",
82     "src/blib2to3/pgen2/grammar.py",
83     "src/blib2to3/pgen2/literals.py",
84     "src/blib2to3/pgen2/parse.py",
85     "src/blib2to3/pgen2/pgen.py",
86     "src/blib2to3/pgen2/tokenize.py",
87     "src/blib2to3/pgen2/token.py",
88     "setup.py",
89 ]
90
91
92 class TestSimpleFormat(BlackBaseTestCase):
93     @parameterized.expand(SIMPLE_CASES_PY2)
94     @pytest.mark.python2
95     @patch("black.dump_to_file", dump_to_stderr)
96     def test_simple_format_py2(self, filename: str) -> None:
97         self.check_file(filename, DEFAULT_MODE)
98
99     @parameterized.expand(SIMPLE_CASES)
100     @patch("black.dump_to_file", dump_to_stderr)
101     def test_simple_format(self, filename: str) -> None:
102         self.check_file(filename, DEFAULT_MODE)
103
104     @parameterized.expand(EXPERIMENTAL_STRING_PROCESSING_CASES)
105     @patch("black.dump_to_file", dump_to_stderr)
106     def test_experimental_format(self, filename: str) -> None:
107         self.check_file(filename, black.Mode(experimental_string_processing=True))
108
109     @parameterized.expand(SOURCES)
110     @patch("black.dump_to_file", dump_to_stderr)
111     def test_source_is_formatted(self, filename: str) -> None:
112         path = THIS_DIR.parent / filename
113         self.check_file(str(path), DEFAULT_MODE, data=False)
114         self.assertFalse(ff(path))
115
116     def check_file(self, filename: str, mode: black.Mode, *, data: bool = True) -> None:
117         source, expected = read_data(filename, data=data)
118         actual = fs(source, mode=mode)
119         self.assertFormatEqual(expected, actual)
120         black.assert_equivalent(source, actual)
121         black.assert_stable(source, actual, mode)