Refactored and added unit tests for LispFilterInputStream
This commit is contained in:
parent
861d24ac7f
commit
daf35a72fa
|
@ -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";
|
||||
|
|
|
@ -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;
|
|
@ -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 <code>in</code> 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;
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue