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-03-03 15:06:49 -05:00
|
|
|
import static table.FunctionTable.*;
|
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
|
|
|
|
2017-03-15 13:37:39 -04:00
|
|
|
public class FunctionTableTest {
|
2017-02-06 12:02:19 -05:00
|
|
|
|
2017-02-26 16:47:06 -05:00
|
|
|
@FunctionNames({ "GOOD" })
|
|
|
|
public static class GoodFunction extends LispFunction {
|
|
|
|
|
2017-03-07 10:59:51 -05:00
|
|
|
public GoodFunction(String name) {}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@FunctionNames({ "BAD" })
|
|
|
|
public static class BadFunction 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
2017-03-03 15:06:49 -05:00
|
|
|
resetFunctionTable();
|
2017-02-06 12:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
|
|
|
public void tearDown() {
|
2017-03-03 15:06:49 -05:00
|
|
|
resetFunctionTable();
|
2017-02-06 12:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-02-26 16:47:06 -05:00
|
|
|
public void builtInFunctionIsDefined() {
|
2017-03-03 15:06:49 -05:00
|
|
|
assertTrue(isAlreadyDefined("CONS"));
|
2017-02-06 12:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void undefinedFunctionIsNotDefined() {
|
2017-03-03 15:06:49 -05:00
|
|
|
assertFalse(isAlreadyDefined("undefined"));
|
2017-02-06 12:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-02-26 16:47:06 -05:00
|
|
|
public void lookupBuiltInFunction_ReturnsFunction() {
|
2017-03-03 15:06:49 -05:00
|
|
|
assertNotNull(lookupFunction("CONS"));
|
2017-02-06 12:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void lookupUndefinedFunction_ReturnsNull() {
|
2017-03-03 15:06:49 -05:00
|
|
|
assertNull(lookupFunction("undefined"));
|
2017-02-06 12:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-03 15:06:49 -05:00
|
|
|
public void defineFunctionWorks() {
|
2017-02-06 12:02:19 -05:00
|
|
|
String functionName = "testFunction";
|
|
|
|
LispFunction testFunction = createLispFunction();
|
|
|
|
|
2017-03-03 15:06:49 -05:00
|
|
|
assertNull(lookupFunction(functionName));
|
|
|
|
assertFalse(isAlreadyDefined(functionName));
|
2017-02-06 12:02:19 -05:00
|
|
|
|
2017-03-03 15:06:49 -05:00
|
|
|
defineFunction(functionName, testFunction);
|
2017-02-06 12:02:19 -05:00
|
|
|
|
2017-03-03 15:06:49 -05:00
|
|
|
assertTrue(isAlreadyDefined(functionName));
|
|
|
|
assertEquals(testFunction, lookupFunction(functionName));
|
2017-02-06 12:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-03-03 15:06:49 -05:00
|
|
|
public void resetFunctionTableWorks() {
|
2017-02-06 12:02:19 -05:00
|
|
|
String functionName = "testFunction";
|
|
|
|
LispFunction testFunction = createLispFunction();
|
2017-03-03 15:06:49 -05:00
|
|
|
defineFunction(functionName, testFunction);
|
2017-02-26 16:47:06 -05:00
|
|
|
|
2017-03-03 15:06:49 -05:00
|
|
|
resetFunctionTable();
|
2017-02-06 12:02:19 -05:00
|
|
|
|
2017-03-03 15:06:49 -05:00
|
|
|
assertFalse(isAlreadyDefined(functionName));
|
|
|
|
assertNull(lookupFunction(functionName));
|
2017-02-06 12:02:19 -05:00
|
|
|
}
|
|
|
|
|
2017-02-26 16:47:06 -05:00
|
|
|
@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-02-26 16:47:06 -05:00
|
|
|
|
2017-03-03 15:06:49 -05:00
|
|
|
assertTrue(isAlreadyDefined("GOOD"));
|
|
|
|
assertNotNull(lookupFunction("GOOD"));
|
2017-02-26 16:47:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
2017-02-26 16:47:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
|
2017-03-03 15:06:49 -05:00
|
|
|
resetFunctionTable(namelessBuiltins);
|
2017-02-26 16:47:06 -05:00
|
|
|
|
2017-03-03 15:06:49 -05:00
|
|
|
assertFalse(isAlreadyDefined("UGLY"));
|
|
|
|
assertNull(lookupFunction("UGLY"));
|
2017-02-26 16:47:06 -05:00
|
|
|
}
|
|
|
|
|
2017-02-06 12:02:19 -05:00
|
|
|
}
|