transcendental-lisp/test/scanner/LispScannerLineColumnTest.java

139 lines
4.9 KiB
Java
Raw Normal View History

package scanner;
import static org.junit.Assert.assertTrue;
import static testutil.TestUtilities.createInputStreamFromString;
import java.io.InputStream;
import org.junit.Test;
2016-12-16 14:15:29 -05:00
import file.FilePosition;
import token.Token;
2017-03-15 13:37:39 -04:00
public class LispScannerLineColumnTest {
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;
}
2016-12-16 14:15:29 -05:00
public boolean isEqual(FilePosition position) {
return (this.line == position.getLineNumber()) && (this.column == position.getColumnNumber());
2016-12-12 14:57:34 -05:00
}
}
private void assertTokenLineAndColumnsMatch(String input, LineColumn[] expectedLineColumnList) {
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();
2016-12-16 14:15:29 -05:00
assertTrue(lineColumn.isEqual(nextToken.getPosition()));
2016-12-12 14:57:34 -05:00
}
}
@Test
public void givenNothing_RecordsCorrectEofLocation() {
String input = "";
LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 0) };
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
}
@Test
2016-12-11 11:23:44 -05:00
public void givenSimpleString_RecordsCorrectLocation() {
String input = "\"string\"";
LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 1) };
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
}
@Test
2016-12-11 11:23:44 -05:00
public void givenStringWithTrailingSpace_RecordsCorrectLocation() {
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() {
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() {
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() {
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() {
String input = "123456789 ";
LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 1) };
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
}
@Test
2016-12-11 11:23:44 -05:00
public void givenMultipleStrings_RecordsCorrectLocations() {
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() {
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() {
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);
}
@Test
public void givenCommentImmediatelyFollowingNumber_RecordsCorrectLocations() {
String input = "12;comment\n34";
LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 1), LineColumn.create(2, 1) };
assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns);
}
}