2016-12-08 15:30:40 -05:00
|
|
|
package scanner;
|
|
|
|
|
2016-12-11 11:23:44 -05:00
|
|
|
import static org.junit.Assert.*;
|
2016-12-08 15:30:40 -05:00
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
2016-12-10 11:57:49 -05:00
|
|
|
import error.ErrorManager;
|
2016-12-12 10:15:20 -05:00
|
|
|
import scanner.LispScanner.UnterminatedStringException;
|
2016-12-08 15:30:40 -05:00
|
|
|
import testutils.TestUtilities;
|
2016-12-12 10:15:20 -05:00
|
|
|
import token.Token.Type;
|
|
|
|
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-12 14:57:34 -05:00
|
|
|
private void assertTokenTypesMatch(String input, Type[] expectedTypeList) {
|
|
|
|
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
|
|
|
|
LispScanner lispScanner = new LispScanner(stringInputStream, "testFile");
|
|
|
|
|
|
|
|
for (Type expectedType : expectedTypeList)
|
|
|
|
assertEquals(expectedType, lispScanner.getNextToken().getType());
|
|
|
|
|
|
|
|
assertEquals(Type.EOF, lispScanner.getNextToken().getType());
|
|
|
|
}
|
|
|
|
|
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-12 14:57:34 -05:00
|
|
|
Type[] expectedTypes = {};
|
2016-12-08 15:30:40 -05:00
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
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-12 14:57:34 -05:00
|
|
|
Type[] expectedTypes = {};
|
2016-12-09 16:04:38 -05:00
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
2016-12-11 15:09:48 -05:00
|
|
|
@Test
|
|
|
|
public void givenBadCharacter_ExceptionContainsCorrectSeverity() {
|
|
|
|
String input = "abc\ndef[";
|
2016-12-12 14:57:34 -05:00
|
|
|
Type[] expectedTypes = { Type.IDENTIFIER, Type.IDENTIFIER };
|
2016-12-11 15:09:48 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
2016-12-12 10:15:20 -05:00
|
|
|
} catch (BadCharacterException e) {
|
2016-12-11 15:09:48 -05:00
|
|
|
assertTrue(e.getSeverity() < ErrorManager.CRITICAL_LEVEL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-12 10:15:20 -05:00
|
|
|
@Test
|
|
|
|
public void givenBadCharacter_ExceptionContainsMessage() {
|
|
|
|
String input = "abc\ndef[";
|
2016-12-12 14:57:34 -05:00
|
|
|
Type[] expectedTypes = { Type.IDENTIFIER, Type.IDENTIFIER };
|
2016-12-12 10:15:20 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
} 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-12 14:57:34 -05:00
|
|
|
Type[] expectedTypes = { Type.LEFT_PAREN, Type.RIGHT_PAREN };
|
2016-12-08 15:30:40 -05:00
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-12 14:57:34 -05:00
|
|
|
Type[] expectedTypes = { Type.LEFT_PAREN, Type.NUMBER, Type.NUMBER, Type.RIGHT_PAREN };
|
2016-12-08 15:30:40 -05:00
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-12 14:57:34 -05:00
|
|
|
Type[] expectedTypes = { Type.STRING };
|
2016-12-08 15:30:40 -05:00
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-12 14:57:34 -05:00
|
|
|
Type[] expectedTypes = { Type.STRING };
|
2016-12-08 15:30:40 -05:00
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-12 14:57:34 -05:00
|
|
|
Type[] expectedTypes = { Type.STRING };
|
2016-12-08 15:30:40 -05:00
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
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-12 14:57:34 -05:00
|
|
|
Type[] expectedTypes = { Type.STRING };
|
2016-12-09 16:04:38 -05:00
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
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-12 14:57:34 -05:00
|
|
|
Type[] expectedTypes = { Type.STRING };
|
2016-12-10 11:57:49 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
} catch (LispScanner.UnterminatedStringException e) {
|
|
|
|
assertTrue(e.getSeverity() < ErrorManager.CRITICAL_LEVEL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-12 14:57:34 -05:00
|
|
|
Type[] expectedTypes = { Type.STRING };
|
2016-12-10 11:57:49 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
} 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-12 14:57:34 -05:00
|
|
|
Type[] expectedTypes = { Type.IDENTIFIER };
|
2016-12-08 15:30:40 -05:00
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
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-12 14:57:34 -05:00
|
|
|
Type[] expectedTypes = { Type.NUMBER };
|
2016-12-09 16:04:38 -05:00
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-12 14:57:34 -05:00
|
|
|
Type[] expectedTypes = { Type.NUMBER };
|
2016-12-09 16:04:38 -05:00
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
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-12 14:57:34 -05:00
|
|
|
Type[] expectedTypes = { Type.QUOTE_MARK };
|
2016-12-08 15:30:40 -05:00
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-12 14:57:34 -05:00
|
|
|
Type[] expectedTypes = { Type.IDENTIFIER, Type.STRING, Type.NUMBER, Type.LEFT_PAREN, Type.RIGHT_PAREN,
|
|
|
|
Type.QUOTE_MARK, Type.IDENTIFIER };
|
2016-12-10 11:57:49 -05:00
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-12 14:57:34 -05:00
|
|
|
Type[] expectedTypes = { Type.LEFT_PAREN, Type.IDENTIFIER, Type.IDENTIFIER, Type.LEFT_PAREN, Type.IDENTIFIER,
|
|
|
|
Type.RIGHT_PAREN, Type.LEFT_PAREN, Type.IDENTIFIER, Type.IDENTIFIER, Type.RIGHT_PAREN,
|
|
|
|
Type.RIGHT_PAREN };
|
2016-12-08 15:30:40 -05:00
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|