67 lines
2.5 KiB
Java
67 lines
2.5 KiB
Java
package function;
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import org.junit.Test;
|
|
|
|
import function.ArgumentValidator.*;
|
|
import sexpression.*;
|
|
|
|
public class UserDefinedFunctionTester {
|
|
|
|
private static final String FUNCTION_NAME = "TEST";
|
|
|
|
private UserDefinedFunction createNoArgumentFunctionThatReturnsNil() {
|
|
return new UserDefinedFunction(FUNCTION_NAME, Nil.getUniqueInstance(),
|
|
new Cons(Nil.getUniqueInstance(), Nil.getUniqueInstance()));
|
|
}
|
|
|
|
private UserDefinedFunction createOneArgumentFunctionThatReturnsArgument() {
|
|
return new UserDefinedFunction(FUNCTION_NAME, new Cons(new Symbol("X"), Nil.getUniqueInstance()),
|
|
new Cons(new Symbol("X"), Nil.getUniqueInstance()));
|
|
}
|
|
|
|
@Test
|
|
public void testNilFunctionCall() {
|
|
UserDefinedFunction function = createNoArgumentFunctionThatReturnsNil();
|
|
|
|
assertEquals(Nil.getUniqueInstance(), function.call(Nil.getUniqueInstance()));
|
|
}
|
|
|
|
@Test
|
|
public void testNilFunctionToString() {
|
|
UserDefinedFunction function = createNoArgumentFunctionThatReturnsNil();
|
|
Cons expected = new Cons(new Symbol(FUNCTION_NAME),
|
|
new Cons(Nil.getUniqueInstance(),
|
|
new Cons(Nil.getUniqueInstance(), Nil.getUniqueInstance())));
|
|
|
|
assertEquals(expected.toString(), function.getLambdaExpression().toString());
|
|
}
|
|
|
|
@Test
|
|
public void oneArgumentFunction_ReturnsCorrectValue() {
|
|
UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument();
|
|
SExpression argument = new LispNumber(23);
|
|
Cons argumentList = new Cons(argument, Nil.getUniqueInstance());
|
|
|
|
assertEquals(argument.toString(), function.call(argumentList).toString());
|
|
}
|
|
|
|
@Test(expected = TooManyArgumentsException.class)
|
|
public void oneArgumentFunction_ThrowsExceptionWithTooManyArguments() {
|
|
UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument();
|
|
SExpression argument = new LispNumber(23);
|
|
Cons argumentList = new Cons(argument, new Cons(argument, Nil.getUniqueInstance()));
|
|
|
|
function.call(argumentList);
|
|
}
|
|
|
|
@Test(expected = TooFewArgumentsException.class)
|
|
public void oneArgumentFunction_ThrowsExceptionWithTooFewArguments() {
|
|
UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument();
|
|
|
|
function.call(Nil.getUniqueInstance());
|
|
}
|
|
|
|
}
|