-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathname_splitter_spec.rb
278 lines (234 loc) · 9.53 KB
/
name_splitter_spec.rb
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
require 'spec_helper'
RSpec.describe NameSplitter::Splitter do
subject { NameSplitter::Splitter }
it "splits a name into first and last names" do
name = subject.new
name.name = "Jimmy Johnson"
expect(name.name).to eq("Jimmy Johnson")
expect(name.first_name).to eq("Jimmy")
expect(name.last_name).to eq("Johnson")
end
it "splits a name into first, middle, and last names" do
name = subject.new
name.name = "Jimmy Berger Johnson II"
expect(name.name).to eq("Jimmy Johnson, II")
expect(name.first_name).to eq("Jimmy")
expect(name.middle_name).to eq("Berger")
expect(name.last_name).to eq("Johnson")
expect(name.suffix).to eq("II")
end
it "splits a name into first, middle, and last names when last name includes a prefix" do
name = subject.new
name.name = "Jimmy Berger van Hinton III"
expect(name.name).to eq("Jimmy van Hinton, III")
expect(name.first_name).to eq("Jimmy")
expect(name.middle_name).to eq("Berger")
expect(name.last_name).to eq("van Hinton")
expect(name.suffix).to eq("III")
end
it "takes a full name with a prefix and suffix on new and provides access to first, middle, etc." do
name = subject.new("Jimmy Berger van Hinton III")
expect(name.name).to eq("Jimmy van Hinton, III")
expect(name.first_name).to eq("Jimmy")
expect(name.middle_name).to eq("Berger")
expect(name.last_name).to eq("van Hinton")
end
it "takes a full name with a prefix on new and provides access to first, middle, etc." do
name = subject.new("Maggie de Cuevas")
expect(name.name).to eq("Maggie de Cuevas")
expect(name.first_name).to eq("Maggie")
expect(name.middle_name).to eq("")
expect(name.last_name).to eq("de Cuevas")
end
it "takes a salutation and returns it along with the rest of the name" do
name = subject.new("Miss Floura Flanders")
expect(name.name).to eq("Floura Flanders")
expect(name.salutation).to eq("Miss")
expect(name.first_name).to eq("Floura")
expect(name.last_name).to eq("Flanders")
expect(name.middle_name).to eq("")
end
it "takes 2 salutations separated by & or 'and' and places them both in the salutation field" do
name = subject.new("Mr. and Mrs. Jimmy Flanders")
expect(name.salutation).to eq("Mr. and Mrs.")
expect(name.first_name).to eq("Jimmy")
expect(name.last_name).to eq("Flanders")
name = subject.new("Mr. & Mrs. Jimmy Flanders")
expect(name.salutation).to eq("Mr. & Mrs.")
expect(name.first_name).to eq("Jimmy")
expect(name.last_name).to eq("Flanders")
name = subject.new("Mr. & Mrs. Jimmy and Beth Flanders")
expect(name.salutation).to eq("Mr. & Mrs.")
expect(name.first_name).to eq("Jimmy and Beth")
expect(name.last_name).to eq("Flanders")
end
it "allows salutations to include punctuation" do
name = subject.new("Mr. Tom Peterson")
expect(name.salutation).to eq("Mr.")
end
it "takes on MD as a suffix" do
name = subject.new("John Farmer, Md")
expect(name.name).to eq("John Farmer, Md")
expect(name.last_name).to eq("Farmer")
expect(name.suffix).to eq("Md")
expect(name.salutation).to eq("")
end
it "takes a suffix with punctuation" do
name = subject.new("Same Barker, Ph.D.")
expect(name.last_name).to eq("Barker")
expect(name.suffix).to eq("Ph.D.")
end
it "handles a name with salutation, prefix, and suffix" do
name = subject.new("Mr. Fred el Greco, III")
expect(name.last_name).to eq("el Greco")
expect(name.name).to eq("Fred el Greco, III")
end
it "handles a name where the first name consists of two names" do
name = subject.new("Ms. Mary Beth Farmer")
expect(name.last_name).to eq("Farmer")
expect(name.first_name).to eq("Mary Beth")
end
it "handles just a first name that is in two parts" do
name = subject.new("Mary Catherine")
expect(name.first_name).to eq("Mary Catherine")
name = subject.new("Mary Beth")
expect(name.first_name).to eq("Mary Beth")
end
it "handles being passed just a single name" do
name = subject.new("Jimmy")
expect(name.first_name).to eq("Jimmy")
end
it "finds the last name if the name consists of a salutation and one other name" do
name = subject.new("Mr. Martin")
expect(name.last_name).to eq("Martin")
expect(name.name).to eq("Mr. Martin")
end
it "places both first names in the first name field if they are separated by an 'and'" do
name = subject.new("Janet and Tom Smith")
expect(name.last_name).to eq("Smith")
expect(name.first_name).to eq("Janet and Tom")
end
it "recognizes a suffix even if it has multiple punctuation marks" do
name = subject.new("Jim Smith M.D.")
expect(name.last_name).to eq("Smith")
expect(name.first_name).to eq("Jim")
expect(name.suffix).to eq("M.D.")
end
it "does not blow up if no parameter is included on new and attributes are accessed" do
name = subject.new
expect(name.name).to eq("")
expect(name.first_name).to eq("")
expect(name.middle_name).to eq("")
expect(name.last_name).to eq("")
end
it "properly places last name when the name has only salutation and last name for Mr. & Mrs. Stoll" do
name = subject.new("Mr. & Mrs. Stoll")
expect(name.last_name).to eq("Stoll")
end
it "puts multiple names in the first name field if they are anded" do
name = subject.new("Mr. & Mrs. Jimmy Smith and Beth Flanders")
expect(name.salutation).to eq("Mr. & Mrs.")
expect(name.first_name).to eq("Jimmy Smith and Beth")
expect(name.last_name).to eq("Flanders")
end
shared_examples_for "last name first separated by comma" do
context "and nothing other than the first name is provided" do
let(:full_name) { "Smith, Jim" }
it "correctly places the names in the first and last name fields" do
expect(split_name.last_name).to eq("Smith")
expect(split_name.first_name).to eq("Jim")
end
end
context "and a middle name is provided" do
let(:full_name) { "Smith, Jim C."}
it "correctly places the names in the first and last name fields" do
expect(split_name.last_name).to eq("Smith")
expect(split_name.first_name).to eq("Jim")
expect(split_name.middle_name).to eq("C.")
end
end
context "and a second person is provided along with a suffix" do
let(:full_name) { "Bedard, Sheryl A. & Louis J. Jr."}
it "correctly places the names in the first and last name fields" do
expect(split_name.last_name).to eq("Bedard")
expect(split_name.first_name).to eq("Sheryl A. & Louis J.")
expect(split_name.suffix).to eq("Jr.")
end
end
context "and multiple middle names are provided" do
let(:full_name) { "Bedard, John Samuel Benning"}
it "correctly places the names in the first and last name fields, etc" do
expect(split_name.last_name).to eq("Bedard")
expect(split_name.first_name).to eq("John")
expect(split_name.middle_name).to eq("Samuel Benning")
end
end
end
context 'when the last name is listed first with a comma' do
context "and no format is provided" do
let(:split_name) { described_class.new(full_name) }
it_behaves_like "last name first separated by comma"
context "and no space separating the comma and the first name" do
let(:full_name) { "Smith,Jim"}
it "does not quite work properly, as we expect to split on a space" do
expect(split_name.first_name).to eq("Smith,Jim")
expect(split_name.last_name).to eq("")
end
end
end
context "and the last_first format option is provided" do
let(:split_name) { described_class.new(full_name, format: NameSplitter::Splitter::LAST_COMMA_FIRST_FORMAT) }
it_behaves_like "last name first separated by comma"
context "and no space separating the comma and the first name" do
let(:full_name) { "Smith,Jim"}
it "works properly and places the names in the correct fields" do
expect(split_name.first_name).to eq("Jim")
expect(split_name.last_name).to eq("Smith")
end
end
end
context 'and also includes salutations' do
it "correctly places the names in the first and last name and salutation fields" do
name = subject.new("Smith, Mr. Jim")
expect(name.last_name).to eq("Smith")
expect(name.first_name).to eq("Jim")
expect(name.salutation).to eq("Mr.")
name = subject.new("Smith, Mr. & Mrs. Jim")
expect(name.last_name).to eq("Smith")
expect(name.first_name).to eq("Jim")
expect(name.salutation).to eq("Mr. & Mrs.")
name = subject.new("Smith, Mr. & Mrs. Jim & Janet")
expect(name.last_name).to eq("Smith")
expect(name.first_name).to eq("Jim & Janet")
expect(name.salutation).to eq("Mr. & Mrs.")
end
end
end
it 'handles space' do
name = subject.new
name.name = " "
expect(name.first_name).to eq("")
expect(name.middle_name).to eq("")
expect(name.last_name).to eq("")
end
it 'handles empty string' do
name = subject.new
name.name = ""
expect(name.first_name).to eq("")
expect(name.middle_name).to eq("")
expect(name.last_name).to eq("")
end
it 'handles nil' do
name = subject.new
name.name = nil
expect(name.first_name).to eq("")
expect(name.middle_name).to eq("")
expect(name.last_name).to eq("")
end
describe ".call" do
it "calls new with the passed parameter" do
expect(NameSplitter::Splitter).to receive(:new).with("Joan of Arc")
NameSplitter::Splitter.call("Joan of Arc")
end
end
end