forked from derek73/python-nameparser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_output_format.py
More file actions
162 lines (138 loc) · 7.55 KB
/
Copy pathtest_output_format.py
File metadata and controls
162 lines (138 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from nameparser import HumanName
from tests.base import HumanNameTestBase
class HumanNameOutputFormatTests(HumanNameTestBase):
def test_formatting_init_argument(self) -> None:
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)",
string_format="TEST1")
self.assertEqual(str(hn), "TEST1")
def test_formatting_constants_attribute(self) -> None:
from nameparser.config import CONSTANTS
CONSTANTS.string_format = "TEST2"
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
self.assertEqual(str(hn), "TEST2")
def test_capitalize_name_constants_attribute(self) -> None:
from nameparser.config import CONSTANTS
CONSTANTS.capitalize_name = True
hn = HumanName("bob v. de la macdole-eisenhower phd")
self.assertEqual(str(hn), "Bob V. de la MacDole-Eisenhower Ph.D.")
def test_force_mixed_case_capitalization_constants_attribute(self) -> None:
from nameparser.config import CONSTANTS
CONSTANTS.force_mixed_case_capitalization = True
hn = HumanName('Shirley Maclaine')
hn.capitalize()
self.assertEqual(str(hn), "Shirley MacLaine")
def test_capitalize_name_and_force_mixed_case_capitalization_constants_attributes(self) -> None:
from nameparser.config import CONSTANTS
CONSTANTS.capitalize_name = True
CONSTANTS.force_mixed_case_capitalization = True
hn = HumanName('Shirley Maclaine')
self.assertEqual(str(hn), "Shirley MacLaine")
def test_quote_nickname_formating(self) -> None:
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
hn.string_format = "{title} {first} {middle} {last} {suffix} '{nickname}'"
self.assertEqual(str(hn), "Rev John A. Kenneth Doe III 'Kenny'")
hn.string_format = "{last}, {title} {first} {middle}, {suffix} '{nickname}'"
self.assertEqual(str(hn), "Doe, Rev John A. Kenneth, III 'Kenny'")
def test_formating_removing_keys_from_format_string(self) -> None:
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
hn.string_format = "{title} {first} {middle} {last} {suffix} '{nickname}'"
self.assertEqual(str(hn), "Rev John A. Kenneth Doe III 'Kenny'")
hn.string_format = "{last}, {title} {first} {middle}, {suffix}"
self.assertEqual(str(hn), "Doe, Rev John A. Kenneth, III")
hn.string_format = "{last}, {title} {first} {middle}"
self.assertEqual(str(hn), "Doe, Rev John A. Kenneth")
hn.string_format = "{last}, {first} {middle}"
self.assertEqual(str(hn), "Doe, John A. Kenneth")
hn.string_format = "{last}, {first}"
self.assertEqual(str(hn), "Doe, John")
hn.string_format = "{first} {last}"
self.assertEqual(str(hn), "John Doe")
def test_formating_removing_pieces_from_name_buckets(self) -> None:
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
hn.string_format = "{title} {first} {middle} {last} {suffix} '{nickname}'"
self.assertEqual(str(hn), "Rev John A. Kenneth Doe III 'Kenny'")
hn.string_format = "{title} {first} {middle} {last} {suffix}"
self.assertEqual(str(hn), "Rev John A. Kenneth Doe III")
hn.middle = ''
self.assertEqual(str(hn), "Rev John Doe III")
hn.suffix = ''
self.assertEqual(str(hn), "Rev John Doe")
hn.title = ''
self.assertEqual(str(hn), "John Doe")
def test_formating_of_nicknames_with_parenthesis(self) -> None:
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
hn.string_format = "{title} {first} {middle} {last} {suffix} ({nickname})"
self.assertEqual(str(hn), "Rev John A. Kenneth Doe III (Kenny)")
hn.nickname = ''
self.assertEqual(str(hn), "Rev John A. Kenneth Doe III")
def test_formating_of_nicknames_with_single_quotes(self) -> None:
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
hn.string_format = "{title} {first} {middle} {last} {suffix} '{nickname}'"
self.assertEqual(str(hn), "Rev John A. Kenneth Doe III 'Kenny'")
hn.nickname = ''
self.assertEqual(str(hn), "Rev John A. Kenneth Doe III")
def test_formating_of_nicknames_with_double_quotes(self) -> None:
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
hn.string_format = "{title} {first} {middle} {last} {suffix} \"{nickname}\""
self.assertEqual(str(hn), "Rev John A. Kenneth Doe III \"Kenny\"")
hn.nickname = ''
self.assertEqual(str(hn), "Rev John A. Kenneth Doe III")
def test_formating_of_nicknames_in_middle(self) -> None:
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
hn.string_format = "{title} {first} ({nickname}) {middle} {last} {suffix}"
self.assertEqual(str(hn), "Rev John (Kenny) A. Kenneth Doe III")
hn.nickname = ''
self.assertEqual(str(hn), "Rev John A. Kenneth Doe III")
def test_empty_field_drops_surrounding_whitespace(self) -> None:
# issue #139: adjacent whitespace/punctuation should be dropped when a field is empty
hn = HumanName("John Smith")
hn.string_format = "{last} {suffix}, {first}"
self.assertEqual(str(hn), "Smith, John")
def test_empty_field_present_suffix_unaffected(self) -> None:
hn = HumanName("John Smith Jr")
hn.string_format = "{last} {suffix}, {first}"
self.assertEqual(str(hn), "Smith Jr, John")
def test_multiple_empty_fields_before_comma(self) -> None:
hn = HumanName("John Smith")
hn.string_format = "{title} {suffix}, {first} {last}"
self.assertEqual(str(hn), "John Smith")
def test_remove_emojis(self) -> None:
hn = HumanName("Sam Smith 😊")
self.m(hn.first, "Sam", hn)
self.m(hn.last, "Smith", hn)
self.assertEqual(str(hn), "Sam Smith")
def test_keep_non_emojis(self) -> None:
hn = HumanName("∫≜⩕ Smith 😊")
self.m(hn.first, "∫≜⩕", hn)
self.m(hn.last, "Smith", hn)
self.assertEqual(str(hn), "∫≜⩕ Smith")
def test_keep_emojis(self) -> None:
from nameparser.config import Constants
constants = Constants()
constants.regexes.emoji = False # type: ignore[assignment]
hn = HumanName("∫≜⩕ Smith😊", constants)
self.m(hn.first, "∫≜⩕", hn)
self.m(hn.last, "Smith😊", hn)
self.assertEqual(str(hn), "∫≜⩕ Smith😊")
# test cleanup
def test_remove_bidi_control_chars(self) -> None:
# LRM/RLM and friends ride along with copy-pasted names and stick to
# the parts they surround. Covers every character in the bidi set.
for mark in ("\u200e", "\u200f", "\u061c", "\u202a", "\u202b",
"\u202c", "\u202d", "\u202e", "\u2066", "\u2067",
"\u2068", "\u2069"):
hn = HumanName(mark + "John" + mark + " Smith")
self.m(hn.first, "John", hn)
self.m(hn.last, "Smith", hn)
def test_bidi_stripped_name_compares_equal(self) -> None:
# The reported symptom: an invisible RLM around an RTL name makes the
# parsed part fail equality against the clean string (issue #266).
hn = HumanName("\u200fمحمد بن سلمان\u200f")
self.assertEqual(hn.first, "محمد")
def test_keep_bidi_control_chars(self) -> None:
from nameparser.config import Constants
constants = Constants()
constants.regexes.bidi = False # type: ignore[assignment]
hn = HumanName("\u200fJohn\u200f Smith", constants)
self.m(hn.first, "\u200fJohn\u200f", hn)
self.m(hn.last, "Smith", hn)