package function.builtin;

import static testutil.TestUtilities.*;

import org.junit.Test;

import function.ArgumentValidator.*;
import function.builtin.EVAL.UndefinedFunctionException;
import sexpression.Cons;

public class APPLYTester {

    @Test
    public void testApply() {
        String input = "(apply '+ '(1 2 3))";

        assertSExpressionsMatch(parseString("6"), evaluateString(input));
    }

    @Test
    public void testApplyWithLambdaExpression() {
        String input = "(apply (lambda (x) (+ x 1)) '(25))";

        assertSExpressionsMatch(parseString("26"), evaluateString(input));
    }

    @Test
    public void testApplyWithQuotedLambdaExpression() {
        String input = "(apply '(lambda (x) (+ x 1)) '(25))";

        assertSExpressionsMatch(parseString("26"), evaluateString(input));
    }

    @Test
    public void testStaticApplyCall() {
        String argumentList = "(+ (25 10))";
        Cons parsedArgumentList = (Cons) parseString(argumentList);

        assertSExpressionsMatch(parseString("35"), APPLY.apply(parsedArgumentList));
    }

    @Test(expected = UndefinedFunctionException.class)
    public void testApplyWithUndefinedFunction() {
        evaluateString("(apply 'f '(1 2 3))");
    }

    @Test(expected = BadArgumentTypeException.class)
    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)");
    }

    @Test(expected = DottedArgumentListException.class)
    public void testCondWithDottedArgumentList_ThrowsException() {
        evaluateString("(apply 'apply (cons 'T 'T))");
    }

}
