@@ -11,6 +11,7 @@ module RSpec::Core
11
11
before do
12
12
allow ( example . execution_result ) . to receive ( :exception ) { exception }
13
13
example . metadata [ :absolute_file_path ] = __FILE__
14
+ allow ( exception ) . to receive ( :cause ) if RSpec ::Support ::RubyFeatures . supports_exception_cause?
14
15
end
15
16
16
17
describe "#fully_formatted" do
@@ -49,9 +50,9 @@ module RSpec::Core
49
50
end
50
51
51
52
it "allows the caller to specify additional indentation" do
52
- presenter = Formatters ::ExceptionPresenter . new ( exception , example , :indentation => 4 )
53
+ the_presenter = Formatters ::ExceptionPresenter . new ( exception , example , :indentation => 4 )
53
54
54
- expect ( presenter . fully_formatted ( 1 ) ) . to eq ( <<-EOS . gsub ( /^ +\| / , '' ) )
55
+ expect ( the_presenter . fully_formatted ( 1 ) ) . to eq ( <<-EOS . gsub ( /^ +\| / , '' ) )
55
56
|
56
57
| 1) Example
57
58
| Failure/Error: # The failure happened here!#{ encoding_check }
@@ -64,9 +65,9 @@ module RSpec::Core
64
65
it 'passes the indentation on to the `:detail_formatter` lambda so it can align things' do
65
66
detail_formatter = Proc . new { "Some Detail" }
66
67
67
- presenter = Formatters ::ExceptionPresenter . new ( exception , example , :indentation => 4 ,
68
+ the_presenter = Formatters ::ExceptionPresenter . new ( exception , example , :indentation => 4 ,
68
69
:detail_formatter => detail_formatter )
69
- expect ( presenter . fully_formatted ( 1 ) ) . to eq ( <<-EOS . gsub ( /^ +\| / , '' ) )
70
+ expect ( the_presenter . fully_formatted ( 1 ) ) . to eq ( <<-EOS . gsub ( /^ +\| / , '' ) )
70
71
|
71
72
| 1) Example
72
73
| Some Detail
@@ -78,11 +79,11 @@ module RSpec::Core
78
79
end
79
80
80
81
it 'allows the caller to omit the description' do
81
- presenter = Formatters ::ExceptionPresenter . new ( exception , example ,
82
+ the_presenter = Formatters ::ExceptionPresenter . new ( exception , example ,
82
83
:detail_formatter => Proc . new { "Detail!" } ,
83
84
:description_formatter => Proc . new { } )
84
85
85
- expect ( presenter . fully_formatted ( 1 ) ) . to eq ( <<-EOS . gsub ( /^ +\| / , '' ) )
86
+ expect ( the_presenter . fully_formatted ( 1 ) ) . to eq ( <<-EOS . gsub ( /^ +\| / , '' ) )
86
87
|
87
88
| 1) Detail!
88
89
| Failure/Error: # The failure happened here!#{ encoding_check }
@@ -93,9 +94,9 @@ module RSpec::Core
93
94
end
94
95
95
96
it 'allows the failure/error line to be used as the description' do
96
- presenter = Formatters ::ExceptionPresenter . new ( exception , example , :description_formatter => lambda { |p | p . failure_slash_error_line } )
97
+ the_presenter = Formatters ::ExceptionPresenter . new ( exception , example , :description_formatter => lambda { |p | p . failure_slash_error_line } )
97
98
98
- expect ( presenter . fully_formatted ( 1 ) ) . to eq ( <<-EOS . gsub ( /^ +\| / , '' ) )
99
+ expect ( the_presenter . fully_formatted ( 1 ) ) . to eq ( <<-EOS . gsub ( /^ +\| / , '' ) )
99
100
|
100
101
| 1) Failure/Error: # The failure happened here!#{ encoding_check }
101
102
| Boom
@@ -105,13 +106,13 @@ module RSpec::Core
105
106
end
106
107
107
108
it 'allows a caller to specify extra details that are added to the bottom' do
108
- presenter = Formatters ::ExceptionPresenter . new (
109
+ the_presenter = Formatters ::ExceptionPresenter . new (
109
110
exception , example , :extra_detail_formatter => lambda do |failure_number , colorizer , indentation |
110
111
"#{ indentation } extra detail for failure: #{ failure_number } \n "
111
112
end
112
113
)
113
114
114
- expect ( presenter . fully_formatted ( 2 ) ) . to eq ( <<-EOS . gsub ( /^ +\| / , '' ) )
115
+ expect ( the_presenter . fully_formatted ( 2 ) ) . to eq ( <<-EOS . gsub ( /^ +\| / , '' ) )
115
116
|
116
117
| 2) Example
117
118
| Failure/Error: # The failure happened here!#{ encoding_check }
@@ -121,8 +122,35 @@ module RSpec::Core
121
122
| extra detail for failure: 2
122
123
EOS
123
124
end
124
- end
125
125
126
+ let ( :the_exception ) { instance_double ( Exception , :cause => second_exception , :message => "Boom\n Bam" , :backtrace => [ "#{ __FILE__ } :#{ line_num } " ] ) }
127
+
128
+ let ( :second_exception ) do
129
+ instance_double ( Exception , :cause => first_exception , :message => "Second\n exception" , :backtrace => [ "#{ __FILE__ } :#{ __LINE__ } " ] )
130
+ end
131
+
132
+ let ( :first_exception ) do
133
+ instance_double ( Exception , :cause => nil , :message => "Real\n culprit" , :backtrace => [ "#{ __FILE__ } :#{ __LINE__ } " ] )
134
+ end
135
+
136
+ it 'includes the first exception that caused the failure' , :if => RSpec ::Support ::RubyFeatures . supports_exception_cause? do
137
+ the_presenter = Formatters ::ExceptionPresenter . new ( the_exception , example )
138
+
139
+ expect ( the_presenter . fully_formatted ( 1 ) ) . to eq ( <<-EOS . gsub ( /^ +\| / , '' ) )
140
+ |
141
+ | 1) Example
142
+ | Failure/Error: # The failure happened here!#{ encoding_check }
143
+ | Boom
144
+ | Bam
145
+ | # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{ line_num }
146
+ | # ------------------
147
+ | # --- Caused by: ---
148
+ | # Real
149
+ | # culprit
150
+ | # ./spec/rspec/core/formatters/exception_presenter_spec.rb:133
151
+ EOS
152
+ end
153
+ end
126
154
describe "#read_failed_line" do
127
155
def read_failed_line
128
156
presenter . send ( :read_failed_line )
0 commit comments