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 charactersRead; @Before public void setUp() { charactersRead = new StringBuilder(); } @Test public void noBytesIn_noBytesOut() throws IOException { String input = ""; assertEquals(input, getLispFilterInputStreamResult(input)); } @Test public void oneCharacter_notRemoved() throws IOException { String input = "x"; assertEquals(input, getLispFilterInputStreamResult(input)); } @Test public void allNonCommentCharacters_notRemoved() throws IOException { String input = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + "`1234567890-=~!@#$%^&*()_+[]\\',./{}|:\"<>?"; assertEquals(input, getLispFilterInputStreamResult(input)); } @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) { charactersRead.append((char) c); c = inputStream.read(); } return charactersRead.toString(); } }