2017-02-06 12:02:19 -05:00
|
|
|
package table;
|
|
|
|
|
|
|
|
import static org.junit.Assert.*;
|
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
|
|
|
|
|
|
|
import org.junit.*;
|
|
|
|
|
|
|
|
public class ExecutionContextTester {
|
|
|
|
|
|
|
|
private ExecutionContext executionContext;
|
|
|
|
|
|
|
|
public ExecutionContextTester() {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|