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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,7 @@
package function.builtin.math; package function.builtin.math;
import static sexpression.LispNumber.ONE;
import function.*; import function.*;
import sexpression.*; import sexpression.*;
@ -18,7 +20,7 @@ public class MULTIPLY extends LispFunction {
public LispNumber call(Cons argumentList) { public LispNumber call(Cons argumentList) {
argumentValidator.validate(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) { private LispNumber multiply(LispNumber number1, LispNumber number2) {

View File

@ -1,5 +1,7 @@
package function.builtin.math; package function.builtin.math;
import static sexpression.LispNumber.ZERO;
import function.*; import function.*;
import sexpression.*; import sexpression.*;
@ -18,7 +20,7 @@ public class PLUS extends LispFunction {
public LispNumber call(Cons argumentList) { public LispNumber call(Cons argumentList) {
argumentValidator.validate(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) { private LispNumber add(LispNumber number1, LispNumber number2) {

View File

@ -1,5 +1,8 @@
package function.builtin.predicate; package function.builtin.predicate;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import function.*; import function.*;
import sexpression.*; import sexpression.*;
@ -17,7 +20,7 @@ public class ATOM extends LispFunction {
argumentValidator.validate(argumentList); argumentValidator.validate(argumentList);
SExpression argument = argumentList.getFirst(); 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; package function.builtin.predicate;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import function.*; import function.*;
import sexpression.*; import sexpression.*;
@ -35,7 +38,7 @@ public class EQ extends LispFunction {
} }
private SExpression atomEq(SExpression firstArgument, SExpression secondArgument) { 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) { private boolean isEq(SExpression firstArgument, SExpression secondArgument) {
@ -43,7 +46,7 @@ public class EQ extends LispFunction {
} }
private SExpression listEq(SExpression firstArgument, SExpression secondArgument) { 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; package function.builtin.predicate;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import function.*; import function.*;
import sexpression.*; import sexpression.*;
@ -28,7 +31,7 @@ public class EQUAL extends LispFunction {
} }
private SExpression equal(SExpression firstArgument, SExpression secondArgument) { 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; package function.builtin.predicate;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import function.*; import function.*;
import sexpression.*; import sexpression.*;
@ -16,7 +19,7 @@ public class LISTP extends LispFunction {
public SExpression call(Cons argumentList) { public SExpression call(Cons argumentList) {
argumentValidator.validate(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; package function.builtin.predicate;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import function.*; import function.*;
import sexpression.*; import sexpression.*;
@ -16,7 +19,7 @@ public class NULL extends LispFunction {
public SExpression call(Cons argumentList) { public SExpression call(Cons argumentList) {
argumentValidator.validate(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; package function.builtin.predicate;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import function.*; import function.*;
import sexpression.*; import sexpression.*;
@ -24,7 +27,7 @@ public class NUMERIC_EQUAL extends LispFunction {
Cons remainingArguments = (Cons) argumentList.getRest(); Cons remainingArguments = (Cons) argumentList.getRest();
if (remainingArguments.isNull()) if (remainingArguments.isNull())
return Symbol.T; return T;
SExpression firstArgument = argumentList.getFirst(); SExpression firstArgument = argumentList.getFirst();
LispNumber number1 = (LispNumber) firstArgument; LispNumber number1 = (LispNumber) firstArgument;
@ -32,7 +35,7 @@ public class NUMERIC_EQUAL extends LispFunction {
LispNumber number2 = (LispNumber) secondArgument; LispNumber number2 = (LispNumber) secondArgument;
if (!isEqual(number1, number2)) if (!isEqual(number1, number2))
return Nil.getInstance(); return NIL;
return callTailRecursive(remainingArguments); return callTailRecursive(remainingArguments);
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,7 @@
package token; package token;
import static sexpression.Nil.NIL;
import java.util.function.Supplier; import java.util.function.Supplier;
import file.FilePosition; import file.FilePosition;
@ -16,7 +18,7 @@ public class QuoteMark extends Token {
Token nextToken = getNextToken.get(); Token nextToken = getNextToken.get();
SExpression argument = nextToken.parseSExpression(getNextToken); 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; package token;
import static sexpression.Nil.NIL;
import java.util.function.Supplier; import java.util.function.Supplier;
import error.LineColumnException; import error.LineColumnException;
import file.FilePosition; import file.FilePosition;
import sexpression.*; import sexpression.SExpression;
public class RightParenthesis extends Token { public class RightParenthesis extends Token {
@ -19,7 +21,7 @@ public class RightParenthesis extends Token {
@Override @Override
public SExpression parseSExpressionTail(Supplier<Token> getNextToken) { public SExpression parseSExpressionTail(Supplier<Token> getNextToken) {
return Nil.getInstance(); return NIL;
} }
public static class StartsWithRightParenthesisException extends LineColumnException { public static class StartsWithRightParenthesisException extends LineColumnException {

View File

@ -2,6 +2,8 @@ package function;
import static error.ErrorManager.Severity.ERROR; import static error.ErrorManager.Severity.ERROR;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import org.junit.*; import org.junit.*;
@ -13,10 +15,10 @@ public class ArgumentValidatorTester {
private ArgumentValidator validator; private ArgumentValidator validator;
private Cons makeArgumentListOfSize(int size) { private Cons makeArgumentListOfSize(int size) {
Cons argumentList = Nil.getInstance(); Cons argumentList = NIL;
for (int i = 0; i < size; i++) for (int i = 0; i < size; i++)
argumentList = new Cons(Nil.getInstance(), argumentList); argumentList = new Cons(NIL, argumentList);
return argumentList; return argumentList;
} }
@ -73,14 +75,14 @@ public class ArgumentValidatorTester {
@Test @Test
public void tooManyArgumentsException_HasCorrectSeverity() { public void tooManyArgumentsException_HasCorrectSeverity() {
TooManyArgumentsException e = new TooManyArgumentsException("TEST", Nil.getInstance()); TooManyArgumentsException e = new TooManyArgumentsException("TEST", NIL);
assertEquals(ERROR, e.getSeverity()); assertEquals(ERROR, e.getSeverity());
} }
@Test @Test
public void tooManyArgumentsException_HasMessageText() { public void tooManyArgumentsException_HasMessageText() {
TooManyArgumentsException e = new TooManyArgumentsException("TEST", Nil.getInstance()); TooManyArgumentsException e = new TooManyArgumentsException("TEST", NIL);
assertNotNull(e.getMessage()); assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 0); assertTrue(e.getMessage().length() > 0);
@ -88,14 +90,14 @@ public class ArgumentValidatorTester {
@Test @Test
public void tooFewArgumentsException_HasCorrectSeverity() { public void tooFewArgumentsException_HasCorrectSeverity() {
TooFewArgumentsException e = new TooFewArgumentsException("TEST", Nil.getInstance()); TooFewArgumentsException e = new TooFewArgumentsException("TEST", NIL);
assertEquals(ERROR, e.getSeverity()); assertEquals(ERROR, e.getSeverity());
} }
@Test @Test
public void tooFewArgumentsException_HasMessageText() { public void tooFewArgumentsException_HasMessageText() {
TooFewArgumentsException e = new TooFewArgumentsException("TEST", Nil.getInstance()); TooFewArgumentsException e = new TooFewArgumentsException("TEST", NIL);
assertNotNull(e.getMessage()); assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 0); assertTrue(e.getMessage().length() > 0);
@ -103,14 +105,14 @@ public class ArgumentValidatorTester {
@Test @Test
public void BadArgumentTypeException_HasCorrectSeverity() { 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()); assertEquals(ERROR, e.getSeverity());
} }
@Test @Test
public void BadArgumentTypeException_HasMessageText() { public void BadArgumentTypeException_HasMessageText() {
BadArgumentTypeException e = new BadArgumentTypeException("TEST", Nil.getInstance(), SExpression.class); BadArgumentTypeException e = new BadArgumentTypeException("TEST", NIL, SExpression.class);
assertNotNull(e.getMessage()); assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 0); assertTrue(e.getMessage().length() > 0);
@ -130,7 +132,7 @@ public class ArgumentValidatorTester {
@Test @Test
public void correctFirstAndRestArgumentTypes_DoesNotThrowException() { 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.setFirstArgumentExpectedType(Symbol.class);
validator.setTrailingArgumentExpectedType(Cons.class); validator.setTrailingArgumentExpectedType(Cons.class);
@ -139,7 +141,7 @@ public class ArgumentValidatorTester {
@Test(expected = BadArgumentTypeException.class) @Test(expected = BadArgumentTypeException.class)
public void badFirstArgumentType_ThrowsException() { 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.setFirstArgumentExpectedType(Cons.class);
validator.setTrailingArgumentExpectedType(Cons.class); validator.setTrailingArgumentExpectedType(Cons.class);
@ -148,7 +150,7 @@ public class ArgumentValidatorTester {
@Test(expected = BadArgumentTypeException.class) @Test(expected = BadArgumentTypeException.class)
public void badTrailingArgumentType_ThrowsException() { 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.setFirstArgumentExpectedType(Symbol.class);
validator.setTrailingArgumentExpectedType(Symbol.class); validator.setTrailingArgumentExpectedType(Symbol.class);
@ -157,7 +159,7 @@ public class ArgumentValidatorTester {
@Test @Test
public void expectedTypeWithNoDisplayName_DoesNotCauseNPE() { 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() {}; SExpression withoutDisplayName = new SExpression() {};
validator.setEveryArgumentExpectedType(withoutDisplayName.getClass()); validator.setEveryArgumentExpectedType(withoutDisplayName.getClass());
@ -172,28 +174,28 @@ public class ArgumentValidatorTester {
@Test(expected = DottedArgumentListException.class) @Test(expected = DottedArgumentListException.class)
public void givenDottedArgumentList_ThrowsException() { public void givenDottedArgumentList_ThrowsException() {
Cons argumentList = new Cons(Symbol.T, Symbol.T); Cons argumentList = new Cons(T, T);
validator.validate(argumentList); validator.validate(argumentList);
} }
@Test(expected = DottedArgumentListException.class) @Test(expected = DottedArgumentListException.class)
public void givenLargeDottedArgumentList_ThrowsException() { 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); validator.validate(argumentList);
} }
@Test @Test
public void DottedArgumentListException_HasCorrectSeverity() { public void DottedArgumentListException_HasCorrectSeverity() {
DottedArgumentListException e = new DottedArgumentListException("TEST", Nil.getInstance()); DottedArgumentListException e = new DottedArgumentListException("TEST", NIL);
assertEquals(ERROR, e.getSeverity()); assertEquals(ERROR, e.getSeverity());
} }
@Test @Test
public void dottedArgumentListException_HasMessageText() { public void dottedArgumentListException_HasMessageText() {
DottedArgumentListException e = new DottedArgumentListException("TEST", Nil.getInstance()); DottedArgumentListException e = new DottedArgumentListException("TEST", NIL);
assertNotNull(e.getMessage()); assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 0); assertTrue(e.getMessage().length() > 0);
@ -202,19 +204,19 @@ public class ArgumentValidatorTester {
@Test(expected = BadArgumentTypeException.class) @Test(expected = BadArgumentTypeException.class)
public void doNotAcceptNil_ThrowsExceptionOnNilArgument() { public void doNotAcceptNil_ThrowsExceptionOnNilArgument() {
validator.doNotAcceptNil(); validator.doNotAcceptNil();
validator.validate(new Cons(Symbol.T, new Cons(Nil.getInstance(), Nil.getInstance()))); validator.validate(new Cons(T, new Cons(NIL, NIL)));
} }
@Test @Test
public void doNotAcceptNil_AllowsEmptyArgumentList() { public void doNotAcceptNil_AllowsEmptyArgumentList() {
validator.doNotAcceptNil(); validator.doNotAcceptNil();
validator.validate(Nil.getInstance()); validator.validate(NIL);
} }
@Test @Test
public void doNotAcceptNil_AllowsProperList() { public void doNotAcceptNil_AllowsProperList() {
validator.doNotAcceptNil(); 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; package function;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static sexpression.Nil.NIL;
import static testutil.TestUtilities.assertSExpressionsMatch; import static testutil.TestUtilities.assertSExpressionsMatch;
import org.junit.Test; import org.junit.Test;
@ -15,27 +16,24 @@ public class UserDefinedFunctionTester {
private static final String FUNCTION_NAME = "TEST"; private static final String FUNCTION_NAME = "TEST";
private UserDefinedFunction createNoArgumentFunctionThatReturnsNil() { private UserDefinedFunction createNoArgumentFunctionThatReturnsNil() {
return new UserDefinedFunction(FUNCTION_NAME, Nil.getInstance(), return new UserDefinedFunction(FUNCTION_NAME, NIL, new Cons(NIL, NIL));
new Cons(Nil.getInstance(), Nil.getInstance()));
} }
private UserDefinedFunction createOneArgumentFunctionThatReturnsArgument() { private UserDefinedFunction createOneArgumentFunctionThatReturnsArgument() {
return new UserDefinedFunction(FUNCTION_NAME, new Cons(new Symbol("X"), Nil.getInstance()), return new UserDefinedFunction(FUNCTION_NAME, new Cons(new Symbol("X"), NIL), new Cons(new Symbol("X"), NIL));
new Cons(new Symbol("X"), Nil.getInstance()));
} }
@Test @Test
public void nilFunctionCall() { public void nilFunctionCall() {
UserDefinedFunction function = createNoArgumentFunctionThatReturnsNil(); UserDefinedFunction function = createNoArgumentFunctionThatReturnsNil();
assertEquals(Nil.getInstance(), function.call(Nil.getInstance())); assertEquals(NIL, function.call(NIL));
} }
@Test @Test
public void nilFunctionToString() { public void nilFunctionToString() {
UserDefinedFunction function = createNoArgumentFunctionThatReturnsNil(); UserDefinedFunction function = createNoArgumentFunctionThatReturnsNil();
Cons expected = new Cons(new Symbol(FUNCTION_NAME), Cons expected = new Cons(new Symbol(FUNCTION_NAME), new Cons(NIL, new Cons(NIL, NIL)));
new Cons(Nil.getInstance(), new Cons(Nil.getInstance(), Nil.getInstance())));
assertSExpressionsMatch(expected, function.getLambdaExpression()); assertSExpressionsMatch(expected, function.getLambdaExpression());
} }
@ -44,7 +42,7 @@ public class UserDefinedFunctionTester {
public void oneArgumentFunction_ReturnsCorrectValue() { public void oneArgumentFunction_ReturnsCorrectValue() {
UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument(); UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument();
SExpression argument = new LispNumber("23"); SExpression argument = new LispNumber("23");
Cons argumentList = new Cons(argument, Nil.getInstance()); Cons argumentList = new Cons(argument, NIL);
assertSExpressionsMatch(argument, function.call(argumentList)); assertSExpressionsMatch(argument, function.call(argumentList));
} }
@ -53,7 +51,7 @@ public class UserDefinedFunctionTester {
public void oneArgumentFunction_ThrowsExceptionWithTooManyArguments() { public void oneArgumentFunction_ThrowsExceptionWithTooManyArguments() {
UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument(); UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument();
SExpression argument = new LispNumber("23"); 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); function.call(argumentList);
} }
@ -62,13 +60,12 @@ public class UserDefinedFunctionTester {
public void oneArgumentFunction_ThrowsExceptionWithTooFewArguments() { public void oneArgumentFunction_ThrowsExceptionWithTooFewArguments() {
UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument(); UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument();
function.call(Nil.getInstance()); function.call(NIL);
} }
@Test @Test
public void illegalKeywordRestPositionException_HasCorrectAttributes() { public void illegalKeywordRestPositionException_HasCorrectAttributes() {
IllegalKeywordRestPositionException e = new IllegalKeywordRestPositionException(FUNCTION_NAME, IllegalKeywordRestPositionException e = new IllegalKeywordRestPositionException(FUNCTION_NAME, NIL);
Nil.getInstance());
assertNotNull(e.getMessage()); assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 0); assertTrue(e.getMessage().length() > 0);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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