Skip to content

Commit 2a2c0d5

Browse files
committed
test: restructure test_cross_language to use assertEndsWith for full suffix matching
Apply vstinner's review suggestion: use assertEndsWith instead of assertIn for more precise test assertions. Split cases into method hints (checked via Did you mean pattern) and raw hints (checked via exact suffix).
1 parent 57a4d39 commit 2a2c0d5

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

Lib/test/test_traceback.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4564,31 +4564,39 @@ def __init__(self):
45644564
actual = self.get_suggestion(Outer(), 'target')
45654565
self.assertIn("'.normal.target'", actual)
45664566

4567+
@force_not_colorized
45674568
def test_cross_language(self):
45684569
cases = [
4569-
# (obj, attr, expected_substring)
4570-
([], 'push', "'.append'"),
4571-
([], 'concat', "'.extend'"),
4572-
([], 'addAll', "'.extend'"),
4573-
([], 'contains', "Use 'x in list'."),
4574-
([], 'add', "Did you mean to use a 'set' object?"),
4575-
('', 'toUpperCase', "'.upper'"),
4576-
('', 'toLowerCase', "'.lower'"),
4577-
('', 'trimStart', "'.lstrip'"),
4578-
('', 'trimEnd', "'.rstrip'"),
4579-
({}, 'keySet', "'.keys'"),
4580-
({}, 'entrySet', "'.items'"),
4581-
({}, 'entries', "'.items'"),
4582-
({}, 'putAll', "'.update'"),
4583-
({}, 'put', "d[k] = v"),
4570+
# (type, attr, hint_attr)
4571+
(list, 'push', 'append'),
4572+
(list, 'concat', 'extend'),
4573+
(list, 'addAll', 'extend'),
4574+
(str, 'toUpperCase', 'upper'),
4575+
(str, 'toLowerCase', 'lower'),
4576+
(str, 'trimStart', 'lstrip'),
4577+
(str, 'trimEnd', 'rstrip'),
4578+
(dict, 'keySet', 'keys'),
4579+
(dict, 'entrySet', 'items'),
4580+
(dict, 'entries', 'items'),
4581+
(dict, 'putAll', 'update'),
4582+
]
4583+
for test_type, attr, hint_attr in cases:
4584+
with self.subTest(type=test_type.__name__, attr=attr):
4585+
obj = test_type()
4586+
actual = self.get_suggestion(obj, attr)
4587+
self.assertEndsWith(actual, f"Did you mean '.{hint_attr}'?")
4588+
4589+
cases = [
4590+
# (type, attr, hint)
4591+
(list, 'contains', "Use 'x in list'."),
4592+
(list, 'add', "Did you mean to use a 'set' object?"),
4593+
(dict, 'put', "Use d[k] = v."),
45844594
]
4585-
for obj, attr, expected in cases:
4586-
with self.subTest(type=type(obj).__name__, attr=attr):
4595+
for test_type, attr, expected in cases:
4596+
with self.subTest(type=test_type, attr=attr):
4597+
obj = test_type()
45874598
actual = self.get_suggestion(obj, attr)
4588-
self.assertIn(expected, actual)
4589-
# push hint should not repeat the wrong attribute name
4590-
actual = self.get_suggestion([], 'push')
4591-
self.assertNotIn("instead of", actual)
4599+
self.assertEndsWith(actual, expected)
45924600

45934601
def test_cross_language_levenshtein_takes_priority(self):
45944602
# Levenshtein catches trim->strip and indexOf->index before

0 commit comments

Comments
 (0)