2016-12-20 12:03:37 -05:00
|
|
|
package function;
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
2017-02-06 13:44:35 -05:00
|
|
|
import static testutil.TestUtilities.assertSExpressionsMatch;
|
2016-12-20 12:03:37 -05:00
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import function.ArgumentValidator.*;
|
|
|
|
import sexpression.*;
|
|
|
|
|
|
|
|
public class UserDefinedFunctionTester {
|
|
|
|
|
|
|
|
private static final String FUNCTION_NAME = "TEST";
|
|
|
|
|
|
|
|
private UserDefinedFunction createNoArgumentFunctionThatReturnsNil() {
|
2016-12-29 13:32:45 -05:00
|
|
|
return new UserDefinedFunction(FUNCTION_NAME, Nil.getInstance(),
|
|
|
|
new Cons(Nil.getInstance(), Nil.getInstance()));
|
2016-12-20 12:03:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private UserDefinedFunction createOneArgumentFunctionThatReturnsArgument() {
|
2016-12-29 13:32:45 -05:00
|
|
|
return new UserDefinedFunction(FUNCTION_NAME, new Cons(new Symbol("X"), Nil.getInstance()),
|
|
|
|
new Cons(new Symbol("X"), Nil.getInstance()));
|
2016-12-20 12:03:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testNilFunctionCall() {
|
|
|
|
UserDefinedFunction function = createNoArgumentFunctionThatReturnsNil();
|
|
|
|
|
2016-12-29 13:32:45 -05:00
|
|
|
assertEquals(Nil.getInstance(), function.call(Nil.getInstance()));
|
2016-12-20 12:03:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testNilFunctionToString() {
|
|
|
|
UserDefinedFunction function = createNoArgumentFunctionThatReturnsNil();
|
|
|
|
Cons expected = new Cons(new Symbol(FUNCTION_NAME),
|
2017-02-06 13:43:27 -05:00
|
|
|
new Cons(Nil.getInstance(), new Cons(Nil.getInstance(), Nil.getInstance())));
|
2016-12-20 12:03:37 -05:00
|
|
|
|
2016-12-22 10:32:48 -05:00
|
|
|
assertSExpressionsMatch(expected, function.getLambdaExpression());
|
2016-12-20 12:03:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void oneArgumentFunction_ReturnsCorrectValue() {
|
|
|
|
UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument();
|
2016-12-25 13:56:24 -05:00
|
|
|
SExpression argument = new LispNumber("23");
|
2016-12-29 13:32:45 -05:00
|
|
|
Cons argumentList = new Cons(argument, Nil.getInstance());
|
2016-12-20 12:03:37 -05:00
|
|
|
|
2016-12-22 10:32:48 -05:00
|
|
|
assertSExpressionsMatch(argument, function.call(argumentList));
|
2016-12-20 12:03:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = TooManyArgumentsException.class)
|
|
|
|
public void oneArgumentFunction_ThrowsExceptionWithTooManyArguments() {
|
|
|
|
UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument();
|
2016-12-25 13:56:24 -05:00
|
|
|
SExpression argument = new LispNumber("23");
|
2016-12-29 13:32:45 -05:00
|
|
|
Cons argumentList = new Cons(argument, new Cons(argument, Nil.getInstance()));
|
2016-12-20 12:03:37 -05:00
|
|
|
|
|
|
|
function.call(argumentList);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = TooFewArgumentsException.class)
|
|
|
|
public void oneArgumentFunction_ThrowsExceptionWithTooFewArguments() {
|
|
|
|
UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument();
|
|
|
|
|
2016-12-29 13:32:45 -05:00
|
|
|
function.call(Nil.getInstance());
|
2016-12-20 12:03:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|