class RSpec::Puppet::ManifestMatchers::ParameterMatcher
Attributes
@!attribute [r] errors
@return [Array<Object < StandardError>] All expectation errors generated on this parameter.
Public Class Methods
@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
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 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
Private Instance Methods
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 58 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
# File lib/rspec-puppet/matchers/parameter_matcher.rb, line 106 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
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 91 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
# File lib/rspec-puppet/matchers/parameter_matcher.rb, line 77 def check_proc(expected, actual) expected_return = @should_match actual_return = expected.call(actual) actual_return == expected_return end
# File lib/rspec-puppet/matchers/parameter_matcher.rb, line 84 def check_regexp(expected, actual) !!(actual.to_s.match expected) == @should_match end
# File lib/rspec-puppet/matchers/parameter_matcher.rb, line 121 def check_string(expected, actual) (expected.to_s == actual.to_s) == @should_match end