2016-12-22 10:32:48 -05:00
|
|
|
package function.builtin;
|
|
|
|
|
2017-03-02 09:54:23 -05:00
|
|
|
import static function.builtin.APPLY.apply;
|
2016-12-22 10:32:48 -05:00
|
|
|
import static testutil.TestUtilities.*;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import function.ArgumentValidator.*;
|
2017-02-09 12:09:51 -05:00
|
|
|
import function.builtin.EVAL.UndefinedFunctionException;
|
2016-12-22 10:32:48 -05:00
|
|
|
import sexpression.Cons;
|
|
|
|
|
2017-03-15 13:37:39 -04:00
|
|
|
public class APPLYTest {
|
2016-12-22 10:32:48 -05:00
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void applyWithSymbol() {
|
2016-12-22 10:32:48 -05:00
|
|
|
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
|
2017-03-02 09:54:23 -05:00
|
|
|
public void applyWithLambdaExpression() {
|
2016-12-22 10:32:48 -05:00
|
|
|
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
|
2017-03-02 09:54:23 -05:00
|
|
|
public void applyWithQuotedLambdaExpression() {
|
2016-12-22 10:32:48 -05:00
|
|
|
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
|
2017-03-02 09:54:23 -05:00
|
|
|
public void staticApplyCall() {
|
2016-12-22 10:32:48 -05:00
|
|
|
String argumentList = "(+ (25 10))";
|
|
|
|
Cons parsedArgumentList = (Cons) parseString(argumentList);
|
|
|
|
|
2017-03-02 09:54:23 -05:00
|
|
|
assertSExpressionsMatch(parseString("35"), apply(parsedArgumentList));
|
2016-12-22 10:32:48 -05:00
|
|
|
}
|
|
|
|
|
2017-02-09 12:09:51 -05:00
|
|
|
@Test(expected = UndefinedFunctionException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void applyWithUndefinedFunction() {
|
2016-12-22 10:32:48 -05:00
|
|
|
evaluateString("(apply 'f '(1 2 3))");
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:55:25 -05:00
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void applyWithNonListSecondArgument() {
|
2016-12-22 10:32:48 -05:00
|
|
|
evaluateString("(apply '+ '2)");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = TooManyArgumentsException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void applyWithTooManyArguments() {
|
2016-12-22 10:32:48 -05:00
|
|
|
evaluateString("(apply '1 '2 '3)");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = TooFewArgumentsException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void applyWithTooFewArguments() {
|
2016-12-22 10:32:48 -05:00
|
|
|
evaluateString("(apply '1)");
|
|
|
|
}
|
|
|
|
|
2016-12-25 12:49:18 -05:00
|
|
|
@Test(expected = DottedArgumentListException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void applyWithDottedArgumentList_ThrowsException() {
|
2016-12-25 12:49:18 -05:00
|
|
|
evaluateString("(apply 'apply (cons 'T 'T))");
|
|
|
|
}
|
|
|
|
|
2016-12-22 10:32:48 -05:00
|
|
|
}
|