diff --git a/src/seedu/addressbook/commands/Command.java b/src/seedu/addressbook/commands/Command.java index 2e4708de9..67b193409 100644 --- a/src/seedu/addressbook/commands/Command.java +++ b/src/seedu/addressbook/commands/Command.java @@ -32,7 +32,7 @@ protected Command() { * @param personsDisplayed used to generate summary * @return summary message for persons displayed */ - protected String getMessageForPersonListShownSummary(List personsDisplayed) { + public static String getMessageForPersonListShownSummary(List personsDisplayed) { return String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, personsDisplayed.size()); } diff --git a/src/seedu/addressbook/data/AddressBook.java b/src/seedu/addressbook/data/AddressBook.java index 32d03913d..6a37d752d 100644 --- a/src/seedu/addressbook/data/AddressBook.java +++ b/src/seedu/addressbook/data/AddressBook.java @@ -143,4 +143,12 @@ public UniquePersonList getAllPersons() { public UniqueTagList getAllTags() { return new UniqueTagList(allTags); } + + @Override + public boolean equals(Object other) { + return other == this // short circuit if same object + || (other instanceof AddressBook // instanceof handles nulls + && this.allPersons.equals(((AddressBook) other).allPersons) + && this.allTags.equals(((AddressBook) other).allTags)); + } } diff --git a/src/seedu/addressbook/data/tag/UniqueTagList.java b/src/seedu/addressbook/data/tag/UniqueTagList.java index 4211752cd..cfea05b86 100644 --- a/src/seedu/addressbook/data/tag/UniqueTagList.java +++ b/src/seedu/addressbook/data/tag/UniqueTagList.java @@ -160,4 +160,11 @@ public Iterator iterator() { return internalList.iterator(); } + @Override + public boolean equals(Object other) { + return other == this // short circuit if same object + || (other instanceof UniqueTagList // instanceof handles nulls + && this.internalList.equals(((UniqueTagList) other).internalList)); + } + } diff --git a/test/java/seedu/addressbook/commands/FindCommandTest.java b/test/java/seedu/addressbook/commands/FindCommandTest.java new file mode 100644 index 000000000..f21b1e8e7 --- /dev/null +++ b/test/java/seedu/addressbook/commands/FindCommandTest.java @@ -0,0 +1,63 @@ +package seedu.addressbook.commands; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.junit.Test; + +import seedu.addressbook.data.AddressBook; +import seedu.addressbook.data.exception.IllegalValueException; +import seedu.addressbook.data.person.ReadOnlyPerson; +import seedu.addressbook.util.TypicalPersons; + +public class FindCommandTest { + + private final AddressBook addressBook = new TypicalPersons().getTypicalAddressBook(); + private final TypicalPersons td = new TypicalPersons(); + + @Test + public void execute() throws IllegalValueException { + //same word, same case: matched + assertFindCommandBehavior(new String[]{"Amy"}, Arrays.asList(td.amy)); + + //same word, different case: not matched + assertFindCommandBehavior(new String[]{"aMy"}, Collections.emptyList()); + + //partial word: not matched + assertFindCommandBehavior(new String[]{"my"}, Collections.emptyList()); + + //multiple words: matched + assertFindCommandBehavior(new String[]{"Amy", "Bill", "Candy", "Destiny"}, + Arrays.asList(td.amy, td.bill, td.candy)); + + //repeated keywords: matched + assertFindCommandBehavior(new String[]{"Amy", "Amy"}, Arrays.asList(td.amy)); + + //Keyword matching a word in address: not matched + assertFindCommandBehavior(new String[]{"Clementi"}, Collections.emptyList()); + } + + /** + * Executes the find command for the given keywords and verifies + * the result matches the persons in the expectedPersonList exactly. + */ + private void assertFindCommandBehavior(String[] keywords, List expectedPersonList) { + FindCommand command = createFindCommand(keywords); + CommandResult result = command.execute(); + + assertEquals(Command.getMessageForPersonListShownSummary(expectedPersonList), result.feedbackToUser); + } + + private FindCommand createFindCommand(String[] keywords) { + final Set keywordSet = new HashSet<>(Arrays.asList(keywords)); + FindCommand command = new FindCommand(keywordSet); + command.setData(addressBook, Collections.emptyList()); + return command; + } + +} diff --git a/test/java/seedu/addressbook/util/TypicalPersons.java b/test/java/seedu/addressbook/util/TypicalPersons.java new file mode 100644 index 000000000..64e0d99c8 --- /dev/null +++ b/test/java/seedu/addressbook/util/TypicalPersons.java @@ -0,0 +1,53 @@ +package seedu.addressbook.util; + +import seedu.addressbook.data.AddressBook; +import seedu.addressbook.data.exception.IllegalValueException; +import seedu.addressbook.data.person.Address; +import seedu.addressbook.data.person.Email; +import seedu.addressbook.data.person.Name; +import seedu.addressbook.data.person.Person; +import seedu.addressbook.data.person.Phone; +import seedu.addressbook.data.tag.UniqueTagList; + +/** + * Class to generate typical test persons + */ +public class TypicalPersons { + + public Person amy, bill, candy; + + public TypicalPersons() { + try { + amy = new Person(new Name("Amy Buck"), new Phone("91119111", false), new Email("ab@gmail.com", false), + new Address("1 Clementi Road", false), new UniqueTagList()); + bill = new Person(new Name("Bill Clint"), new Phone("92229222", false), new Email("bc@gmail.com", false), + new Address("2 Clementi Road", false), new UniqueTagList()); + candy = new Person(new Name("Candy Destiny"), new Phone("93339333", false), + new Email("cd@gmail.com", false), new Address("3 Clementi Road", false), new UniqueTagList()); + } catch (IllegalValueException e) { + e.printStackTrace(); + assert false : "not possible"; + } + } + + private void loadAddressBookWithSampleData(AddressBook ab) { + try { + for (Person p : this.getTypicalPersons()) { + ab.addPerson(new Person(p)); + } + } catch (IllegalValueException e) { + assert false : "not possible"; + } + } + + public Person[] getTypicalPersons() { + return new Person[]{amy, bill, candy}; + } + + public AddressBook getTypicalAddressBook() { + AddressBook ab = new AddressBook(); + loadAddressBookWithSampleData(ab); + return ab; + } + +}