88 lines
3.1 KiB
Java
88 lines
3.1 KiB
Java
|
package scanner;
|
||
|
|
||
|
import static org.junit.Assert.assertTrue;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.io.InputStream;
|
||
|
|
||
|
import org.junit.Before;
|
||
|
import org.junit.Test;
|
||
|
|
||
|
import testutils.TestUtilities;
|
||
|
|
||
|
public class LispScannerLineColumnTester {
|
||
|
|
||
|
@Before
|
||
|
public void setUp() throws Exception {
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void givenSimpleString_RecordsCorrectLocation() throws IOException {
|
||
|
String input = "\"string\"";
|
||
|
LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 1) };
|
||
|
|
||
|
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void givenMultipleStrings_RecordsCorrectLocations() throws IOException {
|
||
|
String input = "\"string1\" \n \"string2 \n with newline\" \n \"string3\"";
|
||
|
LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 1), LineColumn.create(2, 2),
|
||
|
LineColumn.create(4, 3) };
|
||
|
|
||
|
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void givenQuotedList_RecordsCorrectLocations() throws IOException {
|
||
|
String input = "'(1 2 3 4 5)";
|
||
|
LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 1), LineColumn.create(1, 2),
|
||
|
LineColumn.create(1, 3), LineColumn.create(1, 5),
|
||
|
LineColumn.create(1, 7), LineColumn.create(1, 9),
|
||
|
LineColumn.create(1, 11), LineColumn.create(1, 12) };
|
||
|
|
||
|
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void givenListSpanningMultipleLines_RecordsCorrectLocations() throws IOException {
|
||
|
String input = " ( 1 2 \n 3 4 \n5 ) ";
|
||
|
LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 2), LineColumn.create(1, 4),
|
||
|
LineColumn.create(1, 6), LineColumn.create(2, 2),
|
||
|
LineColumn.create(2, 4), LineColumn.create(3, 1),
|
||
|
LineColumn.create(3, 3) };
|
||
|
|
||
|
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
|
||
|
}
|
||
|
|
||
|
private void assertTokenLineAndColumnsMatch(String input, LineColumn[] expectedLineColumnList) throws IOException {
|
||
|
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
|
||
|
LispScanner lispScanner = new LispScanner(stringInputStream, "stringInputStream");
|
||
|
|
||
|
for (LineColumn lineColumn : expectedLineColumnList) {
|
||
|
Token nextToken = lispScanner.nextToken();
|
||
|
assertTrue(lineColumn.isEqual(nextToken));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static class LineColumn {
|
||
|
|
||
|
private int line;
|
||
|
private int column;
|
||
|
|
||
|
public static LineColumn create(int line, int column) {
|
||
|
LineColumn lineColumn = new LineColumn();
|
||
|
lineColumn.line = line;
|
||
|
lineColumn.column = column;
|
||
|
|
||
|
return lineColumn;
|
||
|
}
|
||
|
|
||
|
public boolean isEqual(Token token) {
|
||
|
return (this.line == token.getLine()) && (this.column == token.getColumn());
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|