2016-12-14 12:10:28 -05:00
|
|
|
package parser;
|
|
|
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import error.LispException;
|
2016-12-14 13:09:41 -05:00
|
|
|
import sexpression.Nil;
|
|
|
|
import sexpression.SExpression;
|
2016-12-14 12:10:28 -05:00
|
|
|
import testutils.TestUtilities;
|
|
|
|
|
|
|
|
public class LispParserTester {
|
|
|
|
|
|
|
|
private LispParser createLispParser(String input) {
|
|
|
|
InputStream stringInputStream = TestUtilities.createInputStreamFromString(input);
|
|
|
|
return new LispParser(stringInputStream, "testFile");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void assertList(SExpression sExpression) {
|
|
|
|
assertFalse(sExpression.atomp());
|
|
|
|
assertTrue(sExpression.consp());
|
|
|
|
assertFalse(sExpression.functionp());
|
|
|
|
assertTrue(sExpression.listp());
|
|
|
|
assertFalse(sExpression.nullp());
|
|
|
|
assertFalse(sExpression.numberp());
|
|
|
|
assertFalse(sExpression.stringp());
|
|
|
|
assertFalse(sExpression.symbolp());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void assertNil(SExpression sExpression) {
|
|
|
|
assertEquals(sExpression, Nil.getUniqueInstance());
|
|
|
|
|
|
|
|
assertTrue(sExpression.atomp());
|
|
|
|
assertFalse(sExpression.consp());
|
|
|
|
assertFalse(sExpression.functionp());
|
|
|
|
assertTrue(sExpression.listp());
|
|
|
|
assertTrue(sExpression.nullp());
|
|
|
|
assertFalse(sExpression.numberp());
|
|
|
|
assertFalse(sExpression.stringp());
|
|
|
|
assertTrue(sExpression.symbolp());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void assertNumber(SExpression sExpression) {
|
|
|
|
assertTrue(sExpression.atomp());
|
|
|
|
assertFalse(sExpression.consp());
|
|
|
|
assertFalse(sExpression.functionp());
|
|
|
|
assertFalse(sExpression.listp());
|
|
|
|
assertFalse(sExpression.nullp());
|
|
|
|
assertTrue(sExpression.numberp());
|
|
|
|
assertFalse(sExpression.stringp());
|
|
|
|
assertFalse(sExpression.symbolp());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void assertString(SExpression sExpression) {
|
|
|
|
assertTrue(sExpression.atomp());
|
|
|
|
assertFalse(sExpression.consp());
|
|
|
|
assertFalse(sExpression.functionp());
|
|
|
|
assertFalse(sExpression.listp());
|
|
|
|
assertFalse(sExpression.nullp());
|
|
|
|
assertFalse(sExpression.numberp());
|
|
|
|
assertTrue(sExpression.stringp());
|
|
|
|
assertFalse(sExpression.symbolp());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void assertSymbol(SExpression sExpression) {
|
|
|
|
assertTrue(sExpression.atomp());
|
|
|
|
assertFalse(sExpression.consp());
|
|
|
|
assertFalse(sExpression.functionp());
|
|
|
|
assertFalse(sExpression.listp());
|
|
|
|
assertFalse(sExpression.nullp());
|
|
|
|
assertFalse(sExpression.numberp());
|
|
|
|
assertFalse(sExpression.stringp());
|
|
|
|
assertTrue(sExpression.symbolp());
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-14 13:09:41 -05:00
|
|
|
parser.getNextSExpression();
|
2016-12-14 12:10:28 -05:00
|
|
|
|
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-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-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
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = LispException.class)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = LispException.class)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = LispException.class)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = LispException.class)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = LispException.class)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = LispException.class)
|
|
|
|
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");
|
|
|
|
} catch (LispException e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
assertTrue(parser.isEof());
|
|
|
|
}
|
2016-12-14 12:10:28 -05:00
|
|
|
}
|