2017-01-26 12:30:38 -05:00
|
|
|
package function.builtin.special;
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertNull;
|
2017-11-12 09:42:25 -05:00
|
|
|
import static testutil.TestUtilities.assertSExpressionsMatch;
|
|
|
|
import static testutil.TestUtilities.evaluateString;
|
2017-01-26 12:30:38 -05:00
|
|
|
|
2017-07-19 15:23:15 -04:00
|
|
|
import org.junit.Test;
|
2017-01-26 12:30:38 -05:00
|
|
|
|
2017-11-12 09:42:25 -05:00
|
|
|
import function.ArgumentValidator.BadArgumentTypeException;
|
|
|
|
import function.ArgumentValidator.TooFewArgumentsException;
|
|
|
|
import function.ArgumentValidator.TooManyArgumentsException;
|
2017-01-26 12:30:38 -05:00
|
|
|
import function.builtin.EVAL.UndefinedSymbolException;
|
|
|
|
import sexpression.LispNumber;
|
2017-07-19 15:23:15 -04:00
|
|
|
import table.SymbolTable;
|
|
|
|
import testutil.SymbolAndFunctionCleaner;
|
2017-01-26 12:30:38 -05:00
|
|
|
|
2017-07-19 15:23:15 -04:00
|
|
|
public class SETQTest extends SymbolAndFunctionCleaner {
|
2017-02-05 16:00:56 -05:00
|
|
|
|
2017-02-28 15:01:05 -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() {
|
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);
|
2017-01-27 10:51:25 -05:00
|
|
|
assertSExpressionsMatch(new LispNumber("94"), evaluateString("a"));
|
2017-01-26 12:30:38 -05:00
|
|
|
}
|
|
|
|
|
2017-02-28 15:01:05 -05:00
|
|
|
@Test
|
2017-03-07 16:41:26 -05:00
|
|
|
public void setqLocalVariable() {
|
2017-02-28 15:01:05 -05:00
|
|
|
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)");
|
2017-02-28 15:01:05 -05:00
|
|
|
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);
|
2017-01-27 10:51:25 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|