2016-12-22 10:32:48 -05:00
|
|
|
package function.builtin;
|
|
|
|
|
|
|
|
import static testutil.TestUtilities.*;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import function.ArgumentValidator.*;
|
|
|
|
import sexpression.Cons;
|
|
|
|
|
|
|
|
public class APPLYTester {
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testApply() {
|
|
|
|
String input = "(apply '+ '(1 2 3))";
|
|
|
|
|
2017-01-27 10:51:25 -05:00
|
|
|
assertSExpressionsMatch(parseString("6"), evaluateString(input));
|
2016-12-22 10:32:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testApplyWithLambdaExpression() {
|
|
|
|
String input = "(apply (lambda (x) (+ x 1)) '(25))";
|
|
|
|
|
2017-01-27 10:51:25 -05:00
|
|
|
assertSExpressionsMatch(parseString("26"), evaluateString(input));
|
2016-12-22 10:32:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testApplyWithQuotedLambdaExpression() {
|
|
|
|
String input = "(apply '(lambda (x) (+ x 1)) '(25))";
|
|
|
|
|
2017-01-27 10:51:25 -05:00
|
|
|
assertSExpressionsMatch(parseString("26"), evaluateString(input));
|
2016-12-22 10:32:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testStaticApplyCall() {
|
|
|
|
String argumentList = "(+ (25 10))";
|
|
|
|
Cons parsedArgumentList = (Cons) parseString(argumentList);
|
|
|
|
|
2017-01-27 10:51:25 -05:00
|
|
|
assertSExpressionsMatch(parseString("35"), APPLY.apply(parsedArgumentList));
|
2016-12-22 10:32:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = RuntimeException.class)
|
|
|
|
public void testApplyWithUndefinedFunction() {
|
|
|
|
evaluateString("(apply 'f '(1 2 3))");
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:55:25 -05:00
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
2016-12-22 10:32:48 -05:00
|
|
|
public void testApplyWithNonListSecondArgument() {
|
|
|
|
evaluateString("(apply '+ '2)");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = TooManyArgumentsException.class)
|
|
|
|
public void testApplyWithTooManyArguments() {
|
|
|
|
evaluateString("(apply '1 '2 '3)");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = TooFewArgumentsException.class)
|
|
|
|
public void testApplyWithTooFewArguments() {
|
|
|
|
evaluateString("(apply '1)");
|
|
|
|
}
|
|
|
|
|
2016-12-25 12:49:18 -05:00
|
|
|
@Test(expected = DottedArgumentListException.class)
|
|
|
|
public void testCondWithDottedArgumentList_ThrowsException() {
|
|
|
|
evaluateString("(apply 'apply (cons 'T 'T))");
|
|
|
|
}
|
|
|
|
|
2016-12-22 10:32:48 -05:00
|
|
|
}
|