Skip to content

Commit

Permalink
Add unit tests for new FindByTagCommand class
Browse files Browse the repository at this point in the history
  • Loading branch information
KrashKart committed Oct 15, 2024
1 parent 2d8bebe commit fef1f31
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public boolean equals(Object other) {
}

// instanceof handles nulls
if (!(other instanceof NameContainsKeywordsPredicate)) {
if (!(other instanceof TagContainsKeywordsPredicate)) {
return false;
}

Expand Down
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+")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
}

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

0 comments on commit fef1f31

Please sign in to comment.