-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextBuddyTest.java
More file actions
269 lines (218 loc) · 11.9 KB
/
Copy pathTextBuddyTest.java
File metadata and controls
269 lines (218 loc) · 11.9 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
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
package CE2;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Note that the testing is conducting following the hierarchy (Most independant to most dependant)
* This is to ensure that the most independant class will work as intended before testing classes
* that is dependant on the independant class.
*/
public class TextBuddyTest {
// Checks whether all kinds of valid commands return true
@Test
public void testDecipherValidCommand() {
Decipher decipherTest = new Decipher("add do homework by this wednesday");
assertEquals("add", decipherTest.getCommandType());
assertEquals("do homework by this wednesday", decipherTest.getDescription());
assertTrue(decipherTest.isGoodCommand());
decipherTest = new Decipher("add double spacing");
assertEquals("add", decipherTest.getCommandType());
assertEquals(" double spacing", decipherTest.getDescription());
assertTrue(decipherTest.isGoodCommand());
decipherTest = new Decipher("delete 3");
assertEquals("delete", decipherTest.getCommandType());
assertEquals("3", decipherTest.getDescription());
assertTrue(decipherTest.isGoodCommand());
decipherTest = new Decipher("clear");
assertEquals("clear", decipherTest.getCommandType());
assertTrue(decipherTest.isGoodCommand());
decipherTest = new Decipher("display");
assertEquals("display", decipherTest.getCommandType());
assertTrue(decipherTest.isGoodCommand());
decipherTest = new Decipher("exit");
assertEquals("exit", decipherTest.getCommandType());
assertTrue(decipherTest.isGoodCommand());
decipherTest = new Decipher("sort");
assertEquals("sort", decipherTest.getCommandType());
assertTrue(decipherTest.isGoodCommand());
decipherTest = new Decipher("search homework");
assertEquals("search", decipherTest.getCommandType());
assertEquals("homework", decipherTest.getDescription());
assertTrue(decipherTest.isGoodCommand());
decipherTest = new Decipher("search a1b2c3");
assertEquals("search", decipherTest.getCommandType());
assertEquals(" a1b2c3", decipherTest.getDescription());
assertTrue(decipherTest.isGoodCommand());
}
// Check all kinds of invalid commands return false
@Test
public void testDecipherInvalidCommand() {
Decipher decipherTest = new Decipher("hello 123");
assertEquals("hello", decipherTest.getCommandType());
assertEquals("123", decipherTest.getDescription());
assertFalse(decipherTest.isGoodCommand());
decipherTest = new Decipher("add");
assertEquals("add", decipherTest.getCommandType());
assertFalse(decipherTest.isGoodCommand());
decipherTest = new Decipher("delete");
assertEquals("delete", decipherTest.getCommandType());
assertFalse(decipherTest.isGoodCommand());
decipherTest = new Decipher("delete abc");
assertEquals("delete", decipherTest.getCommandType());
assertEquals("abc", decipherTest.getDescription());
assertFalse(decipherTest.isGoodCommand());
decipherTest = new Decipher("clear 1");
assertEquals("clear", decipherTest.getCommandType());
assertEquals("1", decipherTest.getDescription());
assertFalse(decipherTest.isGoodCommand());
decipherTest = new Decipher("display 4c");
assertEquals("display", decipherTest.getCommandType());
assertEquals("4c", decipherTest.getDescription());
assertFalse(decipherTest.isGoodCommand());
decipherTest = new Decipher("exit program");
assertEquals("exit", decipherTest.getCommandType());
assertEquals("program", decipherTest.getDescription());
assertFalse(decipherTest.isGoodCommand());
decipherTest = new Decipher(" add do homework");
assertEquals("", decipherTest.getCommandType());
assertEquals("add do homework", decipherTest.getDescription());
assertFalse(decipherTest.isGoodCommand());
decipherTest = new Decipher("Clear");
assertEquals("Clear", decipherTest.getCommandType());
assertFalse(decipherTest.isGoodCommand());
decipherTest = new Decipher("s0rt");
assertEquals("s0rt", decipherTest.getCommandType());
assertFalse(decipherTest.isGoodCommand());
decipherTest = new Decipher("sort all");
assertEquals("sort", decipherTest.getCommandType());
assertFalse(decipherTest.isGoodCommand());
decipherTest = new Decipher("search");
assertEquals("search", decipherTest.getCommandType());
assertFalse(decipherTest.isGoodCommand());
decipherTest = new Decipher("Search all");
assertEquals("Search", decipherTest.getCommandType());
assertFalse(decipherTest.isGoodCommand());
}
// Checks whether the formatting of the contents are handled properly even for ridiculous but valid commands
@Test
public void testStorage() {
Storage storageTest = new Storage("storageTest.txt");
storageTest.addLine("do homework by Wed");
assertEquals("1. do homework by Wed", storageTest.printLines());
storageTest.addLine("complete project today");
storageTest.addLine("3ldh5 sjk5h2");
assertEquals("1. do homework by Wed\n2. complete project today\n3. 3ldh5 sjk5h2", storageTest.printLines());
assertEquals("", storageTest.deleteLine("100"));
assertEquals("do homework by Wed", storageTest.deleteLine("1"));
assertEquals("3ldh5 sjk5h2", storageTest.deleteLine("2"));
assertEquals("1. complete project today", storageTest.printLines());
storageTest.clearLines();
assertEquals("", storageTest.printLines());
assertEquals("", storageTest.deleteLine("1"));
assertEquals("", storageTest.deleteLine("10"));
}
// Checks whether every valid command type will do what it is suppose to do and check its response
@Test
public void testLogic() {
Logic logicTest = new Logic("logicTest.txt");
assertEquals("added to logicTest.txt: \"do homework by Wed\"", logicTest.execute("add", "do homework by Wed"));
assertEquals("added to logicTest.txt: \"complete project\"", logicTest.execute("add", "complete project"));
assertEquals("deleted from logicTest.txt: \"do homework by Wed\"", logicTest.execute("delete", "1"));
assertEquals("Line number 2 does not exist", logicTest.execute("delete", "2"));
assertEquals("deleted from logicTest.txt: \"complete project\"", logicTest.execute("delete", "1"));
assertEquals("all content deleted from logicTest.txt", logicTest.execute("clear"));
assertEquals("logicTest.txt is empty", logicTest.execute("display"));
assertEquals("added to logicTest.txt: \"do homework by Wed\"", logicTest.execute("add", "do homework by Wed"));
assertEquals("added to logicTest.txt: \"complete project\"", logicTest.execute("add", "complete project"));
assertEquals("added to logicTest.txt: \"wamBaDada 2356\"", logicTest.execute("add", "wamBaDada 2356"));
assertEquals("1. do homework by Wed\n2. complete project\n3. wamBaDada 2356", logicTest.execute("display"));
assertEquals("Contents sorted!", logicTest.execute("sort"));
assertEquals("1. complete project\n2. do homework by Wed\n3. wamBaDada 2356", logicTest.execute("display"));
assertEquals("1. do homework by Wed", logicTest.execute("search", "homework"));
assertEquals("no lines containing the word: \"important\"", logicTest.execute("search", "important"));
assertEquals("all content deleted from logicTest.txt", logicTest.execute("clear"));
assertEquals("Goodbye!", logicTest.execute("exit"));
}
// Test all the invalid commands given by the user
@Test
public void testOrganiserInvalidCommand() {
Organiser organiserTest = new Organiser("organiserTest.txt");
assertEquals("Unknown command format", organiserTest.processCommand("add"));
assertEquals("Unknown command format", organiserTest.processCommand("delete a"));
assertEquals("Unknown command format", organiserTest.processCommand(" add text"));
assertEquals("Unknown command format", organiserTest.processCommand("display 2"));
assertEquals("Unknown command format", organiserTest.processCommand("exit program"));
assertEquals("Unknown command format", organiserTest.processCommand("Clear"));
assertEquals("Unknown command format", organiserTest.processCommand("123 add5"));
assertEquals("Unknown command format", organiserTest.processCommand("sort now"));
assertEquals("Unknown command format", organiserTest.processCommand("Sort"));
assertEquals("Unknown command format", organiserTest.processCommand("search"));
assertEquals("Unknown command format", organiserTest.processCommand("Search keyword"));
}
// Test all the valid commands and check its appropriate response to different commands
@Test
public void testOrganiserValidCommand() {
Organiser organiserTest = new Organiser("organiserTest.txt");
organiserTest.processCommand("clear");
assertEquals("Welcome to TextBuddy. organiserTest.txt is ready for use", organiserTest.showWelcomeMessage());
assertEquals("command: ", organiserTest.promptForCommand());
assertEquals("added to organiserTest.txt: \"do homework by Wed\"", organiserTest.processCommand("add do homework by Wed"));
assertEquals("added to organiserTest.txt: \"QwErTy\"", organiserTest.processCommand("add QwErTy"));
assertEquals("1. do homework by Wed\n2. QwErTy", organiserTest.processCommand("display"));
assertEquals("deleted from organiserTest.txt: \"QwErTy\"", organiserTest.processCommand("delete 2"));
assertEquals("Line number 2 does not exist", organiserTest.processCommand("delete 2"));
assertEquals("deleted from organiserTest.txt: \"do homework by Wed\"", organiserTest.processCommand("delete 1"));
assertEquals("organiserTest.txt is empty", organiserTest.processCommand("display"));
assertEquals("added to organiserTest.txt: \"a\"", organiserTest.processCommand("add a"));
assertEquals("added to organiserTest.txt: \"b\"", organiserTest.processCommand("add b"));
assertEquals("added to organiserTest.txt: \"c\"", organiserTest.processCommand("add c"));
assertEquals("1. a\n2. b\n3. c", organiserTest.processCommand("display"));
assertEquals("Contents sorted!", organiserTest.processCommand("sort"));
assertEquals("1. a\n2. b\n3. c", organiserTest.processCommand("display"));
assertEquals("1. c", organiserTest.processCommand("search c"));
assertEquals("no lines containing the word: \"z\"", organiserTest.processCommand("search z"));
assertEquals("all content deleted from organiserTest.txt", organiserTest.processCommand("clear"));
assertEquals("organiserTest.txt is empty", organiserTest.processCommand("display"));
}
// Check whether the sorting mechanism works by using "printLines" after sort
// Note that I did not test for "before sorting" because previous test cases had handled them
@Test
public void testSort() {
Storage storageTest = new Storage("sortTest.txt");
storageTest.clearLines();
storageTest.addLine("pear");
storageTest.addLine("apple");
storageTest.addLine("grapes");
storageTest.addLine("apricot");
storageTest.addLine("durian");
storageTest.sortContent();
assertEquals("1. apple\n2. apricot\n3. durian\n4. grapes\n5. pear", storageTest.printLines());
storageTest.clearLines();
storageTest.addLine("aab");
storageTest.addLine("aaa");
storageTest.addLine("aaaaaa");
storageTest.addLine("baaaaaa");
storageTest.addLine("ababa");
storageTest.sortContent();
assertEquals("1. aaa\n2. aaaaaa\n3. aab\n4. ababa\n5. baaaaaa", storageTest.printLines());
}
// Checks whether the search function works for keywords found infront, middle or back.
@Test
public void testSearch() {
Storage storageTest = new Storage("searchTest.txt");
storageTest.clearLines();
storageTest.addLine("do CS2103 project");
storageTest.addLine("buy groceries");
storageTest.addLine("finish CS2103 tutorial");
storageTest.addLine("complete CS2103 notes");
assertEquals("1. do CS2103 project\n2. finish CS2103 tutorial\n3. complete CS2103 notes", storageTest.searchFor("CS2103"));
storageTest.clearLines();
storageTest.addLine("aaa abcdef");
storageTest.addLine("abcdef aaa");
storageTest.addLine("123 bbb 123");
storageTest.addLine("123 bbb123");
assertEquals("1. aaa abcdef\n2. abcdef aaa", storageTest.searchFor("aaa"));
assertEquals("1. 123 bbb 123", storageTest.searchFor("bbb"));
storageTest.clearLines();
assertEquals("", storageTest.searchFor("something"));
}
}