package scanner; import static org.junit.Assert.assertEquals; import java.io.IOException; import java.io.InputStream; import org.junit.Before; import org.junit.Test; import testutils.TestUtilities; public class LispFilterInputStreamTester { private StringBuilder stringBuilder; @Before public void setUp() { stringBuilder = new StringBuilder(); } @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 commentInStringWithEscapedDoubleQuote_NotRemoved() throws IOException { String input = "\"string \\\" ;this should 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 = TestUtilities.createInputStreamFromString(inputString); LispFilterInputStream lispFilterInputStream = new LispFilterInputStream(stringInputStream); return readInputStreamIntoString(lispFilterInputStream); } private String readInputStreamIntoString(InputStream inputStream) throws IOException { int c = inputStream.read(); while (c != -1) { stringBuilder.append((char) c); c = inputStream.read(); } return stringBuilder.toString(); } }