transcendental-lisp/test/function/builtin/special/RECURTest.java

40 lines
1.4 KiB
Java
Raw Normal View History

2017-11-16 20:53:55 -05:00
package function.builtin.special;
import static testutil.TestUtilities.assertSExpressionsMatch;
import static testutil.TestUtilities.evaluateString;
import static testutil.TestUtilities.parseString;
import org.junit.Test;
import function.ArgumentValidator.BadArgumentTypeException;
import function.builtin.special.RECUR.RecurOutsideOfFunctionException;
import testutil.SymbolAndFunctionCleaner;
public class RECURTest extends SymbolAndFunctionCleaner {
@Test(expected = RecurOutsideOfFunctionException.class)
public void recurOutsideOfFunction_ThrowsException() {
evaluateString("(recur)");
}
@Test(expected = RecurOutsideOfFunctionException.class)
public void recurOutsideOfFunction_AfterFunctionCall_ThrowsException() {
evaluateString("(defun f (n) (if (= n 0) 'ZERO n))");
evaluateString("(f 2)");
evaluateString("(recur)");
}
@Test
public void recurCallsCurrentFunction() {
evaluateString("(defun tail-recursive (n) (if (= n 0) 'PASS (recur (- n 1))))");
assertSExpressionsMatch(parseString("PASS"), evaluateString("(tail-recursive 900)"));
}
@Test(expected = BadArgumentTypeException.class)
public void recurInSpecialFunction_DoesNotEvaluateArguments() {
evaluateString("(define-special tail-recursive (n) (if (= n 0) 'PASS (recur (- n 1))))");
evaluateString("(tail-recursive 900)");
}
}