Skip to content

Commit

Permalink
Add tests for relevant classes
Browse files Browse the repository at this point in the history
  • Loading branch information
yockcheng committed Oct 29, 2024
1 parent f1c7e40 commit 1073165
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 34 deletions.
22 changes: 14 additions & 8 deletions src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import java.util.function.Predicate;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.model.Model;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.Person;
import seedu.address.model.person.TagContainsKeywordsPredicate;

/**
Expand All @@ -31,16 +28,24 @@ public class FindCommand extends Command {
+ " 2. " + COMMAND_WORD + " " + PREFIX_TAG + "friend " + PREFIX_TAG + "owesMoney\n"
+ " 3. " + COMMAND_WORD + " alice " + PREFIX_TAG + "isRich";

private final Predicate<Person> predicate;
private final NameContainsKeywordsPredicate namePredicate;
private final TagContainsKeywordsPredicate tagPredicate;

/**
* Creates a {@code FindCommand} with the specified name and tag predicates.
*
* @param namePredicate The predicate used to match persons by name.
* @param tagPredicate The predicate used to match persons by tag.
*/
public FindCommand(NameContainsKeywordsPredicate namePredicate, TagContainsKeywordsPredicate tagPredicate) {
this.predicate = person -> namePredicate.test(person) || tagPredicate.test(person);
this.namePredicate = namePredicate;
this.tagPredicate = tagPredicate;
}

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredPersonList(predicate);
model.updateFilteredPersonList(namePredicate.or(tagPredicate));
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
}
Expand All @@ -57,13 +62,14 @@ public boolean equals(Object other) {
}

FindCommand otherFindCommand = (FindCommand) other;
return predicate.equals(otherFindCommand.predicate);
return namePredicate.equals(otherFindCommand.namePredicate)
&& tagPredicate.equals(otherFindCommand.tagPredicate);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("predicate", predicate)
.add("predicates", " " + namePredicate + ", " + tagPredicate)
.toString();
}
}
65 changes: 47 additions & 18 deletions src/test/java/seedu/address/logic/commands/FindCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalPersons.BENSON;
import static seedu.address.testutil.TypicalPersons.CARL;
import static seedu.address.testutil.TypicalPersons.ELLE;
import static seedu.address.testutil.TypicalPersons.FIONA;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import java.util.Arrays;
import java.util.Collections;
import java.util.Set;

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.NameContainsKeywordsPredicate;
import seedu.address.model.person.TagContainsKeywordsPredicate;
import seedu.address.model.tag.Tag;

/**
* Contains integration tests (interaction with the Model) for {@code FindCommand}.
Expand All @@ -29,21 +33,34 @@ public class FindCommandTest {

@Test
public void equals() {
NameContainsKeywordsPredicate firstPredicate =
NameContainsKeywordsPredicate firstNamePredicate =
new NameContainsKeywordsPredicate(Collections.singletonList("first"));
NameContainsKeywordsPredicate secondPredicate =
NameContainsKeywordsPredicate secondNamePredicate =
new NameContainsKeywordsPredicate(Collections.singletonList("second"));

FindCommand findFirstCommand = new FindCommand(firstPredicate);
FindCommand findSecondCommand = new FindCommand(secondPredicate);
TagContainsKeywordsPredicate firstTagPredicate =
new TagContainsKeywordsPredicate(Set.of(new Tag("friend")));
TagContainsKeywordsPredicate secondTagPredicate =
new TagContainsKeywordsPredicate(Set.of(new Tag("colleague")));

FindCommand findFirstCommand = new FindCommand(firstNamePredicate, firstTagPredicate);
FindCommand findSecondCommand = new FindCommand(secondNamePredicate, secondTagPredicate);

// same object -> returns true
assertTrue(findFirstCommand.equals(findFirstCommand));

// same values -> returns true
FindCommand findFirstCommandCopy = new FindCommand(firstPredicate);
FindCommand findFirstCommandCopy = new FindCommand(firstNamePredicate, firstTagPredicate);
assertTrue(findFirstCommand.equals(findFirstCommandCopy));

FindCommand findFirstNameSecondTag = new FindCommand(firstNamePredicate, secondTagPredicate);

//different tag Predicate -> returns false
assertFalse(findFirstCommand.equals(findFirstNameSecondTag));

//different name Predicate -> returns false
assertFalse(findSecondCommand.equals(findFirstNameSecondTag));

// different types -> returns false
assertFalse(findFirstCommand.equals(1));

Expand All @@ -57,35 +74,47 @@ public void equals() {
@Test
public void execute_zeroKeywords_noPersonFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0);
NameContainsKeywordsPredicate predicate = preparePredicate(" ");
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
NameContainsKeywordsPredicate namePredicate = prepareNamePredicate(" ");
TagContainsKeywordsPredicate tagPredicate = prepareTagPredicate(Set.of());
FindCommand command = new FindCommand(namePredicate, tagPredicate);
expectedModel.updateFilteredPersonList(namePredicate.or(tagPredicate));
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getFilteredPersonList());
}

@Test
public void execute_multipleKeywords_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3);
NameContainsKeywordsPredicate predicate = preparePredicate("Kurz Elle Kunz");
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 4);
NameContainsKeywordsPredicate namePredicate = prepareNamePredicate("Kurz Elle Kunz");
TagContainsKeywordsPredicate tagPredicate = prepareTagPredicate(Set.of(new Tag("owesMoney")));
FindCommand command = new FindCommand(namePredicate, tagPredicate);
expectedModel.updateFilteredPersonList(namePredicate.or(tagPredicate));
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(CARL, ELLE, FIONA), model.getFilteredPersonList());
assertEquals(Arrays.asList(BENSON, CARL, ELLE, FIONA), model.getFilteredPersonList());
}

@Test
public void toStringMethod() {
NameContainsKeywordsPredicate predicate = new NameContainsKeywordsPredicate(Arrays.asList("keyword"));
FindCommand findCommand = new FindCommand(predicate);
String expected = FindCommand.class.getCanonicalName() + "{predicate=" + predicate + "}";
NameContainsKeywordsPredicate namePredicate = new NameContainsKeywordsPredicate(Arrays.asList("keyword"));
TagContainsKeywordsPredicate tagPredicate = new TagContainsKeywordsPredicate(Set.of(new Tag("keyTag")));

FindCommand findCommand = new FindCommand(namePredicate, tagPredicate);
String expected = FindCommand.class.getCanonicalName()
+ "{predicates=" + " " + namePredicate + ", " + tagPredicate + "}";
assertEquals(expected, findCommand.toString());
}

/**
* Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}.
*/
private NameContainsKeywordsPredicate preparePredicate(String userInput) {
return new NameContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+")));
private NameContainsKeywordsPredicate prepareNamePredicate(String userNameInput) {
return new NameContainsKeywordsPredicate(Arrays.asList(userNameInput.split("\\s+")));
}

/**
* Parses {@code userTagInput} into a {@code TagContainsKeywordsPredicate}.
*/
private TagContainsKeywordsPredicate prepareTagPredicate(Set<Tag> userTagInput) {
return new TagContainsKeywordsPredicate(userTagInput);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.Messages.MESSAGE_UNKNOWN_COMMAND;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TUTORIAL;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;
Expand All @@ -26,6 +29,8 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.Person;
import seedu.address.model.person.TagContainsKeywordsPredicate;
import seedu.address.model.tag.Tag;
import seedu.address.testutil.EditPersonDescriptorBuilder;
import seedu.address.testutil.PersonBuilder;
import seedu.address.testutil.PersonUtil;
Expand Down Expand Up @@ -71,10 +76,13 @@ public void parseCommand_exit() throws Exception {

@Test
public void parseCommand_find() throws Exception {
List<String> keywords = Arrays.asList("foo", "bar", "baz");
FindCommand command = (FindCommand) parser.parseCommand(
FindCommand.COMMAND_WORD + " " + keywords.stream().collect(Collectors.joining(" ")));
assertEquals(new FindCommand(new NameContainsKeywordsPredicate(keywords)), command);
List<String> nameKeywords = Arrays.asList("foo", "bar", "baz");
Set<Tag> tags = Set.of(new Tag("friend"), new Tag("isRich"));
String input = FindCommand.COMMAND_WORD + " " + nameKeywords.stream().collect(Collectors.joining(" ")) + " "
+ tags.stream().map(t -> PREFIX_TAG + t.tagName).collect(Collectors.joining(" "));
FindCommand command = (FindCommand) parser.parseCommand(input);
assertEquals(new FindCommand(
new NameContainsKeywordsPredicate(nameKeywords), new TagContainsKeywordsPredicate(tags)), command);
}

@Test
Expand All @@ -89,9 +97,10 @@ public void parseCommand_list() throws Exception {
assertTrue(parser.parseCommand(ListCommand.COMMAND_WORD + " 3") instanceof ListCommand);
}

@Test
public void parseCommand_unmark() throws Exception {
assertTrue(parser.parseCommand(UnmarkCommand.COMMAND_WORD) instanceof UnmarkCommand);
assertTrue(parser.parseCommand(UnmarkCommand.COMMAND_WORD + "3") instanceof UnmarkCommand);
assertTrue(parser.parseCommand(
UnmarkCommand.COMMAND_WORD + " 3 " + PREFIX_TUTORIAL + "1") instanceof UnmarkCommand);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;

import java.util.Arrays;
import java.util.Set;

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.FindCommand;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.TagContainsKeywordsPredicate;
import seedu.address.model.tag.Tag;

public class FindCommandParserTest {

Expand All @@ -23,12 +26,41 @@ public void parse_emptyArg_throwsParseException() {
@Test
public void parse_validArgs_returnsFindCommand() {
// no leading and trailing whitespaces
FindCommand expectedFindCommand =
new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob")));
FindCommand expectedFindCommand = new FindCommand(
new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob")),
new TagContainsKeywordsPredicate(Set.of()));
assertParseSuccess(parser, "Alice Bob", expectedFindCommand);

// multiple whitespaces between keywords
assertParseSuccess(parser, " \n Alice \n \t Bob \t", expectedFindCommand);
}

@Test
public void parse_validTagArgs_returnsFindCommand() {
// Prepare tag keywords
Set<Tag> tags = Set.of(new Tag("friend"), new Tag("isRich"));
FindCommand expectedFindCommand = new FindCommand(
new NameContainsKeywordsPredicate(Arrays.asList("")),
new TagContainsKeywordsPredicate(tags)
);
assertParseSuccess(parser, " t/friend t/isRich", expectedFindCommand);

assertParseSuccess(parser, "\n t/friend \n \t t/isRich \t", expectedFindCommand);
}

@Test
public void parse_validNameAndTagArgs_returnsFindCommand() {

// Expected command
FindCommand expectedFindCommand = new FindCommand(
new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob")),
new TagContainsKeywordsPredicate(Set.of(new Tag("friend"), new Tag("isRich")))
);

// No extra spaces
assertParseSuccess(parser, "Alice Bob t/friend t/isRich", expectedFindCommand);

// With extra spaces and newlines
assertParseSuccess(parser, " \n Alice \n Bob \t t/friend \n t/isRich", expectedFindCommand);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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.Collections;
import java.util.Set;

import org.junit.jupiter.api.Test;

import seedu.address.model.tag.Tag;
import seedu.address.testutil.PersonBuilder;

public class TagContainsKeywordsPredicateTest {

@Test
public void equals() {
Set<Tag> firstTagSet = Set.of(new Tag("friend"));
Set<Tag> secondTagSet = Set.of(new Tag("friend"), new Tag("colleague"));

TagContainsKeywordsPredicate firstPredicate = new TagContainsKeywordsPredicate(firstTagSet);
TagContainsKeywordsPredicate secondPredicate = new TagContainsKeywordsPredicate(secondTagSet);

// same object -> returns true
assertTrue(firstPredicate.equals(firstPredicate));

// same values -> returns true
TagContainsKeywordsPredicate firstPredicateCopy = new TagContainsKeywordsPredicate(firstTagSet);
assertTrue(firstPredicate.equals(firstPredicateCopy));

// different types -> returns false
assertFalse(firstPredicate.equals(1));

// null -> returns false
assertFalse(firstPredicate.equals(null));

// different tags -> returns false
assertFalse(firstPredicate.equals(secondPredicate));
}

@Test
public void test_tagContainsKeywords_returnsTrue() {
// One keyword
TagContainsKeywordsPredicate predicate = new TagContainsKeywordsPredicate(Set.of(new Tag("friend")));
assertTrue(predicate.test(new PersonBuilder().withTags("friend").build()));

// Multiple keywords
predicate = new TagContainsKeywordsPredicate(Set.of(new Tag("friend"), new Tag("colleague")));
assertTrue(predicate.test(new PersonBuilder().withTags("friend", "colleague").build()));

// Only one matching keyword
predicate = new TagContainsKeywordsPredicate(Set.of(new Tag("broke"), new Tag("colleague")));
assertTrue(predicate.test(new PersonBuilder().withTags("friend", "broke").build()));

// Mixed-case keywords
predicate = new TagContainsKeywordsPredicate(Set.of(new Tag("frIEnD"), new Tag("ColLeAGue")));
assertTrue(predicate.test(new PersonBuilder().withTags("friend", "colleague").build()));
}

@Test
public void test_tagDoesNotContainKeywords_returnsFalse() {
// Zero keywords
TagContainsKeywordsPredicate predicate = new TagContainsKeywordsPredicate(Collections.emptySet());
assertFalse(predicate.test(new PersonBuilder().withTags("friend").build()));

// Non-matching keyword
predicate = new TagContainsKeywordsPredicate(Set.of(new Tag("colleague")));
assertFalse(predicate.test(new PersonBuilder().withTags("friend").build()));

// Keywords match phone and address, but does not match tag
predicate = new TagContainsKeywordsPredicate(Set.of(new Tag("12345"),
new Tag("Main"), new Tag("Street")));
assertFalse(predicate.test(new PersonBuilder().withName("Alice").withPhone("12345").build()));
}

@Test
public void toStringMethod() {
Set<Tag> keyTags = Set.of(new Tag("keyword1"), new Tag("keyword2"));
TagContainsKeywordsPredicate predicate = new TagContainsKeywordsPredicate(keyTags);

String expected = TagContainsKeywordsPredicate.class.getCanonicalName() + "{keyTags=" + keyTags + "}";
assertEquals(expected, predicate.toString());
}
}

0 comments on commit 1073165

Please sign in to comment.