@@ -40,6 +40,59 @@ 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_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').
47+ hn = HumanName ('john doe' )
48+ hn .capitalize ()
49+ self .m (hn .surnames , 'Doe' , hn )
50+ self .assertEqual (hn .middle_list , [])
51+ self .assertEqual (hn .surnames_list , ['Doe' ])
52+
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.
56+ hn = HumanName ('Jane Doe' )
57+ hn .capitalize (force = True )
58+ self .m (hn .surnames , 'Doe' , hn )
59+ self .assertEqual (hn .middle_list , [])
60+
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 )
66+ self .assertEqual (hn .title_list , [])
67+ self .assertEqual (hn .first_list , ['Jane' ])
68+ self .assertEqual (hn .last_list , ['Doe' ])
69+
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+
79+ def test_capitalize_empty_suffix_produces_no_spurious_tokens (self ) -> None :
80+ # ''.split(', ') returns [''] just like ''.split(' ') did for the other
81+ # attributes — an absent suffix should produce suffix_list == [], not [''].
82+ hn = HumanName ('JOHN DOE' )
83+ hn .capitalize ()
84+ self .assertEqual (hn .suffix_list , [])
85+
86+ def test_capitalize_single_suffix_still_works (self ) -> None :
87+ hn = HumanName ('JOHN DOE PHD' )
88+ hn .capitalize ()
89+ self .assertEqual (hn .suffix_list , ['Ph.D.' ])
90+
91+ def test_capitalize_multiple_suffixes_still_split_correctly (self ) -> None :
92+ hn = HumanName ('JOHN DOE PHD MD' )
93+ hn .capitalize ()
94+ self .assertEqual (hn .suffix_list , ['Ph.D.' , 'M.D.' ])
95+
4396 # Leaving already-capitalized names alone
4497 def test_no_change_to_mixed_chase (self ) -> None :
4598 hn = HumanName ('Shirley Maclaine' )
0 commit comments