transcendental-lisp/test/function/UserDefinedFunctionTester.java

79 lines
2.9 KiB
Java
Raw Normal View History

package function;
import static org.junit.Assert.*;
2017-02-06 13:44:35 -05:00
import static testutil.TestUtilities.assertSExpressionsMatch;
import org.junit.Test;
import error.ErrorManager;
import function.ArgumentValidator.*;
import function.UserDefinedFunction.IllegalKeywordRestPositionException;
import sexpression.*;
public class UserDefinedFunctionTester {
private static final String FUNCTION_NAME = "TEST";
private UserDefinedFunction createNoArgumentFunctionThatReturnsNil() {
return new UserDefinedFunction(FUNCTION_NAME, Nil.getInstance(),
new Cons(Nil.getInstance(), Nil.getInstance()));
}
private UserDefinedFunction createOneArgumentFunctionThatReturnsArgument() {
return new UserDefinedFunction(FUNCTION_NAME, new Cons(new Symbol("X"), Nil.getInstance()),
new Cons(new Symbol("X"), Nil.getInstance()));
}
@Test
public void nilFunctionCall() {
UserDefinedFunction function = createNoArgumentFunctionThatReturnsNil();
assertEquals(Nil.getInstance(), function.call(Nil.getInstance()));
}
@Test
public void nilFunctionToString() {
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())));
assertSExpressionsMatch(expected, function.getLambdaExpression());
}
@Test
public void oneArgumentFunction_ReturnsCorrectValue() {
UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument();
SExpression argument = new LispNumber("23");
Cons argumentList = new Cons(argument, Nil.getInstance());
assertSExpressionsMatch(argument, function.call(argumentList));
}
@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.getInstance()));
function.call(argumentList);
}
@Test(expected = TooFewArgumentsException.class)
public void oneArgumentFunction_ThrowsExceptionWithTooFewArguments() {
UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument();
function.call(Nil.getInstance());
}
@Test
public void illegalKeywordRestPositionException_HasCorrectAttributes() {
IllegalKeywordRestPositionException e = new IllegalKeywordRestPositionException(FUNCTION_NAME,
Nil.getInstance());
assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 0);
assertEquals(ErrorManager.Severity.ERROR, e.getSeverity());
}
}