|
|
@ -5,6 +5,8 @@ require "lox/error"
|
|
|
|
require "lox/token"
|
|
|
|
require "lox/token"
|
|
|
|
|
|
|
|
|
|
|
|
class TestEnvironment < Lox::Test
|
|
|
|
class TestEnvironment < Lox::Test
|
|
|
|
|
|
|
|
NAME_TOKEN = Lox::Token.new(:IDENTIFIER, "name", "name", 0)
|
|
|
|
|
|
|
|
|
|
|
|
def setup
|
|
|
|
def setup
|
|
|
|
@env = Lox::Environment.new
|
|
|
|
@env = Lox::Environment.new
|
|
|
|
end
|
|
|
|
end
|
|
|
@ -12,12 +14,34 @@ class TestEnvironment < Lox::Test
|
|
|
|
def test_define
|
|
|
|
def test_define
|
|
|
|
@env.define("name", "value")
|
|
|
|
@env.define("name", "value")
|
|
|
|
|
|
|
|
|
|
|
|
assert_equal "value", @env.get(Lox::Token.new(:IDENTIFIER, "name", "name", 0))
|
|
|
|
assert_equal "value", @env.get(NAME_TOKEN)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def test_get
|
|
|
|
def test_get
|
|
|
|
assert_raises Lox::RuntimeError, "Undefined variable name 'name'." do
|
|
|
|
assert_raises Lox::RuntimeError, "Undefined variable name 'name'." do
|
|
|
|
@env.get(Lox::Token.new(:IDENTIFIER, "name", "name", 0))
|
|
|
|
@env.get(NAME_TOKEN)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_enclosing_define
|
|
|
|
|
|
|
|
@env.define("name", "value")
|
|
|
|
|
|
|
|
enclosed = Lox::Environment.new(@env)
|
|
|
|
|
|
|
|
assert_equal "value", enclosed.get(NAME_TOKEN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enclosed.define("name", "foo")
|
|
|
|
|
|
|
|
assert_equal "foo", enclosed.get(NAME_TOKEN)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_enclosing_assign
|
|
|
|
|
|
|
|
@env.define("name", "foo")
|
|
|
|
|
|
|
|
enclosed = Lox::Environment.new(@env)
|
|
|
|
|
|
|
|
enclosed.assign(NAME_TOKEN, "bar")
|
|
|
|
|
|
|
|
assert_equal "bar", enclosed.get(NAME_TOKEN)
|
|
|
|
|
|
|
|
assert_equal "bar", @env.get(NAME_TOKEN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enclosed.define("name", "baz")
|
|
|
|
|
|
|
|
enclosed.assign(NAME_TOKEN, "qux")
|
|
|
|
|
|
|
|
assert_equal "qux", enclosed.get(NAME_TOKEN)
|
|
|
|
|
|
|
|
assert_equal "bar", @env.get(NAME_TOKEN)
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|