Skip to content

Commit

Permalink
Merge pull request AY2324S1-CS2103T-T13-0#168 from ji-just-ji/master
Browse files Browse the repository at this point in the history
Add fields and methods to TypicalModules for Testing purposes
  • Loading branch information
yyyaohhh authored Oct 31, 2023
2 parents 1f74cc8 + 1f70780 commit 9975bf0
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 107 deletions.
8 changes: 4 additions & 4 deletions docs/diagrams/ParserClasses.puml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Class XYZCommand

package "Parser classes"{
Class "<<interface>>\nParser" as Parser
Class AddressBookParser
Class ModulePlanParser
Class XYZCommandParser
Class CliSyntax
Class ParserUtil
Expand All @@ -19,12 +19,12 @@ Class Prefix
}

Class HiddenOutside #FFFFFF
HiddenOutside ..> AddressBookParser
HiddenOutside ..> ModulePlanParser

AddressBookParser .down.> XYZCommandParser: creates >
ModulePlanParser .down.> XYZCommandParser: creates >

XYZCommandParser ..> XYZCommand : creates >
AddressBookParser ..> Command : returns >
ModulePlanParser ..> Command : returns >
XYZCommandParser .up.|> Parser
XYZCommandParser ..> ArgumentMultimap
XYZCommandParser ..> ArgumentTokenizer
Expand Down
1 change: 1 addition & 0 deletions docs/diagrams/StorageClassDiagram.puml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ JsonAddressBookStorage .up.|> AddressBookStorage
JsonAddressBookStorage ..> JsonSerializableAddressBook
JsonSerializableAddressBook --> "*" JsonAdaptedModule


@enduml
18 changes: 15 additions & 3 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_YEAR;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.module.Grade;
Expand Down Expand Up @@ -37,7 +38,7 @@ public class AddCommand extends Command {
+ PREFIX_SEMESTER + "1 "
+ PREFIX_GRADE + "B ";

public static final String MESSAGE_SUCCESS = "New module added: %1$s";
public static final String MESSAGE_ADD_MODULE_SUCCESS = "New module added: %1$s";
public static final String MESSAGE_DUPLICATE_MODULE = "%1$s has already been added.";

private final ModuleCode moduleCode;
Expand All @@ -56,6 +57,18 @@ public AddCommand(ModuleCode moduleCode, Year year, Semester semester, Grade gra
this.grade = grade;
}

/**
* For Testing purposes only
* @param validModule
*/
public AddCommand(Module validModule) {
requireNonNull(validModule);
this.moduleCode = validModule.getModuleCode();
this.year = validModule.getYearTaken();
this.semester = validModule.getSemesterTaken();
this.grade = validModule.getGrade();
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
Expand All @@ -74,8 +87,7 @@ public CommandResult execute(Model model) throws CommandException {
} catch (DuplicateModuleException dme) {
throw new CommandException(String.format(MESSAGE_DUPLICATE_MODULE, moduleCode));
}

return new CommandResult(String.format(MESSAGE_SUCCESS, moduleCode));
return new CommandResult(String.format(MESSAGE_ADD_MODULE_SUCCESS, Messages.format(newModule)));
}

@Override
Expand Down
23 changes: 12 additions & 11 deletions src/main/java/seedu/address/model/module/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,19 @@ public Module(ModuleCode moduleCode, ModuleName moduleName, Description descript

/**
* Constructs a {@code Module} with only user input fields.
* Only used for Tests
*/
// public Module(ModuleCode moduleCode, Year year, Semester semester, Grade grade) {
// requireAllNonNull(moduleCode, year, semester, grade);
// this.moduleCode = moduleCode;
// this.yearTaken = year;
// this.semesterTaken = semester;
// this.grade = grade;
// // Temporary until we get back-end setup
// this.moduleName = null;
// this.description = null;
// this.modularCredit = null;
// }
public Module(ModuleCode moduleCode, Year year, Semester semester, Grade grade) {
requireAllNonNull(moduleCode, year, semester, grade);
this.moduleCode = moduleCode;
this.yearTaken = year;
this.semesterTaken = semester;
this.grade = grade;
// Temporary until we get back-end setup
this.moduleName = null;
this.description = null;
this.modularCredit = null;
}

public Module fillUserInputs(Year yearTaken, Semester semesterTaken, Grade grade) {
requireAllNonNull(yearTaken, semesterTaken, grade);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void execute_newModule_success() {
expectedModel.addModule(validModule);

assertCommandSuccess(new AddCommand(validModule), model,
String.format(AddCommand.MESSAGE_SUCCESS, Messages.format(validModule)),
String.format(AddCommand.MESSAGE_ADD_MODULE_SUCCESS, Messages.format(validModule)),
expectedModel);
}

Expand Down
73 changes: 58 additions & 15 deletions src/test/java/seedu/address/logic/commands/AddCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
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 seedu.address.logic.Messages.MESSAGE_MODULE_NOT_FOUND;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalModules.CS2030S;
import static seedu.address.testutil.TypicalModules.*;

import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -25,6 +26,7 @@
import seedu.address.model.module.Module;
import seedu.address.model.module.ModuleCode;
import seedu.address.model.module.ModuleName;
import seedu.address.model.module.exceptions.DuplicateModuleException;
import seedu.address.model.module.exceptions.ModuleNotFoundException;
import seedu.address.model.moduleplan.ModulePlan;
import seedu.address.model.moduleplan.ModulePlanSemester;
Expand All @@ -35,7 +37,7 @@ public class AddCommandTest {

@Test
public void constructor_nullModule_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new AddCommand(null));
assertThrows(NullPointerException.class, () -> new AddCommand(null, null, null, null));
}

@Test
Expand All @@ -45,18 +47,42 @@ public void execute_moduleAcceptedByModel_addSuccessful() throws Exception {

CommandResult commandResult = new AddCommand(validModule).execute(modelStub);

assertEquals(String.format(AddCommand.MESSAGE_SUCCESS, Messages.format(validModule)),
assertEquals(String.format(AddCommand.MESSAGE_ADD_MODULE_SUCCESS, Messages.format(validModule)),
commandResult.getFeedbackToUser());
assertEquals(Arrays.asList(validModule), modelStub.modulesAdded );
assertEquals(Arrays.asList(validModule), modelStub.modulesAdded);
}
@Test
public void execute_moduleInDataNotInPlan_addSuccessful() throws Exception {
//In ModuleData and not in ModulePlan
ModelStubWithModule modelStub = new ModelStubWithModule(CS2100);
Module validModule = modelStub.getModuleFromDb(CS2030S.getModuleCode());

CommandResult commandResult = new AddCommand(validModule).execute(modelStub);

assertEquals(String.format(AddCommand.MESSAGE_ADD_MODULE_SUCCESS, Messages.format(validModule)),
commandResult.getFeedbackToUser());
assertEquals(Arrays.asList(new Module[]{CS2100, validModule}), modelStub.modulesAdded);
}

@Test
public void execute_duplicateModule_throwsCommandException() {
public void execute_moduleNotInModuleDataNotInModulePlan_throwsCommandException() {
//Not in ModuleData and ModulePlan (Module Not Found)
ModelStubWithModule modelStub = new ModelStubWithModule(CS2030S);

assertThrows(ModuleNotFoundException.class, () -> modelStub.getModuleFromDb(CS1101S.getModuleCode()));
}


@Test
public void execute_duplicateModule_throwsCommandException() throws Exception {
//Both in ModuleData and ModulePlan
Module validModule = new ModuleBuilder().build();
AddCommand addCommand = new AddCommand(validModule);
ModelStub modelStub = new ModelStubWithModule(validModule);

assertThrows(CommandException.class, AddCommand.MESSAGE_DUPLICATE_MODULE, () -> addCommand.execute(modelStub));
ModelStubWithModule modelStub = new ModelStubWithModule(validModule);

assertThrows(CommandException.class,
String.format(AddCommand.MESSAGE_DUPLICATE_MODULE, validModule.getModuleCode()),
() -> addCommand.execute(modelStub));
}


Expand All @@ -83,7 +109,9 @@ public void equals() {
@Test
public void toStringMethod() {
AddCommand addCommand = new AddCommand(CS2030S);
String expected = AddCommand.class.getCanonicalName() + "{toAdd=" + CS2030S + "}";
String expected = AddCommand.class.getCanonicalName() + "{moduleCode=" + CS2030S.getModuleCode()
+ ", year=" + CS2030S.getYearTaken() + ", semester=" + CS2030S.getSemesterTaken()
+ ", grade=" + CS2030S.getGrade() + "}";
assertEquals(expected, addCommand.toString());
}

Expand Down Expand Up @@ -176,17 +204,14 @@ public ReadOnlyModuleData getModuleData() {
throw new AssertionError("This method should not be called.");
}

@Override
public ModuleName getDbModuleName(ModuleCode moduleCode) throws ModuleNotFoundException {
throw new AssertionError("This method should not be called.");
}

@Override
public Description getDbModuleDescription(ModuleCode moduleCode) throws ModuleNotFoundException {
throw new AssertionError("This method should not be called.");
}

@Override
public ModularCredit getDbModularCredit(ModuleCode moduleCode) throws ModuleNotFoundException {
throw new AssertionError("This method should not be called.");
}
Expand All @@ -201,32 +226,50 @@ public boolean checkDbValidModuleCode(ModuleCode moduleCode) {
throw new AssertionError("This method should not be called.");
}

@Override
public Module getModuleFromDb(ModuleCode moduleCode) {
return getTypicalModuleData().getModule(moduleCode);
}

@Override
public ObservableList<ModulePlanSemester> getFilteredModuleList() {
throw new AssertionError("This method should not be called.");
}
}

/**
* A Model stub that contains a single person.
* A Model stub that contains a single module.
*/
private class ModelStubWithModule extends ModelStub {
private final Module module;
final ArrayList<Module> modulesAdded = new ArrayList<>();


ModelStubWithModule(Module module) {
requireNonNull(module);
this.module = module;
modulesAdded.add(module);
}

@Override
public void addModule(Module module) {
if (modulesAdded.contains(module)) {
throw new DuplicateModuleException();
}
modulesAdded.add(module);
}

@Override
public boolean hasModule(Module module) {
requireNonNull(module);
return this.module.isSameModule(module);
return modulesAdded.stream().anyMatch(module::isSameModule);
}


}

/**
* A Model stub that always accept the person being added.
* A Model stub that always accept the Module being added.
*/
private class ModelStubAcceptingModuleAdded extends ModelStub {
final ArrayList<Module> modulesAdded = new ArrayList<>();
Expand Down
64 changes: 0 additions & 64 deletions src/test/java/seedu/address/logic/commands/InfoCommandTest.java

This file was deleted.

Loading

0 comments on commit 9975bf0

Please sign in to comment.