37
37
38
38
class HumanNameTestBase (unittest .TestCase ):
39
39
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
41
42
try :
42
43
self .assertEqual (actual , expected , "'%s' != '%s' for '%s'\n %r" % (
43
44
actual ,
@@ -146,7 +147,7 @@ def test_comparison_case_insensitive(self):
146
147
def test_slice (self ):
147
148
hn = HumanName ("Doe-Ray, Dr. John P., CLU, CFP, LUTC" )
148
149
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 )
150
151
self .m (hn [1 :- 2 ], ['John' , 'P.' , 'Doe-Ray' ], hn )
151
152
152
153
def test_getitem (self ):
@@ -1274,6 +1275,20 @@ def test_empty_attribute_default(self):
1274
1275
self .m (hn .nickname , None , hn )
1275
1276
CONSTANTS .empty_attribute_default = _orig
1276
1277
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 )
1277
1292
1278
1293
class HumanNameNicknameTestCase (HumanNameTestBase ):
1279
1294
# https://code.google.com/p/python-nameparser/issues/detail?id=33
@@ -2036,6 +2051,7 @@ def test_variations_of_TEST_NAMES(self):
2036
2051
hn = HumanName (name )
2037
2052
if len (hn .suffix_list ) > 1 :
2038
2053
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
2039
2055
hn_dict = hn .as_dict ()
2040
2056
attrs = [
2041
2057
'title' ,
@@ -2045,9 +2061,6 @@ def test_variations_of_TEST_NAMES(self):
2045
2061
'suffix' ,
2046
2062
'nickname' ,
2047
2063
]
2048
- for attr in attrs :
2049
- if not getattr (hn , attr ):
2050
- setattr (hn ,attr ,'' )
2051
2064
nocomma = HumanName ("{title} {first} {middle} {last} {suffix}" .format (** hn_dict ))
2052
2065
lastnamecomma = HumanName ("{last}, {title} {first} {middle} {suffix}" .format (** hn_dict ))
2053
2066
if hn .suffix :
@@ -2076,11 +2089,11 @@ def test_variations_of_TEST_NAMES(self):
2076
2089
hn .capitalize ()
2077
2090
print ((repr (hn )))
2078
2091
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
2086
2099
unittest .main ()
0 commit comments