Class: RSpec::Puppet::FunctionMatchers::Run

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

Instance Method Summary collapse

Instance Method Details

#and_raise_error(error_or_message, message = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rspec-puppet/matchers/run.rb', line 71

def and_raise_error(error_or_message, message=nil)
  @has_expected_error = true
  case error_or_message
  when String, Regexp
    @expected_error, @expected_error_message = Exception, error_or_message
  else
    @expected_error, @expected_error_message = error_or_message, message
  end

  if @expected_error_message.is_a? Regexp
    @desc = "raise an #{@expected_error} with the message matching #{@expected_error_message.inspect}"
  else
    @desc = "raise an #{@expected_error}"
    unless @expected_error_message.nil?
      @desc += "with the message #{@expected_error_message.inspect}"
    end
  end
  self
end

#and_return(value) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/rspec-puppet/matchers/run.rb', line 60

def and_return(value)
  @has_expected_return = true
  @expected_return = value
  if value.is_a? Regexp
    @desc = "match #{value.inspect}"
  else
    @desc = "return #{value.inspect}"
  end
  self
end

#descriptionObject



99
100
101
102
103
104
105
# File 'lib/rspec-puppet/matchers/run.rb', line 99

def description
  if @desc
    "run #{func_name}(#{func_params}) and #{@desc}"
  else
    "run #{func_name}(#{func_params}) without error"
  end
end

#failure_messageObject



91
92
93
# File 'lib/rspec-puppet/matchers/run.rb', line 91

def failure_message
  failure_message_generic(:should, @func_obj)
end

#failure_message_when_negatedObject



95
96
97
# File 'lib/rspec-puppet/matchers/run.rb', line 95

def failure_message_when_negated
  failure_message_generic(:should_not, @func_obj)
end

#matches?(func_obj) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rspec-puppet/matchers/run.rb', line 4

def matches?(func_obj)
  @func_obj = func_obj

  @has_returned = false
  begin
    # `*nil` does not evaluate to "no params" on ruby 1.8 :-(
    @actual_return = @params.nil? ? @func_obj.execute(&@block) : @func_obj.execute(*@params, &@block)
    @has_returned = true
  rescue Exception => e
    @actual_error = e
  end

  if @has_expected_error
    if @has_returned
      return false
    elsif @actual_error.is_a?(@expected_error)
      case @expected_error_message
      when nil
        return true
      when Regexp
        return !!(@actual_error.message =~ @expected_error_message)
      else
        return @actual_error.message == @expected_error_message
      end
    else # error did not match
      return false
    end
  elsif @has_expected_return
    if !@has_returned
      return false
    else
      case @expected_return
      when Regexp
        return !!(@actual_return =~ @expected_return)
      else
        return @actual_return == @expected_return
      end
    end
  else
    return @has_returned
  end
end

#supports_block_expectationsObject



107
108
109
# File 'lib/rspec-puppet/matchers/run.rb', line 107

def supports_block_expectations
  true
end

#supports_value_expectationsObject



111
112
113
# File 'lib/rspec-puppet/matchers/run.rb', line 111

def supports_value_expectations
  true
end

#with_lambda(&block) ⇒ Object



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

def with_lambda(&block)
  @block = block
  self
end

#with_params(*params) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/rspec-puppet/matchers/run.rb', line 47

def with_params(*params)
  @params = params
  # stringify immediately to protect us from the params being changed by
  # the subject, e.g. with params.shift
  @func_args = @params.inspect[1..-2]
  self
end