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

require 'spec_helper'

describe "the 'defined' function" do
  before :all do
    Puppet::Parser::Functions.autoloader.loadall
  end

  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