diff --git a/src/scanner/LispScanner.java b/src/scanner/LispScanner.java index d1232ec..c568253 100644 --- a/src/scanner/LispScanner.java +++ b/src/scanner/LispScanner.java @@ -91,7 +91,7 @@ public class LispScanner { } private Token retrieveString(char firstDoubleQuote) throws IOException { - StringBuffer text = new StringBuffer(); + StringBuilder text = new StringBuilder(); int startLine = lineNumber; int startColumn = columnNumber; char prevChar = firstDoubleQuote; @@ -130,7 +130,7 @@ public class LispScanner { } private Token retrieveNumber(char firstDigit) throws IOException { - StringBuffer text = new StringBuffer(); + StringBuilder text = new StringBuilder(); int startColumn = columnNumber; text.append(firstDigit); @@ -164,7 +164,7 @@ public class LispScanner { } private Token retrieveIdentifier(char firstChar) throws IOException { - StringBuffer text = new StringBuffer(); + StringBuilder text = new StringBuilder(); int startColumn = columnNumber; text.append(firstChar); diff --git a/test/scanner/LispFilterInputStreamTester.java b/test/scanner/LispFilterInputStreamTester.java index a24a384..92d65f3 100644 --- a/test/scanner/LispFilterInputStreamTester.java +++ b/test/scanner/LispFilterInputStreamTester.java @@ -12,11 +12,33 @@ import testutils.TestUtilities; public class LispFilterInputStreamTester { - private StringBuilder stringBuilder; + private StringBuilder charactersRead; @Before public void setUp() { - stringBuilder = new StringBuilder(); + charactersRead = new StringBuilder(); + } + + @Test + public void noBytesIn_noBytesOut() throws IOException { + String input = ""; + + assertEquals(input, getLispFilterInputStreamResult(input)); + } + + @Test + public void oneCharacter_notRemoved() throws IOException { + String input = "x"; + + assertEquals(input, getLispFilterInputStreamResult(input)); + } + + @Test + public void allNonCommentCharacters_notRemoved() throws IOException { + String input = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + + "`1234567890-=~!@#$%^&*()_+[]\\',./{}|:\"<>?"; + + assertEquals(input, getLispFilterInputStreamResult(input)); } @Test @@ -91,11 +113,11 @@ public class LispFilterInputStreamTester { int c = inputStream.read(); while (c != -1) { - stringBuilder.append((char) c); + charactersRead.append((char) c); c = inputStream.read(); } - return stringBuilder.toString(); + return charactersRead.toString(); } } diff --git a/test/scanner/LispScannerLineColumnTester.java b/test/scanner/LispScannerLineColumnTester.java index 3a6f148..2c77aa1 100644 --- a/test/scanner/LispScannerLineColumnTester.java +++ b/test/scanner/LispScannerLineColumnTester.java @@ -16,6 +16,14 @@ public class LispScannerLineColumnTester { public void setUp() throws Exception { } + @Test + public void givenNothing_RecordsCorrectEOFLocation() throws IOException { + String input = ""; + LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 0) }; + + assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns); + } + @Test public void givenSimpleString_RecordsCorrectLocation() throws IOException { String input = "\"string\""; diff --git a/test/scanner/LispScannerTextTester.java b/test/scanner/LispScannerTextTester.java new file mode 100644 index 0000000..307cf89 --- /dev/null +++ b/test/scanner/LispScannerTextTester.java @@ -0,0 +1,57 @@ +package scanner; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.io.InputStream; + +import org.junit.Test; + +import testutils.TestUtilities; + +public class LispScannerTextTester { + + @Test + public void givenIdentifier_RecordsCorrectText() throws IOException { + String input = "identifier"; + + assertTokenTextMatches(input, input); + } + + @Test + public void givenNumber_RecordsCorrectText() throws IOException { + String input = "192837456"; + + assertTokenTextMatches(input, input); + } + + @Test + public void givenString_RecordsCorrectText() throws IOException { + String input = "\"String!!! \n More... \""; + + assertTokenTextMatches(input, input); + } + + @Test + public void givenEmptyStream_RecordsCorrectInputStreamName() throws IOException { + String input = ""; + String expectedInputStreamName = "testInputStream"; + + assertInputFileNameMatches(input, expectedInputStreamName); + } + + private void assertTokenTextMatches(String input, String expectedText) throws IOException { + InputStream stringInputStream = TestUtilities.createInputStreamFromString(input); + LispScanner lispScanner = new LispScanner(stringInputStream, "stringInputStream"); + + assertEquals(expectedText, lispScanner.nextToken().getText()); + } + + private void assertInputFileNameMatches(String input, String expectedInputFileName) throws IOException { + InputStream stringInputStream = TestUtilities.createInputStreamFromString(input); + LispScanner lispScanner = new LispScanner(stringInputStream, expectedInputFileName); + + assertEquals(expectedInputFileName, lispScanner.nextToken().getFName()); + } + +} diff --git a/test/scanner/LispScannerTypeTester.java b/test/scanner/LispScannerTypeTester.java index cc920e7..2d64090 100644 --- a/test/scanner/LispScannerTypeTester.java +++ b/test/scanner/LispScannerTypeTester.java @@ -20,6 +20,14 @@ public class LispScannerTypeTester { assertTokenTypesMatch(input, expectedTypes); } + @Test(expected = RuntimeException.class) + public void givenBadCharacter_ThrowsException() throws IOException { + String input = "["; + Token.Type[] expectedTypes = {}; + + assertTokenTypesMatch(input, expectedTypes); + } + @Test public void givenNil_returnsCorrectTokenTypes() throws IOException { String input = "()"; @@ -60,6 +68,14 @@ public class LispScannerTypeTester { assertTokenTypesMatch(input, expectedTypes); } + @Test(expected = RuntimeException.class) + public void givenUnterminatedString_ThrowsException() throws IOException { + String input = "\"oh no!"; + Token.Type[] expectedTypes = { Type.STRING }; + + assertTokenTypesMatch(input, expectedTypes); + } + @Test public void givenIdentifier_returnsCorrectTokenTypes() throws IOException { String input = "abcdefgHIJKLMNOP1234"; @@ -68,6 +84,22 @@ public class LispScannerTypeTester { assertTokenTypesMatch(input, expectedTypes); } + @Test + public void givenSingleDigitNumber_returnsCorrectTokenTypes() throws IOException { + String input = "1"; + Token.Type[] expectedTypes = { Type.NUMBER }; + + assertTokenTypesMatch(input, expectedTypes); + } + + @Test + public void givenMultipleDigitNumber_returnsCorrectTokenTypes() throws IOException { + String input = "1234567890"; + Token.Type[] expectedTypes = { Type.NUMBER }; + + assertTokenTypesMatch(input, expectedTypes); + } + @Test public void givenQuote_returnsCorrectTokenTypes() throws IOException { String input = "'";