summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser/functions/defined_spec.rb
blob: 90f2f52398234d1a8d4015f0c3be970cb8de1a39 (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
#!/usr/bin/env ruby

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

describe "the 'defined' function" do

    before :each do
        Puppet::Node::Environment.stubs(:current).returns(nil)
        @compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("foo"))
        @scope = Puppet::Parser::Scope.new(:compiler => @compiler)
    end

    it "should exist" do
        Puppet::Parser::Functions.function("defined").should == "function_defined"
    end

    it "should be true when the name is defined as a class" do
        @scope.known_resource_types.add Puppet::Resource::Type.new(:hostclass, "yayness")
        @scope.function_defined("yayness").should be_true
    end

    it "should be true when the name is defined as a definition" do
        @scope.known_resource_types.add Puppet::Resource::Type.new(:definition, "yayness")
        @scope.function_defined("yayness").should be_true
    end

    it "should be true when the name is defined as a builtin type" do
        @scope.function_defined("file").should be_true
    end


    it "should be true when any of the provided names are defined" do
        @scope.known_resource_types.add Puppet::Resource::Type.new(:definition, "yayness")
        @scope.function_defined(["meh", "yayness", "booness"]).should be_true
    end

    it "should be false when a single given name is not defined" do
        @scope.function_defined("meh").should be_false
    end

    it "should be false when none of the names are defined" do
        @scope.function_defined(["meh", "yayness", "booness"]).should be_false
    end

    it "should be true when a resource reference is provided and the resource is in the catalog" do
        resource = Puppet::Resource.new("file", "/my/file")
        @compiler.add_resource(@scope, resource)
        @scope.function_defined(resource).should be_true
    end
end