Added more unit tests and continued refactoring the scanner package
This commit is contained in:
parent
9b3261f575
commit
1670a825e5
|
@ -91,7 +91,7 @@ public class LispScanner {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Token retrieveString(char firstDoubleQuote) throws IOException {
|
private Token retrieveString(char firstDoubleQuote) throws IOException {
|
||||||
StringBuffer text = new StringBuffer();
|
StringBuilder text = new StringBuilder();
|
||||||
int startLine = lineNumber;
|
int startLine = lineNumber;
|
||||||
int startColumn = columnNumber;
|
int startColumn = columnNumber;
|
||||||
char prevChar = firstDoubleQuote;
|
char prevChar = firstDoubleQuote;
|
||||||
|
@ -130,7 +130,7 @@ public class LispScanner {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Token retrieveNumber(char firstDigit) throws IOException {
|
private Token retrieveNumber(char firstDigit) throws IOException {
|
||||||
StringBuffer text = new StringBuffer();
|
StringBuilder text = new StringBuilder();
|
||||||
int startColumn = columnNumber;
|
int startColumn = columnNumber;
|
||||||
|
|
||||||
text.append(firstDigit);
|
text.append(firstDigit);
|
||||||
|
@ -164,7 +164,7 @@ public class LispScanner {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Token retrieveIdentifier(char firstChar) throws IOException {
|
private Token retrieveIdentifier(char firstChar) throws IOException {
|
||||||
StringBuffer text = new StringBuffer();
|
StringBuilder text = new StringBuilder();
|
||||||
int startColumn = columnNumber;
|
int startColumn = columnNumber;
|
||||||
|
|
||||||
text.append(firstChar);
|
text.append(firstChar);
|
||||||
|
|
|
@ -12,11 +12,33 @@ import testutils.TestUtilities;
|
||||||
|
|
||||||
public class LispFilterInputStreamTester {
|
public class LispFilterInputStreamTester {
|
||||||
|
|
||||||
private StringBuilder stringBuilder;
|
private StringBuilder charactersRead;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
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
|
@Test
|
||||||
|
@ -91,11 +113,11 @@ public class LispFilterInputStreamTester {
|
||||||
int c = inputStream.read();
|
int c = inputStream.read();
|
||||||
|
|
||||||
while (c != -1) {
|
while (c != -1) {
|
||||||
stringBuilder.append((char) c);
|
charactersRead.append((char) c);
|
||||||
c = inputStream.read();
|
c = inputStream.read();
|
||||||
}
|
}
|
||||||
|
|
||||||
return stringBuilder.toString();
|
return charactersRead.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,14 @@ public class LispScannerLineColumnTester {
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNothing_RecordsCorrectEOFLocation() throws IOException {
|
||||||
|
String input = "";
|
||||||
|
LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 0) };
|
||||||
|
|
||||||
|
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSimpleString_RecordsCorrectLocation() throws IOException {
|
public void givenSimpleString_RecordsCorrectLocation() throws IOException {
|
||||||
String input = "\"string\"";
|
String input = "\"string\"";
|
||||||
|
|
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -20,6 +20,14 @@ public class LispScannerTypeTester {
|
||||||
assertTokenTypesMatch(input, expectedTypes);
|
assertTokenTypesMatch(input, expectedTypes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = RuntimeException.class)
|
||||||
|
public void givenBadCharacter_ThrowsException() throws IOException {
|
||||||
|
String input = "[";
|
||||||
|
Token.Type[] expectedTypes = {};
|
||||||
|
|
||||||
|
assertTokenTypesMatch(input, expectedTypes);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNil_returnsCorrectTokenTypes() throws IOException {
|
public void givenNil_returnsCorrectTokenTypes() throws IOException {
|
||||||
String input = "()";
|
String input = "()";
|
||||||
|
@ -60,6 +68,14 @@ public class LispScannerTypeTester {
|
||||||
assertTokenTypesMatch(input, expectedTypes);
|
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
|
@Test
|
||||||
public void givenIdentifier_returnsCorrectTokenTypes() throws IOException {
|
public void givenIdentifier_returnsCorrectTokenTypes() throws IOException {
|
||||||
String input = "abcdefgHIJKLMNOP1234";
|
String input = "abcdefgHIJKLMNOP1234";
|
||||||
|
@ -68,6 +84,22 @@ public class LispScannerTypeTester {
|
||||||
assertTokenTypesMatch(input, expectedTypes);
|
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
|
@Test
|
||||||
public void givenQuote_returnsCorrectTokenTypes() throws IOException {
|
public void givenQuote_returnsCorrectTokenTypes() throws IOException {
|
||||||
String input = "'";
|
String input = "'";
|
||||||
|
|
Loading…
Reference in New Issue