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 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); } @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) ())"); } @Test(expected = TooFewArgumentsException.class) public void testLambdaWithTooFewArguments() { evaluateString("(lambda ())"); } @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)"); } }