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

112 lines
3.3 KiB
Java
Raw Normal View History

2017-01-27 14:31:41 -05:00
package function.builtin.special;
import static org.junit.Assert.*;
import static testutil.TestUtilities.*;
import org.junit.Test;
import function.ArgumentValidator.*;
import sexpression.*;
public class LAMBDATester {
@Test
public void testLambda() {
String input = "(lambda (x) x)";
assertSExpressionsMatch(parseString("(LAMBDA (X) X)"), evaluateString(input));
}
@Test
public void testLambdaWithNoBody() {
String input = "(lambda ())";
assertSExpressionsMatch(parseString("(LAMBDA ())"), evaluateString(input));
}
2017-01-27 14:31:41 -05:00
@Test
public void lambdaExpressionIsLambdaExpression() {
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"),
new Cons(Nil.getInstance(), new Cons(Nil.getInstance(), Nil.getInstance())));
assertTrue(LAMBDA.isLambdaExpression(lambdaExpression));
}
@Test
public void somethingElseIsNotLambdaExpression() {
assertFalse(LAMBDA.isLambdaExpression(Symbol.T));
}
@Test
public void testCreateLambdaExpression() {
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"),
new Cons(Nil.getInstance(), new Cons(Nil.getInstance(), Nil.getInstance())));
assertSExpressionsMatch(parseString("(:LAMBDA () ())"),
LAMBDA.createFunction(lambdaExpression).getLambdaExpression());
}
@Test(expected = DottedArgumentListException.class)
public void testLambdaWithDottedArgumentList() {
String input = "(apply 'lambda (cons '(x) 1))";
evaluateString(input);
}
@Test(expected = DottedArgumentListException.class)
public void testLambdaWithDottedLambdaList() {
String input = "(funcall 'lambda (cons 'a 'b) ())";
evaluateString(input);
}
2017-01-27 14:31:41 -05:00
@Test(expected = DottedArgumentListException.class)
public void testCreateFunctionWithDottedArgumentList() {
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), new Cons(Nil.getInstance(), LispNumber.ONE));
LAMBDA.createFunction(lambdaExpression);
}
@Test(expected = BadArgumentTypeException.class)
public void testCreateFunctionWithNonList() {
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), LispNumber.ONE);
LAMBDA.createFunction(lambdaExpression);
}
@Test(expected = BadArgumentTypeException.class)
public void testLambdaWithNonSymbolParameter() {
evaluateString("(lambda (1) ())");
}
2017-01-27 14:31:41 -05:00
@Test(expected = TooFewArgumentsException.class)
public void testLambdaWithTooFewArguments() {
evaluateString("(lambda)");
2017-01-27 14:31:41 -05:00
}
@Test
public void anonymousLambdaCall() {
String input = "((lambda (x) x) 203)";
assertSExpressionsMatch(new LispNumber("203"), evaluateString(input));
}
@Test
public void anonymousLambdaCallWithMultipleArguments() {
String input = "((lambda (x y) (+ x y)) 203 2)";
assertSExpressionsMatch(new LispNumber("205"), evaluateString(input));
}
@Test(expected = TooFewArgumentsException.class)
public void anonymousLambdaCallWithTooFewArguments() {
evaluateString("((lambda (x) x))");
}
@Test(expected = TooManyArgumentsException.class)
public void anonymousLambdaCallWithTooManyArguments() {
evaluateString("((lambda (x y) x) 1 2 3)");
}
2017-01-27 14:31:41 -05:00
}