Skip to content

Commit 665f148

Browse files
derek73claude
andcommitted
tests: split capitalize regression test and clarify comments
- Split test_capitalize_empty_name_part_has_no_leading_space_in_surnames into three focused tests (normal path, force=True path, list invariants) so failures self-localize to the scenario - Added test_capitalize_title_and_last_only_no_spurious_tokens covering a name with empty first and middle simultaneously - Sharpened inline comments to accurately describe root cause and why force=True matters (early-return guard) - Added comment to suffix_list line explaining the intentional split(', ') asymmetry vs the space-delimited attributes Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 7fbb73c commit 665f148

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

nameparser/parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,7 @@ def capitalize(self, force: bool | None = None) -> None:
988988
self.first_list = self.cap_piece(self.first, 'first').split()
989989
self.middle_list = self.cap_piece(self.middle, 'middle').split()
990990
self.last_list = self.cap_piece(self.last, 'last').split()
991+
# suffix is stored comma-separated ("Ph.D., J.D."), not space-separated
991992
self.suffix_list = self.cap_piece(self.suffix, 'suffix').split(', ')
992993

993994
def handle_capitalization(self) -> None:

tests/test_capitalization.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,42 @@ def test_capitization_middle_initial_is_also_a_conjunction(self) -> None:
4040
hn.capitalize()
4141
self.m(str(hn), 'Scott E. Werner', hn)
4242

43-
def test_capitalize_empty_name_part_has_no_leading_space_in_surnames(self) -> None:
44-
# capitalize() split each attribute with str.split(' '), which returns
45-
# [''] (rather than []) for an empty string. That spurious element
46-
# leaked into surnames_list (middle_list + last_list) and produced a
47-
# leading space in the surnames property, e.g. ' Doe' instead of 'Doe'.
43+
def test_capitalize_empty_middle_produces_no_leading_space_in_surnames(self) -> None:
44+
# str.split(' ') on an empty string returns [''] rather than [], so an
45+
# absent middle produced a spurious token that leaked into surnames_list
46+
# and caused a leading space in the surnames property (' Doe' not 'Doe').
4847
hn = HumanName('john doe')
4948
hn.capitalize()
5049
self.m(hn.surnames, 'Doe', hn)
5150
self.assertEqual(hn.middle_list, [])
5251
self.assertEqual(hn.surnames_list, ['Doe'])
5352

54-
# force=True on a mixed-case name hits the same code path
53+
def test_capitalize_force_empty_middle_produces_no_leading_space_in_surnames(self) -> None:
54+
# Without force=True, capitalize() exits early for mixed-case names and
55+
# never reaches the split lines. Confirm the fix covers that path too.
5556
hn = HumanName('Jane Doe')
5657
hn.capitalize(force=True)
5758
self.m(hn.surnames, 'Doe', hn)
5859
self.assertEqual(hn.middle_list, [])
5960

60-
# other empty attribute lists are also free of the spurious '' element
61+
def test_capitalize_empty_attributes_produce_no_spurious_tokens(self) -> None:
62+
# Confirm the fix extends beyond surnames: empty attribute lists are []
63+
# not [''], and non-empty ones contain only real tokens.
64+
hn = HumanName('Jane Doe')
65+
hn.capitalize(force=True)
6166
self.assertEqual(hn.title_list, [])
6267
self.assertEqual(hn.first_list, ['Jane'])
6368
self.assertEqual(hn.last_list, ['Doe'])
6469

70+
def test_capitalize_title_and_last_only_no_spurious_tokens(self) -> None:
71+
# title+last with no first or middle leaves first_list and middle_list
72+
# both empty. All-caps triggers capitalize() without force=True.
73+
hn = HumanName('DR DOE')
74+
hn.capitalize()
75+
self.assertEqual(hn.first_list, [])
76+
self.assertEqual(hn.middle_list, [])
77+
self.m(str(hn), 'Dr Doe', hn)
78+
6579
# Leaving already-capitalized names alone
6680
def test_no_change_to_mixed_chase(self) -> None:
6781
hn = HumanName('Shirley Maclaine')

0 commit comments

Comments
 (0)