2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2012-05-25 00:53:54 -04:00
|
|
|
require 'test/unit'
|
2015-01-31 08:10:05 -05:00
|
|
|
require 'tempfile'
|
2012-05-25 00:53:54 -04:00
|
|
|
|
|
|
|
class TestBacktrace < Test::Unit::TestCase
|
|
|
|
def test_exception
|
|
|
|
bt = Fiber.new{
|
|
|
|
begin
|
|
|
|
raise
|
|
|
|
rescue => e
|
|
|
|
e.backtrace
|
|
|
|
end
|
|
|
|
}.resume
|
2013-12-11 02:41:14 -05:00
|
|
|
assert_equal(1, bt.size)
|
2012-05-25 00:53:54 -04:00
|
|
|
assert_match(/.+:\d+:.+/, bt[0])
|
|
|
|
end
|
|
|
|
|
2013-12-12 23:31:06 -05:00
|
|
|
def helper_test_exception_backtrace_locations
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_exception_backtrace_locations
|
2013-12-25 04:35:03 -05:00
|
|
|
backtrace, backtrace_locations = Fiber.new{
|
2013-12-12 23:31:06 -05:00
|
|
|
begin
|
|
|
|
raise
|
|
|
|
rescue => e
|
2013-12-25 04:35:03 -05:00
|
|
|
[e.backtrace, e.backtrace_locations]
|
2013-12-12 23:31:06 -05:00
|
|
|
end
|
|
|
|
}.resume
|
2013-12-25 04:35:03 -05:00
|
|
|
assert_equal(backtrace, backtrace_locations.map{|e| e.to_s})
|
2013-12-12 23:31:06 -05:00
|
|
|
|
2013-12-25 04:35:03 -05:00
|
|
|
backtrace, backtrace_locations = Fiber.new{
|
2013-12-12 23:31:06 -05:00
|
|
|
begin
|
|
|
|
begin
|
|
|
|
helper_test_exception_backtrace_locations
|
|
|
|
rescue
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
rescue => e
|
2013-12-25 04:35:03 -05:00
|
|
|
[e.backtrace, e.backtrace_locations]
|
|
|
|
end
|
|
|
|
}.resume
|
|
|
|
assert_equal(backtrace, backtrace_locations.map{|e| e.to_s})
|
|
|
|
end
|
|
|
|
|
|
|
|
def call_helper_test_exception_backtrace_locations
|
|
|
|
helper_test_exception_backtrace_locations(:bad_argument)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_argument_error_backtrace_locations
|
|
|
|
backtrace, backtrace_locations = Fiber.new{
|
|
|
|
begin
|
|
|
|
helper_test_exception_backtrace_locations(1)
|
|
|
|
rescue ArgumentError => e
|
|
|
|
[e.backtrace, e.backtrace_locations]
|
|
|
|
end
|
|
|
|
}.resume
|
|
|
|
assert_equal(backtrace, backtrace_locations.map{|e| e.to_s})
|
|
|
|
|
|
|
|
backtrace, backtrace_locations = Fiber.new{
|
|
|
|
begin
|
|
|
|
call_helper_test_exception_backtrace_locations
|
|
|
|
rescue ArgumentError => e
|
|
|
|
[e.backtrace, e.backtrace_locations]
|
2013-12-12 23:31:06 -05:00
|
|
|
end
|
|
|
|
}.resume
|
2013-12-25 04:35:03 -05:00
|
|
|
assert_equal(backtrace, backtrace_locations.map{|e| e.to_s})
|
2013-12-12 23:31:06 -05:00
|
|
|
end
|
|
|
|
|
2012-05-25 00:53:54 -04:00
|
|
|
def test_caller_lev
|
|
|
|
cs = []
|
|
|
|
Fiber.new{
|
|
|
|
Proc.new{
|
|
|
|
cs << caller(0)
|
|
|
|
cs << caller(1)
|
|
|
|
cs << caller(2)
|
|
|
|
cs << caller(3)
|
|
|
|
cs << caller(4)
|
|
|
|
cs << caller(5)
|
|
|
|
}.call
|
|
|
|
}.resume
|
2015-10-05 17:34:24 -04:00
|
|
|
assert_equal(2, cs[0].size)
|
|
|
|
assert_equal(1, cs[1].size)
|
|
|
|
assert_equal(0, cs[2].size)
|
|
|
|
assert_equal(nil, cs[3])
|
2012-05-25 00:53:54 -04:00
|
|
|
assert_equal(nil, cs[4])
|
|
|
|
|
|
|
|
#
|
2012-06-22 04:58:29 -04:00
|
|
|
max = 7
|
2012-05-25 00:53:54 -04:00
|
|
|
rec = lambda{|n|
|
|
|
|
if n > 0
|
|
|
|
1.times{
|
|
|
|
rec[n-1]
|
|
|
|
}
|
|
|
|
else
|
2012-05-25 07:01:01 -04:00
|
|
|
(max*3).times{|i|
|
2012-05-25 00:53:54 -04:00
|
|
|
total_size = caller(0).size
|
|
|
|
c = caller(i)
|
|
|
|
if c
|
|
|
|
assert_equal(total_size - i, caller(i).size, "[ruby-dev:45673]")
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
}
|
2014-03-02 10:18:36 -05:00
|
|
|
Fiber.new{
|
2012-05-25 00:53:54 -04:00
|
|
|
rec[max]
|
|
|
|
}.resume
|
|
|
|
end
|
|
|
|
|
2012-05-25 07:01:01 -04:00
|
|
|
def test_caller_lev_and_n
|
|
|
|
m = 10
|
|
|
|
rec = lambda{|n|
|
|
|
|
if n < 0
|
|
|
|
(m*6).times{|lev|
|
2014-03-02 10:18:36 -05:00
|
|
|
(m*6).times{|i|
|
2012-05-25 07:01:01 -04:00
|
|
|
t = caller(0).size
|
2014-03-02 10:18:36 -05:00
|
|
|
r = caller(lev, i)
|
2012-05-25 07:01:01 -04:00
|
|
|
r = r.size if r.respond_to? :size
|
|
|
|
|
2014-03-02 10:18:36 -05:00
|
|
|
# STDERR.puts [t, lev, i, r].inspect
|
|
|
|
if i == 0
|
|
|
|
assert_equal(0, r, [t, lev, i, r].inspect)
|
2012-05-25 07:01:01 -04:00
|
|
|
elsif t < lev
|
2014-03-02 10:18:36 -05:00
|
|
|
assert_equal(nil, r, [t, lev, i, r].inspect)
|
2012-05-25 07:01:01 -04:00
|
|
|
else
|
2014-03-02 10:18:36 -05:00
|
|
|
if t - lev > i
|
|
|
|
assert_equal(i, r, [t, lev, i, r].inspect)
|
2012-05-25 07:01:01 -04:00
|
|
|
else
|
2014-03-02 10:18:36 -05:00
|
|
|
assert_equal(t - lev, r, [t, lev, i, r].inspect)
|
2012-05-25 07:01:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rec[n-1]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
rec[m]
|
|
|
|
end
|
2012-05-26 00:49:23 -04:00
|
|
|
|
2013-09-10 01:37:39 -04:00
|
|
|
def test_caller_with_nil_length
|
|
|
|
assert_equal caller(0), caller(0, nil)
|
|
|
|
end
|
|
|
|
|
2012-06-02 12:46:08 -04:00
|
|
|
def test_caller_locations
|
2012-06-03 23:04:34 -04:00
|
|
|
cs = caller(0); locs = caller_locations(0).map{|loc|
|
|
|
|
loc.to_s
|
2012-05-26 00:49:23 -04:00
|
|
|
}
|
2012-06-03 23:04:34 -04:00
|
|
|
assert_equal(cs, locs)
|
2012-05-26 00:49:23 -04:00
|
|
|
end
|
2012-11-19 01:07:06 -05:00
|
|
|
|
2012-12-30 23:03:55 -05:00
|
|
|
def test_caller_locations_with_range
|
|
|
|
cs = caller(0,2); locs = caller_locations(0..1).map { |loc|
|
|
|
|
loc.to_s
|
|
|
|
}
|
|
|
|
assert_equal(cs, locs)
|
|
|
|
end
|
|
|
|
|
2012-12-01 07:19:52 -05:00
|
|
|
def test_caller_locations_to_s_inspect
|
|
|
|
cs = caller(0); locs = caller_locations(0)
|
|
|
|
cs.zip(locs){|str, loc|
|
|
|
|
assert_equal(str, loc.to_s)
|
|
|
|
assert_equal(str.inspect, loc.inspect)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-01-31 08:10:05 -05:00
|
|
|
def test_caller_locations_path
|
|
|
|
loc, = caller_locations(0, 1)
|
|
|
|
assert_equal(__FILE__, loc.path)
|
2015-01-31 08:57:34 -05:00
|
|
|
Tempfile.create(%w"caller_locations .rb") do |f|
|
|
|
|
f.puts "caller_locations(0, 1)[0].tap {|loc| puts loc.path}"
|
|
|
|
f.close
|
|
|
|
dir, base = File.split(f.path)
|
|
|
|
assert_in_out_err(["-C", dir, base], "", [base])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_caller_locations_absolute_path
|
|
|
|
loc, = caller_locations(0, 1)
|
2015-01-31 08:10:05 -05:00
|
|
|
assert_equal(__FILE__, loc.absolute_path)
|
|
|
|
Tempfile.create(%w"caller_locations .rb") do |f|
|
2015-01-31 08:57:34 -05:00
|
|
|
f.puts "caller_locations(0, 1)[0].tap {|loc| puts loc.absolute_path}"
|
2015-01-31 08:10:05 -05:00
|
|
|
f.close
|
2015-01-31 08:57:34 -05:00
|
|
|
assert_in_out_err(["-C", *File.split(f.path)], "", [File.realpath(f.path)])
|
2015-01-31 08:10:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-31 08:57:46 -05:00
|
|
|
def test_caller_locations_lineno
|
|
|
|
loc, = caller_locations(0, 1)
|
|
|
|
assert_equal(__LINE__-1, loc.lineno)
|
|
|
|
Tempfile.create(%w"caller_locations .rb") do |f|
|
|
|
|
f.puts "caller_locations(0, 1)[0].tap {|loc| puts loc.lineno}"
|
|
|
|
f.close
|
|
|
|
assert_in_out_err(["-C", *File.split(f.path)], "", ["1"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-01 04:40:09 -05:00
|
|
|
def test_caller_locations_base_label
|
|
|
|
assert_equal("#{__method__}", caller_locations(0, 1)[0].base_label)
|
2016-02-19 02:48:02 -05:00
|
|
|
loc, = tap {break caller_locations(0, 1)}
|
2015-02-01 04:40:09 -05:00
|
|
|
assert_equal("#{__method__}", loc.base_label)
|
|
|
|
begin
|
|
|
|
raise
|
|
|
|
rescue
|
|
|
|
assert_equal("#{__method__}", caller_locations(0, 1)[0].base_label)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_caller_locations_label
|
|
|
|
assert_equal("#{__method__}", caller_locations(0, 1)[0].label)
|
2016-02-19 02:48:02 -05:00
|
|
|
loc, = tap {break caller_locations(0, 1)}
|
2015-02-01 04:40:09 -05:00
|
|
|
assert_equal("block in #{__method__}", loc.label)
|
|
|
|
begin
|
|
|
|
raise
|
|
|
|
rescue
|
|
|
|
assert_equal("rescue in #{__method__}", caller_locations(0, 1)[0].label)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-19 01:07:06 -05:00
|
|
|
def th_rec q, n=10
|
|
|
|
if n > 1
|
|
|
|
th_rec q, n-1
|
|
|
|
else
|
|
|
|
q.pop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_thread_backtrace
|
|
|
|
begin
|
2016-08-30 02:22:30 -04:00
|
|
|
q = Thread::Queue.new
|
2012-11-19 01:07:06 -05:00
|
|
|
th = Thread.new{
|
|
|
|
th_rec q
|
|
|
|
}
|
|
|
|
sleep 0.5
|
|
|
|
th_backtrace = th.backtrace
|
|
|
|
th_locations = th.backtrace_locations
|
|
|
|
|
|
|
|
assert_equal(10, th_backtrace.count{|e| e =~ /th_rec/})
|
|
|
|
assert_equal(th_backtrace, th_locations.map{|e| e.to_s})
|
|
|
|
assert_equal(th_backtrace, th.backtrace(0))
|
|
|
|
assert_equal(th_locations.map{|e| e.to_s},
|
|
|
|
th.backtrace_locations(0).map{|e| e.to_s})
|
|
|
|
th_backtrace.size.times{|n|
|
|
|
|
assert_equal(n, th.backtrace(0, n).size)
|
|
|
|
assert_equal(n, th.backtrace_locations(0, n).size)
|
|
|
|
}
|
|
|
|
n = th_backtrace.size
|
|
|
|
assert_equal(n, th.backtrace(0, n + 1).size)
|
|
|
|
assert_equal(n, th.backtrace_locations(0, n + 1).size)
|
|
|
|
ensure
|
|
|
|
q << true
|
2014-05-26 01:38:10 -04:00
|
|
|
th.join
|
2012-11-19 01:07:06 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-23 03:08:24 -05:00
|
|
|
|
|
|
|
def test_thread_backtrace_locations_with_range
|
|
|
|
begin
|
2016-08-30 02:22:30 -04:00
|
|
|
q = Thread::Queue.new
|
2013-02-23 03:08:24 -05:00
|
|
|
th = Thread.new{
|
|
|
|
th_rec q
|
|
|
|
}
|
|
|
|
sleep 0.5
|
|
|
|
bt = th.backtrace(0,2)
|
|
|
|
locs = th.backtrace_locations(0..1).map { |loc|
|
|
|
|
loc.to_s
|
|
|
|
}
|
|
|
|
assert_equal(bt, locs)
|
|
|
|
ensure
|
|
|
|
q << true
|
2014-05-26 01:13:40 -04:00
|
|
|
th.join
|
2013-02-23 03:08:24 -05:00
|
|
|
end
|
|
|
|
end
|
2014-05-21 02:11:22 -04:00
|
|
|
|
|
|
|
def test_core_backtrace_alias
|
|
|
|
obj = BasicObject.new
|
|
|
|
e = assert_raise(NameError) do
|
|
|
|
class << obj
|
|
|
|
alias foo bar
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_not_match(/\Acore#/, e.backtrace_locations[0].base_label)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_core_backtrace_undef
|
|
|
|
obj = BasicObject.new
|
|
|
|
e = assert_raise(NameError) do
|
|
|
|
class << obj
|
|
|
|
undef foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_not_match(/\Acore#/, e.backtrace_locations[0].base_label)
|
|
|
|
end
|
2014-05-21 02:11:25 -04:00
|
|
|
|
|
|
|
def test_core_backtrace_hash_merge
|
|
|
|
e = assert_raise(TypeError) do
|
|
|
|
{**nil}
|
|
|
|
end
|
|
|
|
assert_not_match(/\Acore#/, e.backtrace_locations[0].base_label)
|
|
|
|
end
|
2012-05-25 07:01:01 -04:00
|
|
|
end
|