Class: Spec::Matchers::RaiseException

Inherits:
Object
  • Object
show all
Defined in:
lib/spec/matchers/raise_exception.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(expected_exception_or_message = Exception, expected_message = nil, &block) ⇒ RaiseException

Returns a new instance of RaiseException.



4
5
6
7
8
9
10
11
12
13
# File 'lib/spec/matchers/raise_exception.rb', line 4

def initialize(expected_exception_or_message=Exception, expected_message=nil, &block)
  @block = block
  @actual_exception = nil
  case expected_exception_or_message
  when String, Regexp
    @expected_exception, @expected_message = Exception, expected_exception_or_message
  else
    @expected_exception, @expected_message = expected_exception_or_message, expected_message
  end
end

Instance Method Details

#descriptionObject



67
68
69
# File 'lib/spec/matchers/raise_exception.rb', line 67

def description
  "raise #{expected_exception}"
end

#eval_blockObject



38
39
40
41
42
43
44
45
46
# File 'lib/spec/matchers/raise_exception.rb', line 38

def eval_block
  @eval_block = true
  begin
    @block[@actual_exception]
    @eval_block_passed = true
  rescue Exception => err
    @actual_exception = err
  end
end

#failure_message_for_shouldObject



59
60
61
# File 'lib/spec/matchers/raise_exception.rb', line 59

def failure_message_for_should
  @eval_block ? @actual_exception.message : "expected #{expected_exception}#{given_exception}"
end

#failure_message_for_should_notObject



63
64
65
# File 'lib/spec/matchers/raise_exception.rb', line 63

def failure_message_for_should_not
  "expected no #{expected_exception}#{given_exception}"
end

#matches?(given_proc) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/spec/matchers/raise_exception.rb', line 15

def matches?(given_proc)
  @raised_expected_exception = false
  @with_expected_message = false
  @eval_block = false
  @eval_block_passed = false
  begin
    given_proc.call
  rescue @expected_exception => @actual_exception
    @raised_expected_exception = true
    @with_expected_message = verify_message
  rescue Exception => @actual_exception
    # This clause should be empty, but rcov will not report it as covered
    # unless something (anything) is executed within the clause
    rcov_exception_report = "http://eigenclass.org/hiki.rb?rcov-0.8.0"
  end

  unless negative_expectation?
    eval_block if @raised_expected_exception && @with_expected_message && @block
  end

  (@raised_expected_exception & @with_expected_message) ? (@eval_block ? @eval_block_passed : true) : false
end

#verify_messageObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/spec/matchers/raise_exception.rb', line 48

def verify_message
  case @expected_message
  when nil
    true
  when Regexp
    @expected_message =~ @actual_exception.message
  else
    @expected_message == @actual_exception.message
  end
end