Disallow appending improper lists
This commit is contained in:
parent
ac365918b2
commit
9d13c3d892
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 () () ())");
|
||||
|
|
Loading…
Reference in New Issue