[ruby/json] Appease ruby/ruby CI
[ruby.git] / spec / ruby / core / thread / backtrace_locations_spec.rb
blob09fe622e0daf2bfdad4e47ef7731f8765409fc51
1 require_relative '../../spec_helper'
3 describe "Thread#backtrace_locations" do
4   it "returns an Array" do
5     locations = Thread.current.backtrace_locations
6     locations.should be_an_instance_of(Array)
7     locations.should_not be_empty
8   end
10   it "sets each element to a Thread::Backtrace::Location" do
11     locations = Thread.current.backtrace_locations
12     locations.each { |loc| loc.should be_an_instance_of(Thread::Backtrace::Location) }
13   end
15   it "can be called on any Thread" do
16     locations = Thread.new { Thread.current.backtrace_locations }.value
17     locations.should be_an_instance_of(Array)
18     locations.should_not be_empty
19     locations.each { |loc| loc.should be_an_instance_of(Thread::Backtrace::Location) }
20   end
22   it "can be called with a number of locations to omit" do
23     locations1 = Thread.current.backtrace_locations
24     locations2 = Thread.current.backtrace_locations(2)
25     locations2.length.should == locations1[2..-1].length
26     locations2.map(&:to_s).should == locations1[2..-1].map(&:to_s)
27   end
29   it "can be called with a maximum number of locations to return as second parameter" do
30     locations1 = Thread.current.backtrace_locations
31     locations2 = Thread.current.backtrace_locations(2, 3)
32     locations2.map(&:to_s).should == locations1[2..4].map(&:to_s)
33   end
35   it "can be called with a range" do
36     locations1 = Thread.current.backtrace_locations
37     locations2 = Thread.current.backtrace_locations(2..4)
38     locations2.map(&:to_s).should == locations1[2..4].map(&:to_s)
39   end
41   it "can be called with a range whose end is negative" do
42     Thread.current.backtrace_locations(2..-1).map(&:to_s).should == Thread.current.backtrace_locations[2..-1].map(&:to_s)
43     Thread.current.backtrace_locations(2..-2).map(&:to_s).should == Thread.current.backtrace_locations[2..-2].map(&:to_s)
44   end
46   it "can be called with an endless range" do
47     locations1 = Thread.current.backtrace_locations(0)
48     locations2 = Thread.current.backtrace_locations(eval("(2..)"))
49     locations2.map(&:to_s).should == locations1[2..-1].map(&:to_s)
50   end
52   it "can be called with an beginless range" do
53     locations1 = Thread.current.backtrace_locations(0)
54     locations2 = Thread.current.backtrace_locations((..5))
55     locations2.map(&:to_s)[eval("(2..)")].should == locations1[(..5)].map(&:to_s)[eval("(2..)")]
56   end
58   it "returns nil if omitting more locations than available" do
59     Thread.current.backtrace_locations(100).should == nil
60     Thread.current.backtrace_locations(100..-1).should == nil
61   end
63   it "returns [] if omitting exactly the number of locations available" do
64     omit = Thread.current.backtrace_locations.length
65     Thread.current.backtrace_locations(omit).should == []
66   end
68   it "without argument is the same as showing all locations with 0..-1" do
69     Thread.current.backtrace_locations.map(&:to_s).should == Thread.current.backtrace_locations(0..-1).map(&:to_s)
70   end
72   it "the first location reports the call to #backtrace_locations" do
73     Thread.current.backtrace_locations(0..0)[0].to_s.should =~ /\A#{__FILE__ }:#{__LINE__ }:in [`'](?:Thread#)?backtrace_locations'\z/
74   end
76   it "[1..-1] is the same as #caller_locations(0..-1) for Thread.current" do
77     Thread.current.backtrace_locations(1..-1).map(&:to_s).should == caller_locations(0..-1).map(&:to_s)
78   end
79 end