Skip to content

Commit

Permalink
[#110] Add unit tests for FindCommand class (#120)
Browse files Browse the repository at this point in the history
* Add testutil classes to facilitate testing

* Add TestMain class to facilitate testing of commands

* Add equals() methond in AddressBook, UniquePersonList and UniqueTagList

* Add FindCommandTest and CommandTest

* Fix code style

* Add indentation for equals() method

* Reimplement FindCommandTest without use of TestMain

* Remove duplicate equals() method in UniquePersonList

* Remove TestPerson class

* Refactor FindCommandTest

* Refactor FindCommandTest

* Refactor FindCommandTest

* Remove empty line at top of assertCommandBehavior

* Refactor TypicalPersons.java and shift instantiating a FindCommand in FindCommandTest to asserCommandBehavior method

* Rename assertCommandBehavior to assertFindCommandBehavior

* Remove PersonBuilder and make TypicalPersons fields non-static

* Refactor FindCommandTest by inlining test methods

* Remove unused imports

* Add new test cases

* Remove trailing empty lines

* Fix coding style

* Remove unused imports

* Remove trailing whitespaces
  • Loading branch information
awhdes authored and pyokagan committed Jan 3, 2017
1 parent 753fcc1 commit a909e09
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/seedu/addressbook/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected Command() {
* @param personsDisplayed used to generate summary
* @return summary message for persons displayed
*/
protected String getMessageForPersonListShownSummary(List<? extends ReadOnlyPerson> personsDisplayed) {
public static String getMessageForPersonListShownSummary(List<? extends ReadOnlyPerson> personsDisplayed) {
return String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, personsDisplayed.size());
}

Expand Down
8 changes: 8 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
7 changes: 7 additions & 0 deletions src/seedu/addressbook/data/tag/UniqueTagList.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,11 @@ public Iterator<Tag> 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));
}

}
63 changes: 63 additions & 0 deletions test/java/seedu/addressbook/commands/FindCommandTest.java
Original file line number Diff line number Diff line change
@@ -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<ReadOnlyPerson> expectedPersonList) {
FindCommand command = createFindCommand(keywords);
CommandResult result = command.execute();

assertEquals(Command.getMessageForPersonListShownSummary(expectedPersonList), result.feedbackToUser);
}

private FindCommand createFindCommand(String[] keywords) {
final Set<String> keywordSet = new HashSet<>(Arrays.asList(keywords));
FindCommand command = new FindCommand(keywordSet);
command.setData(addressBook, Collections.emptyList());
return command;
}

}
53 changes: 53 additions & 0 deletions test/java/seedu/addressbook/util/TypicalPersons.java
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit a909e09

Please sign in to comment.