Skip to content

Commit

Permalink
Fix bugs and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yyihaoc committed Oct 16, 2024
1 parent 277ec4d commit ad58d90
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 27 deletions.
92 changes: 71 additions & 21 deletions src/test/java/seedu/address/logic/commands/FindCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
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 static seedu.address.testutil.TypicalPersons.JAVIER;
import static seedu.address.testutil.TypicalPersons.KELLY;
import static seedu.address.testutil.TypicalPersons.LENOR;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBookWithTags;

import java.util.Arrays;
import java.util.Collections;
Expand All @@ -19,29 +22,37 @@
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.RoleContainsKeywordsPredicate;

/**
* Contains integration tests (interaction with the Model) for {@code FindCommand}.
*/
public class FindCommandTest {
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private Model model = new ModelManager(getTypicalAddressBookWithTags(), new UserPrefs());
private Model expectedModel = new ModelManager(getTypicalAddressBookWithTags(), new UserPrefs());

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

NameContainsKeywordsPredicate secondNamePredicate =
new NameContainsKeywordsPredicate(Collections.singletonList("second"));
RoleContainsKeywordsPredicate secondRolePredicate =
new RoleContainsKeywordsPredicate(Collections.singletonList("second"));

FindCommand findFirstCommand = new FindCommand(firstPredicate);
FindCommand findSecondCommand = new FindCommand(secondPredicate);
FindCommand findFirstCommand = new FindCommand(firstNamePredicate, firstRolePredicate);
FindCommand findSecondCommand = new FindCommand(secondNamePredicate, secondRolePredicate);
FindCommand findThirdCommand = new FindCommand(firstNamePredicate, secondRolePredicate);
FindCommand findFourthCommand = new FindCommand(secondNamePredicate, firstRolePredicate);

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

// same values -> returns true
FindCommand findFirstCommandCopy = new FindCommand(firstPredicate);
FindCommand findFirstCommandCopy = new FindCommand(firstNamePredicate, firstRolePredicate);
assertTrue(findFirstCommand.equals(findFirstCommandCopy));

// different types -> returns false
Expand All @@ -50,42 +61,81 @@ public void equals() {
// null -> returns false
assertFalse(findFirstCommand.equals(null));

// different person -> returns false
// different name and role predicate -> returns false
assertFalse(findFirstCommand.equals(findSecondCommand));

// different name predicate only -> returns false
assertFalse(findFirstCommand.equals(findFourthCommand));

// different role predicate only -> returns false
assertFalse(findFirstCommand.equals(findThirdCommand));
}

@Test
public void execute_zeroKeywords_noPersonFound() {
public void execute_zeroNameAndRoleKeywords_noPersonFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0);
NameContainsKeywordsPredicate predicate = preparePredicate(" ");
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
NameContainsKeywordsPredicate namePredicate = prepareNamePredicate(" ");
RoleContainsKeywordsPredicate rolePredicate = prepareRolePredicate(" ");
FindCommand command = new FindCommand(namePredicate, rolePredicate);
expectedModel.updateFilteredPersonList(namePredicate.or(rolePredicate));
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getFilteredPersonList());
}

@Test
public void execute_multipleKeywords_multiplePersonsFound() {
public void execute_multipleNameKeywordsOnly_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3);
NameContainsKeywordsPredicate predicate = preparePredicate("Kurz Elle Kunz");
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
NameContainsKeywordsPredicate namePredicate = prepareNamePredicate("Kurz Elle Kunz");
RoleContainsKeywordsPredicate rolePredicate = prepareRolePredicate(" ");
FindCommand command = new FindCommand(namePredicate, rolePredicate);
expectedModel.updateFilteredPersonList(namePredicate.or(rolePredicate));
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(CARL, ELLE, FIONA), model.getFilteredPersonList());
}

@Test
public void execute_multipleRoleKeywordsOnly_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 2);
NameContainsKeywordsPredicate namePredicate = prepareNamePredicate(" ");
RoleContainsKeywordsPredicate rolePredicate = prepareRolePredicate("friend");
FindCommand command = new FindCommand(namePredicate, rolePredicate);
expectedModel.updateFilteredPersonList(namePredicate.or(rolePredicate));
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(JAVIER, LENOR), model.getFilteredPersonList());
}

@Test
public void execute_multipleRoleAndRoleKeywords_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3);
NameContainsKeywordsPredicate namePredicate = prepareNamePredicate("javier");
RoleContainsKeywordsPredicate rolePredicate = prepareRolePredicate("relative");
FindCommand command = new FindCommand(namePredicate, rolePredicate);
expectedModel.updateFilteredPersonList(namePredicate.or(rolePredicate));
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(JAVIER, KELLY, LENOR), 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 + "}";
NameContainsKeywordsPredicate namePredicate = new NameContainsKeywordsPredicate(Arrays.asList("keyword"));
RoleContainsKeywordsPredicate rolePredicate = new RoleContainsKeywordsPredicate(Arrays.asList("friend"));
FindCommand findCommand = new FindCommand(namePredicate, rolePredicate);
String expected = FindCommand.class.getCanonicalName() + "{namePredicate=" + namePredicate
+ ", rolePredicate=" + rolePredicate + "}";
assertEquals(expected, findCommand.toString());
}

/**
* Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}.
*/
private NameContainsKeywordsPredicate preparePredicate(String userInput) {
private NameContainsKeywordsPredicate prepareNamePredicate(String userInput) {
return new NameContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+")));
}

/**
* Parses {@code userInput} into a {@code RoleContainsKeywordsPredicate}.
*/
private RoleContainsKeywordsPredicate prepareRolePredicate(String userInput) {
return new RoleContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.Messages.MESSAGE_UNKNOWN_COMMAND;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;

Expand All @@ -25,6 +26,7 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.Person;
import seedu.address.model.person.RoleContainsKeywordsPredicate;
import seedu.address.testutil.EditPersonDescriptorBuilder;
import seedu.address.testutil.PersonBuilder;
import seedu.address.testutil.PersonUtil;
Expand All @@ -33,6 +35,7 @@ public class AddressBookParserTest {

private final AddressBookParser parser = new AddressBookParser();


@Test
public void parseCommand_add() throws Exception {
Person person = new PersonBuilder().build();
Expand Down Expand Up @@ -68,14 +71,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);
FindCommand.COMMAND_WORD + " " + keywords.stream().map(keyword ->
new StringBuilder(PREFIX_NAME.getPrefix()).append(" ").append(keyword).toString())
.collect(Collectors.joining(" ")));

assertEquals(new FindCommand(new NameContainsKeywordsPredicate(keywords),
new RoleContainsKeywordsPredicate(List.of())), command);
}


@Test
public void parseCommand_help() throws Exception {
assertTrue(parser.parseCommand(HelpCommand.COMMAND_WORD) instanceof HelpCommand);
Expand All @@ -98,4 +108,6 @@ public void parseCommand_unrecognisedInput_throwsParseException() {
public void parseCommand_unknownCommand_throwsParseException() {
assertThrows(ParseException.class, MESSAGE_UNKNOWN_COMMAND, () -> parser.parseCommand("unknownCommand"));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;

import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.Test;

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

public class FindCommandParserTest {

Expand All @@ -24,11 +25,12 @@ public void parse_emptyArg_throwsParseException() {
public void parse_validArgs_returnsFindCommand() {
// no leading and trailing whitespaces
FindCommand expectedFindCommand =
new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob")));
assertParseSuccess(parser, "Alice Bob", expectedFindCommand);
new FindCommand(new NameContainsKeywordsPredicate(List.of("chen", "albert")),
new RoleContainsKeywordsPredicate(List.of("member", "exco")));
assertParseSuccess(parser, " n/chen n/albert r/member r/exco ", expectedFindCommand);

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

}
21 changes: 21 additions & 0 deletions src/test/java/seedu/address/testutil/TypicalPersons.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public class TypicalPersons {
.withEmail("[email protected]").withTelegram("fionaKunz").build();
public static final Person GEORGE = new PersonBuilder().withName("George Best").withPhone("9482442")
.withEmail("[email protected]").withTelegram("georgeBest").build();
public static final Person JAVIER = new PersonBuilder().withName("Javier Tan").withPhone("93453421")
.withEmail("[email protected]").withTelegram("javierTan").withTags("friend").build();
public static final Person KELLY = new PersonBuilder().withName("Kelly Lim").withPhone("89453765")
.withEmail("[email protected]").withTelegram("kellyLim").withTags("relative").build();
public static final Person LENOR = new PersonBuilder().withName("Lenor Kim").withPhone("90784567")
.withEmail("[email protected]").withTelegram("lenor").withTags("relative", "friend").build();

// Manually added
public static final Person HOON = new PersonBuilder().withName("Hoon Meier").withPhone("8482424")
Expand Down Expand Up @@ -73,4 +79,19 @@ public static AddressBook getTypicalAddressBook() {
public static List<Person> getTypicalPersons() {
return new ArrayList<>(Arrays.asList(ALICE, BENSON, CARL, DANIEL, ELLE, FIONA, GEORGE));
}

public static List<Person> getTypicalPersonsWithTags() {
return new ArrayList<>(Arrays.asList(ALICE, BENSON, CARL, DANIEL, ELLE, FIONA, GEORGE, JAVIER, KELLY, LENOR));
}

/**
* Returns an {@code AddressBook} with all the typical persons including tags.
*/
public static AddressBook getTypicalAddressBookWithTags() {
AddressBook ab = new AddressBook();
for (Person person : getTypicalPersonsWithTags()) {
ab.addPerson(person);
}
return ab;
}
}

0 comments on commit ad58d90

Please sign in to comment.