Class: RSpec::Puppet::ManifestMatchers::CreateGeneric

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/rspec-puppet/matchers/create_generic.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ CreateGeneric

Returns a new instance of CreateGeneric.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 7

def initialize(*args, &block)
  @exp_resource_type = args.shift.to_s.gsub(/^(create|contain)_/, '')
  @args = args
  @block = block
  @referenced_type = referenced_type(@exp_resource_type)
  @title = args[0]

  @errors = []
  @expected_params = []
  @expected_undef_params = []
  @notifies = []
  @subscribes = []
  @requires = []
  @befores = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 61

def method_missing(method, *args, &block)
  if method.to_s =~ /^with_/
    param = method.to_s.gsub(/^with_/, '')
    @expected_params << [param, args[0]]
    self
  elsif method.to_s =~ /^only_with_/
    param = method.to_s.gsub(/^only_with_/, '')
    @expected_params_count = (@expected_params_count || 0) + 1
    @expected_params << [param, args[0]]
    self
  elsif method.to_s =~ /^without_/
    param = method.to_s.gsub(/^without_/, '')
    @expected_undef_params << [param, args[0]]
    self
  else
    super
  end
end

Instance Method Details

#descriptionObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 114

def description
  values = []

  if @expected_params_count
    values << "exactly #{@expected_params_count} parameters"
  end

  if @expected_params.any?
    values.concat(generate_param_list(@expected_params, :should))
  end

  if @expected_undef_params.any?
    values.concat(generate_param_list(@expected_undef_params, :not))
  end

  unless values.empty?
    if values.length == 1
      value_str = " with #{values.first}"
    else
      value_str = " with #{values[0..-2].join(", ")} and #{values[-1]}"
    end
  end

  "contain #{@referenced_type}[#{@title}]#{value_str}"
end

#failure_message_for_shouldObject



106
107
108
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 106

def failure_message_for_should
  "expected that the catalogue would contain #{@referenced_type}[#{@title}]#{errors}"
end

#failure_message_for_should_notObject



110
111
112
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 110

def failure_message_for_should_not
  "expected that the catalogue would not contain #{@referenced_type}[#{@title}]#{errors}"
end

#matches?(catalogue) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 80

def matches?(catalogue)
  ret = true
  resource = catalogue.resource(@referenced_type, @title)

  if resource.nil?
    false
  else
    rsrc_hsh = resource.to_hash
    if @expected_params_count
      unless rsrc_hsh.size == @expected_params_count
        ret = false
        (@errors ||= []) << "exactly #{@expected_params_count} parameters but the catalogue contains #{rsrc_hsh.size}"
      end
    end

    check_params(rsrc_hsh, @expected_params, :should) if @expected_params.any?
    check_params(rsrc_hsh, @expected_undef_params, :not) if @expected_undef_params.any?
    check_befores(catalogue, resource) if @befores.any?
    check_requires(catalogue, resource) if @requires.any?
    check_notifies(catalogue, resource) if @notifies.any?
    check_subscribes(catalogue, resource) if @subscribes.any?

    @errors.empty?
  end
end

#only_with(*args, &block) ⇒ Object



29
30
31
32
33
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 29

def only_with(*args, &block)
  params = args.shift
  @expected_params_count = (@expected_params_count || 0) + params.size
  self.with(params, &block)
end

#that_comes_before(resource) ⇒ Object



56
57
58
59
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 56

def that_comes_before(resource)
  @befores << resource
  self
end

#that_notifies(resource) ⇒ Object



41
42
43
44
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 41

def that_notifies(resource)
  @notifies << resource
  self
end

#that_requires(resource) ⇒ Object



51
52
53
54
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 51

def that_requires(resource)
  @requires << resource
  self
end

#that_subscribes_to(resource) ⇒ Object



46
47
48
49
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 46

def that_subscribes_to(resource)
  @subscribes << resource
  self
end

#with(*args, &block) ⇒ Object



23
24
25
26
27
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 23

def with(*args, &block)
  params = args.shift
  @expected_params = @expected_params | params.to_a
  self
end

#without(*args, &block) ⇒ Object



35
36
37
38
39
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 35

def without(*args, &block)
  params = args.shift
  @expected_undef_params = @expected_undef_params | Array(params)
  self
end