From 9d13c3d892ba67696b3193eb20eef51ef498470a Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Wed, 14 Mar 2018 21:01:39 -0400 Subject: [PATCH] Disallow appending improper lists --- src/function/builtin/cons/APPEND.java | 9 +++++++++ test/function/builtin/cons/APPENDTest.java | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/function/builtin/cons/APPEND.java b/src/function/builtin/cons/APPEND.java index 2e95e02..fd4c5a1 100644 --- a/src/function/builtin/cons/APPEND.java +++ b/src/function/builtin/cons/APPEND.java @@ -46,8 +46,10 @@ public class APPEND extends LispFunction { } private ArgumentValidator argumentValidator; + private String name; public APPEND(String name) { + this.name = name; this.argumentValidator = new ArgumentValidator(name); this.argumentValidator.setExactNumberOfArguments(2); this.argumentValidator.setEveryArgumentExpectedType(Cons.class); @@ -60,8 +62,15 @@ public class APPEND extends LispFunction { Cons rest = (Cons) argumentList.getRest(); Cons firstList = (Cons) argumentList.getFirst(); Cons secondList = (Cons) rest.getFirst(); + validateLists(firstList, secondList); return append(firstList, secondList); } + private void validateLists(Cons firstList, Cons secondList) { + ArgumentValidator properListValidatorOne = new ArgumentValidator(name + "|list-one|"); + ArgumentValidator properListValidatorTwo = new ArgumentValidator(name + "|list-two|"); + properListValidatorOne.validate(firstList); + properListValidatorTwo.validate(secondList); + } } diff --git a/test/function/builtin/cons/APPENDTest.java b/test/function/builtin/cons/APPENDTest.java index e6b3e1a..38ce2b2 100644 --- a/test/function/builtin/cons/APPENDTest.java +++ b/test/function/builtin/cons/APPENDTest.java @@ -7,6 +7,7 @@ import static testutil.TestUtilities.parseString; import org.junit.Test; import function.ArgumentValidator.BadArgumentTypeException; +import function.ArgumentValidator.DottedArgumentListException; import function.ArgumentValidator.TooFewArgumentsException; import function.ArgumentValidator.TooManyArgumentsException; import testutil.SymbolAndFunctionCleaner; @@ -51,6 +52,16 @@ public class APPENDTest extends SymbolAndFunctionCleaner { assertSExpressionsMatch(parseString("(4 5 6)"), evaluateString("y")); } + @Test(expected = DottedArgumentListException.class) + public void appendWithDottedFirstList() { + evaluateString("(append '(1 2) (cons 3 4))"); + } + + @Test(expected = DottedArgumentListException.class) + public void appendWithDottedSecondList() { + evaluateString("(append (cons 1 2) '(3 4))"); + } + @Test(expected = TooManyArgumentsException.class) public void appendWithTooManyArguments() { evaluateString("(append () () ())");