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

Fix test cases #192

Merged
merged 16 commits into from
Nov 10, 2023
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ public CommandResult execute(Model model) throws CommandException {
return new CommandResult(SHOWING_CONFIRMATION_MESSAGE, false, false, false, false);
}
}

public void setConfirmed(boolean isConfirmed) {
this.isConfirmed = isConfirmed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ public CommandResult execute(Model model) throws CommandException {
return new CommandResult(String.format(MESSAGE_DELETE_GROUP_SUCCESS, groupToDelete.getNumber()),
false, false, true, false);
}

/**
* Gets the group number of the group to be deleted.
*
* @return The group number.
*/
public int getGroupNumber() {
return groupNumber;
}
}
11 changes: 9 additions & 2 deletions src/main/java/seedu/address/ui/ConfirmationPopup.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public class ConfirmationPopup extends UiPart<Stage> {
private static final String FXML = "ClearConfirmationPopup.fxml";

@FXML
private Button yesButton;
Button yesButton;

@FXML
private Button cancelButton;
Button cancelButton;

@FXML
private Label confirmationMessage;
Expand Down Expand Up @@ -90,4 +90,11 @@ public boolean isShowing() {
public void focus() {
getRoot().requestFocus();
}

/**
* Returns true if the confirmation was confirmed.
*/
public boolean isConfirmed() {
return isConfirmed;
}
}
230 changes: 213 additions & 17 deletions src/test/java/seedu/address/logic/commands/ClearCommandTest.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,230 @@
package seedu.address.logic.commands;

import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import javafx.application.Platform;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import seedu.address.commons.core.GuiSettings;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyUserPrefs;
import seedu.address.model.group.Group;
import seedu.address.model.group.tasks.TaskList;
import seedu.address.model.person.Email;
import seedu.address.model.person.Person;
import javafx.collections.ObservableList;

import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Predicate;

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 seedu.address.ui.JavaFxInitializer;


public class ClearCommandTest {

@BeforeAll
public static void init() {
JavaFxInitializer.initialize();
}

@AfterAll
public static void cleanup() {
JavaFxInitializer.cleanup();
}

@Test
public void execute_emptyAddressBook_success() {
Model model = new ModelManager();
Model expectedModel = new ModelManager();
public void execute_clearCommandConfirmed_success() throws CommandException {
// Arrange
ModelStub modelStub = new ModelStub();
ClearCommand clearCommand = new ClearCommand();
clearCommand.setConfirmed(true);

// Act
Platform.runLater(() -> {
try {
CommandResult commandResult = clearCommand.execute(modelStub);

assertCommandSuccess(new ClearCommand(), model, ClearCommand.SHOWING_CONFIRMATION_MESSAGE, expectedModel,
false);
// Assert
assertEquals(ClearCommand.SHOWING_CONFIRMATION_MESSAGE, commandResult.getFeedbackToUser());
assertFalse(commandResult.isShowHelp());
assertFalse(commandResult.isExit());
assertFalse(commandResult.isGroupCommand());
assertTrue(commandResult.isClear());
assertEquals(0, modelStub.getAddressBook().getPersonList().size());
} catch (CommandException e) {
// Handle exception
}
});
}

@Test
public void execute_nonEmptyAddressBook_success() {
Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());
expectedModel.setAddressBook(new AddressBook());
public void execute_clearCommandNotConfirmed_success() throws CommandException {
// Arrange
ModelStub modelStub = new ModelStub();
ClearCommand clearCommand = new ClearCommand();
clearCommand.setConfirmed(false);

assertCommandSuccess(new ClearCommand(), model, ClearCommand.SHOWING_CONFIRMATION_MESSAGE, expectedModel,
false);
// Act
Platform.runLater(() -> {
try {
CommandResult commandResult = clearCommand.execute(modelStub);

// Assert
assertEquals(ClearCommand.SHOWING_CONFIRMATION_MESSAGE, commandResult.getFeedbackToUser());
assertFalse(commandResult.isShowHelp());
assertFalse(commandResult.isExit());
assertFalse(commandResult.isGroupCommand());
assertFalse(commandResult.isClear());
assertEquals(0, modelStub.getAddressBook().getPersonList().size());
} catch (CommandException e) {
// Handle exception if needed
}
});
}

// Rest of the class remains unchanged
private static class ModelStub implements Model {
private final AddressBook addressBook = new AddressBook();

@Override
public void setUserPrefs(ReadOnlyUserPrefs userPrefs) {
throw new AssertionError("This method should not be called.");
}

@Override
public ReadOnlyUserPrefs getUserPrefs() {
return null;
}

@Override
public GuiSettings getGuiSettings() {
return null;
}

@Override
public void setGuiSettings(GuiSettings guiSettings) {

}

@Override
public Path getAddressBookFilePath() {
// Implement if needed
return null;
}

@Override
public void setAddressBookFilePath(Path addressBookFilePath) {
throw new AssertionError("This method should not be called.");
}

@Override
public void setAddressBook(ReadOnlyAddressBook addressBook) {
throw new AssertionError("This method should not be called.");
}

@Override
public ReadOnlyAddressBook getAddressBook() {
return addressBook;
}

@Override
public boolean hasPerson(Person person) {
return addressBook.hasPerson(person);
}

@Override
public void deletePerson(Person target) {
addressBook.removePerson(target);
}

@Override
public void addPerson(Person person) {
addressBook.addPerson(person);
}

@Override
public void setPerson(Person target, Person editedPerson) {
addressBook.setPerson(target, editedPerson);
}

@Override
public ObservableList<Person> getFilteredPersonList() {
return null;
}

@Override
public void updateFilteredPersonList(Predicate<Person> predicate) {
throw new AssertionError("This method should not be called.");
}


@Override
public void addGroup(Group group) {
throw new AssertionError("This method should not be called.");
}

@Override
public void addPersonToGroup(Person person, Group group) {
throw new AssertionError("This method should not be called.");
}

@Override
public ObservableList<Group> getFilteredGroupList() {
return null;
}

@Override
public void updateFilteredGroupList(Predicate<Group> predicate) {
throw new AssertionError("This method should not be called.");
}

@Override
public Optional<Person> getPersonWithEmail(Email email) {
return Optional.empty();
}

@Override
public Optional<Group> getGroupWithNumber(int number) {
return Optional.empty();
}


@Override
public boolean personIsInAGroup(Person person) {
// Implement if needed
return false;
}

@Override
public Group getGroupThatPersonIsIn(Person person) {
return null;
}

@Override
public void removePersonFromGroup(Person person, Group group) {
throw new AssertionError("This method should not be called.");
}

@Override
public void addTasksToGroup(TaskList taskList, Group group) {
throw new AssertionError("This method should not be called.");
}

@Override
public boolean hasGroup(Group group) {
return false;
}

@Override
public void deleteGroup(Group group) {
throw new AssertionError("This method should not be called.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import org.junit.jupiter.api.Test;

Expand All @@ -15,20 +17,54 @@ public class DeleteGroupCommandTest {

@Test
public void execute_validGroupNumber_success() throws CommandException {
// Create a model with typical groups and user preferences
Model model = new ModelManager(TypicalGroups.getTypicalAddressBook(), new UserPrefs());

// Create a DeleteGroupCommand for a valid group number
DeleteGroupCommand deleteGroupCommand = new DeleteGroupCommand(1);
String expectedMessage = String.format(DeleteGroupCommand.MESSAGE_DELETE_GROUP_SUCCESS, TypicalGroups.GROUP1);

// Build the expected success message
String expectedMessage = String.format(DeleteGroupCommand.MESSAGE_DELETE_GROUP_SUCCESS, 1);

// Execute the DeleteGroupCommand and get the result
CommandResult commandResult = deleteGroupCommand.execute(model);

// Assert that the feedback message matches the expected success message
assertEquals(expectedMessage, commandResult.getFeedbackToUser());
}

@Test
public void execute_invalidGroupNumber_throwsCommandException() {
// Create a model with typical groups and user preferences
Model model = new ModelManager(TypicalGroups.getTypicalAddressBook(), new UserPrefs());

// Choose a group number that is known to be non-existent (e.g., 5000)
int nonExistentGroupNumber = 5000;

// Ensure that the chosen group number is non-existent
assertTrue(model.getGroupWithNumber(nonExistentGroupNumber).isEmpty(),
"Precondition: Group with the chosen number should not exist in the model.");

// Create a DeleteGroupCommand for the non-existent group number
DeleteGroupCommand deleteGroupCommand = new DeleteGroupCommand(nonExistentGroupNumber);

// Use assertThrows to verify that executing the command throws the expected exception
assertThrows(CommandException.class, () -> deleteGroupCommand.execute(model),
DeleteGroupCommand.MESSAGE_DELETE_GROUP_NOT_FOUND);

// Ensure that the model remains unchanged after the command execution attempt
assertEquals(TypicalGroups.getTypicalGroups().size(), model.getFilteredGroupList().size());
}

@Test
public void execute_zeroGroupNumber_throwsCommandException() {
// Create a model with typical groups and user preferences
Model model = new ModelManager(TypicalGroups.getTypicalAddressBook(), new UserPrefs());
DeleteGroupCommand deleteGroupCommand = new DeleteGroupCommand(4);

// Create a DeleteGroupCommand for a group number of zero
DeleteGroupCommand deleteGroupCommand = new DeleteGroupCommand(0);

// Use assertThrows to verify that executing the command throws the expected exception
assertThrows(CommandException.class, () -> deleteGroupCommand.execute(model));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package seedu.address.logic.parser;


import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUP;
import static seedu.address.testutil.Assert.assertThrows;

Expand All @@ -17,15 +19,22 @@ public class DeleteGroupCommandParserTest {
public void parse_validArgs_returnsDeleteGroupCommand() {
// Test valid input
String userInput = " " + PREFIX_GROUP + "1";
DeleteGroupCommand expectedCommand = new DeleteGroupCommand(1);
CommandParserTestUtil.assertParseSuccess(parser, userInput, expectedCommand);
}
int groupNumber = 1;
DeleteGroupCommand expectedCommand = new DeleteGroupCommand(groupNumber);

// Use assertDoesNotThrow to check if parsing is successful
assertDoesNotThrow(() -> {
DeleteGroupCommand actualCommand = parser.parse(userInput);

// Compare the groupNumber attribute directly
assertEquals(expectedCommand.getGroupNumber(), actualCommand.getGroupNumber());
});
}
@Test
public void parse_missingGroupPrefix_throwsParseException() {
// Test input without group prefix
String userInput = "1";
String expectedMessage = String.format(DeleteGroupCommand.MESSAGE_USAGE);
String expectedMessage = String.format("Invalid command format! \n" + DeleteGroupCommand.MESSAGE_USAGE);
CommandParserTestUtil.assertParseFailure(parser, userInput, expectedMessage);
}

Expand Down
Loading