Add blocks and scope
parent
1b91ca792e
commit
cb7974f132
@ -1 +1 @@
|
|||||||
4ade8f4433feb634e5c96f97b05adfc1
|
53f18a0e7ed4ab1114f57c8bbdaf9abd
|
||||||
|
|||||||
@ -0,0 +1,84 @@
|
|||||||
|
package interpreter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDefineAndGet(t *testing.T) {
|
||||||
|
env := newEnvironment(nil)
|
||||||
|
env.define("x", 42)
|
||||||
|
|
||||||
|
value := env.get("x")
|
||||||
|
if value != 42 {
|
||||||
|
t.Errorf("expected 42, got %v", value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetUndefinedVariable(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r == nil {
|
||||||
|
t.Errorf("expected panic for undefined variable")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
env := newEnvironment(nil)
|
||||||
|
env.get("x")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAssign(t *testing.T) {
|
||||||
|
env := newEnvironment(nil)
|
||||||
|
env.define("x", 42)
|
||||||
|
env.assign("x", 43)
|
||||||
|
|
||||||
|
value := env.get("x")
|
||||||
|
if value != 43 {
|
||||||
|
t.Errorf("expected 43, got %v", value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAssignUndefinedVariable(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r == nil {
|
||||||
|
t.Errorf("expected panic for assigning undefined variable")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
env := newEnvironment(nil)
|
||||||
|
env.assign("x", 43)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnclosingEnvironmentGet(t *testing.T) {
|
||||||
|
global := newEnvironment(nil)
|
||||||
|
global.define("x", 42)
|
||||||
|
|
||||||
|
local := newEnvironment(global)
|
||||||
|
value := local.get("x")
|
||||||
|
if value != 42 {
|
||||||
|
t.Errorf("expected 42, got %v", value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnclosingEnvironmentAssign(t *testing.T) {
|
||||||
|
global := newEnvironment(nil)
|
||||||
|
global.define("x", 42)
|
||||||
|
|
||||||
|
local := newEnvironment(global)
|
||||||
|
local.assign("x", 43)
|
||||||
|
|
||||||
|
value := global.get("x")
|
||||||
|
if value != 43 {
|
||||||
|
t.Errorf("expected 43, got %v", value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnclosingEnvironmentAssignUndefined(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r == nil {
|
||||||
|
t.Errorf("expected panic for assigning undefined variable in enclosing environment")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
global := newEnvironment(nil)
|
||||||
|
local := newEnvironment(global)
|
||||||
|
local.assign("x", 43)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue