Skip to content

Commit 51f80f4

Browse files
committed
Correct offsets for tests to precisely locate the quote
1 parent 816957c commit 51f80f4

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

Lib/test/test_fstring.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,50 @@ def test_ast_line_numbers_with_parentheses(self):
411411

412412
expr = """
413413
x = (
414+
u'wat',
415+
u"wat",
416+
b'wat',
417+
b"wat",
418+
f'wat',
419+
f"wat",
420+
)
421+
422+
y = (
423+
u'''wat''',
424+
u\"\"\"wat\"\"\",
425+
b'''wat''',
426+
b\"\"\"wat\"\"\",
427+
f'''wat''',
428+
f\"\"\"wat\"\"\",
429+
)
430+
"""
431+
t = ast.parse(expr)
432+
self.assertEqual(type(t), ast.Module)
433+
self.assertEqual(len(t.body), 2)
434+
x, y = t.body
435+
436+
# Check the single quoted string offsets first.
437+
offsets = [
438+
(elt.col_offset, elt.end_col_offset)
439+
for elt in x.value.elts
440+
]
441+
self.assertTrue(all(
442+
offset == (4, 10)
443+
for offset in offsets
444+
))
445+
446+
# Check the triple quoted string offsets.
447+
offsets = [
448+
(elt.col_offset, elt.end_col_offset)
449+
for elt in y.value.elts
450+
]
451+
self.assertTrue(all(
452+
offset == (4, 14)
453+
for offset in offsets
454+
))
455+
456+
expr = """
457+
x = (
414458
'PERL_MM_OPT', (
415459
f'wat'
416460
f'some_string={f(x)} '
@@ -444,7 +488,11 @@ def test_ast_line_numbers_with_parentheses(self):
444488
self.assertEqual(wat2.lineno, 5)
445489
self.assertEqual(wat2.end_lineno, 6)
446490
self.assertEqual(wat2.col_offset, 32)
447-
self.assertEqual(wat2.end_col_offset, 18)
491+
# wat ends at the offset 17, but the whole f-string
492+
# ends at the offset 18 (since the quote is part of the
493+
# f-string but not the wat string)
494+
self.assertEqual(wat2.end_col_offset, 17)
495+
self.assertEqual(fstring.end_col_offset, 18)
448496

449497
def test_docstring(self):
450498
def f():

0 commit comments

Comments
 (0)