2017-02-06 12:02:19 -05:00
|
|
|
package table;
|
|
|
|
|
2017-11-12 09:42:25 -05:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertNotEquals;
|
|
|
|
import static org.junit.Assert.assertNull;
|
2017-03-02 09:54:23 -05:00
|
|
|
import static sexpression.Nil.NIL;
|
|
|
|
import static sexpression.Symbol.T;
|
2017-02-06 12:02:19 -05:00
|
|
|
|
2017-11-12 09:42:25 -05:00
|
|
|
import org.junit.After;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
2017-02-06 12:02:19 -05:00
|
|
|
|
2017-03-15 13:37:39 -04:00
|
|
|
public class ExecutionContextTest {
|
2017-02-06 12:02:19 -05:00
|
|
|
|
|
|
|
private ExecutionContext executionContext;
|
|
|
|
|
2017-03-15 13:37:39 -04:00
|
|
|
public ExecutionContextTest() {
|
2017-02-06 12:02:19 -05:00
|
|
|
this.executionContext = ExecutionContext.getInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() {
|
|
|
|
executionContext.clearContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
|
|
|
public void tearDown() {
|
|
|
|
executionContext.clearContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void assignANewScope() {
|
|
|
|
SymbolTable scope = new SymbolTable();
|
|
|
|
executionContext.setScope(scope);
|
|
|
|
|
|
|
|
assertEquals(scope, executionContext.getScope());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void clearContext() {
|
|
|
|
SymbolTable scope = new SymbolTable();
|
|
|
|
executionContext.setScope(scope);
|
|
|
|
|
|
|
|
assertEquals(scope, executionContext.getScope());
|
|
|
|
executionContext.clearContext();
|
|
|
|
assertNotEquals(scope, executionContext.getScope());
|
|
|
|
assertNull(executionContext.getScope().getParent());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void lookupVariable() {
|
2017-03-02 09:54:23 -05:00
|
|
|
executionContext.getScope().put("test", T);
|
2017-02-06 12:02:19 -05:00
|
|
|
|
2017-03-02 09:54:23 -05:00
|
|
|
assertEquals(T, executionContext.lookupSymbolValue("test"));
|
2017-02-06 12:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void lookupLocalVariable() {
|
|
|
|
SymbolTable scope = new SymbolTable(executionContext.getScope());
|
|
|
|
|
2017-03-02 09:54:23 -05:00
|
|
|
scope.put("local", T);
|
2017-02-06 12:02:19 -05:00
|
|
|
executionContext.setScope(scope);
|
|
|
|
|
2017-03-02 09:54:23 -05:00
|
|
|
assertEquals(T, executionContext.lookupSymbolValue("local"));
|
2017-02-06 12:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void lookupGlobalVariable() {
|
|
|
|
SymbolTable scope = new SymbolTable(executionContext.getScope());
|
|
|
|
|
2017-03-02 09:54:23 -05:00
|
|
|
executionContext.getScope().put("global", T);
|
2017-02-06 12:02:19 -05:00
|
|
|
executionContext.setScope(scope);
|
|
|
|
|
2017-03-02 09:54:23 -05:00
|
|
|
assertEquals(T, executionContext.lookupSymbolValue("global"));
|
2017-02-06 12:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void lookupShadowedVariable() {
|
|
|
|
SymbolTable scope = new SymbolTable(executionContext.getScope());
|
|
|
|
|
2017-03-02 09:54:23 -05:00
|
|
|
scope.put("shadowed", NIL);
|
|
|
|
executionContext.getScope().put("shadowed", T);
|
2017-02-06 12:02:19 -05:00
|
|
|
executionContext.setScope(scope);
|
|
|
|
|
2017-03-02 09:54:23 -05:00
|
|
|
assertEquals(NIL, executionContext.lookupSymbolValue("shadowed"));
|
2017-02-06 12:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|