2016-12-09 12:29:56 -05:00
|
|
|
package scanner;
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertTrue;
|
2016-12-14 14:41:43 -05:00
|
|
|
import static testutil.TestUtilities.createInputStreamFromString;
|
2016-12-09 12:29:56 -05:00
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
2016-12-12 10:15:20 -05:00
|
|
|
import token.Token;
|
2016-12-09 12:29:56 -05:00
|
|
|
|
|
|
|
public class LispScannerLineColumnTester {
|
|
|
|
|
2016-12-12 14:57:34 -05:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void assertTokenLineAndColumnsMatch(String input, LineColumn[] expectedLineColumnList) {
|
2016-12-14 14:41:43 -05:00
|
|
|
InputStream stringInputStream = createInputStreamFromString(input);
|
2016-12-12 14:57:34 -05:00
|
|
|
LispScanner lispScanner = new LispScanner(stringInputStream, "testFile");
|
|
|
|
|
|
|
|
for (LineColumn lineColumn : expectedLineColumnList) {
|
|
|
|
Token nextToken = lispScanner.getNextToken();
|
|
|
|
assertTrue(lineColumn.isEqual(nextToken));
|
|
|
|
}
|
2016-12-09 12:29:56 -05:00
|
|
|
}
|
|
|
|
|
2016-12-09 16:04:38 -05:00
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenNothing_RecordsCorrectEOFLocation() {
|
2016-12-09 16:04:38 -05:00
|
|
|
String input = "";
|
|
|
|
LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 0) };
|
|
|
|
|
|
|
|
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
|
|
|
|
}
|
|
|
|
|
2016-12-09 12:29:56 -05:00
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenSimpleString_RecordsCorrectLocation() {
|
2016-12-09 12:29:56 -05:00
|
|
|
String input = "\"string\"";
|
|
|
|
LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 1) };
|
|
|
|
|
|
|
|
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
|
|
|
|
}
|
|
|
|
|
2016-12-10 11:57:49 -05:00
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenStringWithTrailingSpace_RecordsCorrectLocation() {
|
2016-12-10 11:57:49 -05:00
|
|
|
String input = "\"string\" ";
|
|
|
|
LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 1) };
|
|
|
|
|
|
|
|
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenIdentifier_RecordsCorrectLocation() {
|
2016-12-10 11:57:49 -05:00
|
|
|
String input = "identifier";
|
|
|
|
LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 1) };
|
|
|
|
|
|
|
|
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenIdentifierWithTrailingSpace_RecordsCorrectLocation() {
|
2016-12-10 11:57:49 -05:00
|
|
|
String input = "identifier ";
|
|
|
|
LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 1) };
|
|
|
|
|
|
|
|
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenNumber_RecordsCorrectLocation() {
|
2016-12-10 11:57:49 -05:00
|
|
|
String input = "123456789";
|
|
|
|
LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 1) };
|
|
|
|
|
|
|
|
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenNumberWithTrailingSpace_RecordsCorrectLocation() {
|
2016-12-10 11:57:49 -05:00
|
|
|
String input = "123456789 ";
|
|
|
|
LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 1) };
|
|
|
|
|
|
|
|
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
|
|
|
|
}
|
|
|
|
|
2016-12-09 12:29:56 -05:00
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenMultipleStrings_RecordsCorrectLocations() {
|
2016-12-09 12:29:56 -05:00
|
|
|
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
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenQuotedList_RecordsCorrectLocations() {
|
2016-12-09 12:29:56 -05:00
|
|
|
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
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenListSpanningMultipleLines_RecordsCorrectLocations() {
|
2016-12-09 12:29:56 -05:00
|
|
|
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) };
|
|
|
|
|
2016-12-11 15:09:48 -05:00
|
|
|
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenCommentImmediatelyFollowingNumber_RecordsCorrectLocations() {
|
|
|
|
String input = "12;comment\n34";
|
|
|
|
LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 1), LineColumn.create(2, 1) };
|
|
|
|
|
2016-12-09 12:29:56 -05:00
|
|
|
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|