Skip to content

Commit 70c78f0

Browse files
committed
Convert Exception#set_backtrace into a shared spec
This can be reused to test assignment of $@
1 parent f8c065e commit 70c78f0

File tree

2 files changed

+69
-66
lines changed

2 files changed

+69
-66
lines changed

core/exception/set_backtrace_spec.rb

Lines changed: 5 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
require_relative '../../spec_helper'
22
require_relative 'fixtures/common'
3+
require_relative 'shared/set_backtrace'
34

45
describe "Exception#set_backtrace" do
5-
it "accepts an Array of Strings" do
6-
err = RuntimeError.new
7-
err.set_backtrace ["unhappy"]
8-
err.backtrace.should == ["unhappy"]
9-
end
10-
116
it "allows the user to set the backtrace from a rescued exception" do
127
bt = ExceptionSpecs::Backtrace.backtrace
138
err = RuntimeError.new
@@ -20,65 +15,9 @@
2015
err.backtrace_locations.should == nil
2116
end
2217

23-
ruby_version_is "3.4" do
24-
it "allows the user to set backtrace locations from a rescued exception" do
25-
bt_locations = ExceptionSpecs::Backtrace.backtrace_locations
26-
err = RuntimeError.new
27-
err.backtrace.should == nil
28-
err.backtrace_locations.should == nil
29-
30-
err.set_backtrace bt_locations
31-
32-
err.backtrace_locations.size.should == bt_locations.size
33-
err.backtrace_locations.each_with_index do |loc, index|
34-
other_loc = bt_locations[index]
35-
36-
loc.path.should == other_loc.path
37-
loc.label.should == other_loc.label
38-
loc.base_label.should == other_loc.base_label
39-
loc.lineno.should == other_loc.lineno
40-
loc.absolute_path.should == other_loc.absolute_path
41-
loc.to_s.should == other_loc.to_s
42-
end
43-
err.backtrace.size.should == err.backtrace_locations.size
44-
end
45-
end
46-
47-
it "accepts an empty Array" do
48-
err = RuntimeError.new
49-
err.set_backtrace []
50-
err.backtrace.should == []
51-
end
52-
53-
it "accepts a String" do
18+
it_behaves_like :exception_set_backtrace, ->(*backtrace) {
5419
err = RuntimeError.new
55-
err.set_backtrace "unhappy"
56-
err.backtrace.should == ["unhappy"]
57-
end
58-
59-
it "accepts nil" do
60-
err = RuntimeError.new
61-
err.set_backtrace nil
62-
err.backtrace.should be_nil
63-
end
64-
65-
it "raises a TypeError when passed a Symbol" do
66-
err = RuntimeError.new
67-
-> { err.set_backtrace :unhappy }.should raise_error(TypeError)
68-
end
69-
70-
it "raises a TypeError when the Array contains a Symbol" do
71-
err = RuntimeError.new
72-
-> { err.set_backtrace ["String", :unhappy] }.should raise_error(TypeError)
73-
end
74-
75-
it "raises a TypeError when the array contains nil" do
76-
err = Exception.new
77-
-> { err.set_backtrace ["String", nil] }.should raise_error(TypeError)
78-
end
79-
80-
it "raises a TypeError when the argument is a nested array" do
81-
err = Exception.new
82-
-> { err.set_backtrace ["String", ["String"]] }.should raise_error(TypeError)
83-
end
20+
err.set_backtrace(backtrace.first) unless backtrace.empty?
21+
err
22+
}
8423
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
require_relative '../fixtures/common'
2+
3+
describe :exception_set_backtrace, shared: true do
4+
it "accepts an Array of Strings" do
5+
err = @method.call(["unhappy"])
6+
err.backtrace.should == ["unhappy"]
7+
end
8+
9+
it "allows the user to set the backtrace from a rescued exception" do
10+
bt = ExceptionSpecs::Backtrace.backtrace
11+
err = @method.call(bt)
12+
err.backtrace.should == bt
13+
end
14+
15+
ruby_version_is "3.4" do
16+
it "allows the user to set backtrace locations from a rescued exception" do
17+
bt_locations = ExceptionSpecs::Backtrace.backtrace_locations
18+
err = @method.call(bt_locations)
19+
err.backtrace_locations.size.should == bt_locations.size
20+
err.backtrace_locations.each_with_index do |loc, index|
21+
other_loc = bt_locations[index]
22+
23+
loc.path.should == other_loc.path
24+
loc.label.should == other_loc.label
25+
loc.base_label.should == other_loc.base_label
26+
loc.lineno.should == other_loc.lineno
27+
loc.absolute_path.should == other_loc.absolute_path
28+
loc.to_s.should == other_loc.to_s
29+
end
30+
err.backtrace.size.should == err.backtrace_locations.size
31+
end
32+
end
33+
34+
it "accepts an empty Array" do
35+
err = @method.call([])
36+
err.backtrace.should == []
37+
end
38+
39+
it "accepts a String" do
40+
err = @method.call("unhappy")
41+
err.backtrace.should == ["unhappy"]
42+
end
43+
44+
it "accepts nil" do
45+
err = @method.call(nil)
46+
err.backtrace.should be_nil
47+
end
48+
49+
it "raises a TypeError when passed a Symbol" do
50+
-> { @method.call(:unhappy) }.should raise_error(TypeError)
51+
end
52+
53+
it "raises a TypeError when the Array contains a Symbol" do
54+
-> { @method.call(["String", :unhappy]) }.should raise_error(TypeError)
55+
end
56+
57+
it "raises a TypeError when the array contains nil" do
58+
-> { @method.call(["String", nil]) }.should raise_error(TypeError)
59+
end
60+
61+
it "raises a TypeError when the argument is a nested array" do
62+
-> { @method.call(["String", ["String"]]) }.should raise_error(TypeError)
63+
end
64+
end

0 commit comments

Comments
 (0)