|
| 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 | + ar = [] |
| 15 | + ecl = Thread.each_caller_location { |x| ar << x } |
| 16 | + |
| 17 | + (ar.map(&:to_s) - Thread.to_enum(:each_caller_location).to_a.map(&:to_s)).should.empty? |
| 18 | + end |
| 19 | + |
| 20 | + it "stops the backtrace iteration if 'break' occurs" do |
| 21 | + i = 0 |
| 22 | + ar = [] |
| 23 | + ecl = Thread.each_caller_location do |x| |
| 24 | + ar << x |
| 25 | + i += 1 |
| 26 | + break x if i == 2 |
| 27 | + end |
| 28 | + |
| 29 | + ar.map(&:to_s).should == caller_locations(1, 2).map(&:to_s) |
| 30 | + ecl.should be_kind_of(Thread::Backtrace::Location) |
| 31 | + end |
| 32 | + |
| 33 | + it "returns nil" do |
| 34 | + Thread.each_caller_location {}.should == nil |
| 35 | + end |
| 36 | + |
| 37 | + it "cannot be iterated with an external iterator" do |
| 38 | + -> { |
| 39 | + Thread.to_enum(:each_caller_location).next |
| 40 | + }.should raise_error(StopIteration, "iteration reached an end") |
| 41 | + end |
| 42 | + |
| 43 | + it "raises LocalJumpError when called without a block" do |
| 44 | + -> { |
| 45 | + Thread.each_caller_location |
| 46 | + }.should raise_error(LocalJumpError, "no block given") |
| 47 | + end |
| 48 | + |
| 49 | + it "doesn't accept positional and keyword arguments" do |
| 50 | + -> { |
| 51 | + Thread.each_caller_location(12, foo: 10) {} |
| 52 | + }.should raise_error(ArgumentError, "wrong number of arguments (given 2, expected 0)") |
| 53 | + end |
| 54 | + end |
| 55 | +end |
0 commit comments