|
| 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 | + ar = [] |
| 7 | + Thread.each_caller_location { ar << _1; } |
| 8 | + |
| 9 | + ar.map(&:to_s).should == caller_locations.map(&:to_s) |
| 10 | + ar[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 | + Thread.to_enum(:each_caller_location).to_a.map(&:to_s).should include(ecl.to_s) |
| 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 "show multiple block farames" do |
| 39 | + i = 0 |
| 40 | + ar = [] |
| 41 | + cl = nil |
| 42 | + ecl = nil |
| 43 | + |
| 44 | + -> { |
| 45 | + -> { |
| 46 | + cl = caller_locations(1, 2) |
| 47 | + ecl = Thread.each_caller_location do |x| |
| 48 | + ar << x |
| 49 | + i += 1 |
| 50 | + break x if i == 2 |
| 51 | + end |
| 52 | + }.() |
| 53 | + }.() |
| 54 | + |
| 55 | + cl.map(&:to_s).should == ar.map(&:to_s) |
| 56 | + ecl.should be_kind_of(Thread::Backtrace::Location) |
| 57 | + end |
| 58 | + |
| 59 | + it "returns nil unconditionally" do |
| 60 | + Thread.each_caller_location {}.should == nil |
| 61 | + end |
| 62 | + |
| 63 | + it "raises StopIteration at the first invocation of 'next'" do |
| 64 | + -> { |
| 65 | + Thread.to_enum(:each_caller_location).next |
| 66 | + }.should raise_error(StopIteration, "iteration reached an end") |
| 67 | + end |
| 68 | + |
| 69 | + it "raises LocalJumpError when called without a block" do |
| 70 | + -> { |
| 71 | + Thread.each_caller_location |
| 72 | + }.should raise_error(LocalJumpError, "no block given") |
| 73 | + end |
| 74 | + |
| 75 | + it "doesn't accept positional and keyword arguments" do |
| 76 | + -> { |
| 77 | + Thread.each_caller_location(12, foo: 10) {} |
| 78 | + }.should raise_error(ArgumentError, "wrong number of arguments (given 2, expected 0)") |
| 79 | + end |
| 80 | + end |
| 81 | +end |
0 commit comments