Skip to content

Commit

Permalink
Merge pull request AY2425S1-CS2103T-F15-2#123 from jessica2828/filter…
Browse files Browse the repository at this point in the history
…-command-tests

Update FilterCommand Tests
  • Loading branch information
JYL27 authored Nov 7, 2024
2 parents a914940 + 1302875 commit 9d8c3ee
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public CommandResult execute(Model model) {
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
29 changes: 27 additions & 2 deletions src/test/java/seedu/address/logic/commands/FilterCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ public void execute_zeroKeywords_noPersonFound() {
assertEquals(Collections.emptyList(), model.getFilteredPersonList());
}

@Test
public void executeNoKeywordsCourseFilter() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0);
CourseContainsKeywordsPredicate predicate =
new CourseContainsKeywordsPredicate(Collections.singletonList(" "));
FilterCommand command = new FilterCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getFilteredPersonList());
}

@Test
public void executeNoKeywordsModuleFilter() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0);
ModuleContainsKeywordsPredicate predicate =
new ModuleContainsKeywordsPredicate(" ");
FilterCommand command = new FilterCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
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);
Expand All @@ -78,7 +101,8 @@ public void execute_multipleKeywords_multiplePersonsFound() {

@Test
public void toStringMethod_namePredicate() {
NameContainsKeywordsPredicate predicate = new NameContainsKeywordsPredicate(Arrays.asList("keyword"));
NameContainsKeywordsPredicate predicate =
new NameContainsKeywordsPredicate(Arrays.asList("keyword"));
FilterCommand filterCommand = new FilterCommand(predicate);
String expected = FilterCommand.class.getCanonicalName()
+ "{namePredicate="
Expand All @@ -90,7 +114,8 @@ public void toStringMethod_namePredicate() {

@Test
public void toStringMethod_modulePredicate() {
ModuleContainsKeywordsPredicate modulePredicate = new ModuleContainsKeywordsPredicate("CS2103T");
ModuleContainsKeywordsPredicate modulePredicate =
new ModuleContainsKeywordsPredicate("CS2103T");
FilterCommand filterCommand = new FilterCommand(modulePredicate);
String expected = FilterCommand.class.getCanonicalName()
+ "{namePredicate=null, modulePredicate="
Expand Down
14 changes: 13 additions & 1 deletion src/test/java/seedu/address/model/ModelManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import org.junit.jupiter.api.Test;

import seedu.address.commons.core.GuiSettings;
import seedu.address.model.person.CourseContainsKeywordsPredicate;
import seedu.address.model.person.Module;
import seedu.address.model.person.ModuleContainsKeywordsPredicate;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.testutil.EduContactsBuilder;

Expand Down Expand Up @@ -167,11 +169,21 @@ public void equals() {
// different eduContacts -> returns false
assertFalse(modelManager.equals(new ModelManager(differentEduContacts, userPrefs)));

// different filteredList -> returns false
// different filteredList (NameContainsKeywordsPredicate) -> returns false
String[] keywords = ALICE.getName().fullName.split("\\s+");
modelManager.updateFilteredPersonList(new NameContainsKeywordsPredicate(Arrays.asList(keywords)));
assertFalse(modelManager.equals(new ModelManager(eduContacts, userPrefs)));

// different filteredList (ModuleContainsKeywordsPredicate) -> returns false
String moduleKeyword = "CS1010";
modelManager.updateFilteredPersonList(new ModuleContainsKeywordsPredicate(moduleKeyword));
assertFalse(modelManager.equals(new ModelManager(eduContacts, userPrefs)));

// different filteredList (CourseContainsKeywordsPredicate) -> return false
String courseKeyword = "Computer Science";
modelManager.updateFilteredPersonList(new CourseContainsKeywordsPredicate(Arrays.asList(courseKeyword)));
assertFalse(modelManager.equals(new ModelManager(eduContacts, userPrefs)));

// different personToDisplay -> returns false
assertFalse(modelManager.equals(new ModelManager(eduContacts, userPrefs)));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 org.junit.jupiter.api.Test;

import seedu.address.testutil.PersonBuilder;

public class ModuleContainsKeywordsPredicateTest {

@Test
public void equals() {
String firstPredicateKeyword = "CS1010";
String secondPredicateKeyword = "CS2030";

ModuleContainsKeywordsPredicate firstPredicate =
new ModuleContainsKeywordsPredicate(firstPredicateKeyword);
ModuleContainsKeywordsPredicate secondPredicate =
new ModuleContainsKeywordsPredicate(secondPredicateKeyword);

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

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

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

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

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

@Test
public void test_moduleContainsKeyword_returnsTrue() {
ModuleContainsKeywordsPredicate predicate = new ModuleContainsKeywordsPredicate("CS1010");
assertTrue(predicate.test(new PersonBuilder().addUngradedModule("CS1010").build()));

predicate = new ModuleContainsKeywordsPredicate("cs1010");
assertTrue(predicate.test(new PersonBuilder().addUngradedModule("CS1010").build()));

predicate = new ModuleContainsKeywordsPredicate("1010");
assertTrue(predicate.test(new PersonBuilder().addUngradedModule("CS1010").build()));
}

@Test
public void test_moduleDoesNotContainKeyword_returnsFalse() {
ModuleContainsKeywordsPredicate predicate = new ModuleContainsKeywordsPredicate("CS9999");
assertFalse(predicate.test(new PersonBuilder().addUngradedModule("CS2030").build()));
}

@Test
public void toStringMethod() {
String keyword = "CS1010";
ModuleContainsKeywordsPredicate predicate = new ModuleContainsKeywordsPredicate(keyword);

String expected = ModuleContainsKeywordsPredicate.class.getCanonicalName() + "{keywords=" + keyword + "}";
assertEquals(expected, predicate.toString());
}
}

0 comments on commit 9d8c3ee

Please sign in to comment.