From daf35a72faa0058271b3c6ab1677be33930837a3 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Thu, 8 Dec 2016 10:59:02 -0500 Subject: [PATCH] Refactored and added unit tests for LispFilterInputStream --- extend.lisp => lisp/extend.lisp | 0 fact.lisp => lisp/fact.lisp | 0 reverse.lisp => lisp/reverse.lisp | 0 test.lisp => lisp/test.lisp | 0 src/main/LispInterpreter.java | 2 +- ...Stream.java => LispFilterInputStream.java} | 6 +- src/scanner/LispScanner.java | 4 +- test/scanner/LispFilterInputStreamTester.java | 85 +++++++++++++++++++ test/scanner/LispFilterStreamTester.java | 22 ----- 9 files changed, 91 insertions(+), 28 deletions(-) rename extend.lisp => lisp/extend.lisp (100%) rename fact.lisp => lisp/fact.lisp (100%) rename reverse.lisp => lisp/reverse.lisp (100%) rename test.lisp => lisp/test.lisp (100%) rename src/scanner/{LispFilterStream.java => LispFilterInputStream.java} (86%) create mode 100644 test/scanner/LispFilterInputStreamTester.java delete mode 100644 test/scanner/LispFilterStreamTester.java diff --git a/extend.lisp b/lisp/extend.lisp similarity index 100% rename from extend.lisp rename to lisp/extend.lisp diff --git a/fact.lisp b/lisp/fact.lisp similarity index 100% rename from fact.lisp rename to lisp/fact.lisp diff --git a/reverse.lisp b/lisp/reverse.lisp similarity index 100% rename from reverse.lisp rename to lisp/reverse.lisp diff --git a/test.lisp b/lisp/test.lisp similarity index 100% rename from test.lisp rename to lisp/test.lisp diff --git a/src/main/LispInterpreter.java b/src/main/LispInterpreter.java index 140ecac..154e1fe 100644 --- a/src/main/LispInterpreter.java +++ b/src/main/LispInterpreter.java @@ -21,7 +21,7 @@ import java.text.MessageFormat; */ public class LispInterpreter { - private static final String GREETING = "SUNY Potsdam Lisp Interpreter - Version 1.0.1"; + private static final String GREETING = "SUNY Potsdam Lisp Interpreter - Version 4.4.3"; private static final String PROMPT = "~ "; public static final String ANSI_RESET = "\u001B[0m"; diff --git a/src/scanner/LispFilterStream.java b/src/scanner/LispFilterInputStream.java similarity index 86% rename from src/scanner/LispFilterStream.java rename to src/scanner/LispFilterInputStream.java index 3758442..18b0794 100644 --- a/src/scanner/LispFilterStream.java +++ b/src/scanner/LispFilterInputStream.java @@ -5,14 +5,14 @@ import java.io.FilterInputStream; import java.io.IOException; /** - * Replaces Lisp comments with newlines in an input stream. + * Removes Lisp comments from an input stream. */ -public class LispFilterStream extends FilterInputStream { +public class LispFilterInputStream extends FilterInputStream { private boolean inQuote; private int nextCharacter; - public LispFilterStream(InputStream in) { + public LispFilterInputStream(InputStream in) { super(in); inQuote = false; nextCharacter = 0; diff --git a/src/scanner/LispScanner.java b/src/scanner/LispScanner.java index df6df9b..e1b0c61 100644 --- a/src/scanner/LispScanner.java +++ b/src/scanner/LispScanner.java @@ -18,7 +18,7 @@ import java.io.IOException; */ public class LispScanner { - private LispFilterStream inStream; + private LispFilterInputStream inStream; private Token currToken; private String fileName; private int line; @@ -35,7 +35,7 @@ public class LispScanner { * the name of the file that in is reading from */ public LispScanner(InputStream in, String fileName) { - this.inStream = new LispFilterStream(new BufferedInputStream(in)); + this.inStream = new LispFilterInputStream(new BufferedInputStream(in)); this.currToken = null; this.fileName = fileName; this.line = 1; diff --git a/test/scanner/LispFilterInputStreamTester.java b/test/scanner/LispFilterInputStreamTester.java new file mode 100644 index 0000000..5776ad2 --- /dev/null +++ b/test/scanner/LispFilterInputStreamTester.java @@ -0,0 +1,85 @@ +package scanner; + +import static org.junit.Assert.assertEquals; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; + +import org.junit.Before; +import org.junit.Test; + +public class LispFilterInputStreamTester { + + @Test + public void oneComment_Removed() throws IOException { + String input = ";comment"; + String expectedResult = ""; + assertEquals(expectedResult, getLispFilterInputStreamResult(input)); + } + + @Test + public void multipleComments_Removed() throws IOException { + String input = ";comment1\n;comment2\n;comment3"; + String expectedResult = "\n\n"; + assertEquals(expectedResult, getLispFilterInputStreamResult(input)); + } + + @Test + public void nil_NotRemoved() throws IOException { + String input = "()"; + String expectedResult = "()"; + assertEquals(expectedResult, getLispFilterInputStreamResult(input)); + } + + @Test + public void interiorComment_Removed() throws IOException { + String input = "(;this is a comment\n)"; + String expectedResult = "(\n)"; + assertEquals(expectedResult, getLispFilterInputStreamResult(input)); + } + + @Test + public void commentInString_NotRemoved() throws IOException { + String input = "\"string;this should remain\""; + assertEquals(input, getLispFilterInputStreamResult(input)); + } + + @Test + public void commentInStringWithNewline_NotRemoved() throws IOException { + String input = "\"string;this should\n remain\""; + assertEquals(input, getLispFilterInputStreamResult(input)); + } + + @Test + public void manyCommentsWithStatements_OnlyCommentsRemoved() throws IOException { + String input = ";first comment \n '(1 2 3) \n ;second comment \n (defun add1 (x) (+ x 1)) ;third comment"; + String expectedResult = "\n '(1 2 3) \n \n (defun add1 (x) (+ x 1)) "; + assertEquals(expectedResult, getLispFilterInputStreamResult(input)); + } + + private String getLispFilterInputStreamResult(String inputString) throws IOException { + InputStream stringInputStream = createInputStreamFromString(inputString); + LispFilterInputStream lispFilterInputStream = new LispFilterInputStream(stringInputStream); + + return readInputStreamIntoString(lispFilterInputStream); + } + + private InputStream createInputStreamFromString(String string) { + return new ByteArrayInputStream(string.getBytes()); + } + + private String readInputStreamIntoString(InputStream inputStream) throws IOException { + StringBuilder stringBuilder = new StringBuilder(); + int c = inputStream.read(); + + while (c != -1) { + stringBuilder.append((char) c); + c = inputStream.read(); + } + + return stringBuilder.toString(); + } + +} diff --git a/test/scanner/LispFilterStreamTester.java b/test/scanner/LispFilterStreamTester.java deleted file mode 100644 index bf27687..0000000 --- a/test/scanner/LispFilterStreamTester.java +++ /dev/null @@ -1,22 +0,0 @@ -package scanner; - -import static org.junit.Assert.*; - -import org.junit.Before; -import org.junit.Test; - -public class LispFilterStreamTester { - - private LispFilterStream lispFilterStream; - - @Before - public void setUp() throws Exception { - lispFilterStream = new LispFilterStream(null); - } - - @Test - public void testRead() { - fail("Not yet implemented"); - } - -}