summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser/scope_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/unit/parser/scope_spec.rb')
-rwxr-xr-xspec/unit/parser/scope_spec.rb142
1 files changed, 84 insertions, 58 deletions
diff --git a/spec/unit/parser/scope_spec.rb b/spec/unit/parser/scope_spec.rb
index 5308856ed..3c0d15cd7 100755
--- a/spec/unit/parser/scope_spec.rb
+++ b/spec/unit/parser/scope_spec.rb
@@ -42,12 +42,16 @@ describe Puppet::Parser::Scope do
end
it "should get its environment from its compiler" do
- env = stub 'environment'
+ env = Puppet::Node::Environment.new
compiler = stub 'compiler', :environment => env
scope = Puppet::Parser::Scope.new :compiler => compiler
scope.environment.should equal(env)
end
+ it "should use the default environment if none is available" do
+ Puppet::Parser::Scope.new.environment.should equal(Puppet::Node::Environment.new)
+ end
+
it "should use the resource type collection helper to find its known resource types" do
Puppet::Parser::Scope.ancestors.should include(Puppet::Resource::TypeCollectionHelper)
end
@@ -55,22 +59,18 @@ describe Puppet::Parser::Scope do
describe "when initializing" do
it "should extend itself with its environment's Functions module as well as the default" do
env = Puppet::Node::Environment.new("myenv")
+ root = Puppet::Node::Environment.root
compiler = stub 'compiler', :environment => env
- mod = Module.new
- root_mod = Module.new
- Puppet::Parser::Functions.expects(:environment_module).with(Puppet::Node::Environment.root).returns root_mod
- Puppet::Parser::Functions.expects(:environment_module).with(env).returns mod
- Puppet::Parser::Scope.new(:compiler => compiler).singleton_class.ancestors.should be_include(mod)
+ scope = Puppet::Parser::Scope.new(:compiler => compiler)
+ scope.singleton_class.ancestors.should be_include(Puppet::Parser::Functions.environment_module(env))
+ scope.singleton_class.ancestors.should be_include(Puppet::Parser::Functions.environment_module(root))
end
- it "should extend itself with the default Functions module if it has no environment" do
- mod = Module.new
- Puppet::Parser::Functions.expects(:environment_module).with(Puppet::Node::Environment.root).returns(mod)
-
- Puppet::Parser::Functions.expects(:environment_module).with(nil).returns mod
-
- Puppet::Parser::Scope.new.singleton_class.ancestors.should be_include(mod)
+ it "should extend itself with the default Functions module if its environment is the default" do
+ root = Puppet::Node::Environment.root
+ scope = Puppet::Parser::Scope.new
+ scope.singleton_class.ancestors.should be_include(Puppet::Parser::Functions.environment_module(root))
end
it "should remember if it is dynamic" do
@@ -83,29 +83,55 @@ describe Puppet::Parser::Scope do
end
describe "when looking up a variable" do
- it "should return ':undefined' for unset variables" do
- @scope.lookupvar("var").should == :undefined
+ it "should support :lookupvar and :setvar for backward compatibility" do
+ @scope.setvar("var", "yep")
+ @scope.lookupvar("var").should == "yep"
+ end
+
+ it "should return nil for unset variables" do
+ @scope["var"].should be_nil
end
it "should be able to look up values" do
- @scope.setvar("var", "yep")
- @scope.lookupvar("var").should == "yep"
+ @scope["var"] = "yep"
+ @scope["var"].should == "yep"
end
it "should be able to look up hashes" do
- @scope.setvar("var", {"a" => "b"})
- @scope.lookupvar("var").should == {"a" => "b"}
+ @scope["var"] = {"a" => "b"}
+ @scope["var"].should == {"a" => "b"}
end
it "should be able to look up variables in parent scopes" do
- @topscope.setvar("var", "parentval")
- @scope.lookupvar("var").should == "parentval"
+ @topscope["var"] = "parentval"
+ @scope["var"].should == "parentval"
end
it "should prefer its own values to parent values" do
- @topscope.setvar("var", "parentval")
- @scope.setvar("var", "childval")
- @scope.lookupvar("var").should == "childval"
+ @topscope["var"] = "parentval"
+ @scope["var"] = "childval"
+ @scope["var"].should == "childval"
+ end
+
+ it "should be able to detect when variables are set" do
+ @scope["var"] = "childval"
+ @scope.should be_include("var")
+ end
+
+ it "should be able to detect when variables are not set" do
+ @scope.should_not be_include("var")
+ end
+
+ it "should support iteration over its variables" do
+ @scope["one"] = "two"
+ @scope["three"] = "four"
+ hash = {}
+ @scope.each { |name, value| hash[name] = value }
+ hash.should == {"one" => "two", "three" => "four" }
+ end
+
+ it "should include Enumerable" do
+ @scope.singleton_class.ancestors.should be_include(Enumerable)
end
describe "and the variable is qualified" do
@@ -133,84 +159,84 @@ describe Puppet::Parser::Scope do
it "should be able to look up explicitly fully qualified variables from main" do
other_scope = create_class_scope("")
- other_scope.setvar("othervar", "otherval")
+ other_scope["othervar"] = "otherval"
- @scope.lookupvar("::othervar").should == "otherval"
+ @scope["::othervar"].should == "otherval"
end
it "should be able to look up explicitly fully qualified variables from other scopes" do
other_scope = create_class_scope("other")
- other_scope.setvar("var", "otherval")
+ other_scope["var"] = "otherval"
- @scope.lookupvar("::other::var").should == "otherval"
+ @scope["::other::var"].should == "otherval"
end
it "should be able to look up deeply qualified variables" do
other_scope = create_class_scope("other::deep::klass")
- other_scope.setvar("var", "otherval")
+ other_scope["var"] = "otherval"
- @scope.lookupvar("other::deep::klass::var").should == "otherval"
+ @scope["other::deep::klass::var"].should == "otherval"
end
- it "should return ':undefined' for qualified variables that cannot be found in other classes" do
+ it "should return nil for qualified variables that cannot be found in other classes" do
other_scope = create_class_scope("other::deep::klass")
- @scope.lookupvar("other::deep::klass::var").should == :undefined
+ @scope["other::deep::klass::var"].should be_nil
end
- it "should warn and return ':undefined' for qualified variables whose classes have not been evaluated" do
+ it "should warn and return nil for qualified variables whose classes have not been evaluated" do
klass = newclass("other::deep::klass")
@scope.expects(:warning)
- @scope.lookupvar("other::deep::klass::var").should == :undefined
+ @scope["other::deep::klass::var"].should be_nil
end
- it "should warn and return ':undefined' for qualified variables whose classes do not exist" do
+ it "should warn and return nil for qualified variables whose classes do not exist" do
@scope.expects(:warning)
- @scope.lookupvar("other::deep::klass::var").should == :undefined
+ @scope["other::deep::klass::var"].should be_nil
end
- it "should return ':undefined' when asked for a non-string qualified variable from a class that does not exist" do
+ it "should return nil when asked for a non-string qualified variable from a class that does not exist" do
@scope.stubs(:warning)
- @scope.lookupvar("other::deep::klass::var").should == :undefined
+ @scope["other::deep::klass::var"].should be_nil
end
- it "should return ':undefined' when asked for a non-string qualified variable from a class that has not been evaluated" do
+ it "should return nil when asked for a non-string qualified variable from a class that has not been evaluated" do
@scope.stubs(:warning)
klass = newclass("other::deep::klass")
- @scope.lookupvar("other::deep::klass::var").should == :undefined
+ @scope["other::deep::klass::var"].should be_nil
end
end
end
- describe "when setvar is called with append=true" do
- it "should raise error if the variable is already defined in this scope" do
+ describe "when variables are set with append=true" do
+ it "should raise an error if the variable is already defined in this scope" do
@scope.setvar("var","1", :append => false)
lambda { @scope.setvar("var","1", :append => true) }.should raise_error(Puppet::ParseError)
end
it "should lookup current variable value" do
- @scope.expects(:lookupvar).with("var").returns("2")
+ @scope.expects(:[]).with("var").returns("2")
@scope.setvar("var","1", :append => true)
end
it "should store the concatenated string '42'" do
@topscope.setvar("var","4", :append => false)
@scope.setvar("var","2", :append => true)
- @scope.lookupvar("var").should == "42"
+ @scope["var"].should == "42"
end
it "should store the concatenated array [4,2]" do
@topscope.setvar("var",[4], :append => false)
@scope.setvar("var",[2], :append => true)
- @scope.lookupvar("var").should == [4,2]
+ @scope["var"].should == [4,2]
end
it "should store the merged hash {a => b, c => d}" do
@topscope.setvar("var",{"a" => "b"}, :append => false)
@scope.setvar("var",{"c" => "d"}, :append => true)
- @scope.lookupvar("var").should == {"a" => "b", "c" => "d"}
+ @scope["var"].should == {"a" => "b", "c" => "d"}
end
it "should raise an error when appending a hash with something other than another hash" do
@@ -285,7 +311,7 @@ describe Puppet::Parser::Scope do
it "should store the variable value" do
@scope.setvar("1", :value, :ephemeral => true)
- @scope.lookupvar("1").should == :value
+ @scope["1"].should == :value
end
it "should remove the variable value when unset_ephemeral_var is called" do
@@ -294,17 +320,17 @@ describe Puppet::Parser::Scope do
@scope.unset_ephemeral_var
- @scope.lookupvar("1").should == :undefined
+ @scope["1"].should be_nil
end
it "should not remove classic variables when unset_ephemeral_var is called" do
- @scope.setvar("myvar", :value1)
+ @scope['myvar'] = :value1
@scope.setvar("1", :value2, :ephemeral => true)
@scope.stubs(:parent).returns(nil)
@scope.unset_ephemeral_var
- @scope.lookupvar("myvar").should == :value1
+ @scope["myvar"].should == :value1
end
it "should raise an error when setting it again" do
@@ -325,7 +351,7 @@ describe Puppet::Parser::Scope do
@scope.setvar("0", :earliest, :ephemeral => true)
@scope.new_ephemeral
@scope.setvar("0", :latest, :ephemeral => true)
- @scope.lookupvar("0").should == :latest
+ @scope["0"].should == :latest
end
it "should be able to report the current level" do
@@ -356,7 +382,7 @@ describe Puppet::Parser::Scope do
@scope.setvar("1", :value1, :ephemeral => true)
@scope.new_ephemeral
@scope.setvar("0", :value2, :ephemeral => true)
- @scope.lookupvar("1").should == :value1
+ @scope["1"].should == :value1
end
describe "when calling unset_ephemeral_var without a level" do
@@ -367,7 +393,7 @@ describe Puppet::Parser::Scope do
@scope.unset_ephemeral_var
- @scope.lookupvar("1").should == :undefined
+ @scope["1"].should be_nil
end
end
@@ -381,7 +407,7 @@ describe Puppet::Parser::Scope do
@scope.unset_ephemeral_var(2)
- @scope.lookupvar("1").should == :value2
+ @scope["1"].should == :value2
end
end
end
@@ -421,22 +447,22 @@ describe Puppet::Parser::Scope do
describe "when unsetting variables" do
it "should be able to unset normal variables" do
- @scope.setvar("foo", "bar")
+ @scope["foo"] = "bar"
@scope.unsetvar("foo")
- @scope.lookupvar("foo").should == :undefined
+ @scope["foo"].should be_nil
end
it "should be able to unset ephemeral variables" do
@scope.setvar("0", "bar", :ephemeral => true)
@scope.unsetvar("0")
- @scope.lookupvar("0").should == :undefined
+ @scope["0"].should be_nil
end
it "should not unset ephemeral variables in previous ephemeral scope" do
@scope.setvar("0", "bar", :ephemeral => true)
@scope.new_ephemeral
@scope.unsetvar("0")
- @scope.lookupvar("0").should == "bar"
+ @scope["0"].should == "bar"
end
end