Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
rcov/ruby/1.8/gems/rspec-expectations-2.5.0/lib/rspec/matchers/raise_error.rb | 130 | 84 | 48.46%
|
25.00%
|
Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.
1 module RSpec |
2 module Matchers |
3 class RaiseError #:nodoc: |
4 def initialize(expected_error_or_message=Exception, expected_message=nil, &block) |
5 @block = block |
6 @actual_error = nil |
7 case expected_error_or_message |
8 when String, Regexp |
9 @expected_error, @expected_message = Exception, expected_error_or_message |
10 else |
11 @expected_error, @expected_message = expected_error_or_message, expected_message |
12 end |
13 end |
14 |
15 def matches?(given_proc) |
16 @raised_expected_error = false |
17 @with_expected_message = false |
18 @eval_block = false |
19 @eval_block_passed = false |
20 begin |
21 given_proc.call |
22 rescue @expected_error => @actual_error |
23 @raised_expected_error = true |
24 @with_expected_message = verify_message |
25 rescue Exception => @actual_error |
26 # This clause should be empty, but rcov will not report it as covered |
27 # unless something (anything) is executed within the clause |
28 rcov_error_report = "http://eigenclass.org/hiki.rb?rcov-0.8.0" |
29 end |
30 |
31 unless negative_expectation? |
32 eval_block if @raised_expected_error && @with_expected_message && @block |
33 end |
34 ensure |
35 return (@raised_expected_error & @with_expected_message) ? (@eval_block ? @eval_block_passed : true) : false |
36 end |
37 |
38 def eval_block |
39 @eval_block = true |
40 begin |
41 @block[@actual_error] |
42 @eval_block_passed = true |
43 rescue Exception => err |
44 @actual_error = err |
45 end |
46 end |
47 |
48 def verify_message |
49 case @expected_message |
50 when nil |
51 true |
52 when Regexp |
53 @expected_message =~ @actual_error.message |
54 else |
55 @expected_message == @actual_error.message |
56 end |
57 end |
58 |
59 def failure_message_for_should |
60 @eval_block ? @actual_error.message : "expected #{expected_error}#{given_error}" |
61 end |
62 |
63 def failure_message_for_should_not |
64 "expected no #{expected_error}#{given_error}" |
65 end |
66 |
67 def description |
68 "raise #{expected_error}" |
69 end |
70 |
71 private |
72 def expected_error |
73 case @expected_message |
74 when nil |
75 @expected_error |
76 when Regexp |
77 "#{@expected_error} with message matching #{@expected_message.inspect}" |
78 else |
79 "#{@expected_error} with #{@expected_message.inspect}" |
80 end |
81 end |
82 |
83 def given_error |
84 @actual_error.nil? ? " but nothing was raised" : ", got #{@actual_error.inspect}" |
85 end |
86 |
87 def negative_expectation? |
88 # YES - I'm a bad person... help me find a better way - ryand |
89 caller.first(3).find { |s| s =~ /should_not/ } |
90 end |
91 end |
92 |
93 # :call-seq: |
94 # should raise_error() |
95 # should raise_error(NamedError) |
96 # should raise_error(NamedError, String) |
97 # should raise_error(NamedError, Regexp) |
98 # should raise_error() { |error| ... } |
99 # should raise_error(NamedError) { |error| ... } |
100 # should raise_error(NamedError, String) { |error| ... } |
101 # should raise_error(NamedError, Regexp) { |error| ... } |
102 # should_not raise_error() |
103 # should_not raise_error(NamedError) |
104 # should_not raise_error(NamedError, String) |
105 # should_not raise_error(NamedError, Regexp) |
106 # |
107 # With no args, matches if any error is raised. |
108 # With a named error, matches only if that specific error is raised. |
109 # With a named error and messsage specified as a String, matches only if both match. |
110 # With a named error and messsage specified as a Regexp, matches only if both match. |
111 # Pass an optional block to perform extra verifications on the exception matched |
112 # |
113 # == Examples |
114 # |
115 # lambda { do_something_risky }.should raise_error |
116 # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError) |
117 # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError) { |error| error.data.should == 42 } |
118 # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, "that was too risky") |
119 # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, /oo ri/) |
120 # |
121 # lambda { do_something_risky }.should_not raise_error |
122 # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError) |
123 # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, "that was too risky") |
124 # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, /oo ri/) |
125 def raise_error(error=Exception, message=nil, &block) |
126 Matchers::RaiseError.new(error, message, &block) |
127 end |
128 alias_method :raise_exception, :raise_error |
129 end |
130 end |
Generated on Fri Apr 22 17:22:41 -0700 2011 with rcov 0.9.8