Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase code coverage for ConfirmOverrideCommand #292

Merged
merged 5 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/main/java/seedu/address/logic/commands/CommandResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public CommandResult(String feedbackToUser, boolean showHelp, boolean exit, bool
/**
* Constructs a {@code CommandResult} with the specified fields.
*/
public CommandResult(String feedbackToUser, boolean showHelp, boolean exit, boolean showOverride,
public CommandResult(String feedbackToUser, boolean exit, boolean showOverride,
Person personToEdit, Appointment appointment) {
this.feedbackToUser = requireNonNull(feedbackToUser);
this.showHelp = showHelp;
this.exit = exit;
this.showOverride = showOverride;
this.showClear = false;
this.personToEdit = personToEdit;
this.appointment = appointment;
this.showHelp = false;
this.personToEdit = requireNonNull(personToEdit);
this.appointment = requireNonNull(appointment);
}

/**
Expand Down Expand Up @@ -105,7 +105,9 @@ public boolean equals(Object other) {
return feedbackToUser.equals(otherCommandResult.feedbackToUser)
&& showHelp == otherCommandResult.showHelp
&& exit == otherCommandResult.exit
&& showClear == otherCommandResult.showClear;
&& showClear == otherCommandResult.showClear
&& appointment == otherCommandResult.appointment
&& personToEdit == otherCommandResult.personToEdit;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
/** Overrides the current appointment and replaces it with the new appointment after confirmation **/
public class ConfirmOverrideCommand extends Command {
public static final String MESSAGE_SUCCESS = "Appointment updated!";
private static Appointment appointment = null;
private static Person personToEdit = null;
private Appointment appointment;
private Person personToEdit;

/**
* Default constructor for a confirm override command
* @param appointment new appointment
* @param personToEdit person whos old appointment will be replaced by the new appointment
* @param personToEdit person whose old appointment will be replaced by the new appointment
*/
public ConfirmOverrideCommand(Appointment appointment, Person personToEdit) {
requireNonNull(appointment);
Expand All @@ -26,26 +26,28 @@ public ConfirmOverrideCommand(Appointment appointment, Person personToEdit) {
this.personToEdit = personToEdit;
}

public static Appointment getAppointment() {
return appointment;
public Appointment getAppointment() {
return this.appointment;
}

public static Person getPersonToEdit() {
return personToEdit;
public Person getPersonToEdit() {
return this.personToEdit;
}

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
requireNonNull(this.appointment);
requireNonNull(this.personToEdit);

Person personWithApt = createPersonWithAppointment(personToEdit);
Person personWithApt = createPersonWithAppointment(this.personToEdit);

assert personWithApt.getAppointment() instanceof Appointment
: "Schedule Command: person should have appointment";

appointment.setPerson(personWithApt); //sets person to appointment
this.appointment.setPerson(personWithApt); //sets person to appointment

model.setPerson(personToEdit, personWithApt);
model.setPerson(this.personToEdit, personWithApt);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(personWithApt)));
}
Expand All @@ -62,7 +64,8 @@ public boolean equals(Object other) {
}

ConfirmOverrideCommand otherOverrideCommand = (ConfirmOverrideCommand) other;
return appointment.equals(otherOverrideCommand.appointment);
return this.appointment.equals(otherOverrideCommand.getAppointment())
&& this.personToEdit.equals(otherOverrideCommand.getPersonToEdit());
}

/**
Expand All @@ -74,6 +77,6 @@ public boolean equals(Object other) {
private Person createPersonWithAppointment(Person personToEdit) {
return new Person(personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(),
personToEdit.getAddress(), personToEdit.getNextOfKinName(), personToEdit.getNextOfKinPhone(),
personToEdit.getFinancialPlans(), personToEdit.getTags(), appointment);
personToEdit.getFinancialPlans(), personToEdit.getTags(), this.appointment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public CommandResult execute(Model model) throws CommandException {

if (!personToEdit.hasNullAppointment()) {
return new CommandResult(CONFIRM_OVERRIDE_MESSAGE,
false, false, true, personToEdit, toAdd);
false, true, personToEdit, toAdd);
}

assert personToEdit.getAppointment() instanceof NullAppointment;
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/seedu/address/logic/commands/CommandResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
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 static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.jupiter.api.Test;

import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.appointment.Appointment;
import seedu.address.model.person.Person;

public class CommandResultTest {
@Test
public void equals() {
Expand Down Expand Up @@ -57,6 +64,34 @@ public void hashcode() {
false, false, true).hashCode());
}

@Test
public void commandResultWithParamsTest() {
Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());

Person person = model.getFilteredPersonList().get(1);
Person person2 = model.getFilteredPersonList().get(2);
Appointment appointment = Appointment.parseAppointmentDescription("Description, 01-01-2023 20:00");
Appointment appointment2 = Appointment.parseAppointmentDescription("Description, 01-01-2023 00:00");

CommandResult commandResult = new CommandResult("feedback", false,
true, person, appointment);

// same object -> returns true
assertTrue(commandResult.equals(commandResult));

// null -> returns false
assertFalse(commandResult.equals(null));

// different types -> returns false
assertFalse(commandResult.equals(0.5f));

//different constructors -> returns false
assertNotEquals(commandResult, new CommandResult("feedback", false, true));
assertNotEquals(commandResult, new CommandResult("feedback", false, true, person, appointment2));
assertNotEquals(commandResult, new CommandResult("feedback", false, true, person2, appointment));
}

@Test
public void toStringMethod() {
CommandResult commandResult = new CommandResult("feedback");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package seedu.address.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 seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

Expand Down Expand Up @@ -33,4 +36,49 @@ public void execute_overridingAppointment_success() {
assertCommandSuccess(new ConfirmOverrideCommand(newAppointment, personToEdit), model,
ConfirmOverrideCommand.MESSAGE_SUCCESS, expectedModel);
}

@Test
public void getMethodTest() {
Appointment newAppointment = Appointment.parseAppointmentDescription(APPOINTMENT_DESCRIPTION_STUB);
Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
Person personToEdit = model.getFilteredPersonList().get(1);
ConfirmOverrideCommand command = new ConfirmOverrideCommand(newAppointment, personToEdit);

assertEquals(command.getAppointment(), newAppointment);
assertEquals(command.getPersonToEdit(), personToEdit);
}

@Test
public void equals() {
Appointment newAppointment = Appointment.parseAppointmentDescription(APPOINTMENT_DESCRIPTION_STUB);
Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
Person personToEdit = model.getFilteredPersonList().get(1);
Person personToEdit2 = model.getFilteredPersonList().get(2);
ConfirmOverrideCommand confirmOverrideCommand = new ConfirmOverrideCommand(newAppointment, personToEdit);

// same values -> returns true
assertTrue(confirmOverrideCommand.equals(new ConfirmOverrideCommand(newAppointment, personToEdit)));

// same object -> returns true
assertTrue(confirmOverrideCommand.equals(confirmOverrideCommand));

// null -> returns false
assertFalse(confirmOverrideCommand.equals(null));

// different types -> returns false
assertFalse(confirmOverrideCommand.equals(0.5f));

// different person value -> returns false
assertFalse(confirmOverrideCommand.equals(new ConfirmOverrideCommand(newAppointment, personToEdit2)));

}

@Test
public void hashcode() {
CommandResult commandResult = new CommandResult("feedback");

// same values -> returns same hashcode
assertEquals(commandResult.hashCode(), new CommandResult("feedback").hashCode());

}
}
Loading