transcendental-lisp/test/scanner/LispScannerTextTester.java

73 lines
2.0 KiB
Java

package scanner;
import static org.junit.Assert.assertEquals;
import java.io.InputStream;
import org.junit.Test;
import testutils.TestUtilities;
public class LispScannerTextTester {
@Test
public void givenIdentifier_RecordsCorrectText() {
String input = "identifier";
assertTokenTextMatches(input, input);
}
@Test
public void givenNumber_RecordsCorrectText() {
String input = "192837456";
assertTokenTextMatches(input, input);
}
@Test
public void givenString_RecordsCorrectText() {
String input = "\"String!!! \n More... \"";
assertTokenTextMatches(input, input);
}
@Test
public void givenEmptyStream_RecordsCorrectFileName() {
String input = "";
String expectedFileName = "testFileName";
assertInputFileNameMatches(input, expectedFileName);
}
@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";
String expected = "abc123";
assertTokenTextMatches(input, expected);
}
private void assertTokenTextMatches(String input, String expectedText) {
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
LispScanner lispScanner = new LispScanner(stringInputStream, "stringInputStream");
assertEquals(expectedText, lispScanner.getNextToken().getText());
}
private void assertInputFileNameMatches(String input, String expectedInputFileName) {
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
LispScanner lispScanner = new LispScanner(stringInputStream, expectedInputFileName);
assertEquals(expectedInputFileName, lispScanner.getNextToken().getFName());
}
}