Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for FindCommand class #110 #120

Merged
merged 23 commits into from
Jan 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
809a9ff
Add testutil classes to facilitate testing
theapeeeee Dec 9, 2016
cb04f84
Add TestMain class to facilitate testing of commands
theapeeeee Dec 9, 2016
228696b
Add equals() methond in AddressBook, UniquePersonList and UniqueTagList
theapeeeee Dec 9, 2016
d664932
Add FindCommandTest and CommandTest
theapeeeee Dec 9, 2016
d40448b
Fix code style
theapeeeee Dec 9, 2016
1680011
Add indentation for equals() method
theapeeeee Dec 10, 2016
ba2d41d
Reimplement FindCommandTest without use of TestMain
theapeeeee Dec 10, 2016
4fe6e4d
Remove duplicate equals() method in UniquePersonList
theapeeeee Dec 11, 2016
cb7db69
Remove TestPerson class
theapeeeee Dec 11, 2016
3d247be
Refactor FindCommandTest
theapeeeee Dec 11, 2016
4ab7163
Refactor FindCommandTest
theapeeeee Dec 11, 2016
acc5927
Refactor FindCommandTest
theapeeeee Dec 18, 2016
6793926
Remove empty line at top of assertCommandBehavior
theapeeeee Dec 18, 2016
a90bdb7
Refactor TypicalPersons.java and shift instantiating a FindCommand in…
theapeeeee Dec 25, 2016
f7e7607
Rename assertCommandBehavior to assertFindCommandBehavior
theapeeeee Dec 25, 2016
fbc8f42
Remove PersonBuilder and make TypicalPersons fields non-static
theapeeeee Dec 27, 2016
71d0d84
Refactor FindCommandTest by inlining test methods
theapeeeee Dec 27, 2016
349fb15
Remove unused imports
theapeeeee Dec 27, 2016
94f4cbd
Add new test cases
theapeeeee Dec 29, 2016
0f539cc
Remove trailing empty lines
theapeeeee Dec 29, 2016
642a760
Fix coding style
theapeeeee Dec 30, 2016
1dd21bb
Remove unused imports
theapeeeee Dec 30, 2016
ce4feb5
Remove trailing whitespaces
Jan 2, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why two blank lines?

//same word, different case: not matched
assertFindCommandBehavior(new String[]{"aMy"}, Collections.emptyList());

//partial word: not matched
assertFindCommandBehavior(new String[]{"my"}, Collections.emptyList());

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 3 blank lines here?

//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());
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra blank line

/**
* 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We did some tweaks to Typical persons in level 4 (see this)
Let's follow the same here.


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;
}

}