2016-12-09 16:04:38 -05:00
|
|
|
package scanner;
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
2016-12-14 14:41:43 -05:00
|
|
|
import static testutil.TestUtilities.createInputStreamFromString;
|
2016-12-09 16:04:38 -05:00
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
2016-12-16 14:15:29 -05:00
|
|
|
import file.FilePosition;
|
|
|
|
|
2017-03-15 13:37:39 -04:00
|
|
|
public class LispScannerTextTest {
|
2016-12-09 16:04:38 -05:00
|
|
|
|
2016-12-12 14:57:34 -05:00
|
|
|
private void assertTokenTextMatches(String input, String[] expectedTextList) {
|
|
|
|
LispScanner lispScanner = createLispScanner(input);
|
|
|
|
|
|
|
|
for (String expectedText : expectedTextList)
|
|
|
|
assertEquals(expectedText, lispScanner.getNextToken().getText());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void assertTokenTextMatches(String input, String expectedText) {
|
|
|
|
LispScanner lispScanner = createLispScanner(input);
|
|
|
|
|
|
|
|
assertEquals(expectedText, lispScanner.getNextToken().getText());
|
|
|
|
}
|
|
|
|
|
|
|
|
private LispScanner createLispScanner(String input) {
|
2016-12-14 14:41:43 -05:00
|
|
|
InputStream stringInputStream = createInputStreamFromString(input);
|
2016-12-12 14:57:34 -05:00
|
|
|
|
|
|
|
return new LispScanner(stringInputStream, "testFile");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void assertInputFileNameMatches(String input, String expectedInputFileName) {
|
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, expectedInputFileName);
|
2016-12-16 14:15:29 -05:00
|
|
|
FilePosition tokenPosition = lispScanner.getNextToken().getPosition();
|
2016-12-12 14:57:34 -05:00
|
|
|
|
2016-12-16 14:15:29 -05:00
|
|
|
assertEquals(expectedInputFileName, tokenPosition.getFileName());
|
2016-12-12 14:57:34 -05:00
|
|
|
}
|
|
|
|
|
2016-12-12 10:15:20 -05:00
|
|
|
@Test
|
|
|
|
public void givenEmptyStream_RecordsCorrectFileName() {
|
|
|
|
String input = "";
|
|
|
|
String expectedFileName = "testFileName";
|
|
|
|
|
|
|
|
assertInputFileNameMatches(input, expectedFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenParenthesis_RecordsCorrectText() {
|
|
|
|
String input = "()";
|
|
|
|
String[] expected = { "(", ")" };
|
|
|
|
|
|
|
|
assertTokenTextMatches(input, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenQuote_RecordsCorrectText() {
|
|
|
|
String input = "'";
|
|
|
|
String expected = "'";
|
|
|
|
|
|
|
|
assertTokenTextMatches(input, expected);
|
|
|
|
}
|
2016-12-12 14:57:34 -05:00
|
|
|
|
2016-12-12 10:15:20 -05:00
|
|
|
@Test
|
2017-02-27 14:10:20 -05:00
|
|
|
public void givenEof_ReordsCorrectText() {
|
2016-12-12 10:15:20 -05:00
|
|
|
String input = "";
|
|
|
|
String expected = "EOF";
|
|
|
|
|
|
|
|
assertTokenTextMatches(input, expected);
|
|
|
|
}
|
|
|
|
|
2016-12-09 16:04:38 -05:00
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenIdentifier_RecordsCorrectText() {
|
2016-12-09 16:04:38 -05:00
|
|
|
String input = "identifier";
|
|
|
|
|
|
|
|
assertTokenTextMatches(input, input);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenNumber_RecordsCorrectText() {
|
2016-12-09 16:04:38 -05:00
|
|
|
String input = "192837456";
|
|
|
|
|
|
|
|
assertTokenTextMatches(input, input);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenString_RecordsCorrectText() {
|
2016-12-09 16:04:38 -05:00
|
|
|
String input = "\"String!!! \n More... \"";
|
|
|
|
|
|
|
|
assertTokenTextMatches(input, input);
|
|
|
|
}
|
|
|
|
|
2016-12-11 15:09:48 -05:00
|
|
|
@Test
|
|
|
|
public void givenNumberFollowedByComment_RecordsCorrectText() {
|
|
|
|
String input = "192837456;comment";
|
|
|
|
String expected = "192837456";
|
|
|
|
|
|
|
|
assertTokenTextMatches(input, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenIdentifiersWithCommentBetween_RecordsCorrectText() {
|
|
|
|
String input = "abc123;comment\nabc222";
|
2016-12-12 10:15:20 -05:00
|
|
|
String[] expected = { "abc123", "abc222" };
|
2016-12-11 15:09:48 -05:00
|
|
|
|
|
|
|
assertTokenTextMatches(input, expected);
|
2016-12-09 16:04:38 -05:00
|
|
|
}
|
|
|
|
|
2017-03-11 15:41:07 -05:00
|
|
|
@Test
|
|
|
|
public void givenBackTickExpression_RecordsCorrectText() {
|
|
|
|
String input = "`(list ,a ,@b)";
|
|
|
|
String[] expected = { "`", "(", "list", ",", "a", ",", "@", "b", ")" };
|
|
|
|
|
|
|
|
assertTokenTextMatches(input, expected);
|
|
|
|
}
|
|
|
|
|
2016-12-09 16:04:38 -05:00
|
|
|
}
|