transcendental-lisp/test/scanner/LispFilterInputStreamTester...

190 lines
5.4 KiB
Java
Raw Normal View History

package scanner;
import static org.junit.Assert.assertEquals;
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
2016-12-11 11:23:44 -05:00
public void noBytesIn_noBytesOut() {
String input = "";
assertEquals(input, getLispFilterInputStreamResult(input));
}
@Test
2016-12-11 11:23:44 -05:00
public void oneCharacter_notRemoved() {
String input = "x";
assertEquals(input, getLispFilterInputStreamResult(input));
}
@Test
2016-12-11 11:23:44 -05:00
public void allNonCommentCharacters_notRemoved() {
String input = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
2016-12-11 11:23:44 -05:00
+ "`1234567890-=~!@#$%^&*()_+[]\\',./{}|:\"<>?";
assertEquals(input, getLispFilterInputStreamResult(input));
}
@Test
2016-12-11 11:23:44 -05:00
public void oneComment_Removed() {
String input = ";comment";
String expectedResult = "";
assertEquals(expectedResult, getLispFilterInputStreamResult(input));
}
@Test
2016-12-11 11:23:44 -05:00
public void multipleComments_Removed() {
String input = ";comment1\n;comment2\n;comment3";
String expectedResult = "\n\n";
assertEquals(expectedResult, getLispFilterInputStreamResult(input));
}
@Test
2016-12-11 11:23:44 -05:00
public void nil_NotRemoved() {
String input = "()";
String expectedResult = "()";
assertEquals(expectedResult, getLispFilterInputStreamResult(input));
}
@Test
2016-12-11 11:23:44 -05:00
public void interiorComment_Removed() {
String input = "(;this is a comment\n)";
String expectedResult = "(\n)";
assertEquals(expectedResult, getLispFilterInputStreamResult(input));
}
@Test
2016-12-11 11:23:44 -05:00
public void commentInString_NotRemoved() {
String input = "\"string;this should remain\"";
assertEquals(input, getLispFilterInputStreamResult(input));
}
@Test
2016-12-11 11:23:44 -05:00
public void commentInStringWithNewline_NotRemoved() {
String input = "\"string;this should\n remain\"";
assertEquals(input, getLispFilterInputStreamResult(input));
}
@Test
2016-12-11 11:23:44 -05:00
public void commentInStringWithEscapedDoubleQuote_NotRemoved() {
String input = "\"string \\\" ;this should remain\"";
assertEquals(input, getLispFilterInputStreamResult(input));
}
@Test
2016-12-11 11:23:44 -05:00
public void manyCommentsWithStatements_OnlyCommentsRemoved() {
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));
}
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) {
InputStream stringInputStream = TestUtilities.createInputStreamFromString(inputString);
2016-12-11 11:23:44 -05:00
return new LispFilterInputStream(stringInputStream);
}
2016-12-11 11:23:44 -05:00
private String readInputStreamIntoString(LispInputStream inputStream) {
int c = inputStream.read();
while (c != -1) {
charactersRead.append((char) c);
c = inputStream.read();
}
return charactersRead.toString();
}
}