class RSpec::Puppet::ManifestMatchers::ParameterMatcher

Attributes

errors[R]

@!attribute [r] errors

@return [Array<Object < StandardError>] All expectation errors
  generated on this parameter.

Public Class Methods

new(parameter, value, type) click to toggle source

@param parameter [Symbol] The specific parameter to check @param value [Object] The expected data to match the parameter against @param type [:should, :not] Whether the given parameter should match

# 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

Public Instance Methods

matches?(resource) click to toggle source

Ensure that the actual parameter matches the expected parameter.

@param resource [Hash<Symbol, Object>] A hash representing a Puppet

resource in the catalog

@return [true, false]

# File lib/rspec-puppet/matchers/parameter_matcher.rb, line 27
def matches?(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)

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

  retval
end

Private Instance Methods

check(expected, actual) click to toggle source

Recursively check that the ‘expected` and `actual` data structures match

@param expected [Object] The expected value of the given resource param @param actual [Object] The value of the resource as found in the catalogue

@return [true, false] If the resource matched

# File lib/rspec-puppet/matchers/parameter_matcher.rb, line 56
def check(expected, actual)
  return false if !expected.is_a?(Proc) && actual.nil? && !expected.nil?

  case expected
  when Proc
    check_proc(expected, actual)
  when Regexp
    check_regexp(expected, actual)
  when Hash
    check_hash(expected, actual)
  when Array
    check_array(expected, actual)
  when RSpec::Puppet::Sensitive
    expected == actual
  else
    check_string(expected, actual)
  end
end
check_array(expected, actual) click to toggle source
# File lib/rspec-puppet/matchers/parameter_matcher.rb, line 104
def check_array(expected, actual)
  op = @should_match ? :'==' : :'!='

  unless actual.class.send(op, expected.class)
    @errors << MatchError.new(@parameter, expected, actual, !@should_match)
    return false
  end

  return false unless expected.size.send(op, actual.size)

  (0...expected.size).all? do |index|
    check(expected[index], actual[index])
  end
end
check_hash(expected, actual) click to toggle source

Ensure that two hashes have the same number of keys, and that for each key in the expected hash, there’s a stringified key in the actual hash with a matching value.

# File lib/rspec-puppet/matchers/parameter_matcher.rb, line 89
def check_hash(expected, actual)
  op = @should_match ? :'==' : :'!='

  unless actual.class.send(op, expected.class)
    @errors << MatchError.new(@parameter, expected, actual, !@should_match)
    return false
  end

  return false unless expected.keys.size.send(op, actual.keys.size)

  expected.keys.all? do |key|
    check(expected[key], actual[key])
  end
end
check_proc(expected, actual) click to toggle source
# File lib/rspec-puppet/matchers/parameter_matcher.rb, line 75
def check_proc(expected, actual)
  expected_return = @should_match
  actual_return   = expected.call(actual)

  actual_return == expected_return
end
check_regexp(expected, actual) click to toggle source
# File lib/rspec-puppet/matchers/parameter_matcher.rb, line 82
def check_regexp(expected, actual)
  !!(actual.to_s.match expected) == @should_match
end
check_string(expected, actual) click to toggle source
# File lib/rspec-puppet/matchers/parameter_matcher.rb, line 119
def check_string(expected, actual)
  (expected.to_s == actual.to_s) == @should_match
end