summaryrefslogtreecommitdiffstats
path: root/spec/unit/dsl/resource_type_api_spec.rb
blob: ea81f7da48bc2bbef9078d568a28b71497726c02 (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 rspec
require 'spec_helper'

require 'puppet/dsl/resource_type_api'

describe Puppet::DSL::ResourceTypeAPI do
  # Verify that the block creates a single AST node through the API,
  # instantiate that AST node into a types, and return that type.
  def test_api_call(&block)
    main_object = Puppet::DSL::ResourceTypeAPI.new
    main_object.instance_eval(&block)
    created_ast_objects = main_object.instance_eval { @__created_ast_objects__ }
    created_ast_objects.length.should == 1
    new_types = created_ast_objects[0].instantiate('')
    new_types.length.should == 1
    new_types[0]
  ensure
    Thread.current[:ruby_file_parse_result] = nil
  end

  [:definition, :node, :hostclass].each do |type|
    method = type == :definition ? "define" : type
    it "should be able to create a #{type}" do
      newtype = test_api_call { send(method, "myname").should == nil }
      newtype.should be_a(Puppet::Resource::Type)
      newtype.type.should == type
    end

    it "should use the provided name when creating a #{type}" do
      newtype = test_api_call { send(method, "myname") }
      newtype.name.should == "myname"
    end

    unless type == :definition
      it "should pass in any provided options when creating a #{type}" do
        newtype = test_api_call { send(method, "myname", :line => 200) }
        newtype.line.should == 200
      end
    end

    it "should set any provided block as the type's ruby code" do
      newtype = test_api_call { send(method, "myname") { 'method_result' } }
      newtype.ruby_code.call.should == 'method_result'
    end
  end

  describe "when creating a definition" do
    it "should use the provided options to define valid arguments for the resource type" do
      newtype = test_api_call { define("myname", :arg1, :arg2) }
      newtype.arguments.should == { 'arg1' => nil, 'arg2' => nil }
    end
  end
end