2016-12-14 12:10:28 -05:00
|
|
|
package parser;
|
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
import static error.ErrorManager.Severity.ERROR;
|
2016-12-14 12:10:28 -05:00
|
|
|
import static org.junit.Assert.*;
|
2017-02-06 13:44:35 -05:00
|
|
|
import static testutil.TestUtilities.*;
|
2017-01-27 11:37:11 -05:00
|
|
|
import static testutil.TypeAssertions.*;
|
2016-12-14 12:10:28 -05:00
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
2017-02-11 10:42:07 -05:00
|
|
|
import error.LispException;
|
2016-12-14 14:41:43 -05:00
|
|
|
import scanner.LispInputStream.UncheckedIOException;
|
|
|
|
import scanner.LispScanner.UnterminatedStringException;
|
2016-12-16 14:00:34 -05:00
|
|
|
import token.Eof.EofEncounteredException;
|
|
|
|
import token.RightParenthesis.StartsWithRightParenthesisException;
|
2016-12-14 14:41:43 -05:00
|
|
|
import token.TokenFactory.BadCharacterException;
|
2016-12-14 12:10:28 -05:00
|
|
|
|
|
|
|
public class LispParserTester {
|
|
|
|
|
|
|
|
private LispParser createLispParser(String input) {
|
2016-12-14 14:41:43 -05:00
|
|
|
InputStream stringInputStream = createInputStreamFromString(input);
|
|
|
|
return new LispParser(stringInputStream, "testFile");
|
|
|
|
}
|
|
|
|
|
|
|
|
private LispParser createIOExceptionThrowingLispParser() {
|
|
|
|
InputStream stringInputStream = createIOExceptionThrowingInputStream();
|
2016-12-14 12:10:28 -05:00
|
|
|
return new LispParser(stringInputStream, "testFile");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testEofMethod_ReturnsTrueWithNoInput() {
|
|
|
|
String input = "";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
assertTrue(parser.isEof());
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testEofMethod_ReturnsFalseWithSomeInput() {
|
|
|
|
String input = "abc";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
assertFalse(parser.isEof());
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testEofMethod_ReturnsTrueAfterSomeInput() {
|
|
|
|
String input = "(yyz 9 9 9)";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
2016-12-16 11:26:53 -05:00
|
|
|
parser.getNextSExpression();
|
2016-12-14 13:09:41 -05:00
|
|
|
assertTrue(parser.isEof());
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testEofMethod_ReturnsFalseAfterMultipleExpressions() {
|
|
|
|
String input = "()()()";
|
|
|
|
LispParser parser = createLispParser(input);
|
2016-12-16 11:26:53 -05:00
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
assertFalse(parser.isEof());
|
|
|
|
parser.getNextSExpression();
|
|
|
|
assertFalse(parser.isEof());
|
|
|
|
parser.getNextSExpression();
|
|
|
|
assertFalse(parser.isEof());
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testEofMethod_ReturnsTrueAfterMultipleExpressions() {
|
|
|
|
String input = "()()()";
|
|
|
|
LispParser parser = createLispParser(input);
|
2016-12-16 11:26:53 -05:00
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
assertFalse(parser.isEof());
|
|
|
|
parser.getNextSExpression();
|
|
|
|
assertFalse(parser.isEof());
|
|
|
|
parser.getNextSExpression();
|
|
|
|
assertFalse(parser.isEof());
|
|
|
|
parser.getNextSExpression();
|
|
|
|
assertTrue(parser.isEof());
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenNil_CreatesCorrectSExpression() {
|
|
|
|
String input = "()";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
assertNil(parser.getNextSExpression());
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenNumber_CreatesCorrectSExpression() {
|
|
|
|
String input = "12";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
assertNumber(parser.getNextSExpression());
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenIdentifier_CreatesCorrectSExpression() {
|
|
|
|
String input = "identifier1";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
assertSymbol(parser.getNextSExpression());
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenString_CreatesCorrectSExpression() {
|
|
|
|
String input = "\"string\"";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
assertString(parser.getNextSExpression());
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenList_CreatesCorrectSExpression() {
|
|
|
|
String input = "(1 2)";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
assertList(parser.getNextSExpression());
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenQuotedIdentifier_CreatesCorrectSExpression() {
|
|
|
|
String input = "'quoted";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
assertList(parser.getNextSExpression());
|
|
|
|
assertTrue(parser.isEof());
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenComplexList_CreatesCorrectSExpression() {
|
|
|
|
String input = "(defun f (x) \n (print \n (list \"x is \" x) \n ) \n )";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
assertList(parser.getNextSExpression());
|
|
|
|
assertTrue(parser.isEof());
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
2016-12-14 14:41:43 -05:00
|
|
|
@Test
|
|
|
|
public void givenMultipleComplexLists_CreatesCorrectSExpressions() {
|
|
|
|
String input = "(defun f (x) \n (print \n (list \"x is \" x) \n ) \n )"
|
|
|
|
+ "(defun f (x) \n (print \n (list \"x is \" x) \n ) \n )";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
|
|
|
assertList(parser.getNextSExpression());
|
|
|
|
assertList(parser.getNextSExpression());
|
|
|
|
assertTrue(parser.isEof());
|
|
|
|
}
|
|
|
|
|
2016-12-14 12:10:28 -05:00
|
|
|
@Test
|
|
|
|
public void givenMultipleExpressions_CreatesCorrectSExpressions() {
|
|
|
|
String input = "(setf x 2) x \"hi\" () 29";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
assertList(parser.getNextSExpression());
|
|
|
|
assertSymbol(parser.getNextSExpression());
|
|
|
|
assertString(parser.getNextSExpression());
|
|
|
|
assertNil(parser.getNextSExpression());
|
|
|
|
assertNumber(parser.getNextSExpression());
|
|
|
|
assertTrue(parser.isEof());
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenNil_CreatesCorrectSExpressionAfterEofCalls() {
|
|
|
|
String input = "()";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
parser.isEof();
|
|
|
|
parser.isEof();
|
2016-12-14 12:10:28 -05:00
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
assertNil(parser.getNextSExpression());
|
|
|
|
assertTrue(parser.isEof());
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
2016-12-14 14:41:43 -05:00
|
|
|
@Test(expected = BadCharacterException.class)
|
2016-12-14 12:10:28 -05:00
|
|
|
public void givenBadToken_ThrowsException() {
|
|
|
|
String input = "[";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
parser.getNextSExpression();
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
2016-12-16 11:26:53 -05:00
|
|
|
@Test
|
|
|
|
public void givenBadToken_ExceptionHasCorrectSeverity() {
|
|
|
|
String input = "[";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
|
|
|
try {
|
|
|
|
parser.getNextSExpression();
|
|
|
|
} catch (BadCharacterException e) {
|
2017-02-11 10:42:07 -05:00
|
|
|
assertEquals(ERROR, e.getSeverity());
|
2016-12-16 11:26:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenBadToken_ExceptionHasMessageText() {
|
|
|
|
String input = "[";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
|
|
|
try {
|
|
|
|
parser.getNextSExpression();
|
|
|
|
} catch (BadCharacterException e) {
|
|
|
|
String message = e.getMessage();
|
|
|
|
|
|
|
|
assertNotNull(message);
|
|
|
|
assertTrue(message.length() > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 14:41:43 -05:00
|
|
|
@Test(expected = UnterminatedStringException.class)
|
2016-12-14 12:10:28 -05:00
|
|
|
public void givenUnterminatedString_ThrowsException() {
|
|
|
|
String input = "\"string";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
parser.getNextSExpression();
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
2016-12-14 14:41:43 -05:00
|
|
|
@Test(expected = EofEncounteredException.class)
|
2016-12-14 12:10:28 -05:00
|
|
|
public void givenUnterminatedList_ThrowsException() {
|
|
|
|
String input = "(bad list";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
parser.getNextSExpression();
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
2016-12-16 11:26:53 -05:00
|
|
|
@Test
|
|
|
|
public void givenUnterminatedList_ExceptionHasCorrectSeverity() {
|
|
|
|
String input = "(bad list";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
|
|
|
try {
|
|
|
|
parser.getNextSExpression();
|
|
|
|
} catch (EofEncounteredException e) {
|
2017-02-11 10:42:07 -05:00
|
|
|
assertEquals(ERROR, e.getSeverity());
|
2016-12-16 11:26:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenUnterminatedList_ExceptionHasMessage() {
|
|
|
|
String input = "(bad list";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
|
|
|
try {
|
|
|
|
parser.getNextSExpression();
|
|
|
|
} catch (EofEncounteredException e) {
|
|
|
|
String message = e.getMessage();
|
|
|
|
|
|
|
|
assertNotNull(message);
|
|
|
|
assertTrue(message.length() > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 14:41:43 -05:00
|
|
|
@Test(expected = StartsWithRightParenthesisException.class)
|
2016-12-14 12:10:28 -05:00
|
|
|
public void givenUnmatchedRightParenthesis_ThrowsException() {
|
|
|
|
String input = ")";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
parser.getNextSExpression();
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
2016-12-16 11:26:53 -05:00
|
|
|
@Test
|
|
|
|
public void givenUnmatchedRightParenthesis_ExceptionHasCorrectSeverity() {
|
|
|
|
String input = ")";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
|
|
|
try {
|
|
|
|
parser.getNextSExpression();
|
|
|
|
} catch (StartsWithRightParenthesisException e) {
|
2017-02-11 10:42:07 -05:00
|
|
|
assertEquals(ERROR, e.getSeverity());
|
2016-12-16 11:26:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void givenUnmatchedRightParenthesis_ExceptionHasMessage() {
|
|
|
|
String input = ")";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
|
|
|
try {
|
|
|
|
parser.getNextSExpression();
|
|
|
|
} catch (StartsWithRightParenthesisException e) {
|
|
|
|
String message = e.getMessage();
|
|
|
|
|
|
|
|
assertNotNull(message);
|
|
|
|
assertTrue(message.length() > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 14:41:43 -05:00
|
|
|
@Test(expected = BadCharacterException.class)
|
2016-12-14 12:10:28 -05:00
|
|
|
public void givenBadCharacter_ThrowsExceptionAfterEofCalled() {
|
|
|
|
String input = "[";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
|
|
|
try {
|
2016-12-14 13:09:41 -05:00
|
|
|
parser.isEof();
|
2016-12-14 12:10:28 -05:00
|
|
|
} catch (LispException e) {
|
|
|
|
fail("Exception thrown too early");
|
|
|
|
}
|
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
parser.getNextSExpression();
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
2016-12-14 14:41:43 -05:00
|
|
|
@Test(expected = BadCharacterException.class)
|
2016-12-14 12:10:28 -05:00
|
|
|
public void givenBadCharacterAfterValidToken_ThrowsExceptionAtTheCorrectTime() {
|
2016-12-14 13:09:41 -05:00
|
|
|
String input = "id[";
|
2016-12-14 12:10:28 -05:00
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
|
|
|
|
try {
|
2016-12-14 13:09:41 -05:00
|
|
|
parser.getNextSExpression();
|
|
|
|
parser.isEof();
|
2016-12-14 12:10:28 -05:00
|
|
|
} catch (LispException e) {
|
|
|
|
fail("Exception thrown too early");
|
|
|
|
}
|
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
parser.getNextSExpression();
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
2016-12-14 13:09:41 -05:00
|
|
|
@Test
|
|
|
|
public void afterException_ReturnsEofCorrectly() {
|
|
|
|
String input = "id[";
|
|
|
|
LispParser parser = createLispParser(input);
|
|
|
|
parser.getNextSExpression();
|
|
|
|
parser.isEof();
|
|
|
|
|
|
|
|
try {
|
|
|
|
parser.getNextSExpression();
|
|
|
|
fail("Expected LispException");
|
2016-12-14 14:41:43 -05:00
|
|
|
} catch (LispException e) {}
|
|
|
|
|
|
|
|
assertTrue(parser.isEof());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = UncheckedIOException.class)
|
|
|
|
public void handlesIOExceptionCorrectly() {
|
|
|
|
LispParser parser = createIOExceptionThrowingLispParser();
|
|
|
|
|
|
|
|
try {
|
|
|
|
parser.isEof();
|
2016-12-14 13:09:41 -05:00
|
|
|
} catch (LispException e) {
|
2016-12-14 14:41:43 -05:00
|
|
|
fail("Exception thrown too early");
|
2016-12-14 13:09:41 -05:00
|
|
|
}
|
2016-12-14 14:41:43 -05:00
|
|
|
|
|
|
|
parser.getNextSExpression();
|
2016-12-14 13:09:41 -05:00
|
|
|
}
|
2016-12-14 14:41:43 -05:00
|
|
|
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|