From 896bb4db2bfd03ddf66a029ffb79ece0b8990396 Mon Sep 17 00:00:00 2001 From: jingting1412 Date: Mon, 6 Nov 2023 09:32:42 +0800 Subject: [PATCH] Fix FindCommand bug --- .../staffsnap/logic/commands/FindCommand.java | 9 ++ .../logic/parser/FindCommandParser.java | 7 +- .../logic/commands/FindCommandTest.java | 82 ++++++++++++++++++- .../logic/parser/FindCommandParserTest.java | 21 +++++ 4 files changed, 117 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/staffsnap/logic/commands/FindCommand.java b/src/main/java/seedu/staffsnap/logic/commands/FindCommand.java index 81675c539f5..c6a40fb3757 100644 --- a/src/main/java/seedu/staffsnap/logic/commands/FindCommand.java +++ b/src/main/java/seedu/staffsnap/logic/commands/FindCommand.java @@ -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); diff --git a/src/main/java/seedu/staffsnap/logic/parser/FindCommandParser.java b/src/main/java/seedu/staffsnap/logic/parser/FindCommandParser.java index c414f37822d..59b68c3e6a2 100644 --- a/src/main/java/seedu/staffsnap/logic/parser/FindCommandParser.java +++ b/src/main/java/seedu/staffsnap/logic/parser/FindCommandParser.java @@ -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; @@ -16,13 +17,17 @@ public class FindCommandParser implements Parser { /** * 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+"); diff --git a/src/test/java/seedu/staffsnap/logic/commands/FindCommandTest.java b/src/test/java/seedu/staffsnap/logic/commands/FindCommandTest.java index c64c7c70ec3..ab4179b5b81 100644 --- a/src/test/java/seedu/staffsnap/logic/commands/FindCommandTest.java +++ b/src/test/java/seedu/staffsnap/logic/commands/FindCommandTest.java @@ -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 = @@ -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()); } @@ -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()); } @@ -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()); } diff --git a/src/test/java/seedu/staffsnap/logic/parser/FindCommandParserTest.java b/src/test/java/seedu/staffsnap/logic/parser/FindCommandParserTest.java index 99129fa10ec..0e6201dfc8c 100644 --- a/src/test/java/seedu/staffsnap/logic/parser/FindCommandParserTest.java +++ b/src/test/java/seedu/staffsnap/logic/parser/FindCommandParserTest.java @@ -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; @@ -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