2017-02-25 19:11:31 -05:00
|
|
|
package function.builtin;
|
|
|
|
|
|
|
|
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-02-25 19:11:31 -05:00
|
|
|
|
2017-07-19 15:23:15 -04:00
|
|
|
import org.junit.Test;
|
2017-02-25 19:11:31 -05:00
|
|
|
|
2017-11-12 09:42:25 -05:00
|
|
|
import function.ArgumentValidator.BadArgumentTypeException;
|
|
|
|
import function.ArgumentValidator.TooFewArgumentsException;
|
|
|
|
import function.ArgumentValidator.TooManyArgumentsException;
|
2017-02-25 19:11:31 -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-02-25 19:11:31 -05:00
|
|
|
|
2017-07-19 15:23:15 -04:00
|
|
|
public class SETTest extends SymbolAndFunctionCleaner {
|
2017-02-25 19:11:31 -05:00
|
|
|
|
|
|
|
@Test
|
2017-03-02 09:54:23 -05:00
|
|
|
public void set() {
|
2017-02-25 19:11:31 -05:00
|
|
|
evaluateString("(set 'a 23)");
|
|
|
|
assertSExpressionsMatch(new LispNumber("23"), evaluateString("a"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void lookupDefinedSymbol() {
|
|
|
|
evaluateString("(set 'a 23)");
|
|
|
|
assertSExpressionsMatch(new LispNumber("23"), executionContext.lookupSymbolValue("A"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void lookupUndefinedSymbol() {
|
|
|
|
assertNull(executionContext.lookupSymbolValue("A"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void setGlobalVariable() {
|
|
|
|
evaluateString("(set 'a 23)");
|
|
|
|
SymbolTable global = executionContext.getScope();
|
|
|
|
executionContext.setScope(new SymbolTable(global));
|
|
|
|
|
|
|
|
evaluateString("(set 'a 94)");
|
|
|
|
executionContext.setScope(global);
|
|
|
|
assertSExpressionsMatch(new LispNumber("94"), evaluateString("a"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = UndefinedSymbolException.class)
|
|
|
|
public void setLocalVariableDefined_DoesNotSetGlobal() {
|
|
|
|
SymbolTable global = executionContext.getScope();
|
|
|
|
SymbolTable local = new SymbolTable(global);
|
|
|
|
local.put("A", new LispNumber("99"));
|
|
|
|
executionContext.setScope(local);
|
|
|
|
|
|
|
|
evaluateString("(set 'a 94)");
|
|
|
|
executionContext.setScope(global);
|
|
|
|
evaluateString("a");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void setLocalVariableUndefined_SetsGlobal() {
|
|
|
|
SymbolTable global = executionContext.getScope();
|
|
|
|
SymbolTable local = new SymbolTable(global);
|
|
|
|
executionContext.setScope(local);
|
|
|
|
|
|
|
|
evaluateString("(set 'a 94)");
|
|
|
|
executionContext.setScope(global);
|
|
|
|
assertSExpressionsMatch(new LispNumber("94"), evaluateString("a"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void setWithNonSymbol() {
|
2017-02-25 19:11:31 -05:00
|
|
|
evaluateString("(set '1 2)");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = TooFewArgumentsException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void setWithTooFewArguments() {
|
2017-02-25 19:11:31 -05:00
|
|
|
evaluateString("(set 'x)");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = TooManyArgumentsException.class)
|
2017-03-02 09:54:23 -05:00
|
|
|
public void setWithTooManyArguments() {
|
2017-02-25 19:11:31 -05:00
|
|
|
evaluateString("(set 'a 'b 'c)");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|