2017-02-06 12:02:19 -05:00
|
|
|
package table;
|
|
|
|
|
|
|
|
import static org.junit.Assert.*;
|
2017-03-02 09:54:23 -05:00
|
|
|
import static sexpression.Nil.NIL;
|
|
|
|
import static sexpression.Symbol.T;
|
2017-02-06 12:02:19 -05:00
|
|
|
|
2017-02-26 16:47:06 -05:00
|
|
|
import java.util.*;
|
|
|
|
|
2017-02-06 12:02:19 -05:00
|
|
|
import org.junit.*;
|
|
|
|
|
2017-02-26 16:47:06 -05:00
|
|
|
import error.ErrorManager;
|
|
|
|
import function.*;
|
2017-02-06 12:02:19 -05:00
|
|
|
import sexpression.*;
|
2017-02-26 16:47:06 -05:00
|
|
|
import table.FunctionTable.LispFunctionInstantiationException;
|
2017-02-06 12:02:19 -05:00
|
|
|
|
|
|
|
public class FunctionTableTester {
|
|
|
|
|
2017-02-26 16:47:06 -05:00
|
|
|
@FunctionNames({ "GOOD" })
|
|
|
|
public static class GoodFunction extends LispFunction {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SExpression call(Cons argList) {
|
2017-03-02 09:54:23 -05:00
|
|
|
return NIL;
|
2017-02-26 16:47:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@FunctionNames({ "BAD" })
|
|
|
|
public static class BadFunction extends LispFunction {
|
|
|
|
|
|
|
|
public BadFunction() {
|
|
|
|
throw new IllegalArgumentException("bad function");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SExpression call(Cons argList) {
|
2017-03-02 09:54:23 -05:00
|
|
|
return NIL;
|
2017-02-26 16:47:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 09:54:23 -05:00
|
|
|
public static class UglyFunction extends LispFunction {
|
2017-02-26 16:47:06 -05:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public SExpression call(Cons argList) {
|
2017-03-02 09:54:23 -05:00
|
|
|
return NIL;
|
2017-02-26 16:47:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 12:02:19 -05:00
|
|
|
private LispFunction createLispFunction() {
|
|
|
|
return new LispFunction() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SExpression call(Cons argList) {
|
2017-03-02 09:54:23 -05:00
|
|
|
return T;
|
2017-02-06 12:02:19 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() {
|
|
|
|
FunctionTable.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
|
|
|
public void tearDown() {
|
|
|
|
FunctionTable.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-02-26 16:47:06 -05:00
|
|
|
public void builtInFunctionIsDefined() {
|
2017-02-06 12:02:19 -05:00
|
|
|
assertTrue(FunctionTable.isAlreadyDefined("CONS"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void undefinedFunctionIsNotDefined() {
|
|
|
|
assertFalse(FunctionTable.isAlreadyDefined("undefined"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-02-26 16:47:06 -05:00
|
|
|
public void lookupBuiltInFunction_ReturnsFunction() {
|
2017-02-06 12:02:19 -05:00
|
|
|
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);
|
2017-02-26 16:47:06 -05:00
|
|
|
|
2017-02-06 12:02:19 -05:00
|
|
|
FunctionTable.reset();
|
|
|
|
|
|
|
|
assertFalse(FunctionTable.isAlreadyDefined(functionName));
|
|
|
|
assertNull(FunctionTable.lookupFunction(functionName));
|
|
|
|
}
|
|
|
|
|
2017-02-26 16:47:06 -05:00
|
|
|
@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<>();
|
2017-03-02 09:54:23 -05:00
|
|
|
namelessBuiltins.add(UglyFunction.class);
|
2017-02-26 16:47:06 -05:00
|
|
|
|
|
|
|
FunctionTable.reset(namelessBuiltins);
|
|
|
|
|
2017-03-02 09:54:23 -05:00
|
|
|
assertFalse(FunctionTable.isAlreadyDefined("UGLY"));
|
|
|
|
assertNull(FunctionTable.lookupFunction("UGLY"));
|
2017-02-26 16:47:06 -05:00
|
|
|
}
|
|
|
|
|
2017-02-06 12:02:19 -05:00
|
|
|
}
|