File tree Expand file tree Collapse file tree 2 files changed +120
-5
lines changed Expand file tree Collapse file tree 2 files changed +120
-5
lines changed Original file line number Diff line number Diff line change 1
1
module RescueSpecs
2
+ class ClassVariableCaptor
3
+ def capture ( msg )
4
+ raise msg
5
+ rescue => @@captured_error
6
+ :caught
7
+ end
8
+
9
+ def captured_error
10
+ self . class . remove_class_variable ( :@@captured_error )
11
+ end
12
+ end
13
+
14
+ class ConstantCaptor
15
+ # Using lambda gets around the dynamic constant assignment warning
16
+ CAPTURE = -> msg {
17
+ begin
18
+ raise msg
19
+ rescue => CapturedError
20
+ :caught
21
+ end
22
+ }
23
+
24
+ def capture ( msg )
25
+ CAPTURE . call ( msg )
26
+ end
27
+
28
+ def captured_error
29
+ self . class . send ( :remove_const , :CapturedError )
30
+ end
31
+ end
32
+
33
+ class GlobalVariableCaptor
34
+ def capture ( msg )
35
+ raise msg
36
+ rescue => $captured_error
37
+ :caught
38
+ end
39
+
40
+ def captured_error
41
+ $captured_error. tap do
42
+ $captured_error = nil # Can't remove globals, only nil them out
43
+ end
44
+ end
45
+ end
46
+
47
+ class InstanceVariableCaptor
48
+ attr_reader :captured_error
49
+
50
+ def capture ( msg )
51
+ raise msg
52
+ rescue => @captured_error
53
+ :caught
54
+ end
55
+ end
56
+
57
+ class LocalVariableCaptor
58
+ attr_reader :captured_error
59
+
60
+ def capture ( msg )
61
+ raise msg
62
+ rescue => captured_error
63
+ @captured_error = captured_error
64
+ :caught
65
+ end
66
+ end
67
+
68
+ class SafeNavigationSetterCaptor
69
+ attr_accessor :captured_error
70
+
71
+ def capture ( msg )
72
+ raise msg
73
+ rescue => self &.captured_error
74
+ :caught
75
+ end
76
+ end
77
+
78
+ class SetterCaptor
79
+ attr_accessor :captured_error
80
+
81
+ def capture ( msg )
82
+ raise msg
83
+ rescue => self . captured_error
84
+ :caught
85
+ end
86
+ end
87
+
88
+ class SquareBracketsCaptor
89
+ def capture ( msg )
90
+ @hash = { }
91
+
92
+ raise msg
93
+ rescue => self [ :error ]
94
+ :caught
95
+ end
96
+
97
+ def []=( key , value )
98
+ @hash [ key ] = value
99
+ end
100
+
101
+ def captured_error
102
+ @hash [ :error ]
103
+ end
104
+ end
105
+
2
106
def self . begin_else ( raise_exception )
3
107
begin
4
108
ScratchPad << :one
Original file line number Diff line number Diff line change @@ -23,11 +23,22 @@ class ArbitraryException < StandardError
23
23
end . should == :caught
24
24
end
25
25
26
- it "can capture the raised exception in a local variable" do
27
- begin
28
- raise SpecificExampleException , "some text"
29
- rescue SpecificExampleException => e
30
- e . message . should == "some text"
26
+ {
27
+ # Standard use case
28
+ 'can capture the raised exception in a local variable' => RescueSpecs ::LocalVariableCaptor ,
29
+ # Exotic use cases
30
+ 'can capture the raised exception in a class variable' => RescueSpecs ::ClassVariableCaptor ,
31
+ 'can capture the raised exception in a constant' => RescueSpecs ::ConstantCaptor ,
32
+ 'can capture the raised exception in a global variable' => RescueSpecs ::GlobalVariableCaptor ,
33
+ 'can capture the raised exception in an instance variable' => RescueSpecs ::InstanceVariableCaptor ,
34
+ 'can capture the raised exception using a safely navigated setter method' => RescueSpecs ::SafeNavigationSetterCaptor ,
35
+ 'can capture the raised exception using a setter method' => RescueSpecs ::SetterCaptor ,
36
+ 'can capture the raised exception using a square brackets setter' => RescueSpecs ::SquareBracketsCaptor ,
37
+ } . each do |description , klass |
38
+ it description do
39
+ captor = klass . new
40
+ captor . capture ( 'some text' ) . should == :caught # Ensure rescue body still runs
41
+ captor . captured_error . message . should == 'some text'
31
42
end
32
43
end
33
44
You can’t perform that action at this time.
0 commit comments