forked from nus-cs2103-AY1718S2/addressbook-level2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParserTest.java
331 lines (286 loc) · 12.8 KB
/
ParserTest.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package seedu.addressbook.parser;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static seedu.addressbook.common.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.addressbook.common.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import seedu.addressbook.commands.AddCommand;
import seedu.addressbook.commands.ClearCommand;
import seedu.addressbook.commands.Command;
import seedu.addressbook.commands.DeleteCommand;
import seedu.addressbook.commands.ExitCommand;
import seedu.addressbook.commands.FindCommand;
import seedu.addressbook.commands.HelpCommand;
import seedu.addressbook.commands.IncorrectCommand;
import seedu.addressbook.commands.ListCommand;
import seedu.addressbook.commands.SortCommand;
import seedu.addressbook.commands.ViewAllCommand;
import seedu.addressbook.commands.ViewCommand;
import seedu.addressbook.data.exception.IllegalValueException;
import seedu.addressbook.data.person.Address;
import seedu.addressbook.data.person.Email;
import seedu.addressbook.data.person.Name;
import seedu.addressbook.data.person.Person;
import seedu.addressbook.data.person.Phone;
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.data.tag.Tag;
import seedu.addressbook.data.tag.UniqueTagList;
public class ParserTest {
private Parser parser;
@Before
public void setUp() {
parser = new Parser();
}
/*
* Note how the names of the test methods does not follow the normal naming convention.
* That is because our coding standard allows a different naming convention for test methods.
*/
@Test
public void parse_emptyInput_returnsIncorrect() {
final String[] emptyInputs = { "", " ", "\n \n" };
final String resultMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE);
parseAndAssertIncorrectWithMessage(resultMessage, emptyInputs);
}
@Test
public void parse_unknownCommandWord_returnsHelp() {
final String input = "unknowncommandword arguments arguments";
parseAndAssertCommandType(input, HelpCommand.class);
}
/*
* Tests for 0-argument commands =======================================================================
*/
@Test
public void parse_helpCommand_parsedCorrectly() {
final String input = "help";
parseAndAssertCommandType(input, HelpCommand.class);
}
@Test
public void parse_clearCommand_parsedCorrectly() {
final String input = "clear";
parseAndAssertCommandType(input, ClearCommand.class);
}
@Test
public void parse_listCommand_parsedCorrectly() {
final String input = "list";
parseAndAssertCommandType(input, ListCommand.class);
}
@Test
public void parse_sortCommand_parsedCorrectly() {
final String input = "sort";
parseAndAssertCommandType(input, SortCommand.class);
}
@Test
public void parse_exitCommand_parsedCorrectly() {
final String input = "exit";
parseAndAssertCommandType(input, ExitCommand.class);
}
/*
* Tests for ingle index argument commands ===============================================================
*/
@Test
public void parse_deleteCommandNoArgs_errorMessage() {
final String[] inputs = { "delete", "delete " };
final String resultMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE);
parseAndAssertIncorrectWithMessage(resultMessage, inputs);
}
@Test
public void parse_deleteCommandArgsIsNotSingleNumber_errorMessage() {
final String[] inputs = { "delete notAnumber ", "delete 8*wh12", "delete 1 2 3 4 5" };
final String resultMessage = MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;
parseAndAssertIncorrectWithMessage(resultMessage, inputs);
}
@Test
public void parse_deleteCommandNumericArg_indexParsedCorrectly() {
final int testIndex = 1;
final String input = "delete " + testIndex;
final DeleteCommand result = parseAndAssertCommandType(input, DeleteCommand.class);
assertEquals(result.getTargetIndex(), testIndex);
}
@Test
public void viewCommandNoArgs_errorMessage() {
final String[] inputs = { "view", "view " };
final String resultMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewCommand.MESSAGE_USAGE);
parseAndAssertIncorrectWithMessage(resultMessage, inputs);
}
@Test
public void parse_viewCommandArgsIsNotSingleNumber_errorMessage() {
final String[] inputs = { "view notAnumber ", "view 8*wh12", "view 1 2 3 4 5" };
final String resultMessage = MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;
parseAndAssertIncorrectWithMessage(resultMessage, inputs);
}
@Test
public void parse_viewCommandNumericArg_indexParsedCorrectly() {
final int testIndex = 2;
final String input = "view " + testIndex;
final ViewCommand result = parseAndAssertCommandType(input, ViewCommand.class);
assertEquals(result.getTargetIndex(), testIndex);
}
@Test
public void parse_viewAllCommandNoArgs_errorMessage() {
final String[] inputs = { "viewall", "viewall " };
final String resultMessage =
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewAllCommand.MESSAGE_USAGE);
parseAndAssertIncorrectWithMessage(resultMessage, inputs);
}
@Test
public void parse_viewAllCommandArgsIsNotSingleNumber_errorMessage() {
final String[] inputs = { "viewall notAnumber ", "viewall 8*wh12", "viewall 1 2 3 4 5" };
final String resultMessage = MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;
parseAndAssertIncorrectWithMessage(resultMessage, inputs);
}
@Test
public void parse_viewAllCommandNumericArg_indexParsedCorrectly() {
final int testIndex = 3;
final String input = "viewall " + testIndex;
final ViewAllCommand result = parseAndAssertCommandType(input, ViewAllCommand.class);
assertEquals(result.getTargetIndex(), testIndex);
}
/*
* Tests for find persons by keyword in name command ===================================================
*/
@Test
public void parse_findCommandInvalidArgs_errorMessage() {
// no keywords
final String[] inputs = {
"find",
"find "
};
final String resultMessage =
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE);
parseAndAssertIncorrectWithMessage(resultMessage, inputs);
}
@Test
public void parse_findCommandValidArgs_parsedCorrectly() {
final String[] keywords = { "key1", "key2", "key3" };
final Set<String> keySet = new HashSet<>(Arrays.asList(keywords));
final String input = "find " + String.join(" ", keySet);
final FindCommand result =
parseAndAssertCommandType(input, FindCommand.class);
assertEquals(keySet, result.getKeywords());
}
@Test
public void parse_findCommandDuplicateKeys_parsedCorrectly() {
final String[] keywords = { "key1", "key2", "key3" };
final Set<String> keySet = new HashSet<>(Arrays.asList(keywords));
// duplicate every keyword
final String input = "find " + String.join(" ", keySet) + " " + String.join(" ", keySet);
final FindCommand result =
parseAndAssertCommandType(input, FindCommand.class);
assertEquals(keySet, result.getKeywords());
}
/*
* Tests for add person command ==============================================================================
*/
@Test
public void parse_addCommandInvalidArgs_errorMessage() {
final String[] inputs = {
"add",
"add ",
"add wrong args format",
// no phone prefix
String.format("add $s $s e/$s a/$s", Name.EXAMPLE, Phone.EXAMPLE, Email.EXAMPLE, Address.EXAMPLE),
// no email prefix
String.format("add $s p/$s $s a/$s", Name.EXAMPLE, Phone.EXAMPLE, Email.EXAMPLE, Address.EXAMPLE),
// no address prefix
String.format("add $s p/$s e/$s $s", Name.EXAMPLE, Phone.EXAMPLE, Email.EXAMPLE, Address.EXAMPLE)
};
final String resultMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE);
parseAndAssertIncorrectWithMessage(resultMessage, inputs);
}
@Test
public void parse_addCommandInvalidPersonDataInArgs_errorMessge() {
final String invalidName = "[]\\[;]";
final String validName = Name.EXAMPLE;
final String invalidPhoneArg = "p/not__numbers";
final String validPhoneArg = "p/" + Phone.EXAMPLE;
final String invalidEmailArg = "e/notAnEmail123";
final String validEmailArg = "e/" + Email.EXAMPLE;
final String invalidTagArg = "t/invalid_-[.tag";
// address can be any string, so no invalid address
final String addCommandFormatString = "add $s $s $s a/" + Address.EXAMPLE;
// test each incorrect person data field argument individually
final String[] inputs = {
// invalid name
String.format(addCommandFormatString, invalidName, validPhoneArg, validEmailArg),
// invalid phone
String.format(addCommandFormatString, validName, invalidPhoneArg, validEmailArg),
// invalid email
String.format(addCommandFormatString, validName, validPhoneArg, invalidEmailArg),
// invalid tag
String.format(addCommandFormatString, validName, validPhoneArg, validEmailArg) + " " + invalidTagArg
};
for (String input : inputs) {
parseAndAssertCommandType(input, IncorrectCommand.class);
}
}
@Test
public void parse_addCommandValidPersonData_parsedCorrectly() {
final Person testPerson = generateTestPerson();
final String input = convertPersonToAddCommandString(testPerson);
final AddCommand result = parseAndAssertCommandType(input, AddCommand.class);
assertEquals(result.getPerson(), testPerson);
}
@Test
public void parse_addCommandDuplicateTags_merged() throws IllegalValueException {
final Person testPerson = generateTestPerson();
String input = convertPersonToAddCommandString(testPerson);
for (Tag tag : testPerson.getTags()) {
// create duplicates by doubling each tag
input += " t/" + tag.tagName;
}
final AddCommand result = parseAndAssertCommandType(input, AddCommand.class);
assertEquals(result.getPerson(), testPerson);
}
private static Person generateTestPerson() {
try {
return new Person(
new Name(Name.EXAMPLE),
new Phone(Phone.EXAMPLE, true),
new Email(Email.EXAMPLE, false),
new Address(Address.EXAMPLE, true),
new UniqueTagList(new Tag("tag1"), new Tag("tag2"), new Tag("tag3"))
);
} catch (IllegalValueException ive) {
throw new RuntimeException("test person data should be valid by definition");
}
}
private static String convertPersonToAddCommandString(ReadOnlyPerson person) {
String addCommand = "add "
+ person.getName().fullName
+ (person.getPhone().isPrivate() ? " pp/" : " p/") + person.getPhone().value
+ (person.getEmail().isPrivate() ? " pe/" : " e/") + person.getEmail().value
+ (person.getAddress().isPrivate() ? " pa/" : " a/") + person.getAddress().value;
for (Tag tag : person.getTags()) {
addCommand += " t/" + tag.tagName;
}
return addCommand;
}
/*
* Utility methods ====================================================================================
*/
/**
* Asserts that parsing the given inputs will return IncorrectCommand with the given feedback message.
*/
private void parseAndAssertIncorrectWithMessage(String feedbackMessage, String... inputs) {
for (String input : inputs) {
final IncorrectCommand result = parseAndAssertCommandType(input, IncorrectCommand.class);
assertEquals(result.feedbackToUser, feedbackMessage);
}
}
/**
* Parses input and asserts the class/type of the returned command object.
*
* @param input to be parsed
* @param expectedCommandClass expected class of returned command
* @return the parsed command object
*/
private <T extends Command> T parseAndAssertCommandType(String input, Class<T> expectedCommandClass) {
final Command result = parser.parseCommand(input);
assertTrue(result.getClass().isAssignableFrom(expectedCommandClass));
return (T) result;
}
}