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.
1 from typing import List, Tuple
2 from black.trans import iter_fexpr_spans
5 def test_fexpr_spans() -> None:
7 string: str, expected_spans: List[Tuple[int, int]], expected_slices: List[str]
9 spans = list(iter_fexpr_spans(string))
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
18 assert spans == expected_spans
20 # Most of these test cases omit the leading 'f' and leading / closing quotes
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)], ["""{''' {'{ '''}"""])
45 '''f\'\'\'-{f"""*{f"+{f'.{x}.'}+"}*"""}-'y\\'\'\'\'''',
47 ['''{f"""*{f"+{f'.{x}.'}+"}*"""}'''],
49 check(r"""{}{""", [(0, 2)], ["{}"])
50 check("""f"{'{'''''''''}\"""", [(2, 15)], ["{'{'''''''''}"])