From efcad73768388471ea453a93ffdb421b655e20d4 Mon Sep 17 00:00:00 2001 From: lr Date: Wed, 16 Oct 2024 21:05:16 +0800 Subject: [PATCH] Add FindNameCommand test Add FindNameCommand test --- .../logic/parser/FindNameCommandParser.java | 6 +- .../logic/commands/FindNameCommandTest.java | 91 +++++++++++++++++++ .../parser/FindNameCommandParserTest.java | 35 +++++++ 3 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 src/test/java/seedu/address/logic/commands/FindNameCommandTest.java create mode 100644 src/test/java/seedu/address/logic/parser/FindNameCommandParserTest.java diff --git a/src/main/java/seedu/address/logic/parser/FindNameCommandParser.java b/src/main/java/seedu/address/logic/parser/FindNameCommandParser.java index 3948ba0420e..687823a1db1 100644 --- a/src/main/java/seedu/address/logic/parser/FindNameCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/FindNameCommandParser.java @@ -9,13 +9,13 @@ import seedu.address.model.person.NameContainsKeywordsPredicate; /** - * Parses input arguments and creates a new FindCommand object + * Parses input arguments and creates a new FindNameCommand object */ public class FindNameCommandParser implements Parser { /** - * Parses the given {@code String} of arguments in the context of the FindCommand - * and returns a FindCommand object for execution. + * Parses the given {@code String} of arguments in the context of the FindNameCommand + * and returns a FindNameCommand object for execution. * @throws ParseException if the user input does not conform the expected format */ public FindNameCommand parse(String args) throws ParseException { diff --git a/src/test/java/seedu/address/logic/commands/FindNameCommandTest.java b/src/test/java/seedu/address/logic/commands/FindNameCommandTest.java new file mode 100644 index 00000000000..775d9ea85e2 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/FindNameCommandTest.java @@ -0,0 +1,91 @@ +package seedu.address.logic.commands; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.testutil.TypicalPersons.CARL; +import static seedu.address.testutil.TypicalPersons.ELLE; +import static seedu.address.testutil.TypicalPersons.FIONA; +import static seedu.address.testutil.TypicalPersons.getTypicalClientHub; + +import java.util.Arrays; +import java.util.Collections; + +import org.junit.jupiter.api.Test; + +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; +import seedu.address.model.person.NameContainsKeywordsPredicate; + +/** + * Contains integration tests (interaction with the Model) for {@code FindCommand}. + */ +public class FindNameCommandTest { + private Model model = new ModelManager(getTypicalClientHub(), new UserPrefs()); + private Model expectedModel = new ModelManager(getTypicalClientHub(), new UserPrefs()); + + @Test + public void equals() { + NameContainsKeywordsPredicate firstPredicate = + new NameContainsKeywordsPredicate(Collections.singletonList("first")); + NameContainsKeywordsPredicate secondPredicate = + new NameContainsKeywordsPredicate(Collections.singletonList("second")); + + FindCommand findFirstCommand = new FindCommand(firstPredicate); + FindCommand findSecondCommand = new FindCommand(secondPredicate); + + // same object -> returns true + assertTrue(findFirstCommand.equals(findFirstCommand)); + + // same values -> returns true + FindCommand findFirstCommandCopy = new FindCommand(firstPredicate); + assertTrue(findFirstCommand.equals(findFirstCommandCopy)); + + // different types -> returns false + assertFalse(findFirstCommand.equals(1)); + + // null -> returns false + assertFalse(findFirstCommand.equals(null)); + + // different person -> returns false + assertFalse(findFirstCommand.equals(findSecondCommand)); + } + + @Test + public void execute_zeroKeywords_noPersonFound() { + String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0); + NameContainsKeywordsPredicate predicate = preparePredicate(" "); + FindCommand command = new FindCommand(predicate); + expectedModel.updateFilteredPersonList(predicate); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + assertEquals(Collections.emptyList(), model.getFilteredPersonList()); + } + + @Test + public void execute_multipleKeywords_multiplePersonsFound() { + String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3); + NameContainsKeywordsPredicate predicate = preparePredicate("Kurz Elle Kunz"); + FindCommand command = new FindCommand(predicate); + expectedModel.updateFilteredPersonList(predicate); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + assertEquals(Arrays.asList(CARL, ELLE, FIONA), model.getFilteredPersonList()); + } + + @Test + public void toStringMethod() { + NameContainsKeywordsPredicate predicate = new NameContainsKeywordsPredicate(Arrays.asList("keyword")); + FindCommand findCommand = new FindCommand(predicate); + String expected = FindCommand.class.getCanonicalName() + "{predicate=" + predicate + "}"; + assertEquals(expected, findCommand.toString()); + } + + /** + * Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}. + */ + private NameContainsKeywordsPredicate preparePredicate(String userInput) { + return new NameContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+"))); + } +} diff --git a/src/test/java/seedu/address/logic/parser/FindNameCommandParserTest.java b/src/test/java/seedu/address/logic/parser/FindNameCommandParserTest.java new file mode 100644 index 00000000000..893f35c42f0 --- /dev/null +++ b/src/test/java/seedu/address/logic/parser/FindNameCommandParserTest.java @@ -0,0 +1,35 @@ +package seedu.address.logic.parser; + +import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; +import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; + +import java.util.Arrays; + +import org.junit.jupiter.api.Test; + +import seedu.address.logic.commands.FindCommand; +import seedu.address.logic.commands.FindNameCommand; +import seedu.address.model.person.NameContainsKeywordsPredicate; + +public class FindNameCommandParserTest { + + private FindNameCommandParser parser = new FindNameCommandParser(); + + @Test + public void parse_emptyArg_throwsParseException() { + assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE)); + } + + @Test + public void parse_validArgs_returnsFindCommand() { + // no leading and trailing whitespaces + FindNameCommand expectedFindCommand = + new FindNameCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob"))); + assertParseSuccess(parser, "Alice Bob", expectedFindCommand); + + // multiple whitespaces between keywords + assertParseSuccess(parser, " \n Alice \n \t Bob \t", expectedFindCommand); + } + +}