2016-12-08 10:59:02 -05:00
|
|
|
package scanner;
|
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
import static error.ErrorManager.Severity.CRITICAL;
|
2017-11-12 09:42:25 -05:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
import static testutil.TestUtilities.createIOExceptionThrowingInputStream;
|
|
|
|
import static testutil.TestUtilities.createInputStreamFromString;
|
2016-12-08 10:59:02 -05:00
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
2017-03-22 14:08:22 -04:00
|
|
|
import scanner.LispInputStream.MaximumUnreadsExceededException;
|
|
|
|
import stream.UncheckedIOException;
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2017-03-15 13:37:39 -04:00
|
|
|
public class LispCommentRemovingInputStreamTest {
|
2016-12-08 10:59:02 -05:00
|
|
|
|
2016-12-12 14:57:34 -05:00
|
|
|
private String getLispCommentRemovingInputStreamResult(String inputString) {
|
|
|
|
return readInputStreamIntoString(createLispInputStream(inputString));
|
|
|
|
}
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2016-12-12 14:57:34 -05:00
|
|
|
private LispInputStream createLispInputStream(String inputString) {
|
2016-12-14 14:41:43 -05:00
|
|
|
InputStream stringInputStream = createInputStreamFromString(inputString);
|
2016-12-12 14:57:34 -05:00
|
|
|
|
|
|
|
return new LispCommentRemovingInputStream(stringInputStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String readInputStreamIntoString(LispInputStream inputStream) {
|
|
|
|
StringBuilder charactersRead = new StringBuilder();
|
|
|
|
for (int c = inputStream.read(); c != -1; c = inputStream.read())
|
|
|
|
charactersRead.append((char) c);
|
|
|
|
|
|
|
|
return charactersRead.toString();
|
|
|
|
}
|
|
|
|
|
2016-12-09 16:04:38 -05:00
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void noBytesIn_noBytesOut() {
|
2016-12-09 16:04:38 -05:00
|
|
|
String input = "";
|
|
|
|
|
2016-12-12 10:15:20 -05:00
|
|
|
assertEquals(input, getLispCommentRemovingInputStreamResult(input));
|
2016-12-09 16:04:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void oneCharacter_notRemoved() {
|
2016-12-09 16:04:38 -05:00
|
|
|
String input = "x";
|
|
|
|
|
2016-12-12 10:15:20 -05:00
|
|
|
assertEquals(input, getLispCommentRemovingInputStreamResult(input));
|
2016-12-09 16:04:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
|
2016-12-12 10:15:20 -05:00
|
|
|
assertEquals(input, getLispCommentRemovingInputStreamResult(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-12 10:15:20 -05:00
|
|
|
assertEquals(expectedResult, getLispCommentRemovingInputStreamResult(input));
|
2016-12-08 10:59:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@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-12 10:15:20 -05:00
|
|
|
assertEquals(expectedResult, getLispCommentRemovingInputStreamResult(input));
|
2016-12-08 10:59:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@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-12 10:15:20 -05:00
|
|
|
assertEquals(expectedResult, getLispCommentRemovingInputStreamResult(input));
|
2016-12-08 10:59:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@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-12 10:15:20 -05:00
|
|
|
assertEquals(expectedResult, getLispCommentRemovingInputStreamResult(input));
|
2016-12-08 10:59:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@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-12 10:15:20 -05:00
|
|
|
assertEquals(input, getLispCommentRemovingInputStreamResult(input));
|
2016-12-08 10:59:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
|
2016-12-12 10:15:20 -05:00
|
|
|
assertEquals(input, getLispCommentRemovingInputStreamResult(input));
|
2016-12-08 15:30:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@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-12 10:15:20 -05:00
|
|
|
assertEquals(input, getLispCommentRemovingInputStreamResult(input));
|
2016-12-08 10:59:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@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-12 10:15:20 -05:00
|
|
|
assertEquals(expectedResult, getLispCommentRemovingInputStreamResult(input));
|
2016-12-08 10:59:02 -05:00
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2016-12-12 10:15:20 -05:00
|
|
|
@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());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = MaximumUnreadsExceededException.class)
|
2016-12-11 11:23:44 -05:00
|
|
|
public void callUnreadMultipleTimes_ThrowsException() {
|
|
|
|
String input = "abc";
|
|
|
|
LispInputStream lispInputStream = createLispInputStream(input);
|
|
|
|
|
|
|
|
lispInputStream.read();
|
|
|
|
lispInputStream.unreadLastCharacter();
|
|
|
|
lispInputStream.unreadLastCharacter();
|
|
|
|
}
|
|
|
|
|
2017-03-22 14:08:22 -04:00
|
|
|
@Test
|
2016-12-12 10:15:20 -05:00
|
|
|
public void callUnreadMultipleTimes_ExceptionHasCorrectSeverity() {
|
|
|
|
String input = "abc";
|
2016-12-11 11:23:44 -05:00
|
|
|
LispInputStream lispInputStream = createLispInputStream(input);
|
|
|
|
|
|
|
|
lispInputStream.read();
|
|
|
|
lispInputStream.unreadLastCharacter();
|
|
|
|
|
2016-12-12 10:15:20 -05:00
|
|
|
try {
|
|
|
|
lispInputStream.unreadLastCharacter();
|
|
|
|
} catch (MaximumUnreadsExceededException e) {
|
2017-02-11 10:42:07 -05:00
|
|
|
assertEquals(CRITICAL, e.getSeverity());
|
2016-12-12 10:15:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = UncheckedIOException.class)
|
|
|
|
public void underlyingInputStreamThrowsIOException_ConvertsToUncheckedIOException() {
|
|
|
|
InputStream ioExceptionThrowingInputStream = createIOExceptionThrowingInputStream();
|
|
|
|
LispInputStream lispInputStream = new LispCommentRemovingInputStream(ioExceptionThrowingInputStream);
|
|
|
|
|
|
|
|
lispInputStream.read();
|
|
|
|
}
|
|
|
|
|
2017-03-22 14:08:22 -04:00
|
|
|
@Test
|
|
|
|
public void underlyingInputStreamThrowsIOException_ExceptionHasCorrectAttributes() {
|
2016-12-12 10:15:20 -05:00
|
|
|
InputStream ioExceptionThrowingInputStream = createIOExceptionThrowingInputStream();
|
|
|
|
LispInputStream lispInputStream = new LispCommentRemovingInputStream(ioExceptionThrowingInputStream);
|
|
|
|
|
|
|
|
try {
|
|
|
|
lispInputStream.read();
|
|
|
|
} catch (UncheckedIOException e) {
|
|
|
|
String message = e.getMessage();
|
|
|
|
assertNotNull(message);
|
|
|
|
assertTrue(message.length() >= 0);
|
2017-03-22 14:08:22 -04:00
|
|
|
assertEquals(CRITICAL, e.getSeverity());
|
2016-12-12 10:15:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-08 10:59:02 -05:00
|
|
|
}
|