Skip to content

Commit

Permalink
Add test cases for CommandHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
juliantayyc committed Oct 21, 2024
1 parent b10f000 commit cf6d079
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/test/java/seedu/address/ui/CommandHistoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package seedu.address.ui;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import seedu.address.ui.util.CommandHistory;

/**
* Unit tests for {@code CommandHistory}.
*/
public class CommandHistoryTest {

private CommandHistory commandHistory;

@BeforeEach
public void setUp() {
commandHistory = new CommandHistory();
}

@Test
public void addCommand_singleCommand_success() {
commandHistory.addCommand("first command");
assertEquals("first command", commandHistory.getPreviousCommand());
}

@Test
public void addCommand_multipleCommands_success() {
commandHistory.addCommand("first command");
commandHistory.addCommand("second command");
commandHistory.addCommand("third command");

assertEquals("third command", commandHistory.getPreviousCommand());
assertEquals("second command", commandHistory.getPreviousCommand());
assertEquals("first command", commandHistory.getPreviousCommand());
}

@Test
public void getPreviousCommand_noHistory_returnsNull() {
assertNull(commandHistory.getPreviousCommand());
}

@Test
public void getPreviousCommand_atStartOfHistory_returnsNull() {
commandHistory.addCommand("first command");

// Move to the start of the history
commandHistory.getPreviousCommand();
assertNull(commandHistory.getPreviousCommand());
}

@Test
public void getNextCommand_noHistory_returnsNull() {
assertNull(commandHistory.getNextCommand());
}

@Test
public void getNextCommand_atEndOfHistory_returnsNull() {
commandHistory.addCommand("first command");
commandHistory.addCommand("second command");

// Move to the start of history
commandHistory.getPreviousCommand();
commandHistory.getPreviousCommand();

// Move forward to the end of history
commandHistory.getNextCommand();
commandHistory.getNextCommand();
assertNull(commandHistory.getNextCommand());
}

@Test
public void getNextCommand_fromMiddleOfHistory_success() {
commandHistory.addCommand("first command");
commandHistory.addCommand("second command");
commandHistory.addCommand("third command");

// Move to the start of history
commandHistory.getPreviousCommand();
commandHistory.getPreviousCommand();
commandHistory.getPreviousCommand();

// Move forward one step in history
assertEquals("second command", commandHistory.getNextCommand());
}

@Test
public void addCommand_afterNavigatingHistory_resetsHistoryIndex() {
commandHistory.addCommand("first command");
commandHistory.addCommand("second command");

// Move to the start of history
commandHistory.getPreviousCommand();
commandHistory.getPreviousCommand();

// Add a new command
commandHistory.addCommand("new command");

// New command should be at the end of history
assertEquals("new command", commandHistory.getPreviousCommand());
assertNull(commandHistory.getNextCommand()); // At the end of history
}
}

0 comments on commit cf6d079

Please sign in to comment.