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(); } }