forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create JUnit tests for PersonFilter and FilterCommand
- Loading branch information
Showing
5 changed files
with
263 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/test/java/seedu/address/logic/commands/FilterCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.ModelManager; | ||
import seedu.address.model.UserPrefs; | ||
import seedu.address.testutil.PersonFilterBuilder; | ||
|
||
import java.util.Arrays; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static seedu.address.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW; | ||
import static seedu.address.logic.commands.CommandTestUtil.*; | ||
import static seedu.address.testutil.TypicalPersons.*; | ||
|
||
/** | ||
* Contains integration tests (interaction with the Model) for {@code FilterCommand}. | ||
*/ | ||
class FilterCommandTest { | ||
|
||
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
|
||
@Test | ||
public void execute_oneField_multiplePersonsDisplayed() { | ||
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3); | ||
FilterCommand.PersonFilter filter = new PersonFilterBuilder().withPhone("948").build(); | ||
FilterCommand command = new FilterCommand(filter); | ||
expectedModel.updateFilteredPersonList(filter::test); | ||
assertCommandSuccess(command, model, expectedMessage, expectedModel); | ||
assertEquals(Arrays.asList(ELLE, FIONA, GEORGE), model.getFilteredPersonList()); | ||
} | ||
@Test | ||
public void execute_multipleKeywords_onePersonFound() { | ||
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 1); | ||
FilterCommand.PersonFilter filter = new PersonFilterBuilder().withName("carl").withEmail("heinz").build(); | ||
FilterCommand command = new FilterCommand(filter); | ||
expectedModel.updateFilteredPersonList(filter::test); | ||
assertCommandSuccess(command, model, expectedMessage, expectedModel); | ||
assertEquals(Arrays.asList(CARL), model.getFilteredPersonList()); | ||
} | ||
@Test | ||
public void toStringMethod() { | ||
FilterCommand.PersonFilter filter = new PersonFilterBuilder().build(); | ||
FilterCommand filterCommand = new FilterCommand(filter); | ||
String expected = FilterCommand.class.getCanonicalName() + "{personFilter=" + filter + "}"; | ||
assertEquals(expected, filterCommand.toString()); | ||
} | ||
|
||
@Test | ||
public void equals() { | ||
FilterCommand.PersonFilter firstFilter = | ||
new FilterCommand.PersonFilter(FILTER_AMY); | ||
FilterCommand.PersonFilter secondFilter = | ||
new FilterCommand.PersonFilter(FILTER_BOB); | ||
|
||
FilterCommand filterFirstCommand = new FilterCommand(firstFilter); | ||
FilterCommand filterSecondCommand = new FilterCommand(secondFilter); | ||
|
||
// same object -> returns true | ||
assertTrue(filterFirstCommand.equals(filterFirstCommand)); | ||
|
||
// same values -> returns true | ||
FilterCommand filterFirstCommandCopy = new FilterCommand(firstFilter); | ||
assertTrue(filterFirstCommand.equals(filterFirstCommandCopy)); | ||
|
||
// different types -> returns false | ||
assertFalse(filterFirstCommand.equals(1)); | ||
|
||
// null -> returns false | ||
assertFalse(filterFirstCommand.equals(null)); | ||
|
||
// different person -> returns false | ||
assertFalse(filterFirstCommand.equals(filterSecondCommand)); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/test/java/seedu/address/logic/commands/PersonFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import seedu.address.logic.commands.FilterCommand.PersonFilter; | ||
import seedu.address.testutil.PersonFilterBuilder; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static seedu.address.logic.commands.CommandTestUtil.*; | ||
|
||
class PersonFilterTest { | ||
@Test | ||
public void equals() { | ||
// same object -> returns true | ||
assertTrue(FILTER_AMY.equals(FILTER_AMY)); | ||
|
||
// null -> returns false | ||
assertFalse(FILTER_AMY.equals(null)); | ||
|
||
// different types -> returns false | ||
assertFalse(FILTER_AMY.equals(5)); | ||
|
||
// different values -> returns false | ||
assertFalse(FILTER_AMY.equals(DESC_BOB)); | ||
|
||
// different name -> returns false | ||
PersonFilter editedAmy = new PersonFilterBuilder(FILTER_AMY).withName(VALID_NAME_BOB).build(); | ||
assertFalse(FILTER_AMY.equals(editedAmy)); | ||
|
||
// different phone -> returns false | ||
editedAmy = new PersonFilterBuilder(FILTER_AMY).withPhone(VALID_PHONE_BOB).build(); | ||
assertFalse(FILTER_AMY.equals(editedAmy)); | ||
|
||
// different email -> returns false | ||
editedAmy = new PersonFilterBuilder(FILTER_AMY).withEmail(VALID_EMAIL_BOB).build(); | ||
assertFalse(FILTER_AMY.equals(editedAmy)); | ||
|
||
// different address -> returns false | ||
editedAmy = new PersonFilterBuilder(FILTER_AMY).withAddress(VALID_ADDRESS_BOB).build(); | ||
assertFalse(FILTER_AMY.equals(editedAmy)); | ||
|
||
// different tags -> returns false | ||
editedAmy = new PersonFilterBuilder(FILTER_AMY).withTags(VALID_TAG_HUSBAND).build(); | ||
assertFalse(FILTER_AMY.equals(editedAmy)); | ||
} | ||
|
||
@Test | ||
public void toStringMethod() { | ||
PersonFilter personFilter = new PersonFilter(); | ||
String expected = PersonFilter.class.getCanonicalName() + "{name=" | ||
+ personFilter.getName() + ", phone=" | ||
+ personFilter.getPhone() + ", email=" | ||
+ personFilter.getEmail() + ", address=" | ||
+ personFilter.getAddress() + ", tags=" | ||
+ personFilter.getTags() + "}"; | ||
assertEquals(expected, personFilter.toString()); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/test/java/seedu/address/testutil/PersonFilterBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package seedu.address.testutil; | ||
|
||
import seedu.address.logic.commands.EditCommand; | ||
import seedu.address.logic.commands.FilterCommand; | ||
import seedu.address.model.person.*; | ||
import seedu.address.model.tag.Tag; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class PersonFilterBuilder { | ||
|
||
private FilterCommand.PersonFilter filter; | ||
|
||
public PersonFilterBuilder() { | ||
filter = new FilterCommand.PersonFilter(); | ||
} | ||
|
||
public PersonFilterBuilder(FilterCommand.PersonFilter filter) { | ||
this.filter = new FilterCommand.PersonFilter(filter); | ||
} | ||
|
||
/** | ||
* Returns an {@code EditPersonDescriptor} with fields containing {@code person}'s details | ||
*/ | ||
public PersonFilterBuilder(Person person) { | ||
filter = new FilterCommand.PersonFilter(); | ||
filter.setName(person.getName().fullName); | ||
filter.setPhone(person.getPhone().value); | ||
filter.setEmail(person.getEmail().value); | ||
filter.setAddress(person.getAddress().value); | ||
filter.setTags(person.getTags()); | ||
} | ||
|
||
/** | ||
* Sets the {@code Name} of the {@code EditPersonDescriptor} that we are building. | ||
*/ | ||
public PersonFilterBuilder withName(String name) { | ||
filter.setName(name); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the {@code Phone} of the {@code EditPersonDescriptor} that we are building. | ||
*/ | ||
public PersonFilterBuilder withPhone(String phone) { | ||
filter.setPhone(phone); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the {@code Email} of the {@code EditPersonDescriptor} that we are building. | ||
*/ | ||
public PersonFilterBuilder withEmail(String email) { | ||
filter.setEmail(email); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the {@code Address} of the {@code EditPersonDescriptor} that we are building. | ||
*/ | ||
public PersonFilterBuilder withAddress(String address) { | ||
filter.setAddress(address); | ||
return this; | ||
} | ||
|
||
/** | ||
* Parses the {@code tags} into a {@code Set<Tag>} and set it to the {@code EditPersonDescriptor} | ||
* that we are building. | ||
*/ | ||
public PersonFilterBuilder withTags(String... tags) { | ||
Set<Tag> tagSet = Stream.of(tags).map(Tag::new).collect(Collectors.toSet()); | ||
filter.setTags(tagSet); | ||
return this; | ||
} | ||
|
||
public FilterCommand.PersonFilter build() { | ||
return filter; | ||
} | ||
} |