2016-12-08 10:59:02 -05:00
|
|
|
package scanner;
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
2016-12-08 15:30:40 -05:00
|
|
|
import testutils.TestUtilities;
|
|
|
|
|
2016-12-08 10:59:02 -05:00
|
|
|
public class LispFilterInputStreamTester {
|
|
|
|
|
2016-12-09 16:04:38 -05:00
|
|
|
private StringBuilder charactersRead;
|
2016-12-08 15:30:40 -05:00
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() {
|
2016-12-09 16:04:38 -05:00
|
|
|
charactersRead = new StringBuilder();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void noBytesIn_noBytesOut() {
|
2016-12-09 16:04:38 -05:00
|
|
|
String input = "";
|
|
|
|
|
|
|
|
assertEquals(input, getLispFilterInputStreamResult(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void oneCharacter_notRemoved() {
|
2016-12-09 16:04:38 -05:00
|
|
|
String input = "x";
|
|
|
|
|
|
|
|
assertEquals(input, getLispFilterInputStreamResult(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void allNonCommentCharacters_notRemoved() {
|
2016-12-09 16:04:38 -05:00
|
|
|
String input = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
2016-12-11 11:23:44 -05:00
|
|
|
+ "`1234567890-=~!@#$%^&*()_+[]\\',./{}|:\"<>?";
|
2016-12-09 16:04:38 -05:00
|
|
|
|
|
|
|
assertEquals(input, getLispFilterInputStreamResult(input));
|
2016-12-08 15:30:40 -05:00
|
|
|
}
|
|
|
|
|
2016-12-08 10:59:02 -05:00
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void oneComment_Removed() {
|
2016-12-08 10:59:02 -05:00
|
|
|
String input = ";comment";
|
|
|
|
String expectedResult = "";
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2016-12-08 10:59:02 -05:00
|
|
|
assertEquals(expectedResult, getLispFilterInputStreamResult(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void multipleComments_Removed() {
|
2016-12-08 10:59:02 -05:00
|
|
|
String input = ";comment1\n;comment2\n;comment3";
|
|
|
|
String expectedResult = "\n\n";
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2016-12-08 10:59:02 -05:00
|
|
|
assertEquals(expectedResult, getLispFilterInputStreamResult(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void nil_NotRemoved() {
|
2016-12-08 10:59:02 -05:00
|
|
|
String input = "()";
|
|
|
|
String expectedResult = "()";
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2016-12-08 10:59:02 -05:00
|
|
|
assertEquals(expectedResult, getLispFilterInputStreamResult(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void interiorComment_Removed() {
|
2016-12-08 10:59:02 -05:00
|
|
|
String input = "(;this is a comment\n)";
|
|
|
|
String expectedResult = "(\n)";
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2016-12-08 10:59:02 -05:00
|
|
|
assertEquals(expectedResult, getLispFilterInputStreamResult(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void commentInString_NotRemoved() {
|
2016-12-08 10:59:02 -05:00
|
|
|
String input = "\"string;this should remain\"";
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2016-12-08 10:59:02 -05:00
|
|
|
assertEquals(input, getLispFilterInputStreamResult(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void commentInStringWithNewline_NotRemoved() {
|
2016-12-08 10:59:02 -05:00
|
|
|
String input = "\"string;this should\n remain\"";
|
2016-12-08 15:30:40 -05:00
|
|
|
|
|
|
|
assertEquals(input, getLispFilterInputStreamResult(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void commentInStringWithEscapedDoubleQuote_NotRemoved() {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "\"string \\\" ;this should remain\"";
|
|
|
|
|
2016-12-08 10:59:02 -05:00
|
|
|
assertEquals(input, getLispFilterInputStreamResult(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void manyCommentsWithStatements_OnlyCommentsRemoved() {
|
2016-12-08 10:59:02 -05:00
|
|
|
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)) ";
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2016-12-08 10:59:02 -05:00
|
|
|
assertEquals(expectedResult, getLispFilterInputStreamResult(input));
|
|
|
|
}
|
|
|
|
|
2016-12-11 11:23:44 -05:00
|
|
|
@Test
|
|
|
|
public void unreadOneCharacter_ReturnsSameCharacter() {
|
|
|
|
String input = "abc";
|
|
|
|
char expectedResult = 'a';
|
|
|
|
LispInputStream lispInputStream = createLispInputStream(input);
|
|
|
|
|
|
|
|
lispInputStream.read();
|
|
|
|
lispInputStream.unreadLastCharacter();
|
|
|
|
|
|
|
|
assertEquals(expectedResult, lispInputStream.read());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void unreadAndRereadSameCharacterMultipleTimes_ReturnsSameCharacter() {
|
|
|
|
String input = "abc";
|
|
|
|
char expectedResult = 'a';
|
|
|
|
LispInputStream lispInputStream = createLispInputStream(input);
|
|
|
|
|
|
|
|
lispInputStream.read();
|
|
|
|
lispInputStream.unreadLastCharacter();
|
|
|
|
lispInputStream.read();
|
|
|
|
lispInputStream.unreadLastCharacter();
|
|
|
|
lispInputStream.read();
|
|
|
|
lispInputStream.unreadLastCharacter();
|
|
|
|
|
|
|
|
assertEquals(expectedResult, lispInputStream.read());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void unreadOneCharacterAndRereadIt_ReturnsNextCharacterOnNextRead() {
|
|
|
|
String input = "abc";
|
|
|
|
char expectedResult = 'b';
|
|
|
|
LispInputStream lispInputStream = createLispInputStream(input);
|
|
|
|
|
|
|
|
lispInputStream.read();
|
|
|
|
lispInputStream.unreadLastCharacter();
|
|
|
|
lispInputStream.read();
|
|
|
|
|
|
|
|
assertEquals(expectedResult, lispInputStream.read());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = LispInputStream.MaximumUnreadsExceededException.class)
|
|
|
|
public void callUnreadMultipleTimes_ThrowsException() {
|
|
|
|
String input = "abc";
|
|
|
|
LispInputStream lispInputStream = createLispInputStream(input);
|
|
|
|
|
|
|
|
lispInputStream.read();
|
|
|
|
lispInputStream.unreadLastCharacter();
|
|
|
|
lispInputStream.unreadLastCharacter();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void unreadNewlineInStringAfterComment_ReturnsNewline() {
|
|
|
|
String input = "a;123\n";
|
|
|
|
char expectedResult = '\n';
|
|
|
|
LispInputStream lispInputStream = createLispInputStream(input);
|
|
|
|
|
|
|
|
lispInputStream.read();
|
|
|
|
lispInputStream.read();
|
|
|
|
lispInputStream.unreadLastCharacter();
|
|
|
|
|
|
|
|
assertEquals(expectedResult, lispInputStream.read());
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getLispFilterInputStreamResult(String inputString) {
|
|
|
|
return readInputStreamIntoString(createLispInputStream(inputString));
|
|
|
|
}
|
|
|
|
|
|
|
|
private LispInputStream createLispInputStream(String inputString) {
|
2016-12-08 15:30:40 -05:00
|
|
|
InputStream stringInputStream = TestUtilities.createInputStreamFromString(inputString);
|
2016-12-08 10:59:02 -05:00
|
|
|
|
2016-12-11 11:23:44 -05:00
|
|
|
return new LispFilterInputStream(stringInputStream);
|
2016-12-08 10:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-12-11 11:23:44 -05:00
|
|
|
private String readInputStreamIntoString(LispInputStream inputStream) {
|
2016-12-08 10:59:02 -05:00
|
|
|
int c = inputStream.read();
|
|
|
|
|
|
|
|
while (c != -1) {
|
2016-12-09 16:04:38 -05:00
|
|
|
charactersRead.append((char) c);
|
2016-12-08 10:59:02 -05:00
|
|
|
c = inputStream.read();
|
|
|
|
}
|
|
|
|
|
2016-12-09 16:04:38 -05:00
|
|
|
return charactersRead.toString();
|
2016-12-08 10:59:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|