class RSpec::Puppet::TypeMatchers::CreateGeneric

Public Class Methods

new(*args, &block) click to toggle source
# File lib/rspec-puppet/matchers/type_matchers.rb, line 6
def initialize(*args, &block)

  @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 91
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 117
def description
  "be a valid type"
end
failure_message() click to toggle source
# File lib/rspec-puppet/matchers/type_matchers.rb, line 121
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 99
def match_default_provider(resource)
  if @exp_provider
    if resource[:provider] == @exp_provider
      return true
    else
      @errors.push("Expected provider: #{@exp_provider} does not match: #{resource[:provider]}")
      return false
    end
  else
    return true
  end
end
match_default_values(resource) click to toggle source
# File lib/rspec-puppet/matchers/type_matchers.rb, line 112
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 86
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 76
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 81
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 58
def matches?(type_title_and_params)
  type   = type_title_and_params[0]
  title  = type_title_and_params[1]
  params = type_title_and_params[2]
  unless match_params(type) && match_props(type) && match_features(type)
   return false
  end
  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 50
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 36
def with_features(features)
  @exp_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 30
def with_parameters(params)
  @exp_parameters = @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 24
def with_properties(props)
  @exp_properties = @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 18
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 45
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 127
def match_attrs(type, attrs, attr_type)
  baddies = []
  attrs.each do |param|
    param = param.to_sym
    if attr_type == :feature
      unless type.provider_feature(param)
        baddies.push(param)
      end
    elsif ! type.send("valid#{attr_type}?".to_sym, param)
      baddies.push(param)
    end
  end
  if baddies.size > 0
    @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 147
def pluralize(name)
  if name == :property
    "properties"
  else
    "#{name}s"
  end
end