summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser/ast/relationship_spec.rb
blob: 2a0f658df2d08f9a889201bcad8e9e5351bd1548 (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
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../../spec_helper'

describe Puppet::Parser::AST::Relationship do
  before do
    @class = Puppet::Parser::AST::Relationship
  end

  it "should set its 'left' and 'right' arguments accordingly" do
    dep = @class.new(:left, :right, '->')
    dep.left.should == :left
    dep.right.should == :right
  end

  it "should set its arrow to whatever arrow is passed" do
    @class.new(:left, :right, '->').arrow.should == '->'
  end

  it "should set its type to :relationship if the relationship type is '<-'" do
    @class.new(:left, :right, '<-').type.should == :relationship
  end

  it "should set its type to :relationship if the relationship type is '->'" do
    @class.new(:left, :right, '->').type.should == :relationship
  end

  it "should set its type to :subscription if the relationship type is '~>'" do
    @class.new(:left, :right, '~>').type.should == :subscription
  end

  it "should set its type to :subscription if the relationship type is '<~'" do
    @class.new(:left, :right, '<~').type.should == :subscription
  end

  it "should set its line and file if provided" do
    dep = @class.new(:left, :right, '->', :line => 50, :file => "/foo")
    dep.line.should == 50
    dep.file.should == "/foo"
  end

  describe "when evaluating" do
    before do
      @compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("foo"))
      @scope = Puppet::Parser::Scope.new(:compiler => @compiler)
    end

    it "should create a relationship with the evaluated source and target and add it to the scope" do
      source = stub 'source', :safeevaluate => :left
      target = stub 'target', :safeevaluate => :right
      @class.new(source, target, '->').evaluate(@scope)
      @compiler.relationships[0].source.should == :left
      @compiler.relationships[0].target.should == :right
    end

    describe "a chained relationship" do
      before do
        @left = stub 'left', :safeevaluate => :left
        @middle = stub 'middle', :safeevaluate => :middle
        @right = stub 'right', :safeevaluate => :right
        @first = @class.new(@left, @middle, '->')
        @second = @class.new(@first, @right, '->')
      end

      it "should evaluate the relationship to the left" do
        @first.expects(:evaluate).with(@scope).returns Puppet::Parser::Relationship.new(:left, :right, :relationship)

        @second.evaluate(@scope)
      end

      it "should use the right side of the left relationship as its source" do
        @second.evaluate(@scope)

        @compiler.relationships[0].source.should == :left
        @compiler.relationships[0].target.should == :middle
        @compiler.relationships[1].source.should == :middle
        @compiler.relationships[1].target.should == :right
      end

      it "should only evaluate a given AST node once" do
        @left.expects(:safeevaluate).once.returns :left
        @middle.expects(:safeevaluate).once.returns :middle
        @right.expects(:safeevaluate).once.returns :right
        @second.evaluate(@scope)
      end
    end
  end
end