forked from nus-cs2103-AY2425S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for new FindByTagCommand class
- Loading branch information
Showing
4 changed files
with
193 additions
and
1 deletion.
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
91 changes: 91 additions & 0 deletions
91
src/test/java/seedu/address/logic/commands/FindByTagCommandTest.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,91 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static seedu.address.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW; | ||
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; | ||
import static seedu.address.testutil.TypicalPersons.ALICE; | ||
import static seedu.address.testutil.TypicalPersons.BENSON; | ||
import static seedu.address.testutil.TypicalPersons.DANIEL; | ||
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.model.Model; | ||
import seedu.address.model.ModelManager; | ||
import seedu.address.model.UserPrefs; | ||
import seedu.address.model.person.TagContainsKeywordsPredicate; | ||
|
||
public class FindByTagCommandTest { | ||
|
||
private final Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
private final Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
|
||
@Test | ||
public void equals() { | ||
TagContainsKeywordsPredicate firstPredicate = | ||
new TagContainsKeywordsPredicate(Collections.singletonList("CS2100classmate")); | ||
TagContainsKeywordsPredicate secondPredicate = | ||
new TagContainsKeywordsPredicate(Collections.singletonList("CS1101STA")); | ||
|
||
FindByTagCommand findFirstCommand = new FindByTagCommand(firstPredicate); | ||
FindByTagCommand findSecondCommand = new FindByTagCommand(secondPredicate); | ||
|
||
// same object -> returns true | ||
assertEquals(findFirstCommand, findFirstCommand); | ||
|
||
// same values -> returns true | ||
FindByTagCommand findFirstCommandCopy = new FindByTagCommand(firstPredicate); | ||
assertEquals(findFirstCommand, findFirstCommandCopy); | ||
|
||
// different types -> returns false | ||
assertNotEquals(1, findFirstCommand); | ||
|
||
// null -> returns false | ||
assertNotEquals(null, findFirstCommand); | ||
|
||
// different person -> returns false | ||
assertNotEquals(findFirstCommand, findSecondCommand); | ||
} | ||
|
||
@Test | ||
public void execute_zeroKeywords_noPersonFound() { | ||
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0); | ||
TagContainsKeywordsPredicate predicate = preparePredicate(" "); | ||
FindByTagCommand command = new FindByTagCommand(predicate); | ||
expectedModel.updateFilteredPersonList(predicate); | ||
assertCommandSuccess(command, model, expectedMessage, expectedModel); | ||
assertEquals(Collections.emptyList(), model.getFilteredPersonList()); | ||
} | ||
|
||
@Test | ||
public void execute_resultsWithPartialMatch_multiplePersonsFound() { | ||
System.out.println(expectedModel.getAddressBook().toString()); | ||
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3); | ||
TagContainsKeywordsPredicate predicate = preparePredicate("en"); | ||
FindByTagCommand command = new FindByTagCommand(predicate); | ||
expectedModel.updateFilteredPersonList(predicate); | ||
assertCommandSuccess(command, model, expectedMessage, expectedModel); | ||
assertEquals(Arrays.asList(ALICE, BENSON, DANIEL), model.getFilteredPersonList()); | ||
} | ||
|
||
@Test | ||
public void toStringMethod() { | ||
TagContainsKeywordsPredicate predicate = new TagContainsKeywordsPredicate(List.of("keyword")); | ||
FindByTagCommand findCommand = new FindByTagCommand(predicate); | ||
String expected = FindByTagCommand.class.getCanonicalName() + "{predicate=" + predicate + "}"; | ||
assertEquals(expected, findCommand.toString()); | ||
} | ||
|
||
/** | ||
* Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}. | ||
*/ | ||
private TagContainsKeywordsPredicate preparePredicate(String userInput) { | ||
return new TagContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+"))); | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -12,9 +12,11 @@ | |
import seedu.address.logic.commands.FindByContactCommand; | ||
import seedu.address.logic.commands.FindByEmailCommand; | ||
import seedu.address.logic.commands.FindByNameCommand; | ||
import seedu.address.logic.commands.FindByTagCommand; | ||
import seedu.address.model.person.ContactContainsKeywordsPredicate; | ||
import seedu.address.model.person.EmailContainsKeywordsPredicate; | ||
import seedu.address.model.person.NameContainsKeywordsPredicate; | ||
import seedu.address.model.person.TagContainsKeywordsPredicate; | ||
|
||
public class FindCommandParserTest { | ||
|
||
|
@@ -64,4 +66,17 @@ public void parse_validArgs_returnsFindByEmailCommand() { | |
assertParseSuccess(parser, "e/ \n [email protected] \n \t [email protected] \t", expectedFindCommand); | ||
} | ||
|
||
@Test | ||
public void parse_validArgs_returnsFindByTagCommand() { | ||
FindByTagCommand expectedFindCommand = | ||
new FindByTagCommand(new TagContainsKeywordsPredicate( | ||
Arrays.asList("PC2174ALecturer", "PC2032classmate"))); | ||
|
||
// no leading and trailing whitespaces | ||
assertParseSuccess(parser, "t/PC2174ALecturer PC2032classmate", expectedFindCommand); | ||
|
||
// multiple whitespaces between keywords | ||
assertParseSuccess(parser, "t/ \n PC2174ALecturer \n \t PC2032classmate \t", expectedFindCommand); | ||
} | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
src/test/java/seedu/address/model/person/TagContainsKeywordsPredicateTest.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,86 @@ | ||
package seedu.address.model.person; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.testutil.PersonBuilder; | ||
|
||
public class TagContainsKeywordsPredicateTest { | ||
|
||
@Test | ||
public void equals() { | ||
List<String> firstPredicateKeywordList = Collections.singletonList("first"); | ||
List<String> secondPredicateKeywordList = Arrays.asList("first", "second"); | ||
|
||
TagContainsKeywordsPredicate firstPredicate = new TagContainsKeywordsPredicate(firstPredicateKeywordList); | ||
TagContainsKeywordsPredicate secondPredicate = new TagContainsKeywordsPredicate(secondPredicateKeywordList); | ||
|
||
// same object -> returns true | ||
assertTrue(firstPredicate.equals(firstPredicate)); | ||
|
||
// same values -> returns true | ||
TagContainsKeywordsPredicate firstPredicateCopy = new TagContainsKeywordsPredicate(firstPredicateKeywordList); | ||
assertTrue(firstPredicate.equals(firstPredicateCopy)); | ||
|
||
// different types -> returns false | ||
assertFalse(firstPredicate.equals(1)); | ||
|
||
// null -> returns false | ||
assertFalse(firstPredicate.equals(null)); | ||
|
||
// different person -> returns false | ||
assertFalse(firstPredicate.equals(secondPredicate)); | ||
} | ||
|
||
@Test | ||
public void test_tagContainsKeywords_returnsTrue() { | ||
// One keyword | ||
TagContainsKeywordsPredicate predicate = | ||
new TagContainsKeywordsPredicate(Collections.singletonList("CS")); | ||
assertTrue(predicate.test(new PersonBuilder().withTags("CS2100", "PC2174A").build())); | ||
|
||
// Multiple keywords | ||
predicate = new TagContainsKeywordsPredicate(Arrays.asList("CS", "PC")); | ||
assertTrue(predicate.test(new PersonBuilder().withTags("CS2100", "PC2174A").build())); | ||
|
||
// Only one matching keyword | ||
predicate = new TagContainsKeywordsPredicate(Arrays.asList("IS1108", "NUSFloorball")); | ||
assertTrue(predicate.test(new PersonBuilder().withTags("NUSFloorball", "PC2174A").build())); | ||
|
||
// Mixed-case keywords | ||
predicate = new TagContainsKeywordsPredicate(Arrays.asList("nUsfLoOrBaLL", "NUscHoIr")); | ||
assertTrue(predicate.test(new PersonBuilder().withTags("NUSFloorball", "NUSChoir").build())); | ||
} | ||
|
||
@Test | ||
public void test_tagDoesNotContainKeywords_returnsFalse() { | ||
// Zero keywords | ||
TagContainsKeywordsPredicate predicate = new TagContainsKeywordsPredicate(Collections.emptyList()); | ||
assertFalse(predicate.test(new PersonBuilder().withTags("CS2100", "PC2174A").build())); | ||
|
||
// Non-matching keyword | ||
predicate = new TagContainsKeywordsPredicate(Arrays.asList("IS", "8")); | ||
assertFalse(predicate.test(new PersonBuilder().withTags("CS2100", "PC2174A").build())); | ||
|
||
// Keywords match phone, email address and name, but does not match tag | ||
predicate = new TagContainsKeywordsPredicate(Arrays.asList("12345", "[email protected]", "Main", "Street")); | ||
assertFalse(predicate.test(new PersonBuilder().withTags("Alice").withPhone("12345") | ||
.withEmail("[email protected]").withAddress("Main Street").withTags("hehe").build())); | ||
} | ||
|
||
@Test | ||
public void toStringMethod() { | ||
List<String> keywords = List.of("keyword1", "keyword2"); | ||
TagContainsKeywordsPredicate predicate = new TagContainsKeywordsPredicate(keywords); | ||
|
||
String expected = TagContainsKeywordsPredicate.class.getCanonicalName() + "{keywords=" + keywords + "}"; | ||
assertEquals(expected, predicate.toString()); | ||
} | ||
} |