Class: RSpec::Puppet::ManifestMatchers::CountGeneric

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

Constant Summary collapse

DEFAULT_RESOURCES =
[
  'Class[main]',
  'Class[Settings]',
  'Stage[main]'
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, count, *method) ⇒ CountGeneric

Returns a new instance of CountGeneric.



14
15
16
17
18
19
20
21
22
# File 'lib/rspec-puppet/matchers/count_generic.rb', line 14

def initialize(type, count, *method)
  @type = if type.nil?
            method[0].to_s.gsub(/^have_(.+)_resource_count$/, '\1')
          else
            type
          end
  @resource_type = referenced_type(@type)
  @expected_number = count.to_i
end

Instance Attribute Details

#resource_typeObject (readonly)

Returns the value of attribute resource_type.



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

def resource_type
  @resource_type
end

Instance Method Details

#descriptionObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rspec-puppet/matchers/count_generic.rb', line 44

def description
  desc = []

  desc << "contain exactly #{@expected_number}"
  if @type == 'class'
    desc << (@expected_number == 1 ? 'class' : 'classes').to_s
  else
    desc << @resource_type.to_s unless @type == 'resource'
    desc << (@expected_number == 1 ? 'resource' : 'resources').to_s
  end

  desc.join(' ')
end

#failure_messageObject



58
59
60
# File 'lib/rspec-puppet/matchers/count_generic.rb', line 58

def failure_message
  "expected that the catalogue would #{description} but it contains #{@actual_number}"
end

#failure_message_when_negatedObject



62
63
64
# File 'lib/rspec-puppet/matchers/count_generic.rb', line 62

def failure_message_when_negated
  "expected that the catalogue would not #{description} but it does"
end

#matches?(catalogue) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rspec-puppet/matchers/count_generic.rb', line 24

def matches?(catalogue)
  @catalogue = catalogue.call

  resources = @catalogue.resources.reject do |res|
    DEFAULT_RESOURCES.include?(res.ref)
  end

  @actual_number = if @type == 'resource'
                     resources.count do |res|
                       !%w[Class Node].include?(res.type)
                     end
                   else
                     resources.count do |res|
                       res.type == @resource_type
                     end
                   end

  @actual_number == @expected_number
end

#supports_block_expectationsObject



66
67
68
# File 'lib/rspec-puppet/matchers/count_generic.rb', line 66

def supports_block_expectations
  true
end

#supports_value_expectationsObject



70
71
72
# File 'lib/rspec-puppet/matchers/count_generic.rb', line 70

def supports_value_expectations
  true
end