class RSpec::Puppet::TypeMatchers::CreateGeneric

Public Class Methods

new(*_args) click to toggle source
# File lib/rspec-puppet/matchers/type_matchers.rb, line 6
def initialize(*_args)
  @exp_provider = nil
  @exp_parameters       = []
  @exp_properties       = []
  @exp_features         = []
  @exp_defaults         = {}
  @params_with_values   = {}
  @errors               = []
end

Public Instance Methods

be_valid_resource(type, title, params) click to toggle source

builds the resource with the specified param values

# File lib/rspec-puppet/matchers/type_matchers.rb, line 89
def be_valid_resource(type, title, params)
  params[:name] ||= title
  type.new(params)
end
description() click to toggle source
# File lib/rspec-puppet/matchers/type_matchers.rb, line 110
def description
  'be a valid type'
end
failure_message() click to toggle source
# File lib/rspec-puppet/matchers/type_matchers.rb, line 114
def failure_message
  "Not a valid type #{@errors.inspect}"
end
match_default_provider(resource) click to toggle source

checks that the expected provider is set

# File lib/rspec-puppet/matchers/type_matchers.rb, line 97
def match_default_provider(resource)
  return true unless @exp_provider
  return true if resource[:provider] == @exp_provider

  @errors.push("Expected provider: #{@exp_provider} does not match: #{resource[:provider]}")
  false
end
match_default_values(_resource) click to toggle source
# File lib/rspec-puppet/matchers/type_matchers.rb, line 105
def match_default_values(_resource)
  # TODO: FINISH
  true
end
match_features(type) click to toggle source

checks that the specified features exist

# File lib/rspec-puppet/matchers/type_matchers.rb, line 84
def match_features(type)
  match_attrs(type, @exp_features, :feature)
end
match_params(type) click to toggle source

checks that the specified params exist

# File lib/rspec-puppet/matchers/type_matchers.rb, line 74
def match_params(type)
  match_attrs(type, @exp_parameters, :parameter)
end
match_props(type) click to toggle source

checks that the specified properties exist

# File lib/rspec-puppet/matchers/type_matchers.rb, line 79
def match_props(type)
  match_attrs(type, @exp_properties, :property)
end
matches?(type_title_and_params) click to toggle source

this is the method that drives all of the validation

# File lib/rspec-puppet/matchers/type_matchers.rb, line 57
def matches?(type_title_and_params)
  type   = type_title_and_params[0]
  title  = type_title_and_params[1]
  params = type_title_and_params[2]
  return false unless match_params(type) && match_props(type) && match_features(type)

  if @params_with_values != {} || @exp_provider
    # only build a resource if we are validating provider or setting
    # additional parameters
    resource = be_valid_resource(type, title, params.merge(@params_with_values))
    match_default_provider(resource) and match_default_values(resource)
  else
    true
  end
end
with_defaults(defaults_hash) click to toggle source
# File lib/rspec-puppet/matchers/type_matchers.rb, line 49
def with_defaults(defaults_hash)
  @exp_defaults.merge!(defaults_hash)
  self
end
with_features(features) click to toggle source

ensure the type has the list of features

# File lib/rspec-puppet/matchers/type_matchers.rb, line 35
def with_features(features)
  @exp_features |= Array(features)
  self
end
with_parameters(params) click to toggle source

ensures the listed parameters are valid

# File lib/rspec-puppet/matchers/type_matchers.rb, line 29
def with_parameters(params)
  @exp_parameters |= Array(params)
  self
end
with_properties(props) click to toggle source

ensures the listed properties are valid

# File lib/rspec-puppet/matchers/type_matchers.rb, line 23
def with_properties(props)
  @exp_properties |= Array(props)
  self
end
with_provider(name) click to toggle source

specifies a provider to validate

# File lib/rspec-puppet/matchers/type_matchers.rb, line 17
def with_provider(name)
  @exp_provider = name
  self
end
with_set_attributes(params) click to toggle source

ensures that the specified parameters with their values results in a valid resource

# File lib/rspec-puppet/matchers/type_matchers.rb, line 44
def with_set_attributes(params)
  @params_with_values.merge!(params)
  self
end

Private Instance Methods

match_attrs(type, attrs, attr_type) click to toggle source
# File lib/rspec-puppet/matchers/type_matchers.rb, line 120
def match_attrs(type, attrs, attr_type)
  baddies = []
  attrs.each do |param|
    param = param.to_sym
    if attr_type == :feature
      baddies.push(param) unless type.provider_feature(param)
    elsif !type.send("valid#{attr_type}?".to_sym, param)
      baddies.push(param)
    end
  end
  if baddies.size.positive?
    @errors.push("Invalid #{pluralize(attr_type)}: #{baddies.join(',')}")
    false
  else
    true
  end
end
pluralize(name) click to toggle source
# File lib/rspec-puppet/matchers/type_matchers.rb, line 138
def pluralize(name)
  if name == :property
    'properties'
  else
    "#{name}s"
  end
end