package guitests;

import static org.junit.Assert.assertTrue;
import static seedu.jobs.logic.commands.UndoCommand.MESSAGE_FAILURE;
import static seedu.jobs.logic.commands.UndoCommand.MESSAGE_SUCCESS;

import org.junit.Test;

import seedu.jobs.model.task.UniqueTaskList.IllegalTimeException;
import seedu.jobs.testutil.TestTask;
import seedu.jobs.testutil.TestUtil;

//@@author A0140055W

public class UndoCommandTest extends TaskBookGuiTest {

    @Test
    public void undo() throws IllegalArgumentException, IllegalTimeException {

        commandBox.runCommand("undo");
        assertListSize(td.getTypicalTasks().length);

        commandBox.runCommand("redo");
        // to add a random task to be undone
        TestTask[] currentList = td.getTypicalTasks();
        TestTask taskToAdd = td.CS4101;
        commandBox.runCommand(td.CS4101.getAddCommand());
        currentList = TestUtil.addTasksToList(currentList, taskToAdd);
        for (TestTask t : currentList) {
            System.out.println(t);
        }
        assertUndoSuccess(currentList, currentList.length - 1);
        // to revert back to original

        commandBox.runCommand("redo");

        // add another task for 2 undoes
        taskToAdd = td.CS4102;
        commandBox.runCommand(taskToAdd.getAddCommand());
        currentList = TestUtil.addTasksToList(currentList, taskToAdd);
        // first undo
        assertUndoSuccess(currentList, currentList.length - 1);
        // second undo
        currentList = TestUtil.removeTasksFromList(currentList, currentList[currentList.length - 1]);
        assertUndoSuccess(currentList, currentList.length - 1);
        // third undo
        currentList = TestUtil.removeTasksFromList(currentList, currentList[currentList.length - 1]);
        commandBox.runCommand("undo");
        // last undo
        commandBox.runCommand("undo");

        assertResultMessage(MESSAGE_FAILURE);
    }

    private void assertUndoSuccess(final TestTask[] currentList, int lastIndex)
            throws IllegalArgumentException, IllegalTimeException {
        TestTask taskToBeUndone = currentList[lastIndex];
        TestTask[] expectedResult = TestUtil.removeTasksFromList(currentList, taskToBeUndone);

        commandBox.runCommand("undo");

        // confirm the resultant list after undoing matches the original
        assertTrue(taskListPanel.isListMatching(expectedResult));

        // confirm that the result message is correct
        assertResultMessage(String.format(MESSAGE_SUCCESS, taskToBeUndone));

    }
}

package guitests;

import static org.junit.Assert.assertTrue;
import static seedu.jobs.logic.commands.UndoCommand.MESSAGE_FAILURE;
import static seedu.jobs.logic.commands.UndoCommand.MESSAGE_SUCCESS;

import org.junit.Test;

import seedu.jobs.model.task.UniqueTaskList.IllegalTimeException;
import seedu.jobs.testutil.TestTask;
import seedu.jobs.testutil.TestUtil;

//@@author A0140055W

public class UndoCommandTest extends TaskBookGuiTest {

    @Test
    public void undo() throws IllegalArgumentException, IllegalTimeException {

        commandBox.runCommand("undo");
        assertListSize(td.getTypicalTasks().length);

        commandBox.runCommand("redo");
        // to add a random task to be undone
        TestTask[] currentList = td.getTypicalTasks();
        TestTask taskToAdd = td.CS4101;
        commandBox.runCommand(td.CS4101.getAddCommand());
        currentList = TestUtil.addTasksToList(currentList, taskToAdd);
        assertUndoSuccess(currentList, currentList.length - 1);
        // to revert back to original

        commandBox.runCommand("redo");

        // add another task for 2 undoes
        taskToAdd = td.CS4102;
        commandBox.runCommand(taskToAdd.getAddCommand());
        currentList = TestUtil.addTasksToList(currentList, taskToAdd);
        // first undo
        commandBox.runCommand("undo");
        // second undo
        currentList = TestUtil.removeTasksFromList(currentList, currentList[currentList.length - 1]);
        assertUndoSuccess(currentList, currentList.length - 1);
        // third undo
        currentList = TestUtil.removeTasksFromList(currentList, currentList[currentList.length - 1]);
        commandBox.runCommand("undo");
        // last undo
        commandBox.runCommand("undo");

        assertResultMessage(MESSAGE_FAILURE);
    }

    private void assertUndoSuccess(final TestTask[] currentList, int lastIndex)
            throws IllegalArgumentException, IllegalTimeException {
        TestTask taskToBeUndone = currentList[lastIndex];

        commandBox.runCommand("undo");
        TestTask[] expectedResult = TestUtil.removeTasksFromList(currentList, taskToBeUndone);

        // confirm the resultant list after undoing matches the original
        assertTrue(taskListPanel.isListMatching(expectedResult));

        // confirm that the result message is correct
        assertResultMessage(String.format(MESSAGE_SUCCESS, taskToBeUndone));

    }
}

Fix undo test