This repository has been archived by the owner on Mar 5, 2023. It is now read-only.
/
StringUtilTest.java
143 lines (111 loc) · 5.53 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
package seedu.address.commons.util;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.testutil.Assert.assertThrows;
import java.io.FileNotFoundException;
import org.junit.jupiter.api.Test;
public class StringUtilTest {
//---------------- Tests for isNonZeroUnsignedInteger --------------------------------------
@Test
public void isNonZeroUnsignedInteger() {
// EP: empty strings
assertFalse(StringUtil.isNonZeroUnsignedInteger("")); // Boundary value
assertFalse(StringUtil.isNonZeroUnsignedInteger(" "));
// EP: not a number
assertFalse(StringUtil.isNonZeroUnsignedInteger("a"));
assertFalse(StringUtil.isNonZeroUnsignedInteger("aaa"));
// EP: zero
assertFalse(StringUtil.isNonZeroUnsignedInteger("0"));
// EP: zero as prefix
assertTrue(StringUtil.isNonZeroUnsignedInteger("01"));
// EP: signed numbers
assertFalse(StringUtil.isNonZeroUnsignedInteger("-1"));
assertFalse(StringUtil.isNonZeroUnsignedInteger("+1"));
// EP: numbers with white space
assertFalse(StringUtil.isNonZeroUnsignedInteger(" 10 ")); // Leading/trailing spaces
assertFalse(StringUtil.isNonZeroUnsignedInteger("1 0")); // Spaces in the middle
// EP: number larger than Integer.MAX_VALUE
assertFalse(StringUtil.isNonZeroUnsignedInteger(Long.toString(Integer.MAX_VALUE + 1)));
// EP: valid numbers, should return true
assertTrue(StringUtil.isNonZeroUnsignedInteger("1")); // Boundary value
assertTrue(StringUtil.isNonZeroUnsignedInteger("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_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> StringUtil.containsWordIgnoreCase("typical sentence", null));
}
@Test
public void containsWordIgnoreCase_emptyWord_throwsIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, "Word parameter cannot be empty", ()
-> StringUtil.containsWordIgnoreCase("typical sentence", " "));
}
@Test
public void containsWordIgnoreCase_multipleWords_throwsIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, "Word parameter should be a single word", ()
-> StringUtil.containsWordIgnoreCase("typical sentence", "aaa BBB"));
}
@Test
public void containsWordIgnoreCase_nullSentence_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> StringUtil.containsWordIgnoreCase(null, "abc"));
}
/*
* 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() {
assertTrue(StringUtil.getDetails(new FileNotFoundException("file not found"))
.contains("java.io.FileNotFoundException: file not found"));
}
@Test
public void getDetails_nullGiven_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> StringUtil.getDetails(null));
}
}