changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec/lib/spec/matchers/raise_error.rb

changeset 15: 64acf98d15f4
author: moriq@moriq.com
date: Mon Mar 10 10:12:58 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugins rspec
1module Spec
2 module Matchers
3
4 class RaiseError #:nodoc:
5 def initialize(error_or_message=Exception, message=nil)
6 if String === error_or_message
7 @expected_error = Exception
8 @expected_message = error_or_message
9 else
10 @expected_error = error_or_message
11 @expected_message = message
12 end
13 end
14
15 def matches?(proc)
16 @raised_expected_error = false
17 @raised_other = false
18 begin
19 proc.call
20 rescue @expected_error => @actual_error
21 if @expected_message.nil?
22 @raised_expected_error = true
23 else
24 verify_message
25 end
26 rescue Exception => @actual_error
27 @raised_other = true
28 ensure
29 return @raised_expected_error
30 end
31 end
32
33 def verify_message
34 case @expected_message
35 when Regexp
36 if @expected_message =~ @actual_error.message
37 @raised_expected_error = true
38 else
39 @raised_other = true
40 end
41 else
42 if @expected_message == @actual_error.message
43 @raised_expected_error = true
44 else
45 @raised_other = true
46 end
47 end
48 end
49
50 def failure_message
51 return "expected #{expected_error}#{actual_error}" if @raised_other || !@raised_expected_error
52 end
53
54 def negative_failure_message
55 "expected no #{expected_error}#{actual_error}"
56 end
57
58 def description
59 "raise #{expected_error}"
60 end
61
62 private
63 def expected_error
64 case @expected_message
65 when nil
66 @expected_error
67 when Regexp
68 "#{@expected_error} with message matching #{@expected_message.inspect}"
69 else
70 "#{@expected_error} with #{@expected_message.inspect}"
71 end
72 end
73
74 def actual_error
75 @actual_error.nil? ? " but nothing was raised" : ", got #{@actual_error.inspect}"
76 end
77 end
78
79 # :call-seq:
80 # should raise_error()
81 # should raise_error(NamedError)
82 # should raise_error(NamedError, String)
83 # should raise_error(NamedError, Regexp)
84 # should_not raise_error()
85 # should_not raise_error(NamedError)
86 # should_not raise_error(NamedError, String)
87 # should_not raise_error(NamedError, Regexp)
88 #
89 # With no args, matches if any error is raised.
90 # With a named error, matches only if that specific error is raised.
91 # With a named error and messsage specified as a String, matches only if both match.
92 # With a named error and messsage specified as a Regexp, matches only if both match.
93 #
94 # == Examples
95 #
96 # lambda { do_something_risky }.should raise_error
97 # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError)
98 # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, "that was too risky")
99 # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, /oo ri/)
100 #
101 # lambda { do_something_risky }.should_not raise_error
102 # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError)
103 # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, "that was too risky")
104 # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, /oo ri/)
105 def raise_error(error=Exception, message=nil)
106 Matchers::RaiseError.new(error, message)
107 end
108 end
109end