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