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



11
12
13
14
15
16
17
18
19
# File 'lib/rspec-puppet/matchers/parameter_matcher.rb', line 11

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

  @should_match = (type == :should)

  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



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

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)


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

def matches?(resource)
  actual = resource[@parameter]
  expected = @value

  actual = RSpec::Puppet::Sensitive.new(actual.unwrap) if actual.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive)

  # 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)

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

  retval
end