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

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

describe Puppet::Parser::AST::ResourceReference do

    ast = Puppet::Parser::AST

    before :each do
        @scope = Puppet::Parser::Scope.new()
    end

    def newref(title, type)
        title = stub 'title', :safeevaluate => title
        ref = Puppet::Parser::AST::ResourceReference.new(:type => type, :title => title)
    end

    it "should evaluate correctly reference to builtin types" do
        newref("/tmp/yay", "File").evaluate(@scope).to_s.should == "File[/tmp/yay]"
    end

    %{ "one::two" "one-two"}.each do |type|
        it "should evaluate correctly reference to define" do
            klass = stub 'klass', :title => "three", :classname => type
            @scope.stubs(:find_definition).returns(klass)
        
            newref("three", type).evaluate(@scope).to_ref.should == Puppet::Parser::Resource::Reference.new( :type => type, :title => "three" ).to_ref
        end
    end

    it "should be able to call qualified_class" do
        klass = stub 'klass', :title => "three", :classname => "one"
        @scope.expects(:find_hostclass).with("one").returns(klass)
        newref("three","class").qualified_class(@scope,"one").should == "one" 
    end

    it "should be able to find qualified classes when evaluating" do
        klass = stub 'klass', :title => "one", :classname => "one"
        @scope.stubs(:find_hostclass).returns(klass)
        
        evaled = newref("one", "class").evaluate(@scope)
        evaled.type.should == "Class"
        evaled.title.should == "one"
    end

    it "should return an array of reference if given an array of titles" do
        titles = mock 'titles', :safeevaluate => ["title1","title2"]
        ref = ast::ResourceReference.new( :title => titles, :type => "Resource" )
        ref.stubs(:qualified_type).with(@scope).returns("Resource")

        ref.evaluate(@scope).should have(2).elements
    end

    it "should qualify class of all titles for Class resource references" do
        titles = mock 'titles', :safeevaluate => ["title1","title2"]
        ref = ast::ResourceReference.new( :title => titles, :type => "Class" )
        ref.expects(:qualified_class).with(@scope,"title1").returns("class")
        ref.expects(:qualified_class).with(@scope,"title2").returns("class")

        ref.evaluate(@scope)
    end

    it "should return a correct representation when converting to string" do
        type = stub 'type', :is_a? => true, :to_s => "file"
        title = stub 'title', :is_a? => true, :to_s => "[/tmp/a, /tmp/b]"

        ast::ResourceReference.new( :type => type, :title => title ).to_s.should == "File[/tmp/a, /tmp/b]"
    end
end