From 90c7eda21a16ac2d59f57096985a14318f4a2a3d Mon Sep 17 00:00:00 2001 From: marcellaantania Date: Thu, 19 Oct 2023 04:04:26 +0800 Subject: [PATCH] Add more tests --- .../person/HasLeaveAnyMonthPredicate.java | 6 +--- .../logic/commands/ViewLeaveCommandTest.java | 2 ++ .../logic/parser/AddressBookParserTest.java | 6 ++++ .../parser/ViewLeaveCommandParserTest.java | 33 ++++++++++++++--- .../person/HasLeaveAnyMonthPredicateTest.java | 35 ++++++++++++++++++ .../HasLeaveThisMonthPredicateTest.java | 36 +++++++++++++++++++ 6 files changed, 108 insertions(+), 10 deletions(-) create mode 100644 src/test/java/seedu/address/model/person/HasLeaveAnyMonthPredicateTest.java create mode 100644 src/test/java/seedu/address/model/person/HasLeaveThisMonthPredicateTest.java diff --git a/src/main/java/seedu/address/model/person/HasLeaveAnyMonthPredicate.java b/src/main/java/seedu/address/model/person/HasLeaveAnyMonthPredicate.java index 2a683fe4e74..f7e8d350b80 100644 --- a/src/main/java/seedu/address/model/person/HasLeaveAnyMonthPredicate.java +++ b/src/main/java/seedu/address/model/person/HasLeaveAnyMonthPredicate.java @@ -25,10 +25,6 @@ public boolean equals(Object other) { } // instanceof handles nulls - if (!(other instanceof HasLeaveThisMonthPredicate)) { - return false; - } - - return true; + return other instanceof HasLeaveAnyMonthPredicate; } } diff --git a/src/test/java/seedu/address/logic/commands/ViewLeaveCommandTest.java b/src/test/java/seedu/address/logic/commands/ViewLeaveCommandTest.java index 33687e480a0..60e32de3c2a 100644 --- a/src/test/java/seedu/address/logic/commands/ViewLeaveCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/ViewLeaveCommandTest.java @@ -30,8 +30,10 @@ public void equalsTest() { ViewLeaveCommand command1 = new ViewLeaveCommand(predicate1); ViewLeaveCommand command2 = new ViewLeaveCommand(predicate2); ViewLeaveCommand command3 = new ViewLeaveCommand(new HasLeaveThisMonthPredicate("12")); + assertEquals(command1, command1); assertEquals(command1, command2); assertFalse(command1.equals(command3)); + assertFalse(command1.equals(null)); } @Test diff --git a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java index 24180ef6736..dcbb003bfbd 100644 --- a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java @@ -24,6 +24,7 @@ import seedu.address.logic.commands.HelpCommand; import seedu.address.logic.commands.LeaveCommand; import seedu.address.logic.commands.ListCommand; +import seedu.address.logic.commands.ViewLeaveCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.person.NameContainsKeywordsPredicate; import seedu.address.model.person.Person; @@ -97,6 +98,11 @@ public void parseCommand_leave() throws Exception { assertTrue(parser.parseCommand(LeaveCommand.COMMAND_WORD + " 1 m/2") instanceof LeaveCommand); } + @Test + public void parseCommand_viewLeave() throws Exception { + assertTrue(parser.parseCommand(ViewLeaveCommand.COMMAND_WORD + " m/2") instanceof ViewLeaveCommand); + } + @Test public void parseCommand_unrecognisedInput_throwsParseException() { assertThrows(ParseException.class, String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE), () diff --git a/src/test/java/seedu/address/logic/parser/ViewLeaveCommandParserTest.java b/src/test/java/seedu/address/logic/parser/ViewLeaveCommandParserTest.java index caea6dda801..8aa27dcf7d6 100644 --- a/src/test/java/seedu/address/logic/parser/ViewLeaveCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/ViewLeaveCommandParserTest.java @@ -1,13 +1,19 @@ package seedu.address.logic.parser; import static seedu.address.logic.commands.CommandTestUtil.VALID_DEPARTMENT; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; +import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; import java.util.function.Predicate; import org.junit.jupiter.api.Test; import seedu.address.logic.Messages; +import seedu.address.logic.parser.exceptions.ParseException; +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; import seedu.address.model.person.Department; import seedu.address.model.person.HasLeaveAnyMonthPredicate; import seedu.address.model.person.HasLeaveThisMonthPredicate; @@ -16,20 +22,37 @@ class ViewLeaveCommandParserTest { + private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + private ViewLeaveCommandParser parser = new ViewLeaveCommandParser(); @Test - void parse_success() { + void parse_success() throws ParseException { Predicate testPredicateAnyMonth = new HasLeaveAnyMonthPredicate(); Predicate testPredicateMonth = new HasLeaveThisMonthPredicate("3"); Predicate testPredicateMonths = testPredicateMonth.and(new HasLeaveThisMonthPredicate("1")); Predicate testPredicateMonthAndDept = testPredicateMonth.and( new MatchingDepartmentPredicate(new Department(VALID_DEPARTMENT))); - //assertParseSuccess(parser, "", new ViewLeaveCommand(testPredicateAnyMonth)); - //assertParseSuccess(parser, " m/3", new ViewLeaveCommand(testPredicateMonth)); - //assertParseSuccess(parser, " m/1,3", new ViewLeaveCommand(testPredicateMonths)); - //assertParseSuccess(parser, " m/1 d/Engineering", new ViewLeaveCommand(testPredicateMonthAndDept)); + Model expectedModel1 = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + expectedModel1.updateFilteredPersonList(testPredicateAnyMonth); + assertCommandSuccess(parser.parse(""), model, String.format(Messages.MESSAGE_VIEW_LEAVE_SUCCESS, + expectedModel1.getFilteredPersonList().size()), expectedModel1); + + Model expectedModel2 = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + expectedModel2.updateFilteredPersonList(testPredicateMonth); + assertCommandSuccess(parser.parse(" m/3"), model, String.format(Messages.MESSAGE_VIEW_LEAVE_SUCCESS, + expectedModel2.getFilteredPersonList().size()), expectedModel2); + + Model expectedModel3 = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + expectedModel3.updateFilteredPersonList(testPredicateMonths); + assertCommandSuccess(parser.parse(" m/1,3"), model, String.format(Messages.MESSAGE_VIEW_LEAVE_SUCCESS, + expectedModel3.getFilteredPersonList().size()), expectedModel3); + + Model expectedModel4 = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + expectedModel4.updateFilteredPersonList(testPredicateMonthAndDept); + assertCommandSuccess(parser.parse(" m/1 d/Engineering"), model, String.format(Messages.MESSAGE_VIEW_LEAVE_SUCCESS, + expectedModel4.getFilteredPersonList().size()), expectedModel4); } @Test diff --git a/src/test/java/seedu/address/model/person/HasLeaveAnyMonthPredicateTest.java b/src/test/java/seedu/address/model/person/HasLeaveAnyMonthPredicateTest.java new file mode 100644 index 00000000000..3cd39a949af --- /dev/null +++ b/src/test/java/seedu/address/model/person/HasLeaveAnyMonthPredicateTest.java @@ -0,0 +1,35 @@ +package seedu.address.model.person; + +import org.junit.jupiter.api.Test; +import seedu.address.testutil.PersonBuilder; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class HasLeaveAnyMonthPredicateTest { + + @Test + void test_hasLeaveAnyMonth_returnsTrue() { + HasLeaveAnyMonthPredicate predicate1 = new HasLeaveAnyMonthPredicate(); + assertTrue(predicate1.test(new PersonBuilder().withLeave("111001000011").build())); + } + + @Test + void test_hasLeaveAnyMonth_returnsFalse() { + HasLeaveAnyMonthPredicate predicate1 = new HasLeaveAnyMonthPredicate(); + assertFalse(predicate1.test(new PersonBuilder().withLeave("000000000000").build())); + } + + @Test + void testEquals() { + HasLeaveAnyMonthPredicate predicate1 = new HasLeaveAnyMonthPredicate(); + HasLeaveAnyMonthPredicate predicate2 = new HasLeaveAnyMonthPredicate(); + + assertEquals(predicate1, predicate1); + assertEquals(predicate1, predicate2); + assertNotEquals(predicate1, null); + assertNotEquals(predicate1, 1); + } +} \ No newline at end of file diff --git a/src/test/java/seedu/address/model/person/HasLeaveThisMonthPredicateTest.java b/src/test/java/seedu/address/model/person/HasLeaveThisMonthPredicateTest.java new file mode 100644 index 00000000000..3de8f761076 --- /dev/null +++ b/src/test/java/seedu/address/model/person/HasLeaveThisMonthPredicateTest.java @@ -0,0 +1,36 @@ +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.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; +import seedu.address.testutil.PersonBuilder; + +class HasLeaveThisMonthPredicateTest { + + @Test + void test_hasLeaveThisMonth_returnsTrue() { + HasLeaveThisMonthPredicate predicate1 = new HasLeaveThisMonthPredicate("1"); + assertTrue(predicate1.test(new PersonBuilder().withLeave("111001000011").build())); + } + + @Test + void test_hasLeaveThisMonth_returnsFalse() { + HasLeaveThisMonthPredicate predicate1 = new HasLeaveThisMonthPredicate("4"); + assertFalse(predicate1.test(new PersonBuilder().withLeave("111001000011").build())); + } + + @Test + void testEquals() { + HasLeaveThisMonthPredicate predicate1 = new HasLeaveThisMonthPredicate("1"); + HasLeaveThisMonthPredicate predicate2 = new HasLeaveThisMonthPredicate("1"); + HasLeaveThisMonthPredicate predicate3 = new HasLeaveThisMonthPredicate("12"); + assertEquals(predicate1, predicate1); + assertEquals(predicate1, predicate2); + assertNotEquals(predicate1, predicate3); + assertNotEquals(predicate1, null); + assertNotEquals(predicate1, 1); + } +} \ No newline at end of file