diff --git a/src/function/builtin/LISTP.java b/src/function/builtin/LISTP.java index 4519ba1..f685785 100644 --- a/src/function/builtin/LISTP.java +++ b/src/function/builtin/LISTP.java @@ -1,32 +1,21 @@ package function.builtin; -import function.LispFunction; +import function.*; import sexpression.*; -/** - * LISTP represents the LISTP function in Lisp. - */ public class LISTP extends LispFunction { - // The number of arguments that LISTP takes. - private static final int NUM_ARGS = 1; + private ArgumentValidator argumentValidator; - public SExpression call(Cons argList) { - // retrieve the number of arguments passed to LISTP - int argListLength = LENGTH.getLength(argList); + public LISTP() { + this.argumentValidator = new ArgumentValidator("LISTP"); + this.argumentValidator.setExactNumberOfArguments(1); + } - // make sure we have received the proper number of arguments - if (argListLength != NUM_ARGS) { - Cons originalSExpr = new Cons(new Symbol("LISTP"), argList); - String errMsg = "too " + ((argListLength > NUM_ARGS) ? "many" : "few") + " arguments given to LISTP: " - + originalSExpr; + public SExpression call(Cons argumentList) { + argumentValidator.validate(argumentList); - throw new RuntimeException(errMsg); - } - - SExpression arg = argList.getCar(); - - return (arg.listp() ? Symbol.T : Nil.getInstance()); + return argumentList.getCar().listp() ? Symbol.T : Nil.getInstance(); } } diff --git a/test/function/builtin/LISTPTester.java b/test/function/builtin/LISTPTester.java new file mode 100644 index 0000000..48bab95 --- /dev/null +++ b/test/function/builtin/LISTPTester.java @@ -0,0 +1,39 @@ +package function.builtin; + +import static testutil.TestUtilities.*; + +import org.junit.Test; + +import function.ArgumentValidator.*; + +public class LISTPTester { + + @Test + public void testListpWithList_ReturnsT() { + String input = "(listp '(1))"; + + assertSExpressionsMatch(evaluateString(input), parseString("T")); + } + + @Test + public void testListWithNonList_ReturnsNil() { + String input = "(listp 1)"; + + assertSExpressionsMatch(evaluateString(input), parseString("NIL")); + } + + @Test(expected = TooFewArgumentsException.class) + public void testListpWithTooFewArguments() { + String input = "(listp)"; + + assertSExpressionsMatch(evaluateString(input), parseString("NIL")); + } + + @Test(expected = TooManyArgumentsException.class) + public void testListpWithTooManyArguments() { + String input = "(listp '() '())"; + + assertSExpressionsMatch(evaluateString(input), parseString("NIL")); + } + +}