Skip to content

Commit 404a470

Browse files
committed
Add specs for Thread.each_caller_location
1 parent d598249 commit 404a470

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
require_relative '../../spec_helper'
2+
3+
describe "Thread.each_caller_location" do
4+
ruby_version_is "3.2" do
5+
it "iterates through the current execution stack and matches caller_locations content and type" do
6+
ScratchPad.record []
7+
Thread.each_caller_location { |l| ScratchPad << l; }
8+
9+
ScratchPad.recorded.map(&:to_s).should == caller_locations.map(&:to_s)
10+
ScratchPad.recorded[0].should be_kind_of(Thread::Backtrace::Location)
11+
end
12+
13+
it "returns subset of 'Thread.to_enum(:each_caller_location)' locations" do
14+
i = 0
15+
ar = []
16+
ecl = Thread.each_caller_location do |x|
17+
ar << x
18+
i += 1
19+
break x if i == 1
20+
end
21+
22+
(ar - Thread.to_enum(:each_caller_location).to_a).should.empty?
23+
end
24+
25+
it "stops the backtrace iteration if 'break' occurs" do
26+
i = 0
27+
ar = []
28+
ecl = Thread.each_caller_location do |x|
29+
ar << x
30+
i += 1
31+
break x if i == 2
32+
end
33+
34+
ar.map(&:to_s).should == caller_locations(1, 2).map(&:to_s)
35+
ecl.should be_kind_of(Thread::Backtrace::Location)
36+
end
37+
38+
it "returns nil" do
39+
Thread.each_caller_location {}.should == nil
40+
end
41+
42+
it "cannot be iterated with an external iterator" do
43+
-> {
44+
Thread.to_enum(:each_caller_location).next
45+
}.should raise_error(StopIteration, "iteration reached an end")
46+
end
47+
48+
it "raises LocalJumpError when called without a block" do
49+
-> {
50+
Thread.each_caller_location
51+
}.should raise_error(LocalJumpError, "no block given")
52+
end
53+
54+
it "doesn't accept positional and keyword arguments" do
55+
-> {
56+
Thread.each_caller_location(12, foo: 10) {}
57+
}.should raise_error(ArgumentError, "wrong number of arguments (given 2, expected 0)")
58+
end
59+
end
60+
end

0 commit comments

Comments
 (0)