Class: RSpec::Puppet::ManifestMatchers::ParameterMatcher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameter, value, type) ⇒ ParameterMatcher

Returns a new instance of ParameterMatcher.

Parameters:

  • parameter (Symbol)

    The specific parameter to check

  • value (Object)

    The expected data to match the parameter against

  • type (:should, :not)

    Whether the given parameter should match



9
10
11
12
13
14
15
# File 'lib/rspec-puppet/matchers/parameter_matcher.rb', line 9

def initialize(parameter, value, type)
  @parameter, @value, @type = parameter, value, type

  @should_match = (type == :should)

  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



47
48
49
# File 'lib/rspec-puppet/matchers/parameter_matcher.rb', line 47

def errors
  @errors
end

Instance Method Details

#matches?(resource) ⇒ true, false

Ensure that the actual parameter matches the expected parameter.

Parameters:

  • resource (Hash<Symbol, Object>)

    A hash representing a Puppet resource in the catalog

Returns:

  • (true, false)


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

def matches?(resource)

  @resource = resource

  actual   = @resource[@parameter]
  expected = @value

  # Puppet flattens an array with a single value into just the value and
  # this can cause confusion when testing as people expect when you put
  # an array in, you'll get an array out.
  actual = [actual] if expected.is_a?(Array) && !actual.is_a?(Array)

  retval = check(expected, actual)

  unless retval
    @errors << MatchError.new(@parameter, expected, actual, !@should_match)
  end

  retval
end