Skip to content

Commit

Permalink
Fix Test Cases
Browse files Browse the repository at this point in the history
  • Loading branch information
xCOLOURx committed Oct 19, 2023
1 parent a9522c8 commit f89e0f2
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,6 +40,9 @@ public class ModulePlanSemesterList implements Iterable<ModulePlanSemester> {
public void setSemesters(List<ModulePlanSemester> semesters) {
requireAllNonNull(semesters);

if (!semestersAreUnique(semesters)) {
throw new DuplicateSemesterException();
}
internalList.setAll(semesters);
}

Expand All @@ -51,6 +55,10 @@ public void setSemesters(List<ModulePlanSemester> semesters) {
*/
public void setSemesters(ModulePlanSemesterList replacement) {
requireNonNull(replacement);

if (!semestersAreUnique(replacement.internalList)) {
throw new DuplicateSemesterException();
}
internalList.setAll(replacement.internalList);
}

Expand Down Expand Up @@ -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);

}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<ModulePlanSemester> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 15 additions & 10 deletions src/test/java/seedu/address/logic/parser/AddCommandParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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));

Expand Down Expand Up @@ -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,
Expand All @@ -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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
115 changes: 115 additions & 0 deletions src/test/java/seedu/address/logic/parser/ModulePlanParserTest.java
Original file line number Diff line number Diff line change
@@ -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<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);
}

@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"));
}
}
5 changes: 5 additions & 0 deletions src/test/java/seedu/address/model/ModelManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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));
}
Expand Down
Loading

0 comments on commit f89e0f2

Please sign in to comment.