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

109 lines
3.0 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
2017-03-15 13:37:39 -04:00
public class SETQTest {
2017-01-30 16:12:38 -05:00
private ExecutionContext executionContext;
2017-03-15 13:37:39 -04:00
public SETQTest() {
2017-01-30 16:12:38 -05:00
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();
}
@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() {
2017-03-07 16:41:26 -05:00
evaluateString("(setq 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
2017-03-07 16:41:26 -05:00
public void setqGlobalVariable() {
evaluateString("(setq 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
2017-03-07 16:41:26 -05:00
evaluateString("(setq 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
2017-03-07 16:41:26 -05:00
public void setqLocalVariable() {
SymbolTable global = executionContext.getScope();
SymbolTable local = new SymbolTable(global);
local.put("A", new LispNumber("99"));
executionContext.setScope(local);
2017-03-07 16:41:26 -05:00
evaluateString("(setq a 94)");
assertSExpressionsMatch(new LispNumber("94"), evaluateString("a"));
}
2017-01-26 12:30:38 -05:00
@Test(expected = UndefinedSymbolException.class)
2017-03-07 16:41:26 -05:00
public void setqLocalVariableDefined_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
2017-03-07 16:41:26 -05:00
evaluateString("(setq a 94)");
2017-01-30 16:12:38 -05:00
executionContext.setScope(global);
2017-01-26 12:30:38 -05:00
evaluateString("a");
}
@Test
2017-03-07 16:41:26 -05:00
public void setqLocalVariableUndefined_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
2017-03-07 16:41:26 -05:00
evaluateString("(setq 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)
2017-03-07 16:41:26 -05:00
public void setqWithNonSymbol() {
evaluateString("(setq 1 2)");
2017-01-26 12:30:38 -05:00
}
@Test(expected = TooFewArgumentsException.class)
2017-03-07 16:41:26 -05:00
public void setqWithTooFewArguments() {
evaluateString("(setq x)");
2017-01-26 12:30:38 -05:00
}
@Test(expected = TooManyArgumentsException.class)
2017-03-07 16:41:26 -05:00
public void setqWithTooManyArguments() {
evaluateString("(setq a b c)");
2017-01-26 12:30:38 -05:00
}
}