diff options
Diffstat (limited to 'spec/unit')
-rw-r--r-- | spec/unit/parser/ast/asthash.rb | 64 | ||||
-rwxr-xr-x | spec/unit/parser/ast/leaf.rb | 83 | ||||
-rwxr-xr-x | spec/unit/parser/scope.rb | 10 |
3 files changed, 157 insertions, 0 deletions
diff --git a/spec/unit/parser/ast/asthash.rb b/spec/unit/parser/ast/asthash.rb new file mode 100644 index 000000000..4c700fe4b --- /dev/null +++ b/spec/unit/parser/ast/asthash.rb @@ -0,0 +1,64 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe Puppet::Parser::AST::ASTHash do + before :each do + @scope = Puppet::Parser::Scope.new() + end + + it "should have a [] accessor" do + hash = Puppet::Parser::AST::ASTHash.new(:value => {}) + hash.should respond_to(:[]) + end + + it "should have a merge functionality" do + hash = Puppet::Parser::AST::ASTHash.new(:value => {}) + hash.should respond_to(:merge) + end + + it "should be able to merge 2 AST hashes" do + hash = Puppet::Parser::AST::ASTHash.new(:value => { "a" => "b" }) + + hash.merge(Puppet::Parser::AST::ASTHash.new(:value => {"c" => "d"})) + + hash.value.should == { "a" => "b", "c" => "d" } + end + + it "should be able to merge with a ruby Hash" do + hash = Puppet::Parser::AST::ASTHash.new(:value => { "a" => "b" }) + + hash.merge({"c" => "d"}) + + hash.value.should == { "a" => "b", "c" => "d" } + end + + it "should evaluate each hash value" do + key1 = stub "key1" + value1 = stub "value1" + key2 = stub "key2" + value2 = stub "value2" + + value1.expects(:safeevaluate).with(@scope).returns("b") + value2.expects(:safeevaluate).with(@scope).returns("d") + + operator = Puppet::Parser::AST::ASTHash.new(:value => { key1 => value1, key2 => value2}) + operator.evaluate(@scope) + end + + it "should return an evaluated hash" do + key1 = stub "key1" + value1 = stub "value1", :safeevaluate => "b" + key2 = stub "key2" + value2 = stub "value2", :safeevaluate => "d" + + operator = Puppet::Parser::AST::ASTHash.new(:value => { key1 => value1, key2 => value2}) + operator.evaluate(@scope).should == { key1 => "b", key2 => "d" } + end + + it "should return a valid string with to_s" do + hash = Puppet::Parser::AST::ASTHash.new(:value => { "a" => "b", "c" => "d" }) + + hash.to_s.should == '{a => b, c => d}' + end +end diff --git a/spec/unit/parser/ast/leaf.rb b/spec/unit/parser/ast/leaf.rb index 640c25237..12ec2711e 100755 --- a/spec/unit/parser/ast/leaf.rb +++ b/spec/unit/parser/ast/leaf.rb @@ -94,6 +94,89 @@ describe Puppet::Parser::AST::Undef do end end +describe Puppet::Parser::AST::HashOrArrayAccess do + before :each do + @scope = stub 'scope' + end + + it "should evaluate the variable part if necessary" do + @scope.stubs(:lookupvar).with("a").returns(["b"]) + + variable = stub 'variable', :evaluate => "a" + access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => variable, :key => 0 ) + + variable.expects(:safeevaluate).with(@scope).returns("a") + + access.evaluate(@scope).should == "b" + end + + it "should evaluate the access key part if necessary" do + @scope.stubs(:lookupvar).with("a").returns(["b"]) + + index = stub 'index', :evaluate => 0 + access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => index ) + + index.expects(:safeevaluate).with(@scope).returns(0) + + access.evaluate(@scope).should == "b" + end + + it "should be able to return an array member" do + @scope.stubs(:lookupvar).with("a").returns(["val1", "val2", "val3"]) + + access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => 1 ) + + access.evaluate(@scope).should == "val2" + end + + it "should be able to return an hash value" do + @scope.stubs(:lookupvar).with("a").returns({ "key1" => "val1", "key2" => "val2", "key3" => "val3" }) + + access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "key2" ) + + access.evaluate(@scope).should == "val2" + end + + it "should raise an error if the variable lookup didn't return an hash or an array" do + @scope.stubs(:lookupvar).with("a").returns("I'm a string") + + access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "key2" ) + + lambda { access.evaluate(@scope) }.should raise_error + end + + it "should raise an error if the variable wasn't in the scope" do + @scope.stubs(:lookupvar).with("a").returns(nil) + + access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "key2" ) + + lambda { access.evaluate(@scope) }.should raise_error + end + + it "should return a correct string representation" do + access = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "key2" ) + access.to_s.should == '$a[key2]' + end + + it "should work with recursive hash access" do + @scope.stubs(:lookupvar).with("a").returns({ "key" => { "subkey" => "b" }}) + + access1 = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "key") + access2 = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => access1, :key => "subkey") + + access2.evaluate(@scope).should == 'b' + end + + it "should work with interleaved array and hash access" do + @scope.stubs(:lookupvar).with("a").returns({ "key" => [ "a" , "b" ]}) + + access1 = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => "a", :key => "key") + access2 = Puppet::Parser::AST::HashOrArrayAccess.new(:variable => access1, :key => 1) + + access2.evaluate(@scope).should == 'b' + end +end + describe Puppet::Parser::AST::Regex do before :each do @scope = stub 'scope' diff --git a/spec/unit/parser/scope.rb b/spec/unit/parser/scope.rb index 04ae3047c..2f553e460 100755 --- a/spec/unit/parser/scope.rb +++ b/spec/unit/parser/scope.rb @@ -64,6 +64,11 @@ describe Puppet::Parser::Scope do @scope.lookupvar("var").should == "yep" end + it "should be able to look up hashes" do + @scope.setvar("var", {"a" => "b"}) + @scope.lookupvar("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" @@ -167,6 +172,11 @@ describe Puppet::Parser::Scope do @scope.lookupvar("var").should == [4,2] end + it "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"} + end end describe "when calling number?" do |