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.
Merge pull request nus-cs2103-AY2425S1#89 from weijianwong/branch-arr…
…ow-keys Up arrow goes to previous command
- Loading branch information
Showing
4 changed files
with
206 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,58 @@ | ||
package tuteez.logic.commands; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Represents the history of commands entered by the user. | ||
*/ | ||
public class CommandHistory { | ||
private final List<String> history; | ||
private int currentIndex; | ||
|
||
/** | ||
* Creates a CommandHistory class to remember the history of commands the user entered. | ||
* The current index is initially set to -1 to indicate that no commands have been accessed | ||
* or selected from the history yet. | ||
*/ | ||
public CommandHistory() { | ||
this.history = new ArrayList<>(); | ||
this.currentIndex = -1; | ||
} | ||
|
||
/** | ||
* Adds a new command to the history and resets the current index. | ||
*/ | ||
public void add(String command) { | ||
assert command != null; | ||
history.add(command); | ||
// Reset index to the end of the list. | ||
currentIndex = history.size(); | ||
} | ||
|
||
/** | ||
* Returns the previous command in history, or null if no previous command exists. | ||
*/ | ||
public String getPreviousCommand() { | ||
assert currentIndex >= -1 && currentIndex <= history.size(); | ||
if (currentIndex > 0) { | ||
currentIndex--; | ||
return history.get(currentIndex); | ||
} | ||
return null; // No previous command available | ||
} | ||
|
||
/** | ||
* Returns the next command in history, or an empty string if no next command exists. | ||
*/ | ||
public String getNextCommand() { | ||
assert currentIndex >= -1 && currentIndex <= history.size(); | ||
if (currentIndex < history.size() - 1) { | ||
currentIndex++; | ||
return history.get(currentIndex); | ||
} | ||
currentIndex = history.size(); | ||
return ""; | ||
} | ||
|
||
} |
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
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
89 changes: 89 additions & 0 deletions
89
src/test/java/tuteez/logic/commands/CommandHistoryTest.java
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,89 @@ | ||
package tuteez.logic.commands; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
public class CommandHistoryTest { | ||
private CommandHistory commandHistory; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
commandHistory = new CommandHistory(); | ||
} | ||
|
||
@Test | ||
public void add_nullCommand_throwsException() { | ||
// Check that adding a null command throws an exception | ||
assertThrows(AssertionError.class, () -> commandHistory.add(null)); | ||
} | ||
|
||
@Test | ||
public void add_correctlyAddsCommands() { | ||
commandHistory.add("command1"); | ||
commandHistory.add("command2"); | ||
|
||
// Check if the commands have been added correctly | ||
assertEquals("command2", commandHistory.getPreviousCommand()); | ||
assertEquals("command1", commandHistory.getPreviousCommand()); | ||
|
||
// Ensure currentIndex is reset to the end of the list after adding commands | ||
commandHistory.add("command3"); | ||
assertEquals("command3", commandHistory.getPreviousCommand()); // Should return command3 | ||
} | ||
@Test | ||
public void getPreviousCommand_correctlyGetsPreviousCommands() { | ||
commandHistory.add("command1"); | ||
commandHistory.add("command2"); | ||
commandHistory.add("command3"); | ||
|
||
// Check if the last command can be retrieved | ||
assertEquals("command3", commandHistory.getPreviousCommand()); | ||
assertEquals("command2", commandHistory.getPreviousCommand()); | ||
assertEquals("command1", commandHistory.getPreviousCommand()); | ||
assertNull(commandHistory.getPreviousCommand()); | ||
} | ||
|
||
@Test | ||
public void getNextCommand_correctlyGetsNextCommands() { | ||
commandHistory.add("command1"); | ||
commandHistory.add("command2"); | ||
assertEquals("command2", commandHistory.getPreviousCommand()); | ||
assertEquals("command1", commandHistory.getPreviousCommand()); | ||
assertEquals("command2", commandHistory.getNextCommand()); | ||
assertEquals("", commandHistory.getNextCommand()); // No more next commands | ||
} | ||
|
||
@Test | ||
public void getNextCommand_invalidCurrentIndex_throwsAssertionError() throws Exception { | ||
commandHistory.add("command1"); | ||
commandHistory.add("command2"); | ||
|
||
// Use reflection to set currentIndex to an invalid value (e.g., larger than history.size()) | ||
Field field = CommandHistory.class.getDeclaredField("currentIndex"); | ||
field.setAccessible(true); | ||
field.set(commandHistory, 999); // Invalid index | ||
|
||
// Expect an AssertionError when calling getNextCommand() | ||
assertThrows(AssertionError.class, () -> commandHistory.getNextCommand()); | ||
} | ||
|
||
@Test | ||
public void getPreviousCommand_invalidCurrentIndex_throwsAssertionError() throws Exception { | ||
commandHistory.add("command1"); | ||
commandHistory.add("command2"); | ||
|
||
// Use reflection to set currentIndex to an invalid value (e.g., negative beyond -1) | ||
Field field = CommandHistory.class.getDeclaredField("currentIndex"); | ||
field.setAccessible(true); | ||
field.set(commandHistory, -999); // Invalid index | ||
|
||
// Expect an AssertionError when calling getPreviousCommand() | ||
assertThrows(AssertionError.class, () -> commandHistory.getPreviousCommand()); | ||
} | ||
|
||
} |