Skip to content

Commit 3b348b4

Browse files
authored
Merge pull request nus-cs2103-AY2021S1#107 from jerrylchong/branch-Find-Fix
Fix FindCommand issues and tests
2 parents fe674a9 + 883ed01 commit 3b348b4

File tree

3 files changed

+83
-22
lines changed

3 files changed

+83
-22
lines changed

src/main/java/seedu/address/logic/parser/FindCommandParser.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import seedu.address.logic.commands.FindCommand;
1717
import seedu.address.logic.parser.exceptions.ParseException;
18+
import seedu.address.model.person.NameContainsKeywordsPredicate;
1819
import seedu.address.model.person.PersonHasTagsAndNamePredicate;
1920
import seedu.address.model.person.PersonHasTagsPredicate;
2021
import seedu.address.model.tag.Tag;
@@ -37,12 +38,18 @@ public FindCommand parse(String args) throws ParseException {
3738
if (arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_TAG)) {
3839
Set<String> nameSet = ParserUtil.parseAllNames(argMultimap.getAllValues(PREFIX_NAME));
3940
Set<Tag> tagSet = parseTagsForFind(argMultimap.getAllValues(PREFIX_TAG)).orElse(new HashSet<>());
41+
if ((nameSet.size() == 1 && nameSet.contains("")) || tagSet.isEmpty()) {
42+
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
43+
}
4044
return new FindCommand(
4145
new PersonHasTagsAndNamePredicate(new ArrayList<>(nameSet), new ArrayList<>(tagSet)));
4246
} else if (arePrefixesPresent(argMultimap, PREFIX_NAME)) {
4347
Set<String> nameSet = ParserUtil.parseAllNames(argMultimap.getAllValues(PREFIX_NAME));
48+
if (nameSet.size() == 1 && nameSet.contains("")) {
49+
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
50+
}
4451
return new FindCommand(
45-
new PersonHasTagsAndNamePredicate(new ArrayList<>(nameSet), new ArrayList<>()));
52+
new NameContainsKeywordsPredicate(new ArrayList<>(nameSet)));
4653
} else if (arePrefixesPresent(argMultimap, PREFIX_TAG)) {
4754
Set<Tag> tagSet = parseTagsForFind(argMultimap.getAllValues(PREFIX_TAG)).orElse(new HashSet<>());
4855
return new FindCommand(

src/main/java/seedu/address/logic/parser/ParserUtil.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ public static Name parseName(String name) throws ParseException {
5757
*/
5858
public static Set<String> parseAllNames(Collection<String> names) throws ParseException {
5959
requireNonNull(names);
60-
final Set<String> nameSet = new HashSet<>();
61-
for (String name : names) {
62-
nameSet.add(name);
63-
}
60+
Set<String> nameSet = new HashSet<>(names);
6461
return nameSet;
6562
}
6663

src/test/java/seedu/address/logic/parser/FindCommandParserTest.java

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;
66

77
import java.util.ArrayList;
8+
import java.util.HashSet;
9+
import java.util.Set;
810

911
import org.junit.jupiter.api.Test;
1012

1113
import seedu.address.logic.commands.FindCommand;
14+
import seedu.address.model.person.NameContainsKeywordsPredicate;
1215
import seedu.address.model.person.PersonHasTagsAndNamePredicate;
16+
import seedu.address.model.person.PersonHasTagsPredicate;
1317
import seedu.address.model.tag.Tag;
1418

1519
public class FindCommandParserTest {
@@ -18,7 +22,24 @@ public class FindCommandParserTest {
1822

1923
@Test
2024
public void parse_emptyArg_throwsParseException() {
21-
assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
25+
// no arguments
26+
assertParseFailure(parser, " ",
27+
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
28+
// name prefix but no name argument
29+
assertParseFailure(parser, " n/",
30+
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
31+
// tag prefix but no tag argument
32+
assertParseFailure(parser, "t/",
33+
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
34+
// name and tag prefix but no name argument
35+
assertParseFailure(parser, " n/ t/tag",
36+
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
37+
// name and tag prefix but no tag argument
38+
assertParseFailure(parser, " n/name t/",
39+
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
40+
// name and tag prefix but no arguments
41+
assertParseFailure(parser, " n/ t/",
42+
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
2243
}
2344

2445
@Test
@@ -36,29 +57,65 @@ public void parse_validArgs_returnsFindCommand() {
3657
assertParseSuccess(parser, " \n n/Alice \n t/tag\t", expectedFindCommand);
3758
}
3859

39-
//TODO: Fix this test case
40-
// @Test
41-
// public void parse_multipleNames_returnsFindCommand() {
42-
// // no leading and trailing whitespaces
43-
// ArrayList<String> nameList = new ArrayList<>();
44-
// nameList.add("Alice");
45-
// nameList.add("Bob");
46-
// nameList.add("Candy");
47-
// ArrayList<Tag> tagList = new ArrayList<>();
48-
// tagList.add(new Tag("tag"));
49-
// FindCommand expectedFindCommand =
50-
// new FindCommand(new PersonHasTagsAndNamePredicate(nameList, tagList));
51-
// assertParseSuccess(parser, " n/Alice n/Bob n/Candy t/tag", expectedFindCommand);
52-
// }
60+
@Test
61+
public void parse_multipleNamesAndMultipleTags_returnsFindCommand() {
62+
// no leading and trailing whitespaces
63+
Set<String> nameSet = new HashSet<>();
64+
nameSet.add("Alice");
65+
nameSet.add("Bob");
66+
nameSet.add("Candy");
67+
ArrayList<String> nameList = new ArrayList<>(nameSet);
68+
Set<Tag> tagSet = new HashSet<>();
69+
tagSet.add(new Tag("tag"));
70+
tagSet.add(new Tag("person"));
71+
ArrayList<Tag> tagList = new ArrayList<>(tagSet);
72+
FindCommand expectedFindCommand =
73+
new FindCommand(new PersonHasTagsAndNamePredicate(nameList, tagList));
74+
assertParseSuccess(parser, " n/Alice n/Bob n/Candy t/tag t/person", expectedFindCommand);
75+
}
5376

5477
@Test
5578
public void parse_singleNameAndNoTag_returnsFindCommand() {
5679
// no leading and trailing whitespaces
5780
ArrayList<String> nameList = new ArrayList<>();
5881
nameList.add("Alice");
59-
ArrayList<Tag> tagList = new ArrayList<>();
6082
FindCommand expectedFindCommand =
61-
new FindCommand(new PersonHasTagsAndNamePredicate(nameList, tagList));
83+
new FindCommand(new NameContainsKeywordsPredicate(nameList));
6284
assertParseSuccess(parser, " n/Alice ", expectedFindCommand);
6385
}
86+
87+
@Test
88+
public void parse_multipleNamesAndNoTag_returnsFindCommand() {
89+
// no leading and trailing whitespaces
90+
Set<String> nameSet = new HashSet<>();
91+
nameSet.add("Alice");
92+
nameSet.add("Bob");
93+
nameSet.add("Candy");
94+
ArrayList<String> nameList = new ArrayList<>(nameSet);
95+
FindCommand expectedFindCommand =
96+
new FindCommand(new NameContainsKeywordsPredicate(nameList));
97+
assertParseSuccess(parser, " n/Alice n/Bob n/Candy", expectedFindCommand);
98+
}
99+
100+
@Test
101+
public void parse_noNameAndSingleTag_returnsFindCommand() {
102+
// no leading and trailing whitespaces
103+
ArrayList<Tag> tagList = new ArrayList<>();
104+
tagList.add(new Tag("tag"));
105+
FindCommand expectedFindCommand =
106+
new FindCommand(new PersonHasTagsPredicate(tagList));
107+
assertParseSuccess(parser, " t/tag ", expectedFindCommand);
108+
}
109+
110+
@Test
111+
public void parse_noNameAndMultipleTags_returnsFindCommand() {
112+
// no leading and trailing whitespaces
113+
Set<Tag> tagSet = new HashSet<>();
114+
tagSet.add(new Tag("tag"));
115+
tagSet.add(new Tag("person"));
116+
ArrayList<Tag> tagList = new ArrayList<>(tagSet);
117+
FindCommand expectedFindCommand =
118+
new FindCommand(new PersonHasTagsPredicate(tagList));
119+
assertParseSuccess(parser, " t/tag t/person ", expectedFindCommand);
120+
}
64121
}

0 commit comments

Comments
 (0)