transcendental-lisp/test/table/FunctionTableTest.java

153 lines
3.8 KiB
Java
Raw Normal View History

package table;
import static org.junit.Assert.*;
import static sexpression.Nil.NIL;
import static sexpression.Symbol.T;
2017-03-03 15:06:49 -05:00
import static table.FunctionTable.*;
import java.util.*;
import org.junit.*;
import error.ErrorManager;
import function.*;
import 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
public SExpression call(Cons argList) {
return NIL;
}
}
@FunctionNames({ "BAD" })
public static class BadFunction extends LispFunction {
@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() {
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"));
}
}