87 lines
2.4 KiB
Java
87 lines
2.4 KiB
Java
|
package function.builtin.special;
|
||
|
|
||
|
import static org.junit.Assert.assertNull;
|
||
|
import static testutil.TestUtilities.*;
|
||
|
|
||
|
import org.junit.*;
|
||
|
|
||
|
import function.ArgumentValidator.*;
|
||
|
import function.builtin.EVAL.UndefinedSymbolException;
|
||
|
import sexpression.LispNumber;
|
||
|
import table.SymbolTable;
|
||
|
|
||
|
public class SETFTester {
|
||
|
|
||
|
@Before
|
||
|
public void setUp() {
|
||
|
SETF.setSymbolTable(new SymbolTable());
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void testSetf() {
|
||
|
evaluateString("(setf a 23)");
|
||
|
assertSExpressionsMatch(evaluateString("23"), evaluateString("a"));
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void lookupDefinedSymbol() {
|
||
|
evaluateString("(setf a 23)");
|
||
|
assertSExpressionsMatch(evaluateString("23"), SETF.lookupSymbolValue("A"));
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void lookupUndefinedSymbol() {
|
||
|
assertNull(SETF.lookupSymbolValue("A"));
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void setfGlobalVariable() {
|
||
|
evaluateString("(setf a 23)");
|
||
|
SymbolTable global = SETF.getSymbolTable();
|
||
|
SETF.setSymbolTable(new SymbolTable(global));
|
||
|
|
||
|
evaluateString("(setf a 94)");
|
||
|
SETF.setSymbolTable(global);
|
||
|
assertSExpressionsMatch(evaluateString("94"), evaluateString("a"));
|
||
|
}
|
||
|
|
||
|
@Test(expected = UndefinedSymbolException.class)
|
||
|
public void setfLocalVariableDefined_DoesNotSetGlobal() {
|
||
|
SymbolTable global = SETF.getSymbolTable();
|
||
|
SymbolTable local = new SymbolTable(global);
|
||
|
local.put("A", new LispNumber("99"));
|
||
|
SETF.setSymbolTable(local);
|
||
|
|
||
|
evaluateString("(setf a 94)");
|
||
|
SETF.setSymbolTable(global);
|
||
|
evaluateString("a");
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void setfLocalVariableUndefined_SetsGlobal() {
|
||
|
SymbolTable global = SETF.getSymbolTable();
|
||
|
SymbolTable local = new SymbolTable(global);
|
||
|
SETF.setSymbolTable(local);
|
||
|
|
||
|
evaluateString("(setf a 94)");
|
||
|
SETF.setSymbolTable(global);
|
||
|
assertSExpressionsMatch(evaluateString("94"), evaluateString("a"));
|
||
|
}
|
||
|
|
||
|
@Test(expected = BadArgumentTypeException.class)
|
||
|
public void testSetfWithNonSymbol() {
|
||
|
evaluateString("(setf 1 2)");
|
||
|
}
|
||
|
|
||
|
@Test(expected = TooFewArgumentsException.class)
|
||
|
public void testSetfWithTooFewArguments() {
|
||
|
evaluateString("(setf x)");
|
||
|
}
|
||
|
|
||
|
@Test(expected = TooManyArgumentsException.class)
|
||
|
public void testSetfWithTooManyArguments() {
|
||
|
evaluateString("(setf a b c)");
|
||
|
}
|
||
|
|
||
|
}
|