2016-12-20 12:03:37 -05:00
|
|
|
package function;
|
|
|
|
|
2017-03-01 11:11:59 -05:00
|
|
|
import static org.junit.Assert.*;
|
2017-03-02 09:54:23 -05:00
|
|
|
import static sexpression.Nil.NIL;
|
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;
|
|
|
|
|
2017-03-01 11:11:59 -05:00
|
|
|
import error.ErrorManager;
|
2016-12-20 12:03:37 -05:00
|
|
|
import function.ArgumentValidator.*;
|
2017-03-01 11:11:59 -05:00
|
|
|
import function.UserDefinedFunction.IllegalKeywordRestPositionException;
|
2016-12-20 12:03:37 -05:00
|
|
|
import sexpression.*;
|
|
|
|
|
2017-03-15 13:37:39 -04:00
|
|
|
public class UserDefinedFunctionTest {
|
2016-12-20 12:03:37 -05:00
|
|
|
|
|
|
|
private static final String FUNCTION_NAME = "TEST";
|
|
|
|
|
|
|
|
private UserDefinedFunction createNoArgumentFunctionThatReturnsNil() {
|
2017-03-02 09:54:23 -05:00
|
|
|
return new UserDefinedFunction(FUNCTION_NAME, NIL, new Cons(NIL, NIL));
|
2016-12-20 12:03:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private UserDefinedFunction createOneArgumentFunctionThatReturnsArgument() {
|
2017-03-02 09:54:23 -05:00
|
|
|
return new UserDefinedFunction(FUNCTION_NAME, new Cons(new Symbol("X"), NIL), new Cons(new Symbol("X"), NIL));
|
2016-12-20 12:03:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-01 11:11:59 -05:00
|
|
|
public void nilFunctionCall() {
|
2016-12-20 12:03:37 -05:00
|
|
|
UserDefinedFunction function = createNoArgumentFunctionThatReturnsNil();
|
|
|
|
|
2017-03-02 09:54:23 -05:00
|
|
|
assertEquals(NIL, function.call(NIL));
|
2016-12-20 12:03:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-01 11:11:59 -05:00
|
|
|
public void nilFunctionToString() {
|
2016-12-20 12:03:37 -05:00
|
|
|
UserDefinedFunction function = createNoArgumentFunctionThatReturnsNil();
|
2017-03-02 09:54:23 -05:00
|
|
|
Cons expected = new Cons(new Symbol(FUNCTION_NAME), new Cons(NIL, new Cons(NIL, NIL)));
|
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");
|
2017-03-02 09:54:23 -05:00
|
|
|
Cons argumentList = new Cons(argument, NIL);
|
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");
|
2017-03-02 09:54:23 -05:00
|
|
|
Cons argumentList = new Cons(argument, new Cons(argument, NIL));
|
2016-12-20 12:03:37 -05:00
|
|
|
|
|
|
|
function.call(argumentList);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = TooFewArgumentsException.class)
|
|
|
|
public void oneArgumentFunction_ThrowsExceptionWithTooFewArguments() {
|
|
|
|
UserDefinedFunction function = createOneArgumentFunctionThatReturnsArgument();
|
|
|
|
|
2017-03-02 09:54:23 -05:00
|
|
|
function.call(NIL);
|
2016-12-20 12:03:37 -05:00
|
|
|
}
|
|
|
|
|
2017-03-01 11:11:59 -05:00
|
|
|
@Test
|
|
|
|
public void illegalKeywordRestPositionException_HasCorrectAttributes() {
|
2017-03-02 09:54:23 -05:00
|
|
|
IllegalKeywordRestPositionException e = new IllegalKeywordRestPositionException(FUNCTION_NAME, NIL);
|
2017-03-01 11:11:59 -05:00
|
|
|
|
|
|
|
assertNotNull(e.getMessage());
|
|
|
|
assertTrue(e.getMessage().length() > 0);
|
|
|
|
assertEquals(ErrorManager.Severity.ERROR, e.getSeverity());
|
|
|
|
}
|
|
|
|
|
2016-12-20 12:03:37 -05:00
|
|
|
}
|