]> 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 parser bug where "type" was misinterpreted as a keyword inside a match (#3950)
[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 black.mode import TargetVersion
9 from tests.util import (
10     all_data_cases,
11     assert_format,
12     dump_to_stderr,
13     read_data,
14     read_data_with_mode,
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(subdir: str, filename: str, *, data: bool = True) -> None:
25     args, source, expected = read_data_with_mode(subdir, filename, data=data)
26     assert_format(
27         source,
28         expected,
29         args.mode,
30         fast=args.fast,
31         minimum_version=args.minimum_version,
32     )
33     if args.minimum_version is not None:
34         major, minor = args.minimum_version
35         target_version = TargetVersion[f"PY{major}{minor}"]
36         mode = replace(args.mode, target_versions={target_version})
37         assert_format(
38             source, expected, mode, fast=args.fast, minimum_version=args.minimum_version
39         )
40
41
42 @pytest.mark.filterwarnings("ignore:invalid escape sequence.*:DeprecationWarning")
43 @pytest.mark.parametrize("filename", all_data_cases("cases"))
44 def test_simple_format(filename: str) -> None:
45     check_file("cases", filename)
46
47
48 # =============== #
49 # Unusual cases
50 # =============== #
51
52
53 def test_empty() -> None:
54     source = expected = ""
55     assert_format(source, expected)
56
57
58 def test_patma_invalid() -> None:
59     source, expected = read_data("miscellaneous", "pattern_matching_invalid")
60     mode = black.Mode(target_versions={black.TargetVersion.PY310})
61     with pytest.raises(black.parsing.InvalidInput) as exc_info:
62         assert_format(source, expected, mode, minimum_version=(3, 10))
63
64     exc_info.match("Cannot parse: 10:11")