1 2 3 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
module RSpec
module Matchers
module BuiltIn
class RaiseError
def initialize(expected_error_or_message=Exception, expected_message=nil, &block)
@block = block
@actual_error = nil
case expected_error_or_message
when String, Regexp
@expected_error, @expected_message = Exception, expected_error_or_message
else
@expected_error, @expected_message = expected_error_or_message, expected_message
end
end
def matches?(given_proc, negative_expectation = false)
if negative_expectation && (expecting_specific_exception? || @expected_message)
what_to_deprecate = if expecting_specific_exception? && @expected_message
"`expect { }.not_to raise_error(SpecificErrorClass, message)`"
elsif expecting_specific_exception?
"`expect { }.not_to raise_error(SpecificErrorClass)`"
elsif @expected_message
"`expect { }.not_to raise_error(message)`"
end
RSpec.deprecate(what_to_deprecate, :replacement => "`expect { }.not_to raise_error()`")
end
@raised_expected_error = false
@with_expected_message = false
@eval_block = false
@eval_block_passed = false
unless given_proc.respond_to?(:call)
::Kernel.warn "`raise_error` was called with non-proc object #{given_proc.inspect}"
return false
end
begin
given_proc.call
rescue Exception => @actual_error
if @actual_error == @expected_error || @expected_error === @actual_error
@raised_expected_error = true
@with_expected_message = verify_message
end
end
unless negative_expectation
eval_block if @raised_expected_error && @with_expected_message && @block
end
ensure
return (@raised_expected_error & @with_expected_message) ? (@eval_block ? @eval_block_passed : true) : false
end
alias == matches?
def does_not_match?(given_proc)
!matches?(given_proc, :negative_expectation)
end
def eval_block
@eval_block = true
begin
@block[@actual_error]
@eval_block_passed = true
rescue Exception => err
@actual_error = err
end
end
def verify_message
case @expected_message
when nil
true
when Regexp
@expected_message =~ @actual_error.message
else
@expected_message == @actual_error.message
end
end
def failure_message_for_should
@eval_block ? @actual_error.message : "expected #{expected_error}#{given_error}"
end
def failure_message_for_should_not
"expected no #{expected_error}#{given_error}"
end
def description
"raise #{expected_error}"
end
private
def expected_error
case @expected_message
when nil
@expected_error.inspect
when Regexp
"#{@expected_error} with message matching #{@expected_message.inspect}"
else
"#{@expected_error} with #{@expected_message.inspect}"
end
end
def format_backtrace(backtrace)
formatter = Matchers.configuration.backtrace_formatter
formatter.format_backtrace(backtrace)
end
def given_error
return " but nothing was raised" unless @actual_error
backtrace = format_backtrace(@actual_error.backtrace)
[
", got #{@actual_error.inspect} with backtrace:",
*backtrace
].join("\n # ")
end
def expecting_specific_exception?
@expected_error != Exception
end
end
end
end
end
|