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 constructs.Token; import testutils.TestUtilities; public class LispScannerLineColumnTester { @Before 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\""; LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 1) }; assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns); } @Test public void givenStringWithTrailingSpace_RecordsCorrectLocation() throws IOException { String input = "\"string\" "; LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 1) }; assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns); } @Test public void givenIdentifier_RecordsCorrectLocation() throws IOException { String input = "identifier"; LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 1) }; assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns); } @Test public void givenIdentifierWithTrailingSpace_RecordsCorrectLocation() throws IOException { String input = "identifier "; LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 1) }; assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns); } @Test public void givenNumber_RecordsCorrectLocation() throws IOException { String input = "123456789"; LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 1) }; assertTokenLineAndColumnsMatch(input, expectedLinesAndColumns); } @Test public void givenNumberWithTrailingSpace_RecordsCorrectLocation() throws IOException { String input = "123456789 "; 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.getNextToken(); 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()); } } }