2016-12-08 15:30:40 -05:00
|
|
|
package scanner;
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
2016-12-10 11:57:49 -05:00
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
2016-12-08 15:30:40 -05:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
2016-12-10 11:57:49 -05:00
|
|
|
import constructs.Token;
|
|
|
|
import constructs.Token.Type;
|
|
|
|
import error.ErrorManager;
|
2016-12-08 15:30:40 -05:00
|
|
|
import testutils.TestUtilities;
|
|
|
|
|
2016-12-09 12:29:56 -05:00
|
|
|
public class LispScannerTypeTester {
|
2016-12-08 15:30:40 -05:00
|
|
|
|
|
|
|
@Test
|
2016-12-10 11:57:49 -05:00
|
|
|
public void givenEmptyFile_ReturnsCorrectTypes() throws IOException {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "";
|
|
|
|
Token.Type[] expectedTypes = {};
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
2016-12-09 16:04:38 -05:00
|
|
|
@Test(expected = RuntimeException.class)
|
|
|
|
public void givenBadCharacter_ThrowsException() throws IOException {
|
|
|
|
String input = "[";
|
|
|
|
Token.Type[] expectedTypes = {};
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
2016-12-08 15:30:40 -05:00
|
|
|
@Test
|
2016-12-10 11:57:49 -05:00
|
|
|
public void givenNil_ReturnsCorrectTypes() throws IOException {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "()";
|
|
|
|
Token.Type[] expectedTypes = { Type.LEFT_PAREN, Type.RIGHT_PAREN };
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-10 11:57:49 -05:00
|
|
|
public void givenListOfNumbers_ReturnsCorrectTypes() throws IOException {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "(1 2)";
|
|
|
|
Token.Type[] expectedTypes = { Type.LEFT_PAREN, Type.NUMBER, Type.NUMBER, Type.RIGHT_PAREN };
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-10 11:57:49 -05:00
|
|
|
public void givenString_ReturnsCorrectTypes() throws IOException {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "\"string\"";
|
|
|
|
Token.Type[] expectedTypes = { Type.STRING };
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-10 11:57:49 -05:00
|
|
|
public void givenStringWithEscapedDoubleQuote_ReturnsCorrectTypes() throws IOException {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "\"string \n hi \\\" bye\"";
|
|
|
|
Token.Type[] expectedTypes = { Type.STRING };
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-10 11:57:49 -05:00
|
|
|
public void givenStringWithEscapedDoubleQuoteAndComment_ReturnsCorrectTypes() throws IOException {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "\"string \n hi \\\" ; bye\"";
|
|
|
|
Token.Type[] expectedTypes = { Type.STRING };
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
2016-12-10 11:57:49 -05:00
|
|
|
@Test(expected = LispScanner.UnterminatedStringException.class)
|
2016-12-09 16:04:38 -05:00
|
|
|
public void givenUnterminatedString_ThrowsException() throws IOException {
|
|
|
|
String input = "\"oh no!";
|
|
|
|
Token.Type[] expectedTypes = { Type.STRING };
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
2016-12-10 11:57:49 -05:00
|
|
|
@Test()
|
|
|
|
public void givenUnterminatedString_ExceptionHasCorrectSeverity() throws IOException {
|
|
|
|
String input = "\"oh no!";
|
|
|
|
Token.Type[] expectedTypes = { Type.STRING };
|
|
|
|
|
|
|
|
try {
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
} catch (LispScanner.UnterminatedStringException e) {
|
|
|
|
assertTrue(e.getSeverity() < ErrorManager.CRITICAL_LEVEL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test()
|
|
|
|
public void givenUnterminatedString_ExceptionContainsMessage() throws IOException {
|
|
|
|
String input = "\"oh no!";
|
|
|
|
Token.Type[] expectedTypes = { Type.STRING };
|
|
|
|
|
|
|
|
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-10 11:57:49 -05:00
|
|
|
public void givenIdentifier_ReturnsCorrectTypes() throws IOException {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "abcdefgHIJKLMNOP1234";
|
|
|
|
Token.Type[] expectedTypes = { Type.IDENTIFIER };
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
2016-12-09 16:04:38 -05:00
|
|
|
@Test
|
2016-12-10 11:57:49 -05:00
|
|
|
public void givenSingleDigitNumber_ReturnsCorrectTypes() throws IOException {
|
2016-12-09 16:04:38 -05:00
|
|
|
String input = "1";
|
|
|
|
Token.Type[] expectedTypes = { Type.NUMBER };
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-10 11:57:49 -05:00
|
|
|
public void givenMultipleDigitNumber_ReturnsCorrectTypes() throws IOException {
|
2016-12-09 16:04:38 -05:00
|
|
|
String input = "1234567890";
|
|
|
|
Token.Type[] expectedTypes = { Type.NUMBER };
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
2016-12-08 15:30:40 -05:00
|
|
|
@Test
|
2016-12-10 11:57:49 -05:00
|
|
|
public void givenQuote_ReturnsCorrectTypes() throws IOException {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "'";
|
|
|
|
Token.Type[] expectedTypes = { Type.QUOTE_MARK };
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-12-10 11:57:49 -05:00
|
|
|
public void givenManyTypesWithNoWhitespace_ReturnsCorrectTypes() throws IOException {
|
|
|
|
String input = "xxx\"hi\"999()'aaa";
|
|
|
|
Token.Type[] expectedTypes = { Type.IDENTIFIER, Type.STRING, Type.NUMBER, Type.LEFT_PAREN, Type.RIGHT_PAREN,
|
|
|
|
Type.QUOTE_MARK, Type.IDENTIFIER };
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenFunctionCall_ReturnsCorrectTypes() throws IOException {
|
2016-12-08 15:30:40 -05:00
|
|
|
String input = "(defun myFunction (x)\n (print x))";
|
|
|
|
Token.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 };
|
|
|
|
|
|
|
|
assertTokenTypesMatch(input, expectedTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void assertTokenTypesMatch(String input, Token.Type[] expectedTypeList) throws IOException {
|
|
|
|
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
|
|
|
|
LispScanner lispScanner = new LispScanner(stringInputStream, "stringInputStream");
|
|
|
|
|
|
|
|
for (Token.Type expectedType : expectedTypeList)
|
2016-12-10 11:57:49 -05:00
|
|
|
assertEquals(expectedType, lispScanner.getNextToken().getType());
|
2016-12-08 15:30:40 -05:00
|
|
|
|
2016-12-10 11:57:49 -05:00
|
|
|
assertEquals(Token.Type.EOF, lispScanner.getNextToken().getType());
|
2016-12-08 15:30:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|