forked from nus-cs2103-AY2425S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b10f000
commit cf6d079
Showing
1 changed file
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |