Skip to content

Commit

Permalink
Merge pull request AY2324S1-CS2103T-T09-2#250 from mingyu-wan/add-tes…
Browse files Browse the repository at this point in the history
…t-cases

Add test cases
  • Loading branch information
emzm2023 authored Nov 13, 2023
2 parents 71487c1 + e3b2d8e commit c28d051
Show file tree
Hide file tree
Showing 15 changed files with 769 additions and 9 deletions.
4 changes: 2 additions & 2 deletions docs/DeveloperGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ trimmed to `Tester` and calls `AddDeveloperRoleCommand`.
a new developer role if there is no such role.

<div markdown="span" class="alert alert-warning">:exclamation: **Note:**
Although no changes is made to the address book, but this stage is still committed so that the success command
Although no changes is made to the address book, this stage is still committed so that the success command
message and tab index switched to can be changed, the currentPointer can also note that there is an action done here.</div>

<div markdown="span" class="alert alert-warning">:exclamation: **Note:**
Expand Down Expand Up @@ -488,7 +488,7 @@ trimmed to `Tester` and calls `DeleteDeveloperRoleCommand`.
if `DeveloperRoles#isRemovableRole()` returns true.

<div markdown="span" class="alert alert-warning">:exclamation: **Note:**
Although no changes is made to the address book, but this stage is still committed so that the success command
Although no changes is made to the address book, this stage is still committed so that the success command
message and tab index switched to can be changed, the currentPointer can also note that there is an action done here.</div>

The following sequence diagram shows how the Delete-role operation works:
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/seedu/address/logic/commands/RedoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public CommandResult execute(Model model) throws CommandException {
model.redoAddressBook(model);
String previousCommand = model.getPreviousCommandForRedo();
TabIndex index = model.getPreviousTabIndexForRedo();
handleRoleRedo(previousCommand);
return new CommandResult(MESSAGE_SUCCESS + "\n" + previousCommand, index);
}

private void handleRoleRedo(String previousCommand) {
if (previousCommand.contains("New role for client added: ")) {
ClientRoles.addClientRole(new ClientRoles(previousCommand.substring(27)));
} else if (previousCommand.contains("New role for developer added: ")) {
Expand All @@ -31,6 +35,5 @@ public CommandResult execute(Model model) throws CommandException {
} else if (previousCommand.contains("Role for developers deleted: ")) {
DeveloperRoles.deleteDeveloperRole(new DeveloperRoles(previousCommand.substring(29)));
}
return new CommandResult(MESSAGE_SUCCESS + "\n" + previousCommand, index);
}
}
6 changes: 5 additions & 1 deletion src/main/java/seedu/address/logic/commands/UndoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public CommandResult execute(Model model) throws CommandException {
String previousCommand = model.getPreviousCommandForUndo();
TabIndex index = model.getPreviousTabIndex();
// check if it is any of the role commands
handleRoleUndo(previousCommand);
return new CommandResult(MESSAGE_SUCCESS + "\n" + previousCommand, index);
}

private void handleRoleUndo(String previousCommand) {
if (previousCommand.contains("New role for client added: ")) {
ClientRoles.deleteClientRole(new ClientRoles(previousCommand.substring(27)));
} else if (previousCommand.contains("New role for developer added: ")) {
Expand All @@ -40,6 +45,5 @@ public CommandResult execute(Model model) throws CommandException {
} else if (previousCommand.contains("Role for developers deleted: ")) {
DeveloperRoles.addDeveloperRole(new DeveloperRoles(previousCommand.substring(29)));
}
return new CommandResult(MESSAGE_SUCCESS + "\n" + previousCommand, index);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class AddClientRoleCommand extends Command {
+ "Example: " + PREFIX_ROLE + "Developer ";

public static final String MESSAGE_SUCCESS = "New role for client added: %1$s";
public static final String MESSAGE_DUPLICATE_DEVELOPER = "This client role already exists in the address book!";
public static final String MESSAGE_DUPLICATE_ROLE = "This client role already exists in the address book!";
private final String toAdd;

/**
Expand All @@ -49,7 +49,7 @@ public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (ClientRoles.isValidRole(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_DEVELOPER);
throw new CommandException(MESSAGE_DUPLICATE_ROLE);
}

String successMessage = String.format(MESSAGE_SUCCESS, Messages.format(toAdd));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class AddDeveloperRoleCommand extends Command {
+ "Example: " + PREFIX_ROLE + "Developer ";

public static final String MESSAGE_SUCCESS = "New role for developer added: %1$s";
public static final String MESSAGE_DUPLICATE_DEVELOPER = "This developer role already exists in the address book!";
public static final String MESSAGE_DUPLICATE_ROLE = "This developer role already exists in the address book!";
private final String toAdd;

/**
Expand All @@ -50,7 +50,7 @@ public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (DeveloperRoles.isValidRole(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_DEVELOPER);
throw new CommandException(MESSAGE_DUPLICATE_ROLE);
}

String successMessage = String.format(MESSAGE_SUCCESS, Messages.format(toAdd));
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/seedu/address/model/VersionedAddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,24 @@ public TabIndex getPreviousTabIndexForRedo() {
return tabIndex.get(currentStatePointer - 1);
}

/**
* Gets the current addressbook.
*
* @return The tab index from the previous command.
*/
public ReadOnlyAddressBook getCurrentState(int currentStatePointer) {
return addressBookStateList.get(currentStatePointer);
}

/**
* Gets the current current state pointer.
*
* @return The tab index from the previous command.
*/
public int getCurrentStatePointer() {
return currentStatePointer;
}

/**
* Changes the current model's address book to match the current state.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public static void showClientAtIndex(Model model, Index targetIndex) {
}

/**
* Updates {@code model}'s filtered list to show only the project at the given {@code targetIndex} in the
* Updates {@code model}'s filtered list to show only the client at the given {@code targetIndex} in the
* {@code model}'s address book.
*/
public static void showProjectAtIndex(Model model, Index targetIndex) {
Expand All @@ -314,6 +314,7 @@ public static void showProjectAtIndex(Model model, Index targetIndex) {
Project project = model.getFilteredProjectList().get(targetIndex.getZeroBased());
final String[] splitName = project.getName().split("\\s+");
model.updateFilteredProjectList(new ProjectNameContainsKeywordsPredicate(Arrays.asList(splitName[0])));
assertEquals(4, model.getFilteredProjectList().size());
}

}
103 changes: 103 additions & 0 deletions src/test/java/seedu/address/logic/commands/RedoCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalClients.getTypicalAddressBook;

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.client.ClientRoles;
import seedu.address.model.developer.DeveloperRoles;
import seedu.address.testutil.ClientBuilder;

public class RedoCommandTest {

private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private Model modelAfterChange = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private RedoCommand redoCommand = new RedoCommand();
private UndoCommand undoCommand = new UndoCommand();

@Test
public void execute_validRedo_success() throws CommandException {
ClientBuilder clientBuilder = new ClientBuilder();
model.addClient(clientBuilder.build());
model.commitAddressBook(model, "Add Benson", TabIndex.Client);
model.undoAddressBook(model);

modelAfterChange = model;

redoCommand.execute(model);

assertEquals(model, modelAfterChange); // Ensure that the redo was successful
}

@Test
public void execute_noRedoAvailable_throwsCommandException() {
assertThrows(CommandException.class, () -> redoCommand.execute(model));
}

@Test
public void execute_redoRoleCommand_success() throws CommandException {
// Undo a command that added a new role for a client
ClientRoles.addClientRole(new ClientRoles("NewRole"));
model.commitAddressBook(model, "New role for client added: NewRole", TabIndex.Client);
undoCommand.execute(model);

// Redo the role addition
redoCommand.execute(model);

assertTrue(ClientRoles.isValidRole("NewRole"));
ClientRoles.deleteClientRole(new ClientRoles("NewRole"));
}

@Test
public void execute_redoDeveloperRoleCommand_success() throws CommandException {
// Undo a command that added a new role for a developer
DeveloperRoles.addDeveloperRole(new DeveloperRoles("NewRole"));
model.commitAddressBook(model, "New role for developer added: NewRole", TabIndex.Developer);
undoCommand.execute(model);

// Redo the role addition
redoCommand.execute(model);
assertTrue(DeveloperRoles.isValidRole("NewRole"));
DeveloperRoles.deleteDeveloperRole(new DeveloperRoles("NewRole"));
}

@Test
public void execute_redoDeleteClientRoleCommand_success() throws CommandException {
ClientRoles.addClientRole(new ClientRoles("RoleToDelete"));
model.commitAddressBook(model, "New role for client added: RoleToDelete", TabIndex.Client);

// Undo a command that deleted a role for a client
ClientRoles.deleteClientRole(new ClientRoles("RoleToDelete"));
model.commitAddressBook(model, "Role for clients deleted: RoleToDelete", TabIndex.Client);
undoCommand.execute(model);

// Redo the role deletion
redoCommand.execute(model);

assertFalse(ClientRoles.isValidRole("RoleToDelete"));
}

@Test
public void execute_redoDeleteDeveloperRoleCommand_success() throws CommandException {
DeveloperRoles.addDeveloperRole(new DeveloperRoles("RoleToDelete"));
model.commitAddressBook(model, "New role for developer added: NewRole", TabIndex.Developer);

// Undo a command that deleted a role for a developer
DeveloperRoles.deleteDeveloperRole(new DeveloperRoles("RoleToDelete"));
model.commitAddressBook(model, "Role for developers deleted: RoleToDelete", TabIndex.Developer);
undoCommand.execute(model);

// Redo the role deletion
redoCommand.execute(model);

assertFalse(DeveloperRoles.isValidRole("RoleToDelete"));
}
}
121 changes: 121 additions & 0 deletions src/test/java/seedu/address/logic/commands/UndoCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
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.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalClients.BOB;
import static seedu.address.testutil.TypicalClients.getTypicalAddressBook;

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.client.ClientRoles;
import seedu.address.model.developer.DeveloperRoles;
import seedu.address.testutil.ClientBuilder;
import seedu.address.testutil.DeveloperBuilder;

public class UndoCommandTest {

private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private Model modelAfterChange = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private UndoCommand undoCommand = new UndoCommand();

@Test
public void execute_validUndo_success() throws CommandException {
model.addClient(BOB);
model.commitAddressBook(model, "Add Alice", TabIndex.Client);
undoCommand.execute(model);
assertEquals(model, modelAfterChange);
}

@Test
public void execute_noUndoAvailable_throwsCommandException() {
assertThrows(CommandException.class, () -> undoCommand.execute(model));
}

@Test
public void execute_undoNonRoleCommand_success() throws CommandException {
// Add a new role for a client
ClientBuilder clientBuilder = new ClientBuilder();
model.addClient(clientBuilder.build());
model.commitAddressBook(model, "New client added", TabIndex.Client);

// Undo the role addition
undoCommand.execute(model);

assertFalse(model.getFilteredClientList().contains(clientBuilder.build()));
}

@Test
public void execute_undoClientRoleCommand_success() throws CommandException {
// Add a new role for a client
ClientRoles.addClientRole(new ClientRoles("NewRole"));
model.commitAddressBook(model, "New role for client added: NewRole", TabIndex.Client);

// Undo the role addition
undoCommand.execute(model);

assertFalse(ClientRoles.isValidRole("NewRole"));
}

@Test
public void execute_undoNonDeveloperRoleCommand_success() throws CommandException {
// Add a new role for a client
DeveloperBuilder developerBuilder = new DeveloperBuilder();
model.addDeveloper(developerBuilder.build());
model.commitAddressBook(model, "New Developer Added", TabIndex.Developer);

// Undo the role addition
undoCommand.execute(model);

assertFalse(model.getFilteredClientList().contains(developerBuilder.build()));
}

@Test
public void execute_undoDeveloperRoleCommand_success() throws CommandException {
// Add a new role for a client
DeveloperRoles.addDeveloperRole(new DeveloperRoles("NewRole"));
model.commitAddressBook(model, "New role for developer added: NewRole", TabIndex.Developer);

// Undo the role addition
undoCommand.execute(model);

assertFalse(DeveloperRoles.isValidRole("NewRole"));
}

@Test
public void execute_undoDeleteClientRoleCommand_success() throws CommandException {
ClientRoles.addClientRole(new ClientRoles("NewRole"));
model.commitAddressBook(model, "New role for client added: NewRole", TabIndex.Client);

// Delete a role for a client
ClientRoles.deleteClientRole(new ClientRoles("NewRole"));
model.commitAddressBook(model, "Role for clients deleted: NewRole", TabIndex.Client);

// Undo the role deletion
undoCommand.execute(model);

assertTrue(ClientRoles.isValidRole("NewRole"));
ClientRoles.deleteClientRole(new ClientRoles("NewRole"));
}

@Test
public void execute_undoDeleteDeveloperRoleCommand_success() throws CommandException {
DeveloperRoles.addDeveloperRole(new DeveloperRoles("RoleToDelete"));
model.commitAddressBook(model, "New role for client added: NewRole", TabIndex.Developer);

// Delete a role for a developer
DeveloperRoles.deleteDeveloperRole(new DeveloperRoles("RoleToDelete"));
model.commitAddressBook(model, "Role for developers deleted: RoleToDelete", TabIndex.Developer);

// Undo the role deletion
undoCommand.execute(model);

assertTrue(DeveloperRoles.isValidRole("RoleToDelete"));
DeveloperRoles.deleteDeveloperRole(new DeveloperRoles("RoleToDelete"));
}
}
Loading

0 comments on commit c28d051

Please sign in to comment.