forked from se-edu/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 109
/
StringUtilTest.java
152 lines (118 loc) · 5.43 KB
/
StringUtilTest.java
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
package seedu.address.commons.util;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.io.FileNotFoundException;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class StringUtilTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
//---------------- Tests for isUnsignedPositiveInteger --------------------------------------
@Test
public void isUnsignedPositiveInteger() {
// Equivalence partition: null
assertFalse(StringUtil.isUnsignedInteger(null));
// EP: empty strings
assertFalse(StringUtil.isUnsignedInteger("")); // Boundary value
assertFalse(StringUtil.isUnsignedInteger(" "));
// EP: not a number
assertFalse(StringUtil.isUnsignedInteger("a"));
assertFalse(StringUtil.isUnsignedInteger("aaa"));
// EP: zero
assertFalse(StringUtil.isUnsignedInteger("0"));
// EP: signed numbers
assertFalse(StringUtil.isUnsignedInteger("-1"));
assertFalse(StringUtil.isUnsignedInteger("+1"));
// EP: numbers with white space
assertFalse(StringUtil.isUnsignedInteger(" 10 ")); // Leading/trailing spaces
assertFalse(StringUtil.isUnsignedInteger("1 0")); // Spaces in the middle
// EP: valid numbers, should return true
assertTrue(StringUtil.isUnsignedInteger("1")); // Boundary value
assertTrue(StringUtil.isUnsignedInteger("10"));
}
//---------------- Tests for containsWordIgnoreCase --------------------------------------
/*
* Invalid equivalence partitions for word: null, empty, multiple words
* Invalid equivalence partitions for sentence: null
* The four test cases below test one invalid input at a time.
*/
@Test
public void containsWordIgnoreCase_nullWord_exceptionThrown(){
assertExceptionThrown("typical sentence", null, "Word parameter cannot be null");
}
private void assertExceptionThrown(String sentence, String word, String errorMessage) {
thrown.expect(AssertionError.class);
thrown.expectMessage(errorMessage);
StringUtil.containsWordIgnoreCase(sentence, word);
}
@Test
public void containsWordIgnoreCase_emptyWord_exceptionThrown(){
assertExceptionThrown("typical sentence", " ", "Word parameter cannot be empty");
}
@Test
public void containsWordIgnoreCase_multipleWords_exceptionThrown(){
assertExceptionThrown("typical sentence", "aaa BBB", "Word parameter should be a single word");
}
@Test
public void containsWordIgnoreCase_nullSentence_exceptionThrown(){
assertExceptionThrown(null, "abc", "Sentence parameter cannot be null");
}
/*
* Valid equivalence partitions for word:
* - any word
* - word containing symbols/numbers
* - word with leading/trailing spaces
*
* Valid equivalence partitions for sentence:
* - empty string
* - one word
* - multiple words
* - sentence with extra spaces
*
* Possible scenarios returning true:
* - matches first word in sentence
* - last word in sentence
* - middle word in sentence
* - matches multiple words
*
* Possible scenarios returning false:
* - query word matches part of a sentence word
* - sentence word matches part of the query word
*
* The test method below tries to verify all above with a reasonably low number of test cases.
*/
@Test
public void containsWordIgnoreCase_validInputs_correctResult(){
// Empty sentence
assertFalse(StringUtil.containsWordIgnoreCase("", "abc")); // Boundary case
assertFalse(StringUtil.containsWordIgnoreCase(" ", "123"));
// Matches a partial word only
assertFalse(StringUtil.containsWordIgnoreCase("aaa bbb ccc", "bb")); // Sentence word bigger than query word
assertFalse(StringUtil.containsWordIgnoreCase("aaa bbb ccc", "bbbb")); // Query word bigger than sentence word
// Matches word in the sentence, different upper/lower case letters
assertTrue(StringUtil.containsWordIgnoreCase("aaa bBb ccc", "Bbb")); // First word (boundary case)
assertTrue(StringUtil.containsWordIgnoreCase("aaa bBb ccc@1", "CCc@1")); // Last word (boundary case)
assertTrue(StringUtil.containsWordIgnoreCase(" AAA bBb ccc ", "aaa")); // Sentence has extra spaces
assertTrue(StringUtil.containsWordIgnoreCase("Aaa", "aaa")); // Only one word in sentence (boundary case)
assertTrue(StringUtil.containsWordIgnoreCase("aaa bbb ccc", " ccc ")); // Leading/trailing spaces
// Matches multiple words in sentence
assertTrue(StringUtil.containsWordIgnoreCase("AAA bBb ccc bbb", "bbB"));
}
//---------------- Tests for getDetails --------------------------------------
/*
* Equivalence Partitions: null, valid throwable object
*/
@Test
public void getDetails_exceptionGiven(){
assertThat(StringUtil.getDetails(new FileNotFoundException("file not found")),
containsString("java.io.FileNotFoundException: file not found"));
}
@Test
public void getDetails_nullGiven_assertionError(){
thrown.expect(AssertionError.class);
StringUtil.getDetails(null);
}
}