]> git.madduck.net Git - etc/vim.git/blob - tests/test_trans.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_trans.py
1 from typing import List, Tuple
2
3 from black.trans import iter_fexpr_spans
4
5
6 def test_fexpr_spans() -> None:
7     def check(
8         string: str, expected_spans: List[Tuple[int, int]], expected_slices: List[str]
9     ) -> None:
10         spans = list(iter_fexpr_spans(string))
11
12         # Checking slices isn't strictly necessary, but it's easier to verify at
13         # a glance than only spans
14         assert len(spans) == len(expected_slices)
15         for (i, j), slice in zip(spans, expected_slices):
16             assert 0 <= i <= j <= len(string)
17             assert string[i:j] == slice
18
19         assert spans == expected_spans
20
21     # Most of these test cases omit the leading 'f' and leading / closing quotes
22     # for convenience
23     # Some additional property-based tests can be found in
24     # https://github.com/psf/black/pull/2654#issuecomment-981411748
25     check("""{var}""", [(0, 5)], ["{var}"])
26     check("""f'{var}'""", [(2, 7)], ["{var}"])
27     check("""f'{1 + f() + 2 + "asdf"}'""", [(2, 24)], ["""{1 + f() + 2 + "asdf"}"""])
28     check("""text {var} text""", [(5, 10)], ["{var}"])
29     check("""text {{ {var} }} text""", [(8, 13)], ["{var}"])
30     check("""{a} {b} {c}""", [(0, 3), (4, 7), (8, 11)], ["{a}", "{b}", "{c}"])
31     check("""f'{a} {b} {c}'""", [(2, 5), (6, 9), (10, 13)], ["{a}", "{b}", "{c}"])
32     check("""{ {} }""", [(0, 6)], ["{ {} }"])
33     check("""{ {{}} }""", [(0, 8)], ["{ {{}} }"])
34     check("""{ {{{}}} }""", [(0, 10)], ["{ {{{}}} }"])
35     check("""{{ {{{}}} }}""", [(5, 7)], ["{}"])
36     check("""{{ {{{var}}} }}""", [(5, 10)], ["{var}"])
37     check("""{f"{0}"}""", [(0, 8)], ["""{f"{0}"}"""])
38     check("""{"'"}""", [(0, 5)], ["""{"'"}"""])
39     check("""{"{"}""", [(0, 5)], ["""{"{"}"""])
40     check("""{"}"}""", [(0, 5)], ["""{"}"}"""])
41     check("""{"{{"}""", [(0, 6)], ["""{"{{"}"""])
42     check("""{''' '''}""", [(0, 9)], ["""{''' '''}"""])
43     check("""{'''{'''}""", [(0, 9)], ["""{'''{'''}"""])
44     check("""{''' {'{ '''}""", [(0, 13)], ["""{''' {'{ '''}"""])
45     check(
46         '''f\'\'\'-{f"""*{f"+{f'.{x}.'}+"}*"""}-'y\\'\'\'\'''',
47         [(5, 33)],
48         ['''{f"""*{f"+{f'.{x}.'}+"}*"""}'''],
49     )
50     check(r"""{}{""", [(0, 2)], ["{}"])
51     check("""f"{'{'''''''''}\"""", [(2, 15)], ["{'{'''''''''}"])