2016-12-08 15:30:40 -05:00
|
|
|
package scanner;
|
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
import static error.ErrorManager.Severity.ERROR;
|
2017-02-06 13:44:35 -05:00
|
|
|
import static org.junit.Assert.*;
|
2016-12-14 14:41:43 -05:00
|
|
|
import static testutil.TestUtilities.createInputStreamFromString;
|
2016-12-08 15:30:40 -05:00
|
|
|
|
|
|
|
import java.io.InputStream;
|
2017-02-06 13:44:35 -05:00
|
|
|
import java.util.*;
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2017-02-06 13:44:35 -05:00
|
|
|
import org.junit.*;
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2016-12-12 10:15:20 -05:00
|
|
|
import scanner.LispScanner.UnterminatedStringException;
|
2016-12-14 14:41:43 -05:00
|
|
|
import token.*;
|
2016-12-13 10:41:14 -05:00
|
|
|
import token.Number;
|
2016-12-12 10:15:20 -05:00
|
|
|
import token.TokenFactory.BadCharacterException;
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2016-12-09 12:29:56 -05:00
|
|
|
public class LispScannerTypeTester {
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2016-12-13 10:41:14 -05:00
|
|
|
private List<Class<? extends Token>> expectedTypes;
|
|
|
|
|
|
|
|
private void assertTokenTypesMatch(String input) {
|
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");
|
|
|
|
|
2016-12-13 10:41:14 -05:00
|
|
|
for (Class<? extends Token> type : expectedTypes)
|
|
|
|
assertTrue(type.isInstance(lispScanner.getNextToken()));
|
|
|
|
|
|
|
|
assertTrue(lispScanner.getNextToken() instanceof Eof);
|
|
|
|
}
|
2016-12-12 14:57:34 -05:00
|
|
|
|
2016-12-13 10:41:14 -05:00
|
|
|
@Before
|
|
|
|
public void setUp() {
|
|
|
|
expectedTypes = new ArrayList<>();
|
2016-12-12 14:57:34 -05:00
|
|
|
}
|
|
|
|
|
2016-12-08 15:30:40 -05:00
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenEmptyFile_ReturnsCorrectTypes() {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "";
|
|
|
|
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTokenTypesMatch(input);
|
2016-12-08 15:30:40 -05:00
|
|
|
}
|
|
|
|
|
2016-12-12 10:15:20 -05:00
|
|
|
@Test(expected = BadCharacterException.class)
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenBadCharacter_ThrowsException() {
|
2016-12-09 16:04:38 -05:00
|
|
|
String input = "[";
|
|
|
|
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTokenTypesMatch(input);
|
2016-12-09 16:04:38 -05:00
|
|
|
}
|
|
|
|
|
2016-12-11 15:09:48 -05:00
|
|
|
@Test
|
|
|
|
public void givenBadCharacter_ExceptionContainsCorrectSeverity() {
|
|
|
|
String input = "abc\ndef[";
|
2016-12-13 10:41:14 -05:00
|
|
|
expectedTypes.add(Identifier.class);
|
|
|
|
expectedTypes.add(Identifier.class);
|
2016-12-11 15:09:48 -05:00
|
|
|
|
|
|
|
try {
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTokenTypesMatch(input);
|
2016-12-12 10:15:20 -05:00
|
|
|
} catch (BadCharacterException e) {
|
2017-02-11 10:42:07 -05:00
|
|
|
assertEquals(ERROR, e.getSeverity());
|
2016-12-11 15:09:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-12 10:15:20 -05:00
|
|
|
@Test
|
|
|
|
public void givenBadCharacter_ExceptionContainsMessage() {
|
|
|
|
String input = "abc\ndef[";
|
2016-12-13 10:41:14 -05:00
|
|
|
expectedTypes.add(Identifier.class);
|
|
|
|
expectedTypes.add(Identifier.class);
|
2016-12-12 10:15:20 -05:00
|
|
|
|
|
|
|
try {
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTokenTypesMatch(input);
|
2016-12-12 10:15:20 -05:00
|
|
|
} catch (BadCharacterException e) {
|
|
|
|
String message = e.getMessage();
|
|
|
|
assertNotNull(message);
|
|
|
|
assertTrue(message.length() > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-08 15:30:40 -05:00
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenNil_ReturnsCorrectTypes() {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "()";
|
2016-12-13 10:41:14 -05:00
|
|
|
expectedTypes.add(LeftParenthesis.class);
|
|
|
|
expectedTypes.add(RightParenthesis.class);
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTokenTypesMatch(input);
|
2016-12-08 15:30:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenListOfNumbers_ReturnsCorrectTypes() {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "(1 2)";
|
2016-12-13 10:41:14 -05:00
|
|
|
expectedTypes.add(LeftParenthesis.class);
|
|
|
|
expectedTypes.add(Number.class);
|
|
|
|
expectedTypes.add(Number.class);
|
|
|
|
expectedTypes.add(RightParenthesis.class);
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTokenTypesMatch(input);
|
2016-12-08 15:30:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenString_ReturnsCorrectTypes() {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "\"string\"";
|
2016-12-13 10:41:14 -05:00
|
|
|
expectedTypes.add(QuotedString.class);
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTokenTypesMatch(input);
|
2016-12-08 15:30:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenStringWithEscapedDoubleQuote_ReturnsCorrectTypes() {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "\"string \n hi \\\" bye\"";
|
2016-12-13 10:41:14 -05:00
|
|
|
expectedTypes.add(QuotedString.class);
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTokenTypesMatch(input);
|
2016-12-08 15:30:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenStringWithEscapedDoubleQuoteAndComment_ReturnsCorrectTypes() {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "\"string \n hi \\\" ; bye\"";
|
2016-12-13 10:41:14 -05:00
|
|
|
expectedTypes.add(QuotedString.class);
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTokenTypesMatch(input);
|
2016-12-08 15:30:40 -05:00
|
|
|
}
|
|
|
|
|
2016-12-12 10:15:20 -05:00
|
|
|
@Test(expected = UnterminatedStringException.class)
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenUnterminatedString_ThrowsException() {
|
2016-12-09 16:04:38 -05:00
|
|
|
String input = "\"oh no!";
|
2016-12-13 10:41:14 -05:00
|
|
|
expectedTypes.add(QuotedString.class);
|
2016-12-09 16:04:38 -05:00
|
|
|
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTokenTypesMatch(input);
|
2016-12-09 16:04:38 -05:00
|
|
|
}
|
|
|
|
|
2016-12-10 11:57:49 -05:00
|
|
|
@Test()
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenUnterminatedString_ExceptionHasCorrectSeverity() {
|
2016-12-10 11:57:49 -05:00
|
|
|
String input = "\"oh no!";
|
2016-12-13 10:41:14 -05:00
|
|
|
expectedTypes.add(QuotedString.class);
|
2016-12-10 11:57:49 -05:00
|
|
|
|
|
|
|
try {
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTokenTypesMatch(input);
|
2016-12-10 11:57:49 -05:00
|
|
|
} catch (LispScanner.UnterminatedStringException e) {
|
2017-02-11 10:42:07 -05:00
|
|
|
assertEquals(ERROR, e.getSeverity());
|
2016-12-10 11:57:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test()
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenUnterminatedString_ExceptionContainsMessage() {
|
2016-12-10 11:57:49 -05:00
|
|
|
String input = "\"oh no!";
|
2016-12-13 10:41:14 -05:00
|
|
|
expectedTypes.add(QuotedString.class);
|
2016-12-10 11:57:49 -05:00
|
|
|
|
|
|
|
try {
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTokenTypesMatch(input);
|
2016-12-10 11:57:49 -05:00
|
|
|
} catch (LispScanner.UnterminatedStringException e) {
|
|
|
|
String message = e.getMessage();
|
|
|
|
assertNotNull(message);
|
|
|
|
assertTrue(message.length() > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-08 15:30:40 -05:00
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenIdentifier_ReturnsCorrectTypes() {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "abcdefgHIJKLMNOP1234";
|
2016-12-13 10:41:14 -05:00
|
|
|
expectedTypes.add(Identifier.class);
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTokenTypesMatch(input);
|
2016-12-08 15:30:40 -05:00
|
|
|
}
|
|
|
|
|
2017-02-27 13:34:04 -05:00
|
|
|
@Test
|
|
|
|
public void givenPrefixedIdentifiers_ReturnsCorrectTypes() {
|
|
|
|
String input = "-a +b";
|
|
|
|
expectedTypes.add(Identifier.class);
|
|
|
|
expectedTypes.add(Identifier.class);
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input);
|
|
|
|
}
|
|
|
|
|
2016-12-09 16:04:38 -05:00
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenSingleDigitNumber_ReturnsCorrectTypes() {
|
2016-12-09 16:04:38 -05:00
|
|
|
String input = "1";
|
2016-12-13 10:41:14 -05:00
|
|
|
expectedTypes.add(Number.class);
|
2016-12-09 16:04:38 -05:00
|
|
|
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTokenTypesMatch(input);
|
2016-12-09 16:04:38 -05:00
|
|
|
}
|
|
|
|
|
2017-02-27 13:34:04 -05:00
|
|
|
@Test
|
|
|
|
public void givenPrefixedNegativeNumber_ReturnsCorrectTypes() {
|
|
|
|
String input = "-1";
|
|
|
|
expectedTypes.add(Number.class);
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenPrefixedNumber_ReturnsCorrectTypes() {
|
|
|
|
String input = "+1";
|
|
|
|
expectedTypes.add(Number.class);
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenPrefixedNegativeNumberAndIdentifier_ReturnsCorrectTypes() {
|
|
|
|
String input = "-1apple";
|
|
|
|
expectedTypes.add(Number.class);
|
|
|
|
expectedTypes.add(Identifier.class);
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenPrefixedNumberAndIdentifier_ReturnsCorrectTypes() {
|
|
|
|
String input = "+1apple";
|
|
|
|
expectedTypes.add(Number.class);
|
|
|
|
expectedTypes.add(Identifier.class);
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenPrefixedNegativeNumberAndString_ReturnsCorrectTypes() {
|
|
|
|
String input = "-1\"apple\"";
|
|
|
|
expectedTypes.add(Number.class);
|
|
|
|
expectedTypes.add(QuotedString.class);
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenPrefixedNumberAndString_ReturnsCorrectTypes() {
|
|
|
|
String input = "+1\"apple\"";
|
|
|
|
expectedTypes.add(Number.class);
|
|
|
|
expectedTypes.add(QuotedString.class);
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input);
|
|
|
|
}
|
|
|
|
|
2016-12-09 16:04:38 -05:00
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenMultipleDigitNumber_ReturnsCorrectTypes() {
|
2016-12-09 16:04:38 -05:00
|
|
|
String input = "1234567890";
|
2016-12-13 10:41:14 -05:00
|
|
|
expectedTypes.add(Number.class);
|
2016-12-09 16:04:38 -05:00
|
|
|
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTokenTypesMatch(input);
|
2016-12-09 16:04:38 -05:00
|
|
|
}
|
|
|
|
|
2016-12-08 15:30:40 -05:00
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenQuote_ReturnsCorrectTypes() {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "'";
|
2016-12-13 10:41:14 -05:00
|
|
|
expectedTypes.add(QuoteMark.class);
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTokenTypesMatch(input);
|
2016-12-08 15:30:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenManyTypesWithNoWhitespace_ReturnsCorrectTypes() {
|
2016-12-10 11:57:49 -05:00
|
|
|
String input = "xxx\"hi\"999()'aaa";
|
2016-12-13 10:41:14 -05:00
|
|
|
expectedTypes.add(Identifier.class);
|
|
|
|
expectedTypes.add(QuotedString.class);
|
|
|
|
expectedTypes.add(Number.class);
|
|
|
|
expectedTypes.add(LeftParenthesis.class);
|
|
|
|
expectedTypes.add(RightParenthesis.class);
|
|
|
|
expectedTypes.add(QuoteMark.class);
|
|
|
|
expectedTypes.add(Identifier.class);
|
2016-12-10 11:57:49 -05:00
|
|
|
|
2016-12-13 10:41:14 -05:00
|
|
|
assertTokenTypesMatch(input);
|
2016-12-10 11:57:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-11 11:23:44 -05:00
|
|
|
public void givenFunctionCall_ReturnsCorrectTypes() {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "(defun myFunction (x)\n (print x))";
|
2016-12-13 10:41:14 -05:00
|
|
|
expectedTypes.add(LeftParenthesis.class);
|
|
|
|
expectedTypes.add(Identifier.class);
|
|
|
|
expectedTypes.add(Identifier.class);
|
|
|
|
expectedTypes.add(LeftParenthesis.class);
|
|
|
|
expectedTypes.add(Identifier.class);
|
|
|
|
expectedTypes.add(RightParenthesis.class);
|
|
|
|
expectedTypes.add(LeftParenthesis.class);
|
|
|
|
expectedTypes.add(Identifier.class);
|
|
|
|
expectedTypes.add(Identifier.class);
|
|
|
|
expectedTypes.add(RightParenthesis.class);
|
|
|
|
expectedTypes.add(RightParenthesis.class);
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input);
|
2016-12-08 15:30:40 -05:00
|
|
|
}
|
|
|
|
|
2017-03-11 15:41:07 -05:00
|
|
|
@Test
|
|
|
|
public void givenBackTickExpression_ReturnsCorrectTypes() {
|
|
|
|
String input = "`(list ,a ,@b)";
|
2017-03-12 12:19:45 -04:00
|
|
|
expectedTypes.add(Backquote.class);
|
2017-03-11 15:41:07 -05:00
|
|
|
expectedTypes.add(LeftParenthesis.class);
|
|
|
|
expectedTypes.add(Identifier.class);
|
|
|
|
expectedTypes.add(Comma.class);
|
|
|
|
expectedTypes.add(Identifier.class);
|
|
|
|
expectedTypes.add(Comma.class);
|
|
|
|
expectedTypes.add(AtSign.class);
|
|
|
|
expectedTypes.add(Identifier.class);
|
|
|
|
expectedTypes.add(RightParenthesis.class);
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input);
|
|
|
|
}
|
|
|
|
|
2016-12-08 15:30:40 -05:00
|
|
|
}
|