transcendental-lisp/test/function/builtin/special/SETFTester.java

115 lines
3.2 KiB
Java
Raw Normal View History

2017-01-26 12:30:38 -05:00
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;
2017-01-30 16:12:38 -05:00
import table.*;
2017-01-26 12:30:38 -05:00
public class SETFTester {
2017-01-30 16:12:38 -05:00
private ExecutionContext executionContext;
2017-01-30 16:12:38 -05:00
public SETFTester() {
this.executionContext = ExecutionContext.getInstance();
}
2017-01-26 12:30:38 -05:00
@Before
public void setUp() {
2017-01-30 16:12:38 -05:00
executionContext.clearContext();
2017-01-26 12:30:38 -05:00
}
@After
public void tearDown() {
executionContext.clearContext();
}
2017-01-26 12:30:38 -05:00
@Test
public void setf() {
2017-01-26 12:30:38 -05:00
evaluateString("(setf a 23)");
assertSExpressionsMatch(new LispNumber("23"), evaluateString("a"));
2017-01-26 12:30:38 -05:00
}
@Test
public void setq() {
evaluateString("(setq a 23)");
assertSExpressionsMatch(new LispNumber("23"), evaluateString("a"));
}
2017-01-26 12:30:38 -05:00
@Test
public void lookupDefinedSymbol() {
evaluateString("(setf a 23)");
2017-01-30 16:12:38 -05:00
assertSExpressionsMatch(new LispNumber("23"), executionContext.lookupSymbolValue("A"));
2017-01-26 12:30:38 -05:00
}
@Test
public void lookupUndefinedSymbol() {
2017-01-30 16:12:38 -05:00
assertNull(executionContext.lookupSymbolValue("A"));
2017-01-26 12:30:38 -05:00
}
@Test
public void setfGlobalVariable() {
evaluateString("(setf a 23)");
2017-01-30 16:12:38 -05:00
SymbolTable global = executionContext.getScope();
executionContext.setScope(new SymbolTable(global));
2017-01-26 12:30:38 -05:00
evaluateString("(setf a 94)");
2017-01-30 16:12:38 -05:00
executionContext.setScope(global);
assertSExpressionsMatch(new LispNumber("94"), evaluateString("a"));
2017-01-26 12:30:38 -05:00
}
@Test
public void setfLocalVariable() {
SymbolTable global = executionContext.getScope();
SymbolTable local = new SymbolTable(global);
local.put("A", new LispNumber("99"));
executionContext.setScope(local);
evaluateString("(setf a 94)");
assertSExpressionsMatch(new LispNumber("94"), evaluateString("a"));
}
2017-01-26 12:30:38 -05:00
@Test(expected = UndefinedSymbolException.class)
public void setfLocalVariableDefined_DoesNotSetGlobal() {
2017-01-30 16:12:38 -05:00
SymbolTable global = executionContext.getScope();
2017-01-26 12:30:38 -05:00
SymbolTable local = new SymbolTable(global);
local.put("A", new LispNumber("99"));
2017-01-30 16:12:38 -05:00
executionContext.setScope(local);
2017-01-26 12:30:38 -05:00
evaluateString("(setf a 94)");
2017-01-30 16:12:38 -05:00
executionContext.setScope(global);
2017-01-26 12:30:38 -05:00
evaluateString("a");
}
@Test
public void setfLocalVariableUndefined_SetsGlobal() {
2017-01-30 16:12:38 -05:00
SymbolTable global = executionContext.getScope();
2017-01-26 12:30:38 -05:00
SymbolTable local = new SymbolTable(global);
2017-01-30 16:12:38 -05:00
executionContext.setScope(local);
2017-01-26 12:30:38 -05:00
evaluateString("(setf a 94)");
2017-01-30 16:12:38 -05:00
executionContext.setScope(global);
assertSExpressionsMatch(new LispNumber("94"), evaluateString("a"));
2017-01-26 12:30:38 -05:00
}
@Test(expected = BadArgumentTypeException.class)
public void setfWithNonSymbol() {
2017-01-26 12:30:38 -05:00
evaluateString("(setf 1 2)");
}
@Test(expected = TooFewArgumentsException.class)
public void setfWithTooFewArguments() {
2017-01-26 12:30:38 -05:00
evaluateString("(setf x)");
}
@Test(expected = TooManyArgumentsException.class)
public void setfWithTooManyArguments() {
2017-01-26 12:30:38 -05:00
evaluateString("(setf a b c)");
}
}