Class: RSpec::Puppet::ManifestMatchers::CreateGeneric

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-puppet/matchers/create_generic.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ CreateGeneric

Returns a new instance of CreateGeneric.



4
5
6
7
8
9
10
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 4

def initialize(*args, &block)
  @exp_resource_type = args.shift.to_s.gsub(/^(create|contain)_/, '')
  @args = args
  @block = block
  @referenced_type = referenced_type(@exp_resource_type)
  @title = args[0]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 24

def method_missing(method, *args, &block)
  if method.to_s =~ /^with_/
    param = method.to_s.gsub(/^with_/, '')
    (@expected_params ||= []) << [param, args[0]]
    self
  elsif method.to_s =~ /^without_/
    param = method.to_s.gsub(/^without_/, '')
    (@expected_undef_params ||= []) << param
    self
  else
    super
  end
end

Instance Method Details

#descriptionObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 88

def description
  values = []
  if @expected_params
    @expected_params.each do |name, value|
      if value.kind_of?(Regexp)
        values << "#{name.to_s} matching #{value.inspect}"
      else
        values << "#{name.to_s} => #{value.inspect}"
      end
    end
  end

  if @expected_undef_params
    @expected_undef_params.each do |name, value|
      values << "#{name.to_s} undefined"
    end
  end

  unless values.empty?
    if values.length == 1
      value_str = " with #{values.first}"
    else
      value_str = " with #{values[0..-2].join(", ")} and #{values[-1]}"
    end
  end

  "contain #{@referenced_type}[#{@title}]#{value_str}"
end

#failure_message_for_shouldObject



80
81
82
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 80

def failure_message_for_should
  "expected that the catalogue would contain #{@referenced_type}[#{@title}]#{errors}"
end

#failure_message_for_should_notObject



84
85
86
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 84

def failure_message_for_should_not
  "expected that the catalogue would not contain #{@referenced_type}[#{@title}]#{errors}"
end

#matches?(catalogue) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 38

def matches?(catalogue)
  ret = true
  resource = catalogue.resource(@referenced_type, @title)

  if resource.nil?
    ret = false
  else
    rsrc_hsh = resource.to_hash
    if @expected_params
      @expected_params.each do |name, value|
        if value.kind_of?(Regexp) then
          unless rsrc_hsh[name.to_sym].to_s =~ value
            ret = false
            (@errors ||= []) << "#{name.to_s} matching `#{value.inspect}` but its value of `#{rsrc_hsh[name.to_sym].inspect}` does not"
          end
        elsif value.kind_of?(Array) then
          unless Array(rsrc_hsh[name.to_sym]).flatten.join == value.flatten.join
            ret = false
            (@errors ||= []) << "#{name.to_s} set to `#{value.inspect}` but it is set to `#{rsrc_hsh[name.to_sym].inspect}` in the catalogue"
          end
        else
          unless rsrc_hsh[name.to_sym].to_s == value.to_s
            ret = false
            (@errors ||= []) << "#{name.to_s} set to `#{value.inspect}` but it is set to `#{rsrc_hsh[name.to_sym].inspect}` in the catalogue"
          end
        end
      end
    end

    if @expected_undef_params
      @expected_undef_params.each do |name|
        unless resource.send(:parameters)[name.to_sym].nil?
          ret = false
          (@errors ||= []) << "#{name.to_s} undefined"
        end
      end
    end
  end

  ret
end

#with(*args, &block) ⇒ Object



12
13
14
15
16
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 12

def with(*args, &block)
  params = args.shift
  @expected_params = (@expected_params || []) | params.to_a
  self
end

#without(*args, &block) ⇒ Object



18
19
20
21
22
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 18

def without(*args, &block)
  params = args.shift
  @expected_undef_params = (@expected_undef_params || []) | Array(params)
  self
end