Skip to content

Commit 7fa5301

Browse files
committed
#10 add unit tests, and (as it was expected) fix issues found whilst writing them
1 parent d4ba7c2 commit 7fa5301

File tree

3 files changed

+216
-13
lines changed

3 files changed

+216
-13
lines changed

src/main/java/com/tupilabs/human_name_parser/HumanNameParserBuilder.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package com.tupilabs.human_name_parser;
2525

26+
import java.util.ArrayList;
2627
import java.util.Arrays;
2728
import java.util.Collections;
2829
import java.util.List;
@@ -147,13 +148,15 @@ public HumanNameParserParser build() {
147148
if (this.suffixes == null) {
148149
this.suffixes = DEFAULT_SUFFIXES;
149150
}
150-
return new HumanNameParserParser(
151+
final HumanNameParserParser parser = new HumanNameParserParser(
151152
name,
152153
salutations,
153154
postnominals,
154155
prefixes,
155156
suffixes
156157
);
158+
parser.parse();
159+
return parser;
157160
}
158161

159162
// salutations
@@ -166,8 +169,8 @@ public HumanNameParserBuilder withSalutations(List<String> salutations) {
166169

167170
public HumanNameParserBuilder withExtraSalutations(List<String> salutations) {
168171
Objects.requireNonNull(salutations);
169-
this.salutations = DEFAULT_SALUTATIONS;
170-
this.salutations.addAll(salutations);
172+
this.salutations = new ArrayList<>(salutations);
173+
this.salutations.addAll(DEFAULT_SALUTATIONS);
171174
return this;
172175
}
173176

@@ -181,8 +184,8 @@ public HumanNameParserBuilder withPostnominals(List<String> postnominals) {
181184

182185
public HumanNameParserBuilder withExtraPostnominals(List<String> postnominals) {
183186
Objects.requireNonNull(postnominals);
184-
this.postnominals = DEFAULT_POSTNOMINALS;
185-
this.postnominals.addAll(postnominals);
187+
this.postnominals = new ArrayList<>(postnominals);
188+
this.postnominals.addAll(DEFAULT_POSTNOMINALS);
186189
return this;
187190
}
188191

@@ -196,8 +199,8 @@ public HumanNameParserBuilder withPrefixes(List<String> prefixes) {
196199

197200
public HumanNameParserBuilder withExtraPrefixes(List<String> prefixes) {
198201
Objects.requireNonNull(prefixes);
199-
this.prefixes = DEFAULT_PREFIXES;
200-
this.prefixes.addAll(prefixes);
202+
this.prefixes = new ArrayList<>(prefixes);
203+
this.prefixes.addAll(DEFAULT_PREFIXES);
201204
return this;
202205
}
203206

@@ -211,8 +214,8 @@ public HumanNameParserBuilder withSuffixes(List<String> suffixes) {
211214

212215
public HumanNameParserBuilder withExtraSuffixes(List<String> suffixes) {
213216
Objects.requireNonNull(suffixes);
214-
this.suffixes = DEFAULT_SUFFIXES;
215-
this.suffixes.addAll(suffixes);
217+
this.suffixes = new ArrayList<>(suffixes);
218+
this.suffixes.addAll(DEFAULT_SUFFIXES);
216219
return this;
217220
}
218221
}

src/main/java/com/tupilabs/human_name_parser/HumanNameParserParser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ public class HumanNameParserParser {
5252
private final Name name;
5353

5454
// other helpful values
55-
private List<String> salutations;
56-
private List<String> postnominals;
57-
private List<String> prefixes;
58-
private List<String> suffixes;
55+
List<String> salutations;
56+
List<String> postnominals;
57+
List<String> prefixes;
58+
List<String> suffixes;
5959

6060
// parsed values
6161
private String leadingInit;
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2010-2020 Jason Priem, Bruno P. Kinoshita
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package com.tupilabs.human_name_parser;
25+
26+
import static org.junit.Assert.assertEquals;
27+
import static org.junit.Assert.assertFalse;
28+
import static org.junit.Assert.assertTrue;
29+
30+
import java.util.Arrays;
31+
32+
import org.junit.Test;
33+
34+
/**
35+
* Tests for the {@code HumanNameParserBuilder}.
36+
* @author kinow
37+
*
38+
*/
39+
public class BuilderTest {
40+
41+
// suffixes
42+
43+
@Test
44+
public void testDefaultSuffixes() {
45+
HumanNameParserBuilder builder = new HumanNameParserBuilder("john");
46+
HumanNameParserParser parser = builder.build();
47+
assertTrue(parser.suffixes.contains("senior"));
48+
assertFalse(parser.suffixes.contains("mage"));
49+
}
50+
51+
@Test
52+
public void testReplacingSuffixes() {
53+
HumanNameParserBuilder builder = new HumanNameParserBuilder("john");
54+
HumanNameParserParser parser = builder.withSuffixes(Arrays.asList("mage")).build();
55+
assertFalse(parser.suffixes.contains("senior"));
56+
assertTrue(parser.suffixes.contains("mage"));
57+
}
58+
59+
@Test
60+
public void testExtraSuffixes() {
61+
HumanNameParserBuilder builder = new HumanNameParserBuilder("john");
62+
HumanNameParserParser parser = builder.withExtraSuffixes(Arrays.asList("mage")).build();
63+
assertTrue(parser.suffixes.contains("senior"));
64+
assertTrue(parser.suffixes.contains("mage"));
65+
}
66+
67+
// prefixes
68+
69+
@Test
70+
public void testDefaultPrefixes() {
71+
HumanNameParserBuilder builder = new HumanNameParserBuilder("john");
72+
HumanNameParserParser parser = builder.build();
73+
assertTrue(parser.prefixes.contains("de la"));
74+
}
75+
76+
@Test
77+
public void testReplacingPrefixes() {
78+
HumanNameParserBuilder builder = new HumanNameParserBuilder("john");
79+
HumanNameParserParser parser = builder.withPrefixes(Arrays.asList("sama")).build();
80+
assertFalse(parser.prefixes.contains("de la"));
81+
assertTrue(parser.prefixes.contains("sama"));
82+
}
83+
84+
@Test
85+
public void testExtraPrefixes() {
86+
HumanNameParserBuilder builder = new HumanNameParserBuilder("john");
87+
HumanNameParserParser parser = builder.withExtraPrefixes(Arrays.asList("sama")).build();
88+
assertTrue(parser.prefixes.contains("de la"));
89+
assertTrue(parser.prefixes.contains("sama"));
90+
}
91+
92+
// postnominals
93+
94+
@Test
95+
public void testDefaultPostnominals() {
96+
HumanNameParserBuilder builder = new HumanNameParserBuilder("john");
97+
HumanNameParserParser parser = builder.build();
98+
assertTrue(parser.postnominals.contains("phd"));
99+
}
100+
101+
@Test
102+
public void testReplacingPostnominals() {
103+
HumanNameParserBuilder builder = new HumanNameParserBuilder("john");
104+
// Au.D. is one of the examples from issue #10 on GitHub
105+
HumanNameParserParser parser = builder.withPostnominals(Arrays.asList("Au.D.")).build();
106+
assertFalse(parser.postnominals.contains("phd"));
107+
assertTrue(parser.postnominals.contains("Au.D."));
108+
}
109+
110+
@Test
111+
public void testExtraPostnominals() {
112+
HumanNameParserBuilder builder = new HumanNameParserBuilder("john");
113+
HumanNameParserParser parser = builder.withExtraPostnominals(Arrays.asList("Au.D.")).build();
114+
assertTrue(parser.postnominals.contains("phd"));
115+
assertTrue(parser.postnominals.contains("Au.D."));
116+
}
117+
118+
// salutations
119+
120+
@Test
121+
public void testDefaultSalutations() {
122+
HumanNameParserBuilder builder = new HumanNameParserBuilder("john");
123+
HumanNameParserParser parser = builder.build();
124+
assertTrue(parser.salutations.contains("judge"));
125+
}
126+
127+
@Test
128+
public void testReplacingSalutations() {
129+
HumanNameParserBuilder builder = new HumanNameParserBuilder("john");
130+
HumanNameParserParser parser = builder.withSalutations(Arrays.asList("sinho")).build();
131+
assertFalse(parser.salutations.contains("judge"));
132+
assertTrue(parser.salutations.contains("sinho"));
133+
}
134+
135+
@Test
136+
public void testExtraSalutations() {
137+
HumanNameParserBuilder builder = new HumanNameParserBuilder("john");
138+
HumanNameParserParser parser = builder.withExtraSalutations(Arrays.asList("sinho")).build();
139+
assertTrue(parser.salutations.contains("judge"));
140+
assertTrue(parser.salutations.contains("sinho"));
141+
}
142+
143+
// validations
144+
145+
@Test(expected = NullPointerException.class)
146+
public void testConstructorFailsWithNullString() {
147+
new HumanNameParserBuilder((String) null);
148+
}
149+
150+
@Test(expected = NullPointerException.class)
151+
public void testConstructorFailsWithNullName() {
152+
new HumanNameParserBuilder((Name) null);
153+
}
154+
155+
@Test(expected = NullPointerException.class)
156+
public void testFailsToBuildWithNullSalutations1() {
157+
new HumanNameParserBuilder("john").withSalutations(null).build();
158+
}
159+
160+
@Test(expected = NullPointerException.class)
161+
public void testFailsToBuildWithNullSalutations2() {
162+
new HumanNameParserBuilder("john").withExtraSalutations(null).build();
163+
}
164+
165+
@Test(expected = NullPointerException.class)
166+
public void testFailsToBuildWithNullPostnominals1() {
167+
new HumanNameParserBuilder("john").withPostnominals(null).build();
168+
}
169+
170+
@Test(expected = NullPointerException.class)
171+
public void testFailsToBuildWithNullPostnominals2() {
172+
new HumanNameParserBuilder("john").withExtraPostnominals(null).build();
173+
}
174+
175+
@Test(expected = NullPointerException.class)
176+
public void testFailsToBuildWithNullSuffixes1() {
177+
new HumanNameParserBuilder("john").withSuffixes(null).build();
178+
}
179+
180+
@Test(expected = NullPointerException.class)
181+
public void testFailsToBuildWithNullSuffixes2() {
182+
new HumanNameParserBuilder("john").withExtraSuffixes(null).build();
183+
}
184+
185+
@Test(expected = NullPointerException.class)
186+
public void testFailsToBuildWithNullPrefixes1() {
187+
new HumanNameParserBuilder("john").withPrefixes(null).build();
188+
}
189+
190+
@Test(expected = NullPointerException.class)
191+
public void testFailsToBuildWithNullPrefixes2() {
192+
new HumanNameParserBuilder("john").withExtraPrefixes(null).build();
193+
}
194+
195+
@Test
196+
public void testCreateWithStringOrName() {
197+
assertEquals("ramon", new HumanNameParserBuilder("don ramon valdez").withSalutations(Arrays.asList("don")).build().getFirst());
198+
assertEquals("ramon", new HumanNameParserBuilder(new Name("don ramon valdez")).withSalutations(Arrays.asList("don")).build().getFirst());
199+
}
200+
}

0 commit comments

Comments
 (0)