transcendental-lisp/test/table/FunctionTableTest.java

165 lines
4.3 KiB
Java
Raw Normal View History

package table;
2017-11-12 09:42:25 -05:00
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
2017-11-12 09:42:25 -05:00
import static table.FunctionTable.defineFunction;
import static table.FunctionTable.isAlreadyDefined;
import static table.FunctionTable.lookupFunction;
import static table.FunctionTable.resetFunctionTable;
2017-11-12 09:42:25 -05:00
import java.util.HashSet;
import java.util.Set;
2017-11-12 09:42:25 -05:00
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import error.ErrorManager;
2017-11-12 09:42:25 -05:00
import function.FunctionNames;
import function.LispFunction;
import sexpression.Cons;
import sexpression.SExpression;
import table.FunctionTable.LispFunctionInstantiationException;
2017-03-15 13:37:39 -04:00
public class FunctionTableTest {
@FunctionNames({ "GOOD" })
public static class GoodFunction extends LispFunction {
public GoodFunction(String name) {}
@Override
2018-01-12 21:02:29 -05:00
public SExpression call(Cons argumentList) {
return NIL;
}
}
@FunctionNames({ "BAD" })
public static class BadFunction extends LispFunction {
@Override
2018-01-12 21:02:29 -05:00
public SExpression call(Cons argumentList) {
return NIL;
}
}
public static class UglyFunction extends LispFunction {
@Override
2018-01-12 21:02:29 -05:00
public SExpression call(Cons argumentList) {
return NIL;
}
}
private LispFunction createLispFunction() {
return new LispFunction() {
@Override
2018-01-12 21:02:29 -05:00
public SExpression call(Cons argumentList) {
return T;
}
};
}
@Before
public void setUp() {
2017-03-03 15:06:49 -05:00
resetFunctionTable();
}
@After
public void tearDown() {
2017-03-03 15:06:49 -05:00
resetFunctionTable();
}
@Test
public void builtInFunctionIsDefined() {
2017-03-03 15:06:49 -05:00
assertTrue(isAlreadyDefined("CONS"));
}
@Test
public void undefinedFunctionIsNotDefined() {
2017-03-03 15:06:49 -05:00
assertFalse(isAlreadyDefined("undefined"));
}
@Test
public void lookupBuiltInFunction_ReturnsFunction() {
2017-03-03 15:06:49 -05:00
assertNotNull(lookupFunction("CONS"));
}
@Test
public void lookupUndefinedFunction_ReturnsNull() {
2017-03-03 15:06:49 -05:00
assertNull(lookupFunction("undefined"));
}
@Test
2017-03-03 15:06:49 -05:00
public void defineFunctionWorks() {
String functionName = "testFunction";
LispFunction testFunction = createLispFunction();
2017-03-03 15:06:49 -05:00
assertNull(lookupFunction(functionName));
assertFalse(isAlreadyDefined(functionName));
2017-03-03 15:06:49 -05:00
defineFunction(functionName, testFunction);
2017-03-03 15:06:49 -05:00
assertTrue(isAlreadyDefined(functionName));
assertEquals(testFunction, lookupFunction(functionName));
}
@Test
2017-03-03 15:06:49 -05:00
public void resetFunctionTableWorks() {
String functionName = "testFunction";
LispFunction testFunction = createLispFunction();
2017-03-03 15:06:49 -05:00
defineFunction(functionName, testFunction);
2017-03-03 15:06:49 -05:00
resetFunctionTable();
2017-03-03 15:06:49 -05:00
assertFalse(isAlreadyDefined(functionName));
assertNull(lookupFunction(functionName));
}
@Test
public void resetWithCustomBuitIns() {
Set<Class<? extends LispFunction>> goodBuiltIns = new HashSet<>();
goodBuiltIns.add(GoodFunction.class);
2017-03-03 15:06:49 -05:00
resetFunctionTable(goodBuiltIns);
2017-03-03 15:06:49 -05:00
assertTrue(isAlreadyDefined("GOOD"));
assertNotNull(lookupFunction("GOOD"));
}
@Test(expected = LispFunctionInstantiationException.class)
public void unableToInitializeBuiltIn() {
Set<Class<? extends LispFunction>> badBuiltIns = new HashSet<>();
badBuiltIns.add(BadFunction.class);
2017-03-03 15:06:49 -05:00
resetFunctionTable(badBuiltIns);
}
@Test
public void lispFunctionInstantiationException_HasCorrectAttributes() {
LispFunctionInstantiationException e = new LispFunctionInstantiationException("Bad");
assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 0);
assertEquals(ErrorManager.Severity.CRITICAL, e.getSeverity());
}
@Test
public void namelessBuiltIn_DoesNotCauseNPE() {
Set<Class<? extends LispFunction>> namelessBuiltins = new HashSet<>();
namelessBuiltins.add(UglyFunction.class);
2017-03-03 15:06:49 -05:00
resetFunctionTable(namelessBuiltins);
2017-03-03 15:06:49 -05:00
assertFalse(isAlreadyDefined("UGLY"));
assertNull(lookupFunction("UGLY"));
}
}