3737
3838class HumanNameTestBase (unittest .TestCase ):
3939 def m (self , actual , expected , hn ):
40- """assertEquals with a better message"""
40+ """assertEquals with a better message and awareness of hn.C.empty_attribute_default"""
41+ expected = expected or hn .C .empty_attribute_default
4142 try :
4243 self .assertEqual (actual , expected , "'%s' != '%s' for '%s'\n %r" % (
4344 actual ,
@@ -146,7 +147,7 @@ def test_comparison_case_insensitive(self):
146147 def test_slice (self ):
147148 hn = HumanName ("Doe-Ray, Dr. John P., CLU, CFP, LUTC" )
148149 self .m (list (hn ), ['Dr.' , 'John' , 'P.' , 'Doe-Ray' , 'CLU, CFP, LUTC' ], hn )
149- self .m (hn [1 :], ['John' , 'P.' , 'Doe-Ray' , 'CLU, CFP, LUTC' ,'' ], hn )
150+ self .m (hn [1 :], ['John' , 'P.' , 'Doe-Ray' , 'CLU, CFP, LUTC' ,hn . C . empty_attribute_default ], hn )
150151 self .m (hn [1 :- 2 ], ['John' , 'P.' , 'Doe-Ray' ], hn )
151152
152153 def test_getitem (self ):
@@ -1274,6 +1275,20 @@ def test_empty_attribute_default(self):
12741275 self .m (hn .nickname , None , hn )
12751276 CONSTANTS .empty_attribute_default = _orig
12761277
1278+ def test_empty_attribute_on_instance (self ):
1279+ hn = HumanName ("" , None )
1280+ hn .C .empty_attribute_default = None
1281+ self .m (hn .title , None , hn )
1282+ self .m (hn .first , None , hn )
1283+ self .m (hn .middle , None , hn )
1284+ self .m (hn .last , None , hn )
1285+ self .m (hn .suffix , None , hn )
1286+ self .m (hn .nickname , None , hn )
1287+
1288+ def test_none_empty_attribute_string_formatting (self ):
1289+ hn = HumanName ("" , None )
1290+ hn .C .empty_attribute_default = None
1291+ self .assertEqual ('' , str (hn ), hn )
12771292
12781293class HumanNameNicknameTestCase (HumanNameTestBase ):
12791294 # https://code.google.com/p/python-nameparser/issues/detail?id=33
@@ -2036,6 +2051,7 @@ def test_variations_of_TEST_NAMES(self):
20362051 hn = HumanName (name )
20372052 if len (hn .suffix_list ) > 1 :
20382053 hn = HumanName ("{title} {first} {middle} {last} {suffix}" .format (** hn .as_dict ()).split (',' )[0 ])
2054+ hn .C .empty_attribute_default = '' # format strings below require empty string
20392055 hn_dict = hn .as_dict ()
20402056 attrs = [
20412057 'title' ,
@@ -2045,9 +2061,6 @@ def test_variations_of_TEST_NAMES(self):
20452061 'suffix' ,
20462062 'nickname' ,
20472063 ]
2048- for attr in attrs :
2049- if not getattr (hn , attr ):
2050- setattr (hn ,attr ,'' )
20512064 nocomma = HumanName ("{title} {first} {middle} {last} {suffix}" .format (** hn_dict ))
20522065 lastnamecomma = HumanName ("{last}, {title} {first} {middle} {suffix}" .format (** hn_dict ))
20532066 if hn .suffix :
@@ -2076,11 +2089,11 @@ def test_variations_of_TEST_NAMES(self):
20762089 hn .capitalize ()
20772090 print ((repr (hn )))
20782091 else :
2079- # if log.level > 0:
2080- # for name in TEST_NAMES:
2081- # hn = HumanName(name )
2082- # print((u(name)) )
2083- # print((u(hn)) )
2084- # print((repr(hn)))
2085- # print("\n-------------------------------------------\n")
2092+ print ( "-" * 80 )
2093+ print ( "Running tests" )
2094+ unittest . main ( exit = False )
2095+ print ("-" * 80 )
2096+ print ( "Running tests with empty_attribute_default = None" )
2097+ from nameparser . config import CONSTANTS
2098+ CONSTANTS . empty_attribute_default = None
20862099 unittest .main ()
0 commit comments