From 3a6c45c10b65fda98c1948c1aa6d815c665cf756 Mon Sep 17 00:00:00 2001 From: iynixil Date: Wed, 13 Mar 2024 16:11:29 +0800 Subject: [PATCH] Fix style --- .../logic/commands/FilterCommand.java | 3 +- .../logic/parser/FilterCommandParser.java | 1 - .../logic/commands/FilterCommandTest.java | 89 +++++++++++++++++++ .../logic/parser/FilterCommandParserTest.java | 53 +++++++++++ .../logic/parser/StaffConnectParserTest.java | 11 +++ .../person/PersonHasTagPredicateTest.java | 73 +++++++++++++++ 6 files changed, 227 insertions(+), 3 deletions(-) create mode 100644 src/test/java/staffconnect/logic/commands/FilterCommandTest.java create mode 100644 src/test/java/staffconnect/logic/parser/FilterCommandParserTest.java create mode 100644 src/test/java/staffconnect/model/person/PersonHasTagPredicateTest.java diff --git a/src/main/java/staffconnect/logic/commands/FilterCommand.java b/src/main/java/staffconnect/logic/commands/FilterCommand.java index 9faac02fe10..5a8b51eda3a 100644 --- a/src/main/java/staffconnect/logic/commands/FilterCommand.java +++ b/src/main/java/staffconnect/logic/commands/FilterCommand.java @@ -1,13 +1,12 @@ package staffconnect.logic.commands; import static java.util.Objects.requireNonNull; - -import staffconnect.logic.Messages; // import static staffconnect.logic.parser.CliSyntax.PREFIX_FACULTY; // TODO: add filtering for faculty and module // import static staffconnect.logic.parser.CliSyntax.PREFIX_MODULE; import static staffconnect.logic.parser.CliSyntax.PREFIX_TAG; import staffconnect.commons.util.ToStringBuilder; +import staffconnect.logic.Messages; import staffconnect.model.Model; import staffconnect.model.person.PersonHasTagPredicate; // TagContainsKeywordsPredicate diff --git a/src/main/java/staffconnect/logic/parser/FilterCommandParser.java b/src/main/java/staffconnect/logic/parser/FilterCommandParser.java index f4b96db974b..cdaf2d5954a 100644 --- a/src/main/java/staffconnect/logic/parser/FilterCommandParser.java +++ b/src/main/java/staffconnect/logic/parser/FilterCommandParser.java @@ -6,7 +6,6 @@ // import static staffconnect.logic.parser.CliSyntax.PREFIX_MODULE; import static staffconnect.logic.parser.CliSyntax.PREFIX_TAG; - import staffconnect.commons.exceptions.IllegalValueException; import staffconnect.logic.commands.FilterCommand; import staffconnect.logic.parser.exceptions.ParseException; diff --git a/src/test/java/staffconnect/logic/commands/FilterCommandTest.java b/src/test/java/staffconnect/logic/commands/FilterCommandTest.java new file mode 100644 index 00000000000..a3d04c1de0f --- /dev/null +++ b/src/test/java/staffconnect/logic/commands/FilterCommandTest.java @@ -0,0 +1,89 @@ +package staffconnect.logic.commands; + +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 static staffconnect.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW; +import static staffconnect.logic.commands.CommandTestUtil.assertCommandSuccess; +import static staffconnect.testutil.TypicalPersons.ALICE; +import static staffconnect.testutil.TypicalPersons.BENSON; +import static staffconnect.testutil.TypicalPersons.DANIEL; +import static staffconnect.testutil.TypicalPersons.getTypicalStaffBook; + +import java.util.Arrays; +import java.util.Collections; + +import org.junit.jupiter.api.Test; + +import staffconnect.model.Model; +import staffconnect.model.ModelManager; +import staffconnect.model.UserPrefs; +import staffconnect.model.person.PersonHasTagPredicate; +import staffconnect.model.tag.Tag; + +public class FilterCommandTest { + + private Model model = new ModelManager(getTypicalStaffBook(), new UserPrefs()); + private Model expectedModel = new ModelManager(getTypicalStaffBook(), new UserPrefs()); + + @Test + public void execute_personHasTag_noPersonFound() { + String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0); + PersonHasTagPredicate tagPredicate = prepareTagPredicate("hello"); + FilterCommand command = new FilterCommand(tagPredicate); + expectedModel.updateFilteredPersonList(tagPredicate); + assertCommandSuccess(command, model, expectedMessage, model); + assertEquals(Collections.emptyList(), model.getFilteredPersonList()); + } + + @Test + public void execute_personHasTag_multiplePersonsFound() { + String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3); + PersonHasTagPredicate tagPredicate = prepareTagPredicate("friends"); + FilterCommand command = new FilterCommand(tagPredicate); + expectedModel.updateFilteredPersonList(tagPredicate); + assertCommandSuccess(command, model, expectedMessage, model); + assertEquals(Arrays.asList(ALICE, BENSON, DANIEL), model.getFilteredPersonList()); + } + + @Test + public void equals() { + PersonHasTagPredicate firstTagPredicate = new PersonHasTagPredicate(new Tag("friend")); + PersonHasTagPredicate secondTagPredicate = new PersonHasTagPredicate(new Tag("colleagues")); + + FilterCommand filterTagFirstCommand = new FilterCommand(firstTagPredicate); + FilterCommand filterTagSecondCommand = new FilterCommand(secondTagPredicate); + + // same object -> returns true + assertTrue(filterTagFirstCommand.equals(filterTagFirstCommand)); + + // same values -> returns true + FilterCommand filterTagFirstCommandCopy = new FilterCommand(firstTagPredicate); + assertTrue(filterTagFirstCommand.equals(filterTagFirstCommandCopy)); + + // different types -> returns false + assertFalse(filterTagFirstCommand.equals(1)); + + // null -> returns false + assertFalse(filterTagFirstCommand.equals(null)); + + // different tag -> returns false + assertFalse(filterTagFirstCommand.equals(filterTagSecondCommand)); + } + + @Test + public void toStringMethod() { + PersonHasTagPredicate tagPredicate = new PersonHasTagPredicate(new Tag("hello")); + FilterCommand filterCommand = new FilterCommand(tagPredicate); + String expected = FilterCommand.class.getCanonicalName() + "{tagPredicate=" + tagPredicate + "}"; + assertEquals(expected, filterCommand.toString()); + } + + /** + * Parses {@code userInput} into a {@code PersonHasTagPredicate}. + */ + private PersonHasTagPredicate prepareTagPredicate(String userInput) { + return new PersonHasTagPredicate(new Tag(userInput)); + } + +} diff --git a/src/test/java/staffconnect/logic/parser/FilterCommandParserTest.java b/src/test/java/staffconnect/logic/parser/FilterCommandParserTest.java new file mode 100644 index 00000000000..f62a0565ff0 --- /dev/null +++ b/src/test/java/staffconnect/logic/parser/FilterCommandParserTest.java @@ -0,0 +1,53 @@ +package staffconnect.logic.parser; + +import static staffconnect.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static staffconnect.logic.commands.CommandTestUtil.INVALID_TAG_DESC; +import static staffconnect.logic.commands.CommandTestUtil.TAG_DESC_FRIEND; +import static staffconnect.logic.commands.CommandTestUtil.VALID_TAG_FRIEND; +import static staffconnect.logic.parser.CliSyntax.PREFIX_TAG; +import static staffconnect.logic.parser.CommandParserTestUtil.assertParseFailure; +import static staffconnect.logic.parser.CommandParserTestUtil.assertParseSuccess; + +import org.junit.jupiter.api.Test; + +import staffconnect.logic.commands.FilterCommand; +import staffconnect.model.person.PersonHasTagPredicate; +import staffconnect.model.tag.Tag; + +public class FilterCommandParserTest { + + private static final String TAG_EMPTY = " " + PREFIX_TAG; // should have a space before tag prefix + + private FilterCommandParser parser = new FilterCommandParser(); + + @Test + public void parse_emptyArg_throwsParseException() { + assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, FilterCommand.MESSAGE_USAGE)); + } + + @Test + public void parse_invalidTagName_throwsParseException() { + // no whitespace before PREFIX_TAG + assertParseFailure(parser, PREFIX_TAG + VALID_TAG_FRIEND, + String.format(MESSAGE_INVALID_COMMAND_FORMAT, FilterCommand.MESSAGE_USAGE)); + // tagname is non-alphanumeric (contains '*') + assertParseFailure(parser, INVALID_TAG_DESC, + String.format(MESSAGE_INVALID_COMMAND_FORMAT, FilterCommand.MESSAGE_USAGE)); + + } + + @Test + public void parse_validTagName_success() { + // 1 leading and no trailing whitespaces + FilterCommand expectedFilterCommand = new FilterCommand(new PersonHasTagPredicate(new Tag(VALID_TAG_FRIEND))); + assertParseSuccess(parser, TAG_DESC_FRIEND, expectedFilterCommand); + + // 1 leading and multiple trailing whitespaces + assertParseSuccess(parser, TAG_DESC_FRIEND + " ", expectedFilterCommand); // 1 leading, 3 trailing + + // multiple leading and trailing whitespaces + assertParseSuccess(parser, " " + TAG_DESC_FRIEND + " ", expectedFilterCommand); // 2 leading, 1 trailing + assertParseSuccess(parser, " " + TAG_DESC_FRIEND + " ", expectedFilterCommand); // 5 leading, 3 trailing + } + +} diff --git a/src/test/java/staffconnect/logic/parser/StaffConnectParserTest.java b/src/test/java/staffconnect/logic/parser/StaffConnectParserTest.java index 7662e5b73ff..657444f98b5 100644 --- a/src/test/java/staffconnect/logic/parser/StaffConnectParserTest.java +++ b/src/test/java/staffconnect/logic/parser/StaffConnectParserTest.java @@ -19,12 +19,15 @@ import staffconnect.logic.commands.EditCommand; import staffconnect.logic.commands.EditCommand.EditPersonDescriptor; import staffconnect.logic.commands.ExitCommand; +import staffconnect.logic.commands.FilterCommand; import staffconnect.logic.commands.FindCommand; import staffconnect.logic.commands.HelpCommand; import staffconnect.logic.commands.ListCommand; import staffconnect.logic.parser.exceptions.ParseException; import staffconnect.model.person.NameContainsKeywordsPredicate; import staffconnect.model.person.Person; +import staffconnect.model.person.PersonHasTagPredicate; +import staffconnect.model.tag.Tag; import staffconnect.testutil.EditPersonDescriptorBuilder; import staffconnect.testutil.PersonBuilder; import staffconnect.testutil.PersonUtil; @@ -68,6 +71,14 @@ public void parseCommand_exit() throws Exception { assertTrue(parser.parseCommand(ExitCommand.COMMAND_WORD + " 3") instanceof ExitCommand); } + @Test + public void parseCommand_filter() throws Exception { + String tag = "hello"; + FilterCommand command = (FilterCommand) parser.parseCommand(FilterCommand.COMMAND_WORD + + " t/" + tag); + assertEquals(new FilterCommand(new PersonHasTagPredicate(new Tag(tag))), command); + } + @Test public void parseCommand_find() throws Exception { List keywords = Arrays.asList("foo", "bar", "baz"); diff --git a/src/test/java/staffconnect/model/person/PersonHasTagPredicateTest.java b/src/test/java/staffconnect/model/person/PersonHasTagPredicateTest.java new file mode 100644 index 00000000000..a99e7dcf0ca --- /dev/null +++ b/src/test/java/staffconnect/model/person/PersonHasTagPredicateTest.java @@ -0,0 +1,73 @@ +package staffconnect.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 staffconnect.model.tag.Tag; +import staffconnect.testutil.PersonBuilder; + +public class PersonHasTagPredicateTest { + + @Test + public void equals() { + Tag firstPredicateTag = new Tag("first"); + Tag secondPredicateTag = new Tag("second"); + + PersonHasTagPredicate firstPredicate = new PersonHasTagPredicate(firstPredicateTag); + PersonHasTagPredicate secondPredicate = new PersonHasTagPredicate(secondPredicateTag); + + // same object -> returns true + assertTrue(firstPredicate.equals(firstPredicate)); + + // same values -> returns true + PersonHasTagPredicate firstPredicateCopy = new PersonHasTagPredicate(firstPredicateTag); + 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_personHasTag_returnsTrue() { + // predicate set to track "tester" tag + PersonHasTagPredicate predicate = new PersonHasTagPredicate(new Tag("tester")); + + // person only has tag "tester" + assertTrue(predicate.test(new PersonBuilder().withTags("tester").build())); + + // person has multiple tags and has "tester" + assertTrue(predicate.test(new PersonBuilder().withTags("tester", "tester2").build())); + + // case-insensitivity checks + assertTrue(predicate.test(new PersonBuilder().withTags("tesTER").build())); + assertTrue(predicate.test(new PersonBuilder().withTags("TESTER").build())); + assertTrue(predicate.test(new PersonBuilder().withTags("tEsTeR").build())); + } + + @Test + public void test_personDoesNotHaveTag_returnsFalse() { + // predicate set to track "tester" tag + PersonHasTagPredicate predicate = new PersonHasTagPredicate(new Tag("tester")); + + // person does not have tag "tester" + assertFalse(predicate.test(new PersonBuilder().withTags("tester2").build())); + } + + @Test + public void toStringMethod() { + Tag tag = new Tag("hello"); + PersonHasTagPredicate predicate = new PersonHasTagPredicate(tag); + + String expected = PersonHasTagPredicate.class.getCanonicalName() + "{tag name=" + tag + "}"; + assertEquals(expected, predicate.toString()); + } +}