From 243f8a83ac3f71901091e2b0c36a92d5f85066a2 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Tue, 4 Apr 2017 07:56:35 -0400 Subject: [PATCH] Add terminal history class --- src/terminal/TerminalHistory.java | 42 ++++++++++ test/terminal/TerminalHistoryTest.java | 103 +++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 src/terminal/TerminalHistory.java create mode 100644 test/terminal/TerminalHistoryTest.java diff --git a/src/terminal/TerminalHistory.java b/src/terminal/TerminalHistory.java new file mode 100644 index 0000000..cd7264c --- /dev/null +++ b/src/terminal/TerminalHistory.java @@ -0,0 +1,42 @@ +package terminal; + +import java.util.*; + +public class TerminalHistory { + + private List history; + private int currentLineIndex; + + public TerminalHistory() { + this.history = new ArrayList<>(); + this.currentLineIndex = 0; + } + + public void addLine(String line) { + history.add(line); + currentLineIndex = history.size(); + } + + public String getPreviousLine() { + if (isBeginning()) + return null; + + return history.get(--currentLineIndex); + } + + private boolean isBeginning() { + return currentLineIndex == 0; + } + + public String getNextLine() { + if (isEnd()) + return null; + + return history.get(++currentLineIndex); + } + + private boolean isEnd() { + return currentLineIndex >= history.size() - 1; + } + +} diff --git a/test/terminal/TerminalHistoryTest.java b/test/terminal/TerminalHistoryTest.java new file mode 100644 index 0000000..ec25303 --- /dev/null +++ b/test/terminal/TerminalHistoryTest.java @@ -0,0 +1,103 @@ +package terminal; + +import static org.junit.Assert.*; + +import org.junit.*; + +public class TerminalHistoryTest { + + private TerminalHistory history; + + private void assertAtBeginning() { + assertNull(history.getPreviousLine()); + } + + private void assertAtEnd() { + assertNull(history.getNextLine()); + } + + private void assertPrevious(String expected) { + assertEquals(expected, history.getPreviousLine()); + } + + private void assertNext(String expected) { + assertEquals(expected, history.getNextLine()); + } + + @Before + public void setUp() { + history = new TerminalHistory(); + } + + @After + public void tearDown() {} + + @Test + public void historyStartsWithNoLines() { + assertAtBeginning(); + assertAtEnd(); + } + + @Test + public void addOneLineToHistory() { + history.addLine("test line"); + + assertPrevious("test line"); + assertAtBeginning(); + assertAtEnd(); + } + + @Test + public void moveBackwards() { + history.addLine("one"); + history.addLine("two"); + + assertPrevious("two"); + assertPrevious("one"); + assertAtBeginning(); + } + + @Test + public void moveForwards() { + history.addLine("one"); + history.addLine("two"); + history.getPreviousLine(); + history.getPreviousLine(); + + assertNext("two"); + assertAtEnd(); + } + + @Test + public void moveBackAndForth() { + history.addLine("one"); + history.addLine("two"); + + assertAtEnd(); + assertPrevious("two"); + assertAtEnd(); + assertPrevious("one"); + assertAtBeginning(); + assertNext("two"); + assertAtEnd(); + assertPrevious("one"); + assertAtBeginning(); + assertNext("two"); + assertAtEnd(); + } + + @Test + public void addedLineGoesAtEndOfHistory() { + history.addLine("one"); + history.addLine("two"); + history.getPreviousLine(); + history.getPreviousLine(); + history.addLine("three"); + + assertPrevious("three"); + assertPrevious("two"); + assertPrevious("one"); + assertAtBeginning(); + } + +}