summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser/ast/asthash_spec.rb
blob: d7fbbfae99b197dfd5776d3f8ca8d99154ff0a69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env rspec
require 'spec_helper'

describe Puppet::Parser::AST::ASTHash do
  before :each do
    @scope = Puppet::Parser::Scope.new
  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 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"
    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

  describe "when being initialized without arguments" do
    it "should evaluate to an empty hash" do
      hash = Puppet::Parser::AST::ASTHash.new({})
      hash.evaluate(@scope).should == {}
    end

    it "should support merging" do
      hash = Puppet::Parser::AST::ASTHash.new({})
      hash.merge({"a" => "b"}).should == {"a" => "b"}
    end
  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