diff options
author | Luke Kanies <luke@puppetlabs.com> | 2010-04-12 11:45:46 -0700 |
---|---|---|
committer | Luke Kanies <luke@puppetlabs.com> | 2010-04-12 11:45:46 -0700 |
commit | 0f254bec2937f0b27171dce0352a25fcaddca719 (patch) | |
tree | cf89c488b3b0b918996d528663d498541621efe2 | |
parent | 41aeba49ed4afc523f0e9827076cb3532569f49f (diff) | |
download | puppet-0f254bec2937f0b27171dce0352a25fcaddca719.tar.gz puppet-0f254bec2937f0b27171dce0352a25fcaddca719.tar.xz puppet-0f254bec2937f0b27171dce0352a25fcaddca719.zip |
Fixing Hash functionality with non-constant keys
It was only apparently working with constant keys,
not, say, AST strings.
Signed-off-by: Luke Kanies <luke@puppetlabs.com>
-rw-r--r-- | lib/puppet/parser/ast/asthash.rb | 3 | ||||
-rw-r--r-- | spec/unit/parser/ast/asthash.rb | 32 |
2 files changed, 29 insertions, 6 deletions
diff --git a/lib/puppet/parser/ast/asthash.rb b/lib/puppet/parser/ast/asthash.rb index 3e09457ab..aa5127dd9 100644 --- a/lib/puppet/parser/ast/asthash.rb +++ b/lib/puppet/parser/ast/asthash.rb @@ -9,7 +9,8 @@ class Puppet::Parser::AST items = {} @value.each_pair do |k,v| - items.merge!({ k => v.safeevaluate(scope) }) + key = k.respond_to?(:safeevaluate) ? k.safeevaluate(scope) : k + items.merge!({ key => v.safeevaluate(scope) }) end return items diff --git a/spec/unit/parser/ast/asthash.rb b/spec/unit/parser/ast/asthash.rb index 4c700fe4b..c6839ab4d 100644 --- a/spec/unit/parser/ast/asthash.rb +++ b/spec/unit/parser/ast/asthash.rb @@ -7,11 +7,6 @@ describe Puppet::Parser::AST::ASTHash 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) @@ -46,6 +41,33 @@ describe Puppet::Parser::AST::ASTHash do operator.evaluate(@scope) end + it "should evaluate the hash keys if they are AST instances" do + key1 = stub "key1" + value1 = stub "value1", :safeevaluate => "one" + key2 = stub "key2" + value2 = stub "value2", :safeevaluate => "two" + + key1.expects(:safeevaluate).with(@scope).returns("1") + key2.expects(:safeevaluate).with(@scope).returns("2") + + operator = Puppet::Parser::AST::ASTHash.new(:value => { key1 => value1, key2 => value2}) + hash = operator.evaluate(@scope) + hash["1"].should == "one" + hash["2"].should == "two" + end + + it "should evaluate the hash keys if they are not AST instances" do + key1 = "1" + value1 = stub "value1", :safeevaluate => "one" + key2 = "2" + value2 = stub "value2", :safeevaluate => "two" + + operator = Puppet::Parser::AST::ASTHash.new(:value => { key1 => value1, key2 => value2}) + hash = operator.evaluate(@scope) + hash["1"].should == "one" + hash["2"].should == "two" + end + it "should return an evaluated hash" do key1 = stub "key1" value1 = stub "value1", :safeevaluate => "b" |