transcendental-lisp/test/table/FunctionTableTester.java

154 lines
4.0 KiB
Java

package table;
import static org.junit.Assert.*;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
import java.util.*;
import org.junit.*;
import error.ErrorManager;
import function.*;
import sexpression.*;
import table.FunctionTable.LispFunctionInstantiationException;
public class FunctionTableTester {
@FunctionNames({ "GOOD" })
public static class GoodFunction extends LispFunction {
@Override
public SExpression call(Cons argList) {
return NIL;
}
}
@FunctionNames({ "BAD" })
public static class BadFunction extends LispFunction {
public BadFunction() {
throw new IllegalArgumentException("bad function");
}
@Override
public SExpression call(Cons argList) {
return NIL;
}
}
public static class UglyFunction extends LispFunction {
@Override
public SExpression call(Cons argList) {
return NIL;
}
}
private LispFunction createLispFunction() {
return new LispFunction() {
@Override
public SExpression call(Cons argList) {
return T;
}
};
}
@Before
public void setUp() {
FunctionTable.reset();
}
@After
public void tearDown() {
FunctionTable.reset();
}
@Test
public void builtInFunctionIsDefined() {
assertTrue(FunctionTable.isAlreadyDefined("CONS"));
}
@Test
public void undefinedFunctionIsNotDefined() {
assertFalse(FunctionTable.isAlreadyDefined("undefined"));
}
@Test
public void lookupBuiltInFunction_ReturnsFunction() {
assertNotNull(FunctionTable.lookupFunction("CONS"));
}
@Test
public void lookupUndefinedFunction_ReturnsNull() {
assertNull(FunctionTable.lookupFunction("undefined"));
}
@Test
public void defineFunction() {
String functionName = "testFunction";
LispFunction testFunction = createLispFunction();
assertNull(FunctionTable.lookupFunction(functionName));
assertFalse(FunctionTable.isAlreadyDefined(functionName));
FunctionTable.defineFunction(functionName, testFunction);
assertTrue(FunctionTable.isAlreadyDefined(functionName));
assertEquals(testFunction, FunctionTable.lookupFunction(functionName));
}
@Test
public void resetFunctionTable() {
String functionName = "testFunction";
LispFunction testFunction = createLispFunction();
FunctionTable.defineFunction(functionName, testFunction);
FunctionTable.reset();
assertFalse(FunctionTable.isAlreadyDefined(functionName));
assertNull(FunctionTable.lookupFunction(functionName));
}
@Test
public void resetWithCustomBuitIns() {
Set<Class<? extends LispFunction>> goodBuiltIns = new HashSet<>();
goodBuiltIns.add(GoodFunction.class);
FunctionTable.reset(goodBuiltIns);
assertTrue(FunctionTable.isAlreadyDefined("GOOD"));
assertNotNull(FunctionTable.lookupFunction("GOOD"));
}
@Test(expected = LispFunctionInstantiationException.class)
public void unableToInitializeBuiltIn() {
Set<Class<? extends LispFunction>> badBuiltIns = new HashSet<>();
badBuiltIns.add(BadFunction.class);
FunctionTable.reset(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);
FunctionTable.reset(namelessBuiltins);
assertFalse(FunctionTable.isAlreadyDefined("UGLY"));
assertNull(FunctionTable.lookupFunction("UGLY"));
}
}