Skip to content

Commit

Permalink
Fix FindCommand bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jingting1412 committed Nov 6, 2023
1 parent 2b844b9 commit 896bb4d
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/main/java/seedu/staffsnap/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,21 @@ public class FindCommand extends Command {
+ "Expected outcome: Returns a list of applicants whose name contains any of the words "
+ "\"alice\", \"bob\", or \"charlie\"";

public static final String MESSAGE_WRONG_FORMAT = "Keyword(s) must be alphabetical!"
+ "\nExample: LEE, lee, Johnny Haw";

private final NameContainsKeywordsPredicate predicate;

public FindCommand(NameContainsKeywordsPredicate predicate) {
this.predicate = predicate;
}

/**
* Executes the find command.
* @param model {@code Model} which the command should operate on.
* @return CommandResult which contains the list of applicants
* whose name contains any of the keywords given.
*/
@Override
public CommandResult execute(Model model) {
requireNonNull(model);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.staffsnap.logic.parser;

import static seedu.staffsnap.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.staffsnap.logic.commands.FindCommand.MESSAGE_WRONG_FORMAT;

import java.util.Arrays;

Expand All @@ -16,13 +17,17 @@ public class FindCommandParser implements Parser<FindCommand> {
/**
* 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
* @throws ParseException if the user input is empty
* or if the user input contains characters other than alphabets.
*/
public FindCommand parse(String args) throws ParseException {
String trimmedArgs = args.trim();
if (trimmedArgs.isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
} else if (!trimmedArgs.matches("^[a-zA-Z]*$")) {
throw new ParseException((
String.format(MESSAGE_WRONG_FORMAT, FindCommand.MESSAGE_USAGE)));
}

String[] nameKeywords = trimmedArgs.split("\\s+");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class FindCommandTest {
private Model model = new ModelManager(getTypicalApplicantBook(), new UserPrefs());
private Model expectedModel = new ModelManager(getTypicalApplicantBook(), new UserPrefs());


@Test
public void equals() {
NameContainsKeywordsPredicate firstPredicate =
Expand Down Expand Up @@ -61,7 +62,57 @@ public void execute_zeroKeywords_noApplicantFound() {
String expectedMessage = String.format(MESSAGE_APPLICANTS_LISTED_OVERVIEW, 0);
NameContainsKeywordsPredicate predicate = preparePredicate(" ");
FindCommand command = new FindCommand(predicate);

expectedModel.updateFilteredApplicantList(predicate);

assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getFilteredApplicantList());
}

@Test
public void execute_singleKeyword_noApplicantFound() {
String expectedMessage = String.format(MESSAGE_APPLICANTS_LISTED_OVERVIEW, 0);
NameContainsKeywordsPredicate predicate = preparePredicate("testing");
FindCommand command = new FindCommand(predicate);

expectedModel.updateFilteredApplicantList(predicate);

assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getFilteredApplicantList());
}

@Test
public void execute_singleCompleteKeyword_singleApplicantFound() {
String expectedMessage = String.format(MESSAGE_APPLICANTS_LISTED_OVERVIEW, 1);
NameContainsKeywordsPredicate predicate = preparePredicate("fiona");
FindCommand command = new FindCommand(predicate);

expectedModel.updateFilteredApplicantList(predicate);

assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(FIONA), model.getFilteredApplicantList());
}

@Test
public void execute_singleCompleteKeyword_multipleApplicantsFound() {
String expectedMessage = String.format(MESSAGE_APPLICANTS_LISTED_OVERVIEW, 2);
NameContainsKeywordsPredicate predicate = preparePredicate("Meier");
FindCommand command = new FindCommand(predicate);

expectedModel.updateFilteredApplicantList(predicate);

assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(BENSON, DANIEL), model.getFilteredApplicantList());
}

@Test
public void execute_multipleKeywords_noApplicantFound() {
String expectedMessage = String.format(MESSAGE_APPLICANTS_LISTED_OVERVIEW, 0);
NameContainsKeywordsPredicate predicate = preparePredicate("NO_APPLICANTS_MATCH");
FindCommand command = new FindCommand(predicate);

expectedModel.updateFilteredApplicantList(predicate);

assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getFilteredApplicantList());
}
Expand All @@ -71,7 +122,9 @@ public void execute_multipleKeywords_multipleApplicantsFound() {
String expectedMessage = String.format(MESSAGE_APPLICANTS_LISTED_OVERVIEW, 3);
NameContainsKeywordsPredicate predicate = preparePredicate("Kurz Elle Kunz");
FindCommand command = new FindCommand(predicate);

expectedModel.updateFilteredApplicantList(predicate);

assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(CARL, ELLE, FIONA), model.getFilteredApplicantList());
}
Expand All @@ -81,19 +134,46 @@ public void execute_multipleKeywords_singleApplicantFound() {
String expectedMessage = String.format(MESSAGE_APPLICANTS_LISTED_OVERVIEW, 1);
NameContainsKeywordsPredicate predicate = preparePredicate("Fiona Fluorescence");
FindCommand command = new FindCommand(predicate);

expectedModel.updateFilteredApplicantList(predicate);

assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(FIONA), model.getFilteredApplicantList());
}

@Test
public void execute_singleIncompleteKeyword_noApplicantFound() {
String expectedMessage = String.format(MESSAGE_APPLICANTS_LISTED_OVERVIEW, 0);
NameContainsKeywordsPredicate predicate = preparePredicate("arrr");
FindCommand command = new FindCommand(predicate);

expectedModel.updateFilteredApplicantList(predicate);

assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getFilteredApplicantList());
}

@Test
public void execute_singleIncompleteKeyword_singleApplicantFound() {
String expectedMessage = String.format(MESSAGE_APPLICANTS_LISTED_OVERVIEW, 1);
NameContainsKeywordsPredicate predicate = preparePredicate("car");
FindCommand command = new FindCommand(predicate);

expectedModel.updateFilteredApplicantList(predicate);

assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(CARL), model.getFilteredApplicantList());
}

@Test
public void execute_singleIncompleteKeyword_multipleApplicantsFound() {
String expectedMessage = String.format(MESSAGE_APPLICANTS_LISTED_OVERVIEW, 2);
NameContainsKeywordsPredicate predicate = preparePredicate("Mei");
FindCommand command = new FindCommand(predicate);

expectedModel.updateFilteredApplicantList(predicate);

assertCommandSuccess(command, model, expectedMessage, expectedModel);
System.out.println(model.getFilteredApplicantList());
assertEquals(Arrays.asList(BENSON, DANIEL), model.getFilteredApplicantList());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.staffsnap.logic.parser;

import static seedu.staffsnap.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.staffsnap.logic.commands.FindCommand.MESSAGE_WRONG_FORMAT;
import static seedu.staffsnap.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.staffsnap.logic.parser.CommandParserTestUtil.assertParseSuccess;

Expand All @@ -11,15 +12,35 @@
import seedu.staffsnap.logic.commands.FindCommand;
import seedu.staffsnap.model.applicant.NameContainsKeywordsPredicate;

/**
* Tests the FindCommandParser to ensure it accepts and parses the correct user input.
*/
public class FindCommandParserTest {

private FindCommandParser parser = new FindCommandParser();

@Test
public void parse_emptyArg_throwsParseException() {
//parser should not accept empty strings
assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}

@Test
public void parse_numericalArg_throwsParseException() {
//parser should not accept strings with numbers in it
String userInput = "lee2";
assertParseFailure(parser, userInput,
String.format(MESSAGE_WRONG_FORMAT, FindCommand.MESSAGE_USAGE));
}

@Test
public void parse_symbolsArg_throwsParseException() {
//parser should not accept strings with symbols in it
String userInput = "*";
assertParseFailure(parser, userInput,
String.format(MESSAGE_WRONG_FORMAT, FindCommand.MESSAGE_USAGE));
}

@Test
public void parse_validArgs_returnsFindCommand() {
// no leading and trailing whitespaces
Expand Down

0 comments on commit 896bb4d

Please sign in to comment.