From f89e0f2d63e3687275626afbb76ab4d17464d9ea Mon Sep 17 00:00:00 2001 From: xCOLOURx Date: Thu, 19 Oct 2023 23:43:45 +0800 Subject: [PATCH] Fix Test Cases --- .../model/moduleplan/ModulePlanSemester.java | 18 +++ .../moduleplan/ModulePlanSemesterList.java | 40 ++++-- .../commands/AddCommandIntegrationTest.java | 4 +- .../logic/commands/DeleteCommandTest.java | 4 +- .../logic/commands/EditCommandTest.java | 2 +- .../logic/parser/AddCommandParserTest.java | 25 ++-- .../logic/parser/AddressBookParserTest.java | 2 +- .../logic/parser/DeleteCommandParserTest.java | 2 +- .../logic/parser/InfoCommandParserTest.java | 2 +- .../logic/parser/ModulePlanParserTest.java | 115 ++++++++++++++++++ .../seedu/address/model/ModelManagerTest.java | 5 + .../model/moduleplan/ModulePlanTest.java | 2 + 12 files changed, 195 insertions(+), 26 deletions(-) create mode 100644 src/test/java/seedu/address/logic/parser/ModulePlanParserTest.java diff --git a/src/main/java/seedu/address/model/moduleplan/ModulePlanSemester.java b/src/main/java/seedu/address/model/moduleplan/ModulePlanSemester.java index a372b2e9159..0de4a024be4 100644 --- a/src/main/java/seedu/address/model/moduleplan/ModulePlanSemester.java +++ b/src/main/java/seedu/address/model/moduleplan/ModulePlanSemester.java @@ -137,4 +137,22 @@ public String toString() { return "Year " + year.toString() + " " + semester.toString(); } + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof ModulePlanSemester)) { + return false; + } + + ModulePlanSemester otherModulePlanSemester = (ModulePlanSemester) other; + boolean yearEquals = this.year.equals(otherModulePlanSemester.year); + boolean semesterEquals = this.semester.equals(otherModulePlanSemester.semester); + + return yearEquals && semesterEquals; + } + } diff --git a/src/main/java/seedu/address/model/moduleplan/ModulePlanSemesterList.java b/src/main/java/seedu/address/model/moduleplan/ModulePlanSemesterList.java index e52b6a4722d..9e605c852c6 100644 --- a/src/main/java/seedu/address/model/moduleplan/ModulePlanSemesterList.java +++ b/src/main/java/seedu/address/model/moduleplan/ModulePlanSemesterList.java @@ -8,6 +8,7 @@ import javafx.collections.FXCollections; import javafx.collections.ObservableList; +import seedu.address.logic.parser.Parser; import seedu.address.model.module.Module; import seedu.address.model.module.ModuleCode; import seedu.address.model.module.exceptions.ModuleNotFoundException; @@ -39,6 +40,9 @@ public class ModulePlanSemesterList implements Iterable { public void setSemesters(List semesters) { requireAllNonNull(semesters); + if (!semestersAreUnique(semesters)) { + throw new DuplicateSemesterException(); + } internalList.setAll(semesters); } @@ -51,6 +55,10 @@ public void setSemesters(List semesters) { */ public void setSemesters(ModulePlanSemesterList replacement) { requireNonNull(replacement); + + if (!semestersAreUnique(replacement.internalList)) { + throw new DuplicateSemesterException(); + } internalList.setAll(replacement.internalList); } @@ -141,14 +149,16 @@ public void setModules(Module target, Module editedModule) { throw new ModuleNotFoundException(); } - // TODO: Check if this is correct - if (indexTarget != indexEdit) { - throw new ModuleNotFoundException(); + if (indexTarget == indexEdit) { + internalList.get(indexTarget).setModule(target, editedModule); + refreshList(indexTarget); + } else { + internalList.get(indexTarget).removeModule(target); + internalList.get(indexEdit).addModule(editedModule); + refreshList(indexTarget); + refreshList(indexEdit); } - internalList.get(indexTarget).setModule(target, editedModule); - refreshList(indexTarget); - } /** @@ -238,8 +248,8 @@ public boolean equals(Object other) { return false; } - ModulePlanSemesterList otherUniqueModuleList = (ModulePlanSemesterList) other; - return internalList.equals(otherUniqueModuleList.internalList); + ModulePlanSemesterList otherModulePlanSemesterList = (ModulePlanSemesterList) other; + return internalList.equals(otherModulePlanSemesterList.internalList); } @Override @@ -267,4 +277,18 @@ private void refreshList(int index) { internalList.set(index, temp); } + /** + * Returns true if {@code msodules} contains only unique modules. + */ + private boolean semestersAreUnique(List semesters) { + for (int i = 0; i < semesters.size() - 1; i++) { + for (int j = i + 1; j < semesters.size(); j++) { + if (semesters.get(i).equals(semesters.get(j))) { + return false; + } + } + } + return true; + } + } diff --git a/src/test/java/seedu/address/logic/commands/AddCommandIntegrationTest.java b/src/test/java/seedu/address/logic/commands/AddCommandIntegrationTest.java index af2506aa1b2..3de9ab7a462 100644 --- a/src/test/java/seedu/address/logic/commands/AddCommandIntegrationTest.java +++ b/src/test/java/seedu/address/logic/commands/AddCommandIntegrationTest.java @@ -29,9 +29,9 @@ public void setUp() { @Test public void execute_newModule_success() { - Module validModule = new ModuleBuilder().build(); + Module validModule = new ModuleBuilder().withCode("CS3230").build(); - Model expectedModel = new ModelManager((model.getModulePlan()), new UserPrefs()); + Model expectedModel = new ModelManager((getTypicalModulePlan()), new UserPrefs()); expectedModel.addModule(validModule); assertCommandSuccess(new AddCommand(validModule), model, diff --git a/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java b/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java index 0663d7cf17f..30e40f50d2c 100644 --- a/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java @@ -27,14 +27,14 @@ public class DeleteCommandTest { @Test public void execute_validIndexUnfilteredList_success() { - Module moduleToDelete = new ModuleBuilder().withCode("CS2040S").build(); + Module moduleToDelete = TypicalModules.CS2040S; ModuleCode code = moduleToDelete.getModuleCode(); DeleteCommand deleteCommand = new DeleteCommand(code); String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_MODULE_SUCCESS, Messages.format(moduleToDelete)); - ModelManager expectedModel = new ModelManager(model.getModulePlan(), new UserPrefs()); + ModelManager expectedModel = new ModelManager(TypicalModules.getTypicalModulePlan(), new UserPrefs()); expectedModel.deleteModule(moduleToDelete); assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel); diff --git a/src/test/java/seedu/address/logic/commands/EditCommandTest.java b/src/test/java/seedu/address/logic/commands/EditCommandTest.java index 0e903b54d23..f69992678fd 100644 --- a/src/test/java/seedu/address/logic/commands/EditCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/EditCommandTest.java @@ -45,7 +45,7 @@ public void execute_allFieldsSpecified_success() { String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_MODULE_SUCCESS, Messages.format(editedModule)); - Model expectedModel = new ModelManager(new ModulePlan(model.getModulePlan()), new UserPrefs()); + Model expectedModel = new ModelManager(new ModulePlan(getTypicalModulePlan()), new UserPrefs()); expectedModel.setModule(module, editedModule); assertCommandSuccess(editCommand, model, expectedMessage, expectedModel); diff --git a/src/test/java/seedu/address/logic/parser/AddCommandParserTest.java b/src/test/java/seedu/address/logic/parser/AddCommandParserTest.java index 8ad38393ac0..b98dcca0687 100644 --- a/src/test/java/seedu/address/logic/parser/AddCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddCommandParserTest.java @@ -23,6 +23,7 @@ import static seedu.address.logic.parser.CliSyntax.PREFIX_YEAR; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; +import static seedu.address.testutil.TypicalModules.CS2040S; import static seedu.address.testutil.TypicalModules.CS2101; import org.junit.jupiter.api.Test; @@ -40,22 +41,24 @@ public class AddCommandParserTest { @Test public void parse_allFieldsPresent_success() { - Module expectedModule = new ModuleBuilder(CS2101).build(); - String moduleCodeString = PREFIX_CODE + " " + expectedModule.getModuleCode(); + Module expectedModule = new ModuleBuilder(CS2040S).build(); + String moduleCodeString = " " + PREFIX_CODE + expectedModule.getModuleCode(); // whitespace only preamble // assertParseSuccess(parser, PREAMBLE_WHITESPACE + moduleCodeString + YEAR_DESC_CS2101 // + SEMESTER_DESC_CS2101 + GRADE_DESC_CS2101, new AddCommand(expectedModule)); + System.out.println(moduleCodeString + YEAR_DESC_CS2040S + SEMESTER_DESC_CS2040S + + GRADE_DESC_CS2040S); // no preamble - assertParseSuccess(parser, moduleCodeString + YEAR_DESC_CS2101 + SEMESTER_DESC_CS2101 - + GRADE_DESC_CS2101, new AddCommand(expectedModule)); + assertParseSuccess(parser, moduleCodeString + YEAR_DESC_CS2040S + SEMESTER_DESC_CS2040S + + GRADE_DESC_CS2040S, new AddCommand(expectedModule)); } @Test public void parse_repeatedNonTagValue_failure() { - String validExpectedModuleString = PREFIX_CODE + " " + VALID_CODE_CS2101 + String validExpectedModuleString = " " + PREFIX_CODE + VALID_CODE_CS2101 + YEAR_DESC_CS2101 + SEMESTER_DESC_CS2101 + GRADE_DESC_CS2101; // multiple years @@ -68,11 +71,11 @@ public void parse_repeatedNonTagValue_failure() { // multiple grades assertParseFailure(parser, GRADE_DESC_CS2101 + validExpectedModuleString, - Messages.getErrorMessageForDuplicatePrefixes(PREFIX_EMAIL)); + Messages.getErrorMessageForDuplicatePrefixes(PREFIX_GRADE)); // multiple fields repeated assertParseFailure(parser, - validExpectedModuleString + YEAR_DESC_CS2040S + SEMESTER_DESC_CS2040S + GRADE_DESC_CS2040S + YEAR_DESC_CS2040S + SEMESTER_DESC_CS2040S + GRADE_DESC_CS2040S + validExpectedModuleString, Messages.getErrorMessageForDuplicatePrefixes(PREFIX_YEAR, PREFIX_SEMESTER, PREFIX_GRADE)); @@ -142,7 +145,7 @@ public void parse_compulsoryFieldMissing_failure() { @Test public void parse_invalidValue_failure() { - String moduleCodeString = PREFIX_CODE + " " + VALID_CODE_CS2101; + String moduleCodeString = " " + PREFIX_CODE + VALID_CODE_CS2101; // invalid year assertParseFailure(parser, @@ -164,10 +167,12 @@ public void parse_invalidValue_failure() { moduleCodeString + INVALID_YEAR_DESC + SEMESTER_DESC_CS2101 + INVALID_GRADE_DESC, Year.MESSAGE_CONSTRAINTS); + // non-empty preamble assertParseFailure(parser, - moduleCodeString - + PREAMBLE_NON_EMPTY + YEAR_DESC_CS2101 + SEMESTER_DESC_CS2101 + GRADE_DESC_CS2101, + PREAMBLE_NON_EMPTY + moduleCodeString + + YEAR_DESC_CS2101 + SEMESTER_DESC_CS2101 + GRADE_DESC_CS2101, String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE)); } + } diff --git a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java index 3a6aa3a7785..2c7ad33c899 100644 --- a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java @@ -53,7 +53,7 @@ public void parseCommand_clear() throws Exception { @Test public void parseCommand_delete() throws Exception { DeleteCommand command = (DeleteCommand) parser.parseCommand( - DeleteCommand.COMMAND_WORD + " " + CS2030S); + DeleteCommand.COMMAND_WORD + " " + CS2030S.getModuleCode()); assertEquals(new DeleteCommand(CS2030S.getModuleCode()), command); } diff --git a/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java b/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java index 4f0612f3bc4..25320892f24 100644 --- a/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java @@ -22,7 +22,7 @@ public class DeleteCommandParserTest { @Test public void parse_validArgs_returnsDeleteCommand() { - assertParseSuccess(parser, "CS2030", new DeleteCommand(CS2030S.getModuleCode())); + assertParseSuccess(parser, CS2030S.getModuleCode().toString(), new DeleteCommand(CS2030S.getModuleCode())); } @Test diff --git a/src/test/java/seedu/address/logic/parser/InfoCommandParserTest.java b/src/test/java/seedu/address/logic/parser/InfoCommandParserTest.java index 3feb49af3de..6223e03edaf 100644 --- a/src/test/java/seedu/address/logic/parser/InfoCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/InfoCommandParserTest.java @@ -20,7 +20,7 @@ public void parse_invalidSingleArg_throwsParseException() { assertParseFailure(parser, "a1b2c3", MESSAGE_CONSTRAINTS); // Single word with non-alphanumeric characters - assertParseFailure(parser, "a_b\\c:e", MESSAGE_CONSTRAINTS); + assertParseFailure(parser, "a_b!!c:e", MESSAGE_CONSTRAINTS); } @Test diff --git a/src/test/java/seedu/address/logic/parser/ModulePlanParserTest.java b/src/test/java/seedu/address/logic/parser/ModulePlanParserTest.java new file mode 100644 index 00000000000..f284895e8ff --- /dev/null +++ b/src/test/java/seedu/address/logic/parser/ModulePlanParserTest.java @@ -0,0 +1,115 @@ +package seedu.address.logic.parser; + +import static org.junit.jupiter.api.Assertions.assertEquals; +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.commands.CommandTestUtil.VALID_CODE_CS2040S; +import static seedu.address.testutil.Assert.assertThrows; +import static seedu.address.testutil.TypicalModules.CS2030S; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import org.junit.jupiter.api.Test; + +import seedu.address.logic.commands.AddCommand; +import seedu.address.logic.commands.CalculateCapCommand; +import seedu.address.logic.commands.CalculateMcCommand; +import seedu.address.logic.commands.ClearCommand; +import seedu.address.logic.commands.DeleteCommand; +import seedu.address.logic.commands.EditCommand; +import seedu.address.logic.commands.EditCommand.EditModuleDescriptor; +import seedu.address.logic.commands.ExitCommand; +import seedu.address.logic.commands.FindCommand; +import seedu.address.logic.commands.HelpCommand; +import seedu.address.logic.commands.ListCommand; +import seedu.address.logic.parser.exceptions.ParseException; +import seedu.address.model.module.Module; +import seedu.address.model.module.ModuleCode; +import seedu.address.model.module.NameContainsKeywordsPredicate; +import seedu.address.testutil.EditModuleDescriptorBuilder; +import seedu.address.testutil.ModuleBuilder; +import seedu.address.testutil.ModuleUtil; + +public class ModulePlanParserTest { + + private final ModulePlanParser parser = new ModulePlanParser(); + + @Test + public void parseCommand_add() throws Exception { + Module module = new ModuleBuilder().build(); + AddCommand command = (AddCommand) parser.parseCommand(ModuleUtil.getAddCommand(module)); + assertEquals(new AddCommand(module), command); + } + + @Test + public void parseCommand_clear() throws Exception { + assertTrue(parser.parseCommand(ClearCommand.COMMAND_WORD) instanceof ClearCommand); + assertTrue(parser.parseCommand(ClearCommand.COMMAND_WORD + " 3") instanceof ClearCommand); + } + + @Test + public void parseCommand_delete() throws Exception { + DeleteCommand command = (DeleteCommand) parser.parseCommand( + DeleteCommand.COMMAND_WORD + " " + CS2030S.getModuleCode()); + assertEquals(new DeleteCommand(CS2030S.getModuleCode()), command); + } + + @Test + public void parseCommand_edit() throws Exception { + Module module = new ModuleBuilder().build(); + EditModuleDescriptor descriptor = new EditModuleDescriptorBuilder(module).build(); + EditCommand command = (EditCommand) parser.parseCommand(EditCommand.COMMAND_WORD + " " + + CS2030S.getModuleCode() + " " + ModuleUtil.getEditModuleDescriptorDetails(descriptor)); + assertEquals(new EditCommand(CS2030S.getModuleCode(), descriptor), command); + } + + @Test + public void parseCommand_exit() throws Exception { + assertTrue(parser.parseCommand(ExitCommand.COMMAND_WORD) instanceof ExitCommand); + assertTrue(parser.parseCommand(ExitCommand.COMMAND_WORD + " 3") instanceof ExitCommand); + } + + @Test + public void parseCommand_find() throws Exception { + List 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); + } + + @Test + public void parseCommand_help() throws Exception { + assertTrue(parser.parseCommand(HelpCommand.COMMAND_WORD) instanceof HelpCommand); + assertTrue(parser.parseCommand(HelpCommand.COMMAND_WORD + " 3") instanceof HelpCommand); + } + + @Test + public void parseCommand_list() throws Exception { + assertTrue(parser.parseCommand(ListCommand.COMMAND_WORD) instanceof ListCommand); + assertTrue(parser.parseCommand(ListCommand.COMMAND_WORD + " 3") instanceof ListCommand); + } + + @Test + public void parseCommand_calculateCap() throws Exception { + assertTrue(parser.parseCommand(CalculateCapCommand.COMMAND_WORD) instanceof CalculateCapCommand); + } + + @Test + public void parseCommand_calculateMc() throws Exception { + assertTrue(parser.parseCommand(CalculateMcCommand.COMMAND_WORD) instanceof CalculateMcCommand); + } + + @Test + public void parseCommand_unrecognisedInput_throwsParseException() { + assertThrows(ParseException.class, String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE), () + -> parser.parseCommand("")); + } + + @Test + public void parseCommand_unknownCommand_throwsParseException() { + assertThrows(ParseException.class, MESSAGE_UNKNOWN_COMMAND, () -> parser.parseCommand("unknownCommand")); + } +} diff --git a/src/test/java/seedu/address/model/ModelManagerTest.java b/src/test/java/seedu/address/model/ModelManagerTest.java index 8a5c40ad5a9..f9b4757d852 100644 --- a/src/test/java/seedu/address/model/ModelManagerTest.java +++ b/src/test/java/seedu/address/model/ModelManagerTest.java @@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.testutil.Assert.assertThrows; import static seedu.address.testutil.TypicalModules.CS2101; +import static seedu.address.testutil.TypicalModules.getTypicalModulePlan; import java.nio.file.Path; @@ -14,6 +15,7 @@ import seedu.address.commons.core.GuiSettings; import seedu.address.model.moduleplan.ModulePlan; +import seedu.address.model.moduleplan.ModulePlanSemester; import seedu.address.testutil.TypicalModules; public class ModelManagerTest { @@ -82,6 +84,9 @@ public void hasModule_moduleNotInModulePlan_returnsFalse() { @Test public void hasModule_moduleInModulePlan_returnsTrue() { + ModulePlanSemester m = new ModulePlanSemester(CS2101.getYearTaken(), CS2101.getSemesterTaken()); + ModulePlan mp = (ModulePlan) modelManager.getModulePlan(); + mp.addSemester(m); modelManager.addModule(CS2101); assertTrue(modelManager.hasModule(CS2101)); } diff --git a/src/test/java/seedu/address/model/moduleplan/ModulePlanTest.java b/src/test/java/seedu/address/model/moduleplan/ModulePlanTest.java index 89a3db5a497..9bca439e740 100644 --- a/src/test/java/seedu/address/model/moduleplan/ModulePlanTest.java +++ b/src/test/java/seedu/address/model/moduleplan/ModulePlanTest.java @@ -75,12 +75,14 @@ public void hasPerson_personNotInAddressBook_returnsFalse() { @Test public void hasModule_moduleInModulePlan_returnsTrue() { + modulePlan.addSemester(new ModulePlanSemester(CS2040S.getYearTaken(), CS2040S.getSemesterTaken())); modulePlan.addModule(CS2040S); assertTrue(modulePlan.hasModule(CS2040S)); } @Test public void hasModule_moduleWithSameIdentityFieldsInModulePlan_returnsTrue() { + modulePlan.addSemester(new ModulePlanSemester(CS2040S.getYearTaken(), CS2040S.getSemesterTaken())); modulePlan.addModule(CS2040S); Module editedCS2040S = new ModuleBuilder() .withCode("CS2040S")