Skip to content

Commit

Permalink
Update test cases and class file
Browse files Browse the repository at this point in the history
Let's
* modify the regex to account for whitespace characters
* implement unit tests for AddressBookParser and FindByNameCommand
* modify unit test for FindCommandParserTest
* implement equals method (overwriting) for FindByNameCommand
  • Loading branch information
KrashKart committed Oct 10, 2024
1 parent c789ccb commit 58ea80c
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public AbstractFindCommand(ContainsKeywordsPredicate predicate) {
this.predicate = predicate;
}

protected ContainsKeywordsPredicate getPredicate() {
return this.predicate;
}

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
Expand All @@ -34,21 +38,6 @@ public CommandResult execute(Model model) {
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof AbstractFindCommand)) {
return false;
}

AbstractFindCommand otherFindCommand = (AbstractFindCommand) other;
return this.predicate.equals(otherFindCommand.predicate);
}

@Override
public String toString() {
return new ToStringBuilder(this)
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/seedu/address/logic/commands/FindByNameCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,19 @@ public class FindByNameCommand extends AbstractFindCommand {
public FindByNameCommand(NameContainsKeywordsPredicate predicate) {
super(predicate);
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof FindByNameCommand)) {
return false;
}

FindByNameCommand otherFindCommand = (FindByNameCommand) other;
return this.getPredicate().equals(otherFindCommand.getPredicate());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class FindCommandParser implements Parser<AbstractFindCommand> {

public static final Pattern KEYWORD_EXTRACTOR =
Pattern.compile("^(?<tag>/[enc]) (?<argument>.+)$");
Pattern.compile("^(?<tag>/[cen])\\s*(?<arguments>[\\S\\s]+)$");

/**
* Parses the given {@code String} of arguments in the context of the FindCommand
Expand All @@ -37,13 +37,14 @@ public AbstractFindCommand parse(String args) throws ParseException {

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

// return approppriate FindCommand class depending on tag
switch (tag) {
case "/n":
return new FindByNameCommand(
new NameContainsKeywordsPredicate(Arrays.asList(searchTerm)));
new NameContainsKeywordsPredicate(Arrays.asList(searchTermArray)));
default:
return null; // temporary value, this should not occur due to regex
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.getTypicalAddressBook;

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 FindByNameCommandTest {
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void equals() {
NameContainsKeywordsPredicate firstPredicate =
new NameContainsKeywordsPredicate(Collections.singletonList("first"));
NameContainsKeywordsPredicate secondPredicate =
new NameContainsKeywordsPredicate(Collections.singletonList("second"));

FindByNameCommand findFirstCommand = new FindByNameCommand(firstPredicate);
FindByNameCommand findSecondCommand = new FindByNameCommand(secondPredicate);

// same object -> returns true
assertTrue(findFirstCommand.equals(findFirstCommand));

// same values -> returns true
FindByNameCommand findFirstCommandCopy = new FindByNameCommand(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(" ");
FindByNameCommand command = new FindByNameCommand(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");
FindByNameCommand command = new FindByNameCommand(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"));
FindByNameCommand findCommand = new FindByNameCommand(predicate);
String expected = FindByNameCommand.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+")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindByNameCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
Expand Down Expand Up @@ -68,12 +69,21 @@ public void parseCommand_exit() throws Exception {
assertTrue(parser.parseCommand(ExitCommand.COMMAND_WORD + " 3") instanceof ExitCommand);
}

// @Test
// public void parseCommand_find() throws Exception {
// List<String> keywords = Arrays.asList("foo", "bar", "baz");
// FindCommand command = (FindCommand) parser.parseCommand(
// FindCommand.COMMAND_WORD + " " + keywords.stream().collect(Collectors.joining(" ")));
// assertEquals(new FindCommand(new NameContainsKeywordsPredicate(keywords)), command);
// }

@Test
public void parseCommand_find() throws Exception {
public void parseCommand_findByName() throws Exception {
List<String> keywords = Arrays.asList("foo", "bar", "baz");
FindCommand command = (FindCommand) parser.parseCommand(
FindCommand.COMMAND_WORD + " " + keywords.stream().collect(Collectors.joining(" ")));
assertEquals(new FindCommand(new NameContainsKeywordsPredicate(keywords)), command);
FindByNameCommand command = (FindByNameCommand) parser.parseCommand(
FindByNameCommand.COMMAND_WORD + " /n "
+ keywords.stream().collect(Collectors.joining(" ")));
assertEquals(new FindByNameCommand(new NameContainsKeywordsPredicate(keywords)), command);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.junit.jupiter.api.Test;

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

Expand All @@ -21,14 +22,15 @@ public void parse_emptyArg_throwsParseException() {
}

@Test
public void parse_validArgs_returnsFindCommand() {
public void parse_validArgs_returnsFindByNameCommand() {
FindByNameCommand expectedFindCommand =
new FindByNameCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob")));

// no leading and trailing whitespaces
FindCommand expectedFindCommand =
new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob")));
assertParseSuccess(parser, "Alice Bob", expectedFindCommand);
assertParseSuccess(parser, "/n Alice Bob", expectedFindCommand);

// multiple whitespaces between keywords
assertParseSuccess(parser, " \n Alice \n \t Bob \t", expectedFindCommand);
assertParseSuccess(parser, "/n \n Alice \n \t Bob \t", expectedFindCommand);
}

}

0 comments on commit 58ea80c

Please sign in to comment.