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
3 from black.trans import iter_fexpr_spans
6 def test_fexpr_spans() -> None:
8 string: str, expected_spans: List[Tuple[int, int]], expected_slices: List[str]
10 spans = list(iter_fexpr_spans(string))
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
19 assert spans == expected_spans
21 # Most of these test cases omit the leading 'f' and leading / closing quotes
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)], ["""{''' {'{ '''}"""])
46 '''f\'\'\'-{f"""*{f"+{f'.{x}.'}+"}*"""}-'y\\'\'\'\'''',
48 ['''{f"""*{f"+{f'.{x}.'}+"}*"""}'''],
50 check(r"""{}{""", [(0, 2)], ["{}"])
51 check("""f"{'{'''''''''}\"""", [(2, 15)], ["{'{'''''''''}"])