Skip to content

Commit

Permalink
Update test case results
Browse files Browse the repository at this point in the history
  • Loading branch information
yooplo committed Oct 10, 2024
1 parent 46aeb80 commit b71dbee
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 deletions.
38 changes: 9 additions & 29 deletions src/main/java/seedu/address/logic/parser/FindCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,56 +19,36 @@
public class FindCommandParser implements Parser<AbstractFindCommand> {

public static final Pattern KEYWORD_EXTRACTOR =
Pattern.compile("^(?<tag>/[cen])");

Pattern.compile("^(?<tag>/[cen])\\s*(?<arguments>[\\S\\s]+)$");
/**
* Parses the given {@code String} of arguments in the context of the FindCommand
* and returns a FindCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public AbstractFindCommand parse(String args) throws ParseException {
String trimmedArgs = args.trim();
String[] tagAndSearchTerms = getTagAndSearchTerms(trimmedArgs);
String tag = tagAndSearchTerms[0];
String searchTerms = tagAndSearchTerms[1];
Matcher m = KEYWORD_EXTRACTOR.matcher(tag); // check if the correct tag exists

String trimmedArgs = args.trim(); // trim space
Matcher m = KEYWORD_EXTRACTOR.matcher(trimmedArgs); // find tag and search words
// will throw exception if no args/command format not correct
if (args.isEmpty() || !m.matches()) {
if (trimmedArgs.isEmpty() || !m.matches()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AbstractFindCommand.MESSAGE_USAGE));
}

// extract tag and search argument
String tag = m.group("tag");
String searchTerms = m.group("arguments");
String[] searchTermArray = searchTerms.split("\\s+");

// return appropriate FindCommand class and specific warning message to user based on the tag
// return approppriate FindCommand class depending on tag
// return appropriate FindCommand class depending on tag
switch (tag) {
case "/n":
if (searchTerms.isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindByNameCommand.MESSAGE_USAGE));
}
return new FindByNameCommand(
new NameContainsKeywordsPredicate(Arrays.asList(searchTermArray)));
case "/e":
if (searchTerms.isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindByEmailCommand.MESSAGE_USAGE));
}
return new FindByEmailCommand(
new EmailContainsKeywordsPredicate(Arrays.asList(searchTermArray)));

Check warning on line 49 in src/main/java/seedu/address/logic/parser/FindCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/FindCommandParser.java#L48-L49

Added lines #L48 - L49 were not covered by tests
default:
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AbstractFindCommand.MESSAGE_USAGE));
return null; // temporary value, this should not occur due to regex
}
}
private String[] getTagAndSearchTerms(String input) {
int index = input.indexOf(' ');
String[] result = new String[2];
// Check if there is a whitespace character in the string
result[0] = index != -1 ? input.substring(0, index) : input; // Part before the first whitespace
result[1] = index != -1 ? input.substring(index + 1) : ""; // Part after the first whitespace
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,17 @@
"email" : "[email protected]",
"address" : "4th street",
"tags" : [ ]
}, {
"name" : "Reg Loo",
"phone" : "8482424",
"email" : "[email protected]",
"address" : "little india",
"tags" : [ ]
}, {
"name" : "Ina Crust",
"phone" : "8482131",
"email" : "[email protected]",
"address" : "chicago ave",
"tags" : [ ]
} ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void parseCommand_exit() throws Exception {
public void parseCommand_findByName() throws Exception {
List<String> keywords = Arrays.asList("foo", "bar", "baz");
FindByNameCommand command = (FindByNameCommand) parser.parseCommand(
FindByNameCommand.COMMAND_WORD + " /n "
FindByNameCommand.COMMAND_WORD + " "
+ keywords.stream().collect(Collectors.joining(" ")));
assertEquals(new FindByNameCommand(new NameContainsKeywordsPredicate(keywords)), command);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.AbstractFindCommand;
import seedu.address.logic.commands.FindByNameCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.model.person.NameContainsKeywordsPredicate;

public class FindCommandParserTest {

private FindCommandParser parser = new FindCommandParser();
private final FindCommandParser parser = new FindCommandParser();

@Test
public void parse_emptyArg_throwsParseException() {
assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
assertParseFailure(parser, " ",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AbstractFindCommand.MESSAGE_USAGE));
}

@Test
Expand Down

0 comments on commit b71dbee

Please sign in to comment.