Major Refactor - Static constants and test case names

This commit is contained in:
Mike Cifelli 2017-03-02 09:54:23 -05:00
parent 49fee52284
commit 2bd0c1a674
50 changed files with 309 additions and 256 deletions

View File

@ -1,6 +1,7 @@
package function;
import static function.builtin.EVAL.eval;
import static sexpression.Nil.NIL;
import java.text.MessageFormat;
import java.util.ArrayList;
@ -113,7 +114,7 @@ public class UserDefinedFunction extends LispFunction {
}
private SExpression evaluateBody() {
SExpression lastEvaluation = Nil.getInstance();
SExpression lastEvaluation = NIL;
for (Cons expression = body; expression.isCons(); expression = (Cons) expression.getRest())
lastEvaluation = eval(expression.getFirst());

View File

@ -1,5 +1,8 @@
package function.builtin;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import java.text.MessageFormat;
import error.LispException;
@ -32,9 +35,9 @@ public class EVAL extends LispFunction {
public static SExpression lookupSymbol(String symbolName) {
if (symbolName.equals("NIL"))
return Nil.getInstance();
return NIL;
else if (symbolName.equals("T"))
return Symbol.T;
return T;
else if (symbolName.startsWith(":"))
return new Symbol(symbolName);
@ -97,7 +100,7 @@ public class EVAL extends LispFunction {
private Cons evaluateArgList(Cons arguments) {
if (arguments.isNull())
return Nil.getInstance();
return NIL;
SExpression first = eval(arguments.getFirst());
SExpression rest = arguments.getRest();

View File

@ -1,5 +1,7 @@
package function.builtin;
import static sexpression.Nil.NIL;
import environment.RuntimeEnvironment;
import function.*;
import sexpression.*;
@ -20,7 +22,7 @@ public class EXIT extends LispFunction {
argumentValidator.validate(argumentList);
environment.terminateSuccessfully();
return Nil.getInstance();
return NIL;
}
}

View File

@ -1,6 +1,8 @@
package function.builtin;
import static function.builtin.EVAL.eval;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import java.io.*;
import java.text.MessageFormat;
@ -44,7 +46,7 @@ public class LOAD extends LispFunction {
if (parser != null)
wasSuccessful = isSuccessfulEvaluation(parser);
return wasSuccessful ? Symbol.T : Nil.getInstance();
return wasSuccessful ? T : NIL;
}
private LispParser attemptToCreateParserOnFile(String fileName) {

View File

@ -1,5 +1,7 @@
package function.builtin.cons;
import static sexpression.Nil.NIL;
import function.*;
import sexpression.*;
@ -13,7 +15,7 @@ public class LIST extends LispFunction {
}
public static Cons makeList(SExpression sexpr) {
return new Cons(sexpr, Nil.getInstance());
return new Cons(sexpr, NIL);
}
public Cons call(Cons argumentList) {
@ -24,7 +26,7 @@ public class LIST extends LispFunction {
private Cons callRecursive(Cons argumentList) {
if (argumentList.isNull())
return Nil.getInstance();
return NIL;
SExpression firstArgument = argumentList.getFirst();
Cons remainingArguments = (Cons) argumentList.getRest();

View File

@ -1,5 +1,7 @@
package function.builtin.math;
import static sexpression.LispNumber.ONE;
import function.*;
import sexpression.*;
@ -18,7 +20,7 @@ public class MULTIPLY extends LispFunction {
public LispNumber call(Cons argumentList) {
argumentValidator.validate(argumentList);
return mathFunction.callTailRecursive(new Cons(LispNumber.ONE, argumentList));
return mathFunction.callTailRecursive(new Cons(ONE, argumentList));
}
private LispNumber multiply(LispNumber number1, LispNumber number2) {

View File

@ -1,5 +1,7 @@
package function.builtin.math;
import static sexpression.LispNumber.ZERO;
import function.*;
import sexpression.*;
@ -18,7 +20,7 @@ public class PLUS extends LispFunction {
public LispNumber call(Cons argumentList) {
argumentValidator.validate(argumentList);
return mathFunction.callTailRecursive(new Cons(LispNumber.ZERO, argumentList));
return mathFunction.callTailRecursive(new Cons(ZERO, argumentList));
}
private LispNumber add(LispNumber number1, LispNumber number2) {

View File

@ -1,5 +1,8 @@
package function.builtin.predicate;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import function.*;
import sexpression.*;
@ -17,7 +20,7 @@ public class ATOM extends LispFunction {
argumentValidator.validate(argumentList);
SExpression argument = argumentList.getFirst();
return argument.isAtom() ? Symbol.T : Nil.getInstance();
return argument.isAtom() ? T : NIL;
}
}

View File

@ -1,5 +1,8 @@
package function.builtin.predicate;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import function.*;
import sexpression.*;
@ -35,7 +38,7 @@ public class EQ extends LispFunction {
}
private SExpression atomEq(SExpression firstArgument, SExpression secondArgument) {
return isEq(firstArgument, secondArgument) ? Symbol.T : Nil.getInstance();
return isEq(firstArgument, secondArgument) ? T : NIL;
}
private boolean isEq(SExpression firstArgument, SExpression secondArgument) {
@ -43,7 +46,7 @@ public class EQ extends LispFunction {
}
private SExpression listEq(SExpression firstArgument, SExpression secondArgument) {
return (firstArgument == secondArgument) ? Symbol.T : Nil.getInstance();
return (firstArgument == secondArgument) ? T : NIL;
}
}

View File

@ -1,5 +1,8 @@
package function.builtin.predicate;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import function.*;
import sexpression.*;
@ -28,7 +31,7 @@ public class EQUAL extends LispFunction {
}
private SExpression equal(SExpression firstArgument, SExpression secondArgument) {
return isEqual(firstArgument, secondArgument) ? Symbol.T : Nil.getInstance();
return isEqual(firstArgument, secondArgument) ? T : NIL;
}
}

View File

@ -1,5 +1,8 @@
package function.builtin.predicate;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import function.*;
import sexpression.*;
@ -16,7 +19,7 @@ public class LISTP extends LispFunction {
public SExpression call(Cons argumentList) {
argumentValidator.validate(argumentList);
return argumentList.getFirst().isList() ? Symbol.T : Nil.getInstance();
return argumentList.getFirst().isList() ? T : NIL;
}
}

View File

@ -1,5 +1,8 @@
package function.builtin.predicate;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import function.*;
import sexpression.*;
@ -16,7 +19,7 @@ public class NULL extends LispFunction {
public SExpression call(Cons argumentList) {
argumentValidator.validate(argumentList);
return argumentList.getFirst().isNull() ? Symbol.T : Nil.getInstance();
return argumentList.getFirst().isNull() ? T : NIL;
}
}

View File

@ -1,5 +1,8 @@
package function.builtin.predicate;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import function.*;
import sexpression.*;
@ -24,7 +27,7 @@ public class NUMERIC_EQUAL extends LispFunction {
Cons remainingArguments = (Cons) argumentList.getRest();
if (remainingArguments.isNull())
return Symbol.T;
return T;
SExpression firstArgument = argumentList.getFirst();
LispNumber number1 = (LispNumber) firstArgument;
@ -32,7 +35,7 @@ public class NUMERIC_EQUAL extends LispFunction {
LispNumber number2 = (LispNumber) secondArgument;
if (!isEqual(number1, number2))
return Nil.getInstance();
return NIL;
return callTailRecursive(remainingArguments);
}

View File

@ -1,5 +1,8 @@
package function.builtin.predicate;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import function.*;
import sexpression.*;
@ -24,7 +27,7 @@ public class NUMERIC_GREATER extends LispFunction {
Cons remainingArguments = (Cons) argumentList.getRest();
if (remainingArguments.isNull())
return Symbol.T;
return T;
SExpression firstArgument = argumentList.getFirst();
SExpression secondArgument = remainingArguments.getFirst();
@ -32,7 +35,7 @@ public class NUMERIC_GREATER extends LispFunction {
LispNumber number2 = (LispNumber) secondArgument;
if (!isFirstGreater(number1, number2))
return Nil.getInstance();
return NIL;
return callTailRecursive(remainingArguments);
}

View File

@ -1,5 +1,8 @@
package function.builtin.predicate;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import function.*;
import sexpression.*;
@ -24,7 +27,7 @@ public class NUMERIC_LESS extends LispFunction {
Cons remainingArguments = (Cons) argumentList.getRest();
if (remainingArguments.isNull())
return Symbol.T;
return T;
SExpression firstArgument = argumentList.getFirst();
SExpression secondArgument = remainingArguments.getFirst();
@ -32,7 +35,7 @@ public class NUMERIC_LESS extends LispFunction {
LispNumber number2 = (LispNumber) secondArgument;
if (!isFirstLesser(number1, number2))
return Nil.getInstance();
return NIL;
return callTailRecursive(remainingArguments);
}

View File

@ -1,6 +1,7 @@
package function.builtin.special;
import static function.builtin.EVAL.eval;
import static sexpression.Symbol.T;
import function.*;
import sexpression.*;
@ -17,7 +18,7 @@ public class AND extends LispSpecialFunction {
public SExpression call(Cons argumentList) {
argumentValidator.validate(argumentList);
return callTailRecursive(argumentList, Symbol.T);
return callTailRecursive(argumentList, T);
}
private SExpression callTailRecursive(Cons argumentList, SExpression lastValue) {

View File

@ -2,6 +2,8 @@ package function.builtin.special;
import static function.builtin.EVAL.eval;
import static function.builtin.predicate.EQUAL.isEqual;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import function.*;
import sexpression.*;
@ -27,7 +29,7 @@ public class CASE extends LispSpecialFunction {
private SExpression callTailRecursive(SExpression key, Cons argumentList) {
if (argumentList.isNull())
return Nil.getInstance();
return NIL;
Cons clause = (Cons) argumentList.getFirst();
Cons remainingClauses = (Cons) argumentList.getRest();
@ -57,7 +59,7 @@ public class CASE extends LispSpecialFunction {
}
private boolean isKeyListT(SExpression keyList) {
return isEqual(Symbol.T, keyList);
return isEqual(T, keyList);
}
private SExpression advanceCons(SExpression knownCons) {
@ -69,7 +71,7 @@ public class CASE extends LispSpecialFunction {
}
private SExpression evaluateConsequents(SExpression consequentList) {
SExpression lastConsequentValue = Nil.getInstance();
SExpression lastConsequentValue = NIL;
for (; consequentList.isCons(); consequentList = advanceCons(consequentList))
lastConsequentValue = eval(getFirst(consequentList));

View File

@ -1,6 +1,7 @@
package function.builtin.special;
import static function.builtin.EVAL.eval;
import static sexpression.Nil.NIL;
import function.*;
import sexpression.*;
@ -24,7 +25,7 @@ public class COND extends LispSpecialFunction {
private SExpression callTailRecursive(Cons argumentList) {
if (argumentList.isNull())
return Nil.getInstance();
return NIL;
Cons clause = (Cons) argumentList.getFirst();
Cons remainingClauses = (Cons) argumentList.getRest();
@ -37,7 +38,7 @@ public class COND extends LispSpecialFunction {
}
private boolean isTestSuccessful(SExpression test) {
return test != Nil.getInstance();
return test != NIL;
}
private SExpression evaluateConsequents(SExpression consequentList, SExpression test) {

View File

@ -1,6 +1,7 @@
package function.builtin.special;
import static function.builtin.EVAL.eval;
import static sexpression.Nil.NIL;
import function.*;
import sexpression.*;
@ -71,7 +72,7 @@ public class LET extends LispSpecialFunction {
}
private SExpression evaluateBody(Cons body) {
SExpression lastEvaluation = Nil.getInstance();
SExpression lastEvaluation = NIL;
for (; body.isCons(); body = (Cons) body.getRest())
lastEvaluation = eval(body.getFirst());

View File

@ -1,6 +1,7 @@
package function.builtin.special;
import static function.builtin.EVAL.eval;
import static sexpression.Nil.NIL;
import function.*;
import sexpression.*;
@ -17,7 +18,7 @@ public class PROGN extends LispSpecialFunction {
public SExpression call(Cons argumentList) {
argumentValidator.validate(argumentList);
return callTailRecursive(argumentList, Nil.getInstance());
return callTailRecursive(argumentList, NIL);
}
private SExpression callTailRecursive(Cons argumentList, SExpression lastValue) {

View File

@ -3,11 +3,7 @@ package sexpression;
@DisplayName("nil")
public class Nil extends Cons {
private static Nil uniqueInstance = new Nil();
public static Nil getInstance() {
return uniqueInstance;
}
public static final Nil NIL = new Nil();
private Nil() {
super(null, null);

View File

@ -1,5 +1,7 @@
package token;
import static sexpression.Nil.NIL;
import java.util.function.Supplier;
import file.FilePosition;
@ -16,7 +18,7 @@ public class QuoteMark extends Token {
Token nextToken = getNextToken.get();
SExpression argument = nextToken.parseSExpression(getNextToken);
return new Cons(Symbol.createQuote(), new Cons(argument, Nil.getInstance()));
return new Cons(Symbol.createQuote(), new Cons(argument, NIL));
}
}

View File

@ -1,10 +1,12 @@
package token;
import static sexpression.Nil.NIL;
import java.util.function.Supplier;
import error.LineColumnException;
import file.FilePosition;
import sexpression.*;
import sexpression.SExpression;
public class RightParenthesis extends Token {
@ -19,7 +21,7 @@ public class RightParenthesis extends Token {
@Override
public SExpression parseSExpressionTail(Supplier<Token> getNextToken) {
return Nil.getInstance();
return NIL;
}
public static class StartsWithRightParenthesisException extends LineColumnException {

View File

@ -2,6 +2,8 @@ package function;
import static error.ErrorManager.Severity.ERROR;
import static org.junit.Assert.*;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import org.junit.*;
@ -13,10 +15,10 @@ public class ArgumentValidatorTester {
private ArgumentValidator validator;
private Cons makeArgumentListOfSize(int size) {
Cons argumentList = Nil.getInstance();
Cons argumentList = NIL;
for (int i = 0; i < size; i++)
argumentList = new Cons(Nil.getInstance(), argumentList);
argumentList = new Cons(NIL, argumentList);
return argumentList;
}
@ -73,14 +75,14 @@ public class ArgumentValidatorTester {
@Test
public void tooManyArgumentsException_HasCorrectSeverity() {
TooManyArgumentsException e = new TooManyArgumentsException("TEST", Nil.getInstance());
TooManyArgumentsException e = new TooManyArgumentsException("TEST", NIL);
assertEquals(ERROR, e.getSeverity());
}
@Test
public void tooManyArgumentsException_HasMessageText() {
TooManyArgumentsException e = new TooManyArgumentsException("TEST", Nil.getInstance());
TooManyArgumentsException e = new TooManyArgumentsException("TEST", NIL);
assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 0);
@ -88,14 +90,14 @@ public class ArgumentValidatorTester {
@Test
public void tooFewArgumentsException_HasCorrectSeverity() {
TooFewArgumentsException e = new TooFewArgumentsException("TEST", Nil.getInstance());
TooFewArgumentsException e = new TooFewArgumentsException("TEST", NIL);
assertEquals(ERROR, e.getSeverity());
}
@Test
public void tooFewArgumentsException_HasMessageText() {
TooFewArgumentsException e = new TooFewArgumentsException("TEST", Nil.getInstance());
TooFewArgumentsException e = new TooFewArgumentsException("TEST", NIL);
assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 0);
@ -103,14 +105,14 @@ public class ArgumentValidatorTester {
@Test
public void BadArgumentTypeException_HasCorrectSeverity() {
BadArgumentTypeException e = new BadArgumentTypeException("TEST", Nil.getInstance(), SExpression.class);
BadArgumentTypeException e = new BadArgumentTypeException("TEST", NIL, SExpression.class);
assertEquals(ERROR, e.getSeverity());
}
@Test
public void BadArgumentTypeException_HasMessageText() {
BadArgumentTypeException e = new BadArgumentTypeException("TEST", Nil.getInstance(), SExpression.class);
BadArgumentTypeException e = new BadArgumentTypeException("TEST", NIL, SExpression.class);
assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 0);
@ -130,7 +132,7 @@ public class ArgumentValidatorTester {
@Test
public void correctFirstAndRestArgumentTypes_DoesNotThrowException() {
Cons argumentList = new Cons(Symbol.T, new Cons(Nil.getInstance(), Nil.getInstance()));
Cons argumentList = new Cons(T, new Cons(NIL, NIL));
validator.setFirstArgumentExpectedType(Symbol.class);
validator.setTrailingArgumentExpectedType(Cons.class);
@ -139,7 +141,7 @@ public class ArgumentValidatorTester {
@Test(expected = BadArgumentTypeException.class)
public void badFirstArgumentType_ThrowsException() {
Cons argumentList = new Cons(Symbol.T, new Cons(Nil.getInstance(), Nil.getInstance()));
Cons argumentList = new Cons(T, new Cons(NIL, NIL));
validator.setFirstArgumentExpectedType(Cons.class);
validator.setTrailingArgumentExpectedType(Cons.class);
@ -148,7 +150,7 @@ public class ArgumentValidatorTester {
@Test(expected = BadArgumentTypeException.class)
public void badTrailingArgumentType_ThrowsException() {
Cons argumentList = new Cons(Symbol.T, new Cons(Nil.getInstance(), Nil.getInstance()));
Cons argumentList = new Cons(T, new Cons(NIL, NIL));
validator.setFirstArgumentExpectedType(Symbol.class);
validator.setTrailingArgumentExpectedType(Symbol.class);
@ -157,7 +159,7 @@ public class ArgumentValidatorTester {
@Test
public void expectedTypeWithNoDisplayName_DoesNotCauseNPE() {
Cons argumentList = new Cons(Symbol.T, new Cons(Nil.getInstance(), Nil.getInstance()));
Cons argumentList = new Cons(T, new Cons(NIL, NIL));
SExpression withoutDisplayName = new SExpression() {};
validator.setEveryArgumentExpectedType(withoutDisplayName.getClass());
@ -172,28 +174,28 @@ public class ArgumentValidatorTester {
@Test(expected = DottedArgumentListException.class)
public void givenDottedArgumentList_ThrowsException() {
Cons argumentList = new Cons(Symbol.T, Symbol.T);
Cons argumentList = new Cons(T, T);
validator.validate(argumentList);
}
@Test(expected = DottedArgumentListException.class)
public void givenLargeDottedArgumentList_ThrowsException() {
Cons argumentList = new Cons(Symbol.T, new Cons(Symbol.T, Symbol.T));
Cons argumentList = new Cons(T, new Cons(T, T));
validator.validate(argumentList);
}
@Test
public void DottedArgumentListException_HasCorrectSeverity() {
DottedArgumentListException e = new DottedArgumentListException("TEST", Nil.getInstance());
DottedArgumentListException e = new DottedArgumentListException("TEST", NIL);
assertEquals(ERROR, e.getSeverity());
}
@Test
public void dottedArgumentListException_HasMessageText() {
DottedArgumentListException e = new DottedArgumentListException("TEST", Nil.getInstance());
DottedArgumentListException e = new DottedArgumentListException("TEST", NIL);
assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 0);
@ -202,19 +204,19 @@ public class ArgumentValidatorTester {
@Test(expected = BadArgumentTypeException.class)
public void doNotAcceptNil_ThrowsExceptionOnNilArgument() {
validator.doNotAcceptNil();
validator.validate(new Cons(Symbol.T, new Cons(Nil.getInstance(), Nil.getInstance())));
validator.validate(new Cons(T, new Cons(NIL, NIL)));
}
@Test
public void doNotAcceptNil_AllowsEmptyArgumentList() {
validator.doNotAcceptNil();
validator.validate(Nil.getInstance());
validator.validate(NIL);
}
@Test
public void doNotAcceptNil_AllowsProperList() {
validator.doNotAcceptNil();
validator.validate(new Cons(Symbol.T, new Cons(Symbol.T, Nil.getInstance())));
validator.validate(new Cons(T, new Cons(T, NIL)));
}
}

View File

@ -1,6 +1,7 @@
package function;
import static org.junit.Assert.*;
import static sexpression.Nil.NIL;
import static testutil.TestUtilities.assertSExpressionsMatch;
import org.junit.Test;
@ -15,27 +16,24 @@ public class UserDefinedFunctionTester {
private static final String FUNCTION_NAME = "TEST";
private UserDefinedFunction createNoArgumentFunctionThatReturnsNil() {
return new UserDefinedFunction(FUNCTION_NAME, Nil.getInstance(),
new Cons(Nil.getInstance(), Nil.getInstance()));
return new UserDefinedFunction(FUNCTION_NAME, NIL, new Cons(NIL, NIL));
}
private UserDefinedFunction createOneArgumentFunctionThatReturnsArgument() {
return new UserDefinedFunction(FUNCTION_NAME, new Cons(new Symbol("X"), Nil.getInstance()),
new Cons(new Symbol("X"), Nil.getInstance()));
return new UserDefinedFunction(FUNCTION_NAME, new Cons(new Symbol("X"), NIL), new Cons(new Symbol("X"), NIL));
}
@Test
public void nilFunctionCall() {
UserDefinedFunction function = createNoArgumentFunctionThatReturnsNil();
assertEquals(Nil.getInstance(), function.call(Nil.getInstance()));
assertEquals(NIL, function.call(NIL));
}
@Test
public void nilFunctionToString() {
UserDefinedFunction function = createNoArgumentFunctionThatReturnsNil();
Cons expected = new Cons(new Symbol(FUNCTION_NAME),
new Cons(Nil.getInstance(), new Cons(Nil.getInstance(), Nil.getInstance())));
Cons expected = new Cons(new Symbol(FUNCTION_NAME), new Cons(NIL, new Cons(NIL, NIL)));
assertSExpressionsMatch(expected, function.getLambdaExpression());
}
@ -44,7 +42,7 @@ public class UserDefinedFunctionTester {
public void oneArgumentFunction_ReturnsCorrectValue() {
UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument();
SExpression argument = new LispNumber("23");
Cons argumentList = new Cons(argument, Nil.getInstance());
Cons argumentList = new Cons(argument, NIL);
assertSExpressionsMatch(argument, function.call(argumentList));
}
@ -53,7 +51,7 @@ public class UserDefinedFunctionTester {
public void oneArgumentFunction_ThrowsExceptionWithTooManyArguments() {
UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument();
SExpression argument = new LispNumber("23");
Cons argumentList = new Cons(argument, new Cons(argument, Nil.getInstance()));
Cons argumentList = new Cons(argument, new Cons(argument, NIL));
function.call(argumentList);
}
@ -62,13 +60,12 @@ public class UserDefinedFunctionTester {
public void oneArgumentFunction_ThrowsExceptionWithTooFewArguments() {
UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument();
function.call(Nil.getInstance());
function.call(NIL);
}
@Test
public void illegalKeywordRestPositionException_HasCorrectAttributes() {
IllegalKeywordRestPositionException e = new IllegalKeywordRestPositionException(FUNCTION_NAME,
Nil.getInstance());
IllegalKeywordRestPositionException e = new IllegalKeywordRestPositionException(FUNCTION_NAME, NIL);
assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 0);

View File

@ -1,5 +1,6 @@
package function.builtin;
import static function.builtin.APPLY.apply;
import static testutil.TestUtilities.*;
import org.junit.Test;
@ -11,56 +12,56 @@ import sexpression.Cons;
public class APPLYTester {
@Test
public void testApply() {
public void applyWithSymbol() {
String input = "(apply '+ '(1 2 3))";
assertSExpressionsMatch(parseString("6"), evaluateString(input));
}
@Test
public void testApplyWithLambdaExpression() {
public void applyWithLambdaExpression() {
String input = "(apply (lambda (x) (+ x 1)) '(25))";
assertSExpressionsMatch(parseString("26"), evaluateString(input));
}
@Test
public void testApplyWithQuotedLambdaExpression() {
public void applyWithQuotedLambdaExpression() {
String input = "(apply '(lambda (x) (+ x 1)) '(25))";
assertSExpressionsMatch(parseString("26"), evaluateString(input));
}
@Test
public void testStaticApplyCall() {
public void staticApplyCall() {
String argumentList = "(+ (25 10))";
Cons parsedArgumentList = (Cons) parseString(argumentList);
assertSExpressionsMatch(parseString("35"), APPLY.apply(parsedArgumentList));
assertSExpressionsMatch(parseString("35"), apply(parsedArgumentList));
}
@Test(expected = UndefinedFunctionException.class)
public void testApplyWithUndefinedFunction() {
public void applyWithUndefinedFunction() {
evaluateString("(apply 'f '(1 2 3))");
}
@Test(expected = BadArgumentTypeException.class)
public void testApplyWithNonListSecondArgument() {
public void applyWithNonListSecondArgument() {
evaluateString("(apply '+ '2)");
}
@Test(expected = TooManyArgumentsException.class)
public void testApplyWithTooManyArguments() {
public void applyWithTooManyArguments() {
evaluateString("(apply '1 '2 '3)");
}
@Test(expected = TooFewArgumentsException.class)
public void testApplyWithTooFewArguments() {
public void applyWithTooFewArguments() {
evaluateString("(apply '1)");
}
@Test(expected = DottedArgumentListException.class)
public void testCondWithDottedArgumentList_ThrowsException() {
public void applyWithDottedArgumentList_ThrowsException() {
evaluateString("(apply 'apply (cons 'T 'T))");
}

View File

@ -1,87 +1,88 @@
package function.builtin;
import static function.builtin.EVAL.lookupSymbol;
import static org.junit.Assert.*;
import static sexpression.Nil.NIL;
import static testutil.TestUtilities.*;
import org.junit.Test;
import function.ArgumentValidator.*;
import function.builtin.EVAL.*;
import sexpression.Nil;
public class EVALTester {
@Test
public void testEval() {
public void evalNumber() {
String input = "(eval 9)";
assertSExpressionsMatch(parseString("9"), evaluateString(input));
}
@Test
public void testEvalNil() {
public void evalNil() {
String input = "(eval ())";
assertSExpressionsMatch(parseString("()"), evaluateString(input));
}
@Test
public void testLookupSymbol() {
public void lookupKeywordSymbol() {
String symbol = ":symbol";
assertSExpressionsMatch(parseString(symbol), EVAL.lookupSymbol(symbol));
assertSExpressionsMatch(parseString(symbol), lookupSymbol(symbol));
}
@Test
public void testLookupT() {
public void lookupT() {
String symbol = "T";
assertSExpressionsMatch(parseString(symbol), EVAL.lookupSymbol(symbol));
assertSExpressionsMatch(parseString(symbol), lookupSymbol(symbol));
}
@Test
public void testLookupNil() {
public void lookupNil() {
String symbol = "NIL";
assertSExpressionsMatch(parseString(symbol), EVAL.lookupSymbol(symbol));
assertSExpressionsMatch(parseString(symbol), lookupSymbol(symbol));
}
@Test
public void testLookupUndefinedSymbol() {
public void lookupUndefinedSymbol() {
assertNull(EVAL.lookupSymbol("undefined"));
}
@Test(expected = UndefinedFunctionException.class)
public void testEvalUndefinedFunction() {
public void evalUndefinedFunction() {
String input = "(funcall 'eval '(undefined))";
evaluateString(input);
}
@Test(expected = UndefinedSymbolException.class)
public void testEvalUndefinedSymbol() {
public void evalUndefinedSymbol() {
String input = "(eval undefined)";
evaluateString(input);
}
@Test(expected = DottedArgumentListException.class)
public void testEvalWithDottedLambdaList() {
public void evalWithDottedLambdaList() {
String input = "(funcall 'eval (cons '+ 1))";
evaluateString(input);
}
@Test(expected = TooManyArgumentsException.class)
public void testEvalWithTooManyArguments() {
public void evalWithTooManyArguments() {
evaluateString("(eval '1 '2 '3)");
}
@Test(expected = TooFewArgumentsException.class)
public void testEvalWithTooFewArguments() {
public void evalWithTooFewArguments() {
evaluateString("(eval)");
}
@Test
public void undefinedFunctionException_HasMessageText() {
UndefinedFunctionException e = new UndefinedFunctionException(Nil.getInstance());
UndefinedFunctionException e = new UndefinedFunctionException(NIL);
assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 0);
@ -89,7 +90,7 @@ public class EVALTester {
@Test
public void undefinedSymbolException_HasMessageText() {
UndefinedSymbolException e = new UndefinedSymbolException(Nil.getInstance());
UndefinedSymbolException e = new UndefinedSymbolException(NIL);
assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 0);

View File

@ -41,7 +41,7 @@ public class EXITTester {
}
@Test(expected = TooManyArgumentsException.class)
public void testExitWithTooManyArguments() {
public void exitWithTooManyArguments() {
evaluateString("(exit 1)");
}

View File

@ -20,14 +20,14 @@ public class FUNCALLTester {
}
@Test
public void testFuncallWithNumbers() {
public void funcallWithNumbers() {
String input = "(funcall '+ 1 2 3)";
assertSExpressionsMatch(parseString("6"), evaluateString(input));
}
@Test
public void testFuncallWithUserDefinedFunction() {
public void funcallWithUserDefinedFunction() {
String defineUserFunction = "(defun x (n m) (+ n m))";
String input = "(funcall 'x 2 30)";
@ -36,7 +36,7 @@ public class FUNCALLTester {
}
@Test(expected = TooFewArgumentsException.class)
public void testFuncallWithTooFewArguments() {
public void funcallWithTooFewArguments() {
evaluateString("(funcall)");
}

View File

@ -75,17 +75,17 @@ public class LOADTester {
}
@Test(expected = BadArgumentTypeException.class)
public void testLoadWithBadArgumentType() {
public void loadWithBadArgumentType() {
evaluateString("(load '1)");
}
@Test(expected = TooManyArgumentsException.class)
public void testLoadWithTooManyArguments() {
public void loadWithTooManyArguments() {
evaluateString("(load \"1\" \"2\")");
}
@Test(expected = TooFewArgumentsException.class)
public void testLoadWithTooFewArguments() {
public void loadWithTooFewArguments() {
evaluateString("(load)");
}

View File

@ -42,12 +42,12 @@ public class PRINTTester {
}
@Test(expected = TooManyArgumentsException.class)
public void testPrintWithTooManyArguments() {
public void printWithTooManyArguments() {
evaluateString("(print '1 '2)");
}
@Test(expected = TooFewArgumentsException.class)
public void testPrintWithTooFewArguments() {
public void printWithTooFewArguments() {
evaluateString("(print)");
}

View File

@ -29,7 +29,7 @@ public class SETTester {
}
@Test
public void testSet() {
public void set() {
evaluateString("(set 'a 23)");
assertSExpressionsMatch(new LispNumber("23"), evaluateString("a"));
}
@ -80,17 +80,17 @@ public class SETTester {
}
@Test(expected = BadArgumentTypeException.class)
public void testSetWithNonSymbol() {
public void setWithNonSymbol() {
evaluateString("(set '1 2)");
}
@Test(expected = TooFewArgumentsException.class)
public void testSetWithTooFewArguments() {
public void setWithTooFewArguments() {
evaluateString("(set 'x)");
}
@Test(expected = TooManyArgumentsException.class)
public void testSetWithTooManyArguments() {
public void setWithTooManyArguments() {
evaluateString("(set 'a 'b 'c)");
}

View File

@ -1,13 +1,13 @@
package function.builtin;
import static org.junit.Assert.*;
import static sexpression.Nil.NIL;
import static testutil.TestUtilities.evaluateString;
import org.junit.*;
import function.ArgumentValidator.*;
import function.builtin.SYMBOL_FUNCTION.UndefinedSymbolFunctionException;
import sexpression.Nil;
import table.FunctionTable;
public class SYMBOL_FUNCTIONTester {
@ -23,21 +23,21 @@ public class SYMBOL_FUNCTIONTester {
}
@Test
public void testSymbolFunction_BuiltInFunction() {
public void symbolFunction_BuiltInFunction() {
String input = "(symbol-function '+)";
assertEquals("#<FUNCTION +>", evaluateString(input).toString());
}
@Test
public void testSymbolFunction_BuiltInSpecialFunction() {
public void symbolFunction_BuiltInSpecialFunction() {
String input = "(symbol-function 'if)";
assertEquals("#<SPECIAL-FUNCTION IF>", evaluateString(input).toString());
}
@Test
public void testSymbolFunction_UserDefinedFunction() {
public void symbolFunction_UserDefinedFunction() {
String defineUserFunction = "(defun y (n m) (+ n m))";
String input = "(symbol-function 'y)";
@ -46,30 +46,30 @@ public class SYMBOL_FUNCTIONTester {
}
@Test(expected = UndefinedSymbolFunctionException.class)
public void testSymbolFunction_NonFunction() {
public void symbolFunction_NonFunction() {
String input = "(symbol-function 'a)";
evaluateString(input);
}
@Test(expected = BadArgumentTypeException.class)
public void testSymbolFunctionWithBadArgumentType() {
public void symbolFunctionWithBadArgumentType() {
evaluateString("(symbol-function 2)");
}
@Test(expected = TooManyArgumentsException.class)
public void testSymbolFunctionWithTooManyArguments() {
public void symbolFunctionWithTooManyArguments() {
evaluateString("(symbol-function 'a 'b)");
}
@Test(expected = TooFewArgumentsException.class)
public void testSymbolFunctionWithTooFewArguments() {
public void symbolFunctionWithTooFewArguments() {
evaluateString("(symbol-function)");
}
@Test
public void undefinedSymbolFunctionException_HasMessageText() {
UndefinedSymbolFunctionException e = new UndefinedSymbolFunctionException(Nil.getInstance());
UndefinedSymbolFunctionException e = new UndefinedSymbolFunctionException(NIL);
assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 0);

View File

@ -10,49 +10,49 @@ import sexpression.*;
public class CONSTester {
@Test
public void testConsWithNilValues() {
public void consWithNilValues() {
String input = "(cons () nil)";
assertSExpressionsMatch(parseString("(())"), evaluateString(input));
}
@Test
public void testConsWithTwoSymbols() {
public void consWithTwoSymbols() {
String input = "(cons 'a 'b)";
assertSExpressionsMatch(new Cons(new Symbol("A"), new Symbol("B")), evaluateString(input));
}
@Test
public void testConsWithListAsRest() {
public void consWithListAsRest() {
String input = "(cons 1 '(2 3))";
assertSExpressionsMatch(parseString("(1 2 3)"), evaluateString(input));
}
@Test
public void testConsWithTwoLists() {
public void consWithTwoLists() {
String input = "(cons '(1 2) '(3 4))";
assertSExpressionsMatch(parseString("((1 2) 3 4)"), evaluateString(input));
}
@Test
public void testConsWithList() {
public void consWithList() {
String input = "(cons nil '(2 3))";
assertSExpressionsMatch(parseString("(nil 2 3)"), evaluateString(input));
}
@Test(expected = TooManyArgumentsException.class)
public void testConsWithTooManyArguments() {
public void consWithTooManyArguments() {
String input = "(cons 1 2 3)";
evaluateString(input);
}
@Test(expected = TooFewArgumentsException.class)
public void testConsWithTooFewArguments() {
public void consWithTooFewArguments() {
String input = "(cons 1)";
evaluateString(input);

View File

@ -9,38 +9,38 @@ import function.ArgumentValidator.*;
public class LENGTHTester {
@Test
public void testLengthOfNil() {
public void lengthOfNil() {
String input = "(length '())";
assertSExpressionsMatch(parseString("0"), evaluateString(input));
}
@Test
public void testLengthOfListOfOneElement() {
public void lengthOfListOfOneElement() {
String input = "(length '(1))";
assertSExpressionsMatch(parseString("1"), evaluateString(input));
}
@Test
public void testLengthOfListOfManyElements() {
public void lengthOfListOfManyElements() {
String input = "(length '(1 2 3 4 5))";
assertSExpressionsMatch(parseString("5"), evaluateString(input));
}
@Test(expected = BadArgumentTypeException.class)
public void testLengthWithNonList() {
public void lengthWithNonList() {
evaluateString("(length 'x)");
}
@Test(expected = TooManyArgumentsException.class)
public void testLengthWithTooManyArguments() {
public void lengthWithTooManyArguments() {
evaluateString("(length '(1 2) '(1 2))");
}
@Test(expected = TooFewArgumentsException.class)
public void testLengthWithTooFewArguments() {
public void lengthWithTooFewArguments() {
evaluateString("(length)");
}

View File

@ -1,5 +1,6 @@
package function.builtin.cons;
import static function.builtin.cons.LIST.makeList;
import static testutil.TestUtilities.*;
import org.junit.Test;
@ -7,50 +8,50 @@ import org.junit.Test;
public class LISTTester {
@Test
public void testListWithNoArguments() {
public void listWithNoArguments() {
String input = "(list)";
assertSExpressionsMatch(parseString("nil"), evaluateString(input));
}
@Test
public void testListWithOneArgument() {
public void listWithOneArgument() {
String input = "(list 1)";
assertSExpressionsMatch(parseString("(1)"), evaluateString(input));
}
@Test
public void testListWithTwoArguments() {
public void listWithTwoArguments() {
String input = "(list 2 3)";
assertSExpressionsMatch(parseString("(2 3)"), evaluateString(input));
}
@Test
public void testListWithManyArguments() {
public void listWithManyArguments() {
String input = "(list 'm 'a 'n 'y 'a 'r 'g 's)";
assertSExpressionsMatch(parseString("(m a n y a r g s)"), evaluateString(input));
}
@Test
public void testListWithOneListArgument() {
public void listWithOneListArgument() {
String input = "(list '(1))";
assertSExpressionsMatch(parseString("((1))"), evaluateString(input));
}
@Test
public void testListWithManyListArguments() {
public void listWithManyListArguments() {
String input = "(list '(1) '(2 3) ())";
assertSExpressionsMatch(parseString("((1) (2 3) ())"), evaluateString(input));
}
@Test
public void testMakeList() {
assertSExpressionsMatch(parseString("(22)"), LIST.makeList(parseString("22")));
public void staticMakeList() {
assertSExpressionsMatch(parseString("(22)"), makeList(parseString("22")));
}
}

View File

@ -9,54 +9,54 @@ import function.ArgumentValidator.*;
public class DIVIDETester {
@Test
public void testDivideWithOne() {
public void divideWithOne() {
String input = "(/ 1)";
assertSExpressionsMatch(parseString("1"), evaluateString(input));
}
@Test
public void testDivideWithTwo() {
public void divideWithTwo() {
String input = "(/ 2)";
assertSExpressionsMatch(parseString("0"), evaluateString(input));
}
@Test
public void testDivideTwoNumbers() {
public void divideTwoNumbers() {
String input = "(/ 24 3)";
assertSExpressionsMatch(parseString("8"), evaluateString(input));
}
@Test
public void testDivideSeveralNumbers() {
public void divideSeveralNumbers() {
String input = "(/ 256 2 2 8)";
assertSExpressionsMatch(parseString("8"), evaluateString(input));
}
@Test
public void testDivideTwoNumbersWithRemainder() {
public void divideTwoNumbersWithRemainder() {
String input = "(/ 9 2)";
assertSExpressionsMatch(parseString("4"), evaluateString(input));
}
@Test
public void testDivideSeveralNumbersWithRemainder() {
public void divideSeveralNumbersWithRemainder() {
String input = "(/ 19 2 5)";
assertSExpressionsMatch(parseString("1"), evaluateString(input));
}
@Test(expected = BadArgumentTypeException.class)
public void testDivideWithNonNumber() {
public void divideWithNonNumber() {
evaluateString("(/ 'x)");
}
@Test(expected = TooFewArgumentsException.class)
public void testDivideWithTooFewArguments() {
public void divideWithTooFewArguments() {
evaluateString("(/)");
}

View File

@ -10,40 +10,40 @@ import sexpression.LispNumber;
public class MINUSTester {
@Test
public void testMinusWithOneNumber() {
public void minusWithOneNumber() {
String input = "(- 27)";
assertSExpressionsMatch(new LispNumber("-27"), evaluateString(input));
}
@Test
public void testMinusWithTwoNumbers() {
public void minusWithTwoNumbers() {
String input = "(- 5 3)";
assertSExpressionsMatch(new LispNumber("2"), evaluateString(input));
}
@Test
public void testMinusWithManyNumbers_PositiveResult() {
public void minusWithManyNumbers_PositiveResult() {
String input = "(- 200 100 10 5)";
assertSExpressionsMatch(new LispNumber("85"), evaluateString(input));
}
@Test
public void testMinusWithManyNumbers_NegativeResult() {
public void minusWithManyNumbers_NegativeResult() {
String input = "(- 100 200 20 5)";
assertSExpressionsMatch(new LispNumber("-125"), evaluateString(input));
}
@Test(expected = BadArgumentTypeException.class)
public void testMinusWithNonNumber() {
public void minusWithNonNumber() {
evaluateString("(- 'a 'b)");
}
@Test(expected = TooFewArgumentsException.class)
public void testMinusWithTooFewArguments() {
public void minusWithTooFewArguments() {
evaluateString("(-)");
}

View File

@ -10,42 +10,42 @@ import sexpression.LispNumber;
public class MULTIPLYTester {
@Test
public void testMultiplyWithNoArguments() {
public void multiplyWithNoArguments() {
String input = "(*)";
assertSExpressionsMatch(new LispNumber("1"), evaluateString(input));
}
@Test
public void testMultiplyWithOneNumber() {
public void multiplyWithOneNumber() {
String input = "(* 8)";
assertSExpressionsMatch(new LispNumber("8"), evaluateString(input));
}
@Test
public void testMultiplyWithTwoNumbers() {
public void multiplyWithTwoNumbers() {
String input = "(* 5 3)";
assertSExpressionsMatch(new LispNumber("15"), evaluateString(input));
}
@Test
public void testMultiplyWithManyNumbers_PositiveResult() {
public void multiplyWithManyNumbers_PositiveResult() {
String input = "(* 2 3 5 1)";
assertSExpressionsMatch(new LispNumber("30"), evaluateString(input));
}
@Test
public void testMultiplyWithManyNumbers_NegativeResult() {
public void multiplyWithManyNumbers_NegativeResult() {
String input = "(* 3 (- 2) 10 2)";
assertSExpressionsMatch(new LispNumber("-120"), evaluateString(input));
}
@Test(expected = BadArgumentTypeException.class)
public void testMultiplyWithNonNumber() {
public void multiplyWithNonNumber() {
evaluateString("(* 'a 'b)");
}

View File

@ -10,42 +10,42 @@ import sexpression.LispNumber;
public class PLUSTester {
@Test
public void testPlusWithNoArguments() {
public void plusWithNoArguments() {
String input = "(+)";
assertSExpressionsMatch(new LispNumber("0"), evaluateString(input));
}
@Test
public void testPlusWithOneNumber() {
public void plusWithOneNumber() {
String input = "(+ 27)";
assertSExpressionsMatch(new LispNumber("27"), evaluateString(input));
}
@Test
public void testPlusWithTwoNumbers() {
public void plusWithTwoNumbers() {
String input = "(+ 5 3)";
assertSExpressionsMatch(new LispNumber("8"), evaluateString(input));
}
@Test
public void testPlusWithManyNumbers_PositiveResult() {
public void plusWithManyNumbers_PositiveResult() {
String input = "(+ 200 100 10 5)";
assertSExpressionsMatch(new LispNumber("315"), evaluateString(input));
}
@Test
public void testPlusWithManyNumbers_NegativeResult() {
public void plusWithManyNumbers_NegativeResult() {
String input = "(+ 100 (- 200) 20 5)";
assertSExpressionsMatch(new LispNumber("-75"), evaluateString(input));
}
@Test(expected = BadArgumentTypeException.class)
public void testPlusWithNonNumber() {
public void plusWithNonNumber() {
evaluateString("(+ 'a 'b)");
}

View File

@ -1,6 +1,9 @@
package function.builtin.special;
import static org.junit.Assert.*;
import static sexpression.LispNumber.ONE;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import static testutil.TestUtilities.*;
import org.junit.Test;
@ -26,21 +29,19 @@ public class LAMBDATester {
@Test
public void lambdaExpressionIsLambdaExpression() {
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"),
new Cons(Nil.getInstance(), new Cons(Nil.getInstance(), Nil.getInstance())));
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), new Cons(NIL, new Cons(NIL, NIL)));
assertTrue(LAMBDA.isLambdaExpression(lambdaExpression));
}
@Test
public void somethingElseIsNotLambdaExpression() {
assertFalse(LAMBDA.isLambdaExpression(Symbol.T));
assertFalse(LAMBDA.isLambdaExpression(T));
}
@Test
public void createLambdaExpression() {
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"),
new Cons(Nil.getInstance(), new Cons(Nil.getInstance(), Nil.getInstance())));
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), new Cons(NIL, new Cons(NIL, NIL)));
assertSExpressionsMatch(parseString("(:LAMBDA () ())"),
LAMBDA.createFunction(lambdaExpression).getLambdaExpression());
@ -62,14 +63,14 @@ public class LAMBDATester {
@Test(expected = DottedArgumentListException.class)
public void createFunctionWithDottedArgumentList() {
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), new Cons(Nil.getInstance(), LispNumber.ONE));
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), new Cons(NIL, ONE));
LAMBDA.createFunction(lambdaExpression);
}
@Test(expected = BadArgumentTypeException.class)
public void createFunctionWithNonList() {
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), LispNumber.ONE);
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), ONE);
LAMBDA.createFunction(lambdaExpression);
}

View File

@ -1,12 +1,13 @@
package function.builtin.special;
import static sexpression.Nil.NIL;
import static testutil.TestUtilities.*;
import org.junit.*;
import function.ArgumentValidator.*;
import function.builtin.EVAL.UndefinedSymbolException;
import sexpression.*;
import sexpression.LispNumber;
import table.ExecutionContext;
public class LETTester {
@ -38,7 +39,7 @@ public class LETTester {
public void emptyLet_ReturnsNil() {
String input = "(let ())";
assertSExpressionsMatch(Nil.getInstance(), evaluateString(input));
assertSExpressionsMatch(NIL, evaluateString(input));
}
@Test

View File

@ -29,7 +29,7 @@ public class LispParserTester {
}
@Test
public void testEofMethod_ReturnsTrueWithNoInput() {
public void eofMethod_ReturnsTrueWithNoInput() {
String input = "";
LispParser parser = createLispParser(input);
@ -37,7 +37,7 @@ public class LispParserTester {
}
@Test
public void testEofMethod_ReturnsFalseWithSomeInput() {
public void eofMethod_ReturnsFalseWithSomeInput() {
String input = "abc";
LispParser parser = createLispParser(input);
@ -45,7 +45,7 @@ public class LispParserTester {
}
@Test
public void testEofMethod_ReturnsTrueAfterSomeInput() {
public void eofMethod_ReturnsTrueAfterSomeInput() {
String input = "(yyz 9 9 9)";
LispParser parser = createLispParser(input);
@ -54,7 +54,7 @@ public class LispParserTester {
}
@Test
public void testEofMethod_ReturnsFalseAfterMultipleExpressions() {
public void eofMethod_ReturnsFalseAfterMultipleExpressions() {
String input = "()()()";
LispParser parser = createLispParser(input);
@ -66,7 +66,7 @@ public class LispParserTester {
}
@Test
public void testEofMethod_ReturnsTrueAfterMultipleExpressions() {
public void eofMethod_ReturnsTrueAfterMultipleExpressions() {
String input = "()()()";
LispParser parser = createLispParser(input);

View File

@ -2,6 +2,7 @@ package sexpression;
import static error.ErrorManager.Severity.ERROR;
import static org.junit.Assert.*;
import static sexpression.Nil.NIL;
import java.math.BigInteger;
@ -20,59 +21,59 @@ public class SExpressionTester {
public void setUp() throws Exception {}
@Test
public void testNilToString() {
public void nilToString() {
String input = "NIL";
assertSExpressionMatchesString(input, Nil.getInstance());
assertSExpressionMatchesString(input, NIL);
}
@Test
public void testNumberToString() {
public void numberToString() {
String input = "12";
assertSExpressionMatchesString(input, new LispNumber(input));
}
@Test
public void testNumberValueToString() {
public void numberValueToString() {
String expected = "12";
assertSExpressionMatchesString(expected, new LispNumber("12"));
}
@Test
public void testStringToString() {
public void stringToString() {
String input = "\"hi\"";
assertSExpressionMatchesString(input, new LispString(input));
}
@Test
public void testSymbolToString() {
public void symbolToString() {
String input = "symbol";
assertSExpressionMatchesString(input.toUpperCase(), new Symbol(input));
}
@Test
public void testSimpleConsToString() {
public void simpleConsToString() {
String expected = "(1)";
Cons cons = new Cons(new LispNumber("1"), Nil.getInstance());
Cons cons = new Cons(new LispNumber("1"), NIL);
assertSExpressionMatchesString(expected, cons);
}
@Test
public void testComplexConsToString() {
public void complexConsToString() {
String expected = "(1 A \"string\")";
Cons list = new Cons(new LispNumber("1"),
new Cons(new Symbol("a"), new Cons(new LispString("\"string\""), Nil.getInstance())));
new Cons(new Symbol("a"), new Cons(new LispString("\"string\""), NIL)));
assertSExpressionMatchesString(expected, list);
}
@Test
public void testImproperListToString() {
public void improperListToString() {
String expected = "(A . B)";
Cons list = new Cons(new Symbol("A"), new Symbol("B"));
@ -80,58 +81,58 @@ public class SExpressionTester {
}
@Test
public void testLambdaExpressionToString() {
public void lambdaExpressionToString() {
String expected = "(LAMBDA)";
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), Nil.getInstance()), null);
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), NIL), null);
assertSExpressionMatchesString(expected, lambda);
}
@Test
public void testLambdaExpressionGetLambdaExpression() {
public void lambdaExpressionGetLambdaExpression() {
String expected = "(LAMBDA)";
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), Nil.getInstance()), null);
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), NIL), null);
assertSExpressionMatchesString(expected, lambda.getLambdaExpression());
}
@Test
public void testLambdaExpressionGetFunction() {
public void lambdaExpressionGetFunction() {
String expected = "(LAMBDA)";
UserDefinedFunction function = new UserDefinedFunction(expected, Nil.getInstance(), Nil.getInstance());
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), Nil.getInstance()), function);
UserDefinedFunction function = new UserDefinedFunction(expected, NIL, NIL);
LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), NIL), function);
assertEquals(function, lambda.getFunction());
}
@Test
public void testFirstOfNilIsNil() {
assertEquals(Nil.getInstance(), Nil.getInstance().getFirst());
public void firstOfNilIsNil() {
assertEquals(NIL, NIL.getFirst());
}
@Test
public void testRestOfNilIsNil() {
assertEquals(Nil.getInstance(), Nil.getInstance().getRest());
public void restOfNilIsNil() {
assertEquals(NIL, NIL.getRest());
}
@Test
public void afterSettingFirstOfNil_ShouldStillBeNil() {
Cons nil = Nil.getInstance();
Cons nil = NIL;
nil.setFirst(new LispNumber("2"));
assertEquals(Nil.getInstance(), nil.getFirst());
assertEquals(NIL, nil.getFirst());
}
@Test
public void afterSettingRestOfNil_ShouldStillBeNil() {
Cons nil = Nil.getInstance();
Cons nil = NIL;
nil.setRest(new LispNumber("2"));
assertEquals(Nil.getInstance(), nil.getRest());
assertEquals(NIL, nil.getRest());
}
@Test
public void testNumberValue() {
public void numberValue() {
BigInteger value = new BigInteger("12");
LispNumber number = new LispNumber(value.toString());
@ -139,12 +140,12 @@ public class SExpressionTester {
}
@Test(expected = InvalidNumberException.class)
public void testInvalidNumberText_ThrowsException() {
public void invalidNumberText_ThrowsException() {
new LispNumber("a");
}
@Test
public void testInvalidNumberException_HasCorrectSeverity() {
public void invalidNumberException_HasCorrectSeverity() {
try {
new LispNumber("a");
} catch (InvalidNumberException e) {
@ -153,7 +154,7 @@ public class SExpressionTester {
}
@Test
public void testInvalidNumberException_HasMessageText() {
public void invalidNumberException_HasMessageText() {
try {
new LispNumber("a");
} catch (InvalidNumberException e) {
@ -165,7 +166,7 @@ public class SExpressionTester {
}
@Test
public void testLispNumberConstants() {
public void lispNumberConstants() {
assertEquals(BigInteger.ZERO, LispNumber.ZERO.getValue());
assertEquals(BigInteger.ONE, LispNumber.ONE.getValue());
}

View File

@ -1,11 +1,11 @@
package table;
import static org.junit.Assert.*;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import org.junit.*;
import sexpression.*;
public class ExecutionContextTester {
private ExecutionContext executionContext;
@ -45,40 +45,40 @@ public class ExecutionContextTester {
@Test
public void lookupVariable() {
executionContext.getScope().put("test", Symbol.T);
executionContext.getScope().put("test", T);
assertEquals(Symbol.T, executionContext.lookupSymbolValue("test"));
assertEquals(T, executionContext.lookupSymbolValue("test"));
}
@Test
public void lookupLocalVariable() {
SymbolTable scope = new SymbolTable(executionContext.getScope());
scope.put("local", Symbol.T);
scope.put("local", T);
executionContext.setScope(scope);
assertEquals(Symbol.T, executionContext.lookupSymbolValue("local"));
assertEquals(T, executionContext.lookupSymbolValue("local"));
}
@Test
public void lookupGlobalVariable() {
SymbolTable scope = new SymbolTable(executionContext.getScope());
executionContext.getScope().put("global", Symbol.T);
executionContext.getScope().put("global", T);
executionContext.setScope(scope);
assertEquals(Symbol.T, executionContext.lookupSymbolValue("global"));
assertEquals(T, executionContext.lookupSymbolValue("global"));
}
@Test
public void lookupShadowedVariable() {
SymbolTable scope = new SymbolTable(executionContext.getScope());
scope.put("shadowed", Nil.getInstance());
executionContext.getScope().put("shadowed", Symbol.T);
scope.put("shadowed", NIL);
executionContext.getScope().put("shadowed", T);
executionContext.setScope(scope);
assertEquals(Nil.getInstance(), executionContext.lookupSymbolValue("shadowed"));
assertEquals(NIL, executionContext.lookupSymbolValue("shadowed"));
}
}

View File

@ -1,6 +1,8 @@
package table;
import static org.junit.Assert.*;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import java.util.*;
@ -18,7 +20,7 @@ public class FunctionTableTester {
@Override
public SExpression call(Cons argList) {
return Nil.getInstance();
return NIL;
}
}
@ -31,15 +33,15 @@ public class FunctionTableTester {
@Override
public SExpression call(Cons argList) {
return Nil.getInstance();
return NIL;
}
}
public static class NamelessFunction extends LispFunction {
public static class UglyFunction extends LispFunction {
@Override
public SExpression call(Cons argList) {
return Nil.getInstance();
return NIL;
}
}
@ -48,7 +50,7 @@ public class FunctionTableTester {
@Override
public SExpression call(Cons argList) {
return Symbol.T;
return T;
}
};
}
@ -140,12 +142,12 @@ public class FunctionTableTester {
@Test
public void namelessBuiltIn_DoesNotCauseNPE() {
Set<Class<? extends LispFunction>> namelessBuiltins = new HashSet<>();
namelessBuiltins.add(NamelessFunction.class);
namelessBuiltins.add(UglyFunction.class);
FunctionTable.reset(namelessBuiltins);
assertFalse(FunctionTable.isAlreadyDefined("NAMELESS"));
assertNull(FunctionTable.lookupFunction("NAMELESS"));
assertFalse(FunctionTable.isAlreadyDefined("UGLY"));
assertNull(FunctionTable.lookupFunction("UGLY"));
}
}

View File

@ -1,11 +1,11 @@
package table;
import static org.junit.Assert.*;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import org.junit.*;
import sexpression.*;
public class SymbolTableTester {
private SymbolTable symbolTable;
@ -22,24 +22,24 @@ public class SymbolTableTester {
@Test
public void lookupSymbolInTable() {
symbolTable.put("symbol", Symbol.T);
symbolTable.put("symbol", T);
assertTrue(symbolTable.contains("symbol"));
}
@Test
public void retrieveSymbolValue() {
symbolTable.put("symbol", Symbol.T);
symbolTable.put("symbol", T);
assertEquals(Symbol.T, symbolTable.get("symbol"));
assertEquals(T, symbolTable.get("symbol"));
}
@Test
public void redefineSymbolValue() {
symbolTable.put("symbol", Symbol.T);
symbolTable.put("symbol", Nil.getInstance());
symbolTable.put("symbol", T);
symbolTable.put("symbol", NIL);
assertEquals(Nil.getInstance(), symbolTable.get("symbol"));
assertEquals(NIL, symbolTable.get("symbol"));
}
@Test
@ -51,11 +51,11 @@ public class SymbolTableTester {
@Test
public void lookupSymbolInParentTable() {
symbolTable.put("symbol", Symbol.T);
symbolTable.put("symbol", T);
SymbolTable childTable = new SymbolTable(symbolTable);
SymbolTable parentTable = childTable.getParent();
assertEquals(Symbol.T, parentTable.get("symbol"));
assertEquals(T, parentTable.get("symbol"));
}
}

View File

@ -1,8 +1,10 @@
package testutil;
import static org.junit.Assert.*;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import sexpression.*;
import sexpression.SExpression;
public final class TypeAssertions {
@ -18,7 +20,7 @@ public final class TypeAssertions {
}
public static void assertNil(SExpression sExpression) {
assertEquals(Nil.getInstance(), sExpression);
assertEquals(NIL, sExpression);
assertTrue(sExpression.isAtom());
assertFalse(sExpression.isCons());
@ -65,7 +67,7 @@ public final class TypeAssertions {
}
public static void assertT(SExpression sExpression) {
assertEquals(Symbol.T, sExpression);
assertEquals(T, sExpression);
}
}

View File

@ -26,65 +26,65 @@ public class TokenFactoryTester {
}
@Test
public void testEofTokenCreation() {
public void eofTokenCreation() {
assertTrue(tokenFactory.createEofToken(testPosition) instanceof Eof);
}
@Test
public void testLeftParenthesisCreation() {
public void leftParenthesisCreation() {
String text = "(";
assertTrue(createToken(text) instanceof LeftParenthesis);
}
@Test
public void testRightParenthesisCreation() {
public void rightParenthesisCreation() {
String text = ")";
assertTrue(createToken(text) instanceof RightParenthesis);
}
@Test
public void testQuoteMarkCreation() {
public void quoteMarkCreation() {
String text = "'";
assertTrue(createToken(text) instanceof QuoteMark);
}
@Test
public void testNumberCreation() {
public void numberCreation() {
String text = "987";
assertTrue(createToken(text) instanceof Number);
}
@Test
public void testPrefixedNumberCreation() {
public void prefixedNumberCreation() {
String text = "-987";
assertTrue(createToken(text) instanceof Number);
}
@Test
public void testIdentifierCreation() {
public void identifierCreation() {
String text = "identifier";
assertTrue(createToken(text) instanceof Identifier);
}
@Test
public void testPrefixedIdentifierCreation() {
public void prefixedIdentifierCreation() {
String text = "-identifier";
assertTrue(createToken(text) instanceof Identifier);
}
@Test
public void testStringCreation() {
public void stringCreation() {
String text = "\"string\"";
assertTrue(createToken(text) instanceof QuotedString);
}
@Test(expected = EmptyTokenTextException.class)
public void testEmptyString_ThrowsException() {
public void emptyString_ThrowsException() {
createToken("");
}
@Test
public void EmptyTokenTextException_ContainsCorrectSeverity() {
public void emptyTokenTextException_ContainsCorrectSeverity() {
try {
createToken("");
} catch (EmptyTokenTextException e) {
@ -93,7 +93,7 @@ public class TokenFactoryTester {
}
@Test
public void EmptyTokenTextException_ContainsMessage() {
public void emptyTokenTextException_ContainsMessage() {
try {
createToken("");
} catch (EmptyTokenTextException e) {
@ -104,7 +104,7 @@ public class TokenFactoryTester {
}
@Test(expected = BadCharacterException.class)
public void testBadCharacter_ThrowsException() {
public void badCharacter_ThrowsException() {
createToken("[abc]");
}

View File

@ -9,7 +9,7 @@ import org.junit.Test;
public class CharactersTester {
@Test
public void testConstructorIsPrivate() throws Exception {
public void constructorIsPrivate() throws Exception {
Constructor<Characters> constructor = Characters.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));