Feature #3917 » 0002-add-test-script-for-Kernel-called_from.patch
test/ruby/test_called_from.rb | ||
---|---|---|
require 'test/unit'
|
||
class TestCalledFrom < Test::Unit::TestCase
|
||
def spec(text)
|
||
yield
|
||
end
|
||
def setup
|
||
#@fname = File.basename(__FILE__)
|
||
#@fname = File.expand_path(File.basename(__FILE__))
|
||
@fname = caller(0).first.split(/:\d+:in/).first
|
||
end
|
||
def f1(n)
|
||
@f1_linenum = __LINE__ + 1
|
||
f2(n)
|
||
end
|
||
def f2(n)
|
||
@f2_linenum = __LINE__ + 1
|
||
f3(n)
|
||
end
|
||
def f3(n)
|
||
@f3_linenum = __LINE__ + 1
|
||
f4(n)
|
||
end
|
||
def f4(n)
|
||
@f4_linenum = __LINE__ + 1
|
||
called_from(n)
|
||
end
|
||
def test_called_from_return_value
|
||
spec 'returns array of filename, line number, and function name' do
|
||
a = f1(nil)
|
||
assert_equal([@fname, @f3_linenum, "f3"], a)
|
||
end
|
||
end
|
||
def test_called_from_with_1
|
||
spec 'if level is not specified then returns the same as arg 1 specified' do
|
||
e = f1(1)
|
||
a = f1(nil)
|
||
assert_equal(e, a)
|
||
end
|
||
end
|
||
def test_called_from_with_n
|
||
spec 'level is specified then returns array according to level' do
|
||
a = f1(2)
|
||
assert_equal([@fname, @f2_linenum, "f2"], a)
|
||
a = f1(3)
|
||
assert_equal([@fname, @f1_linenum, "f1"], a)
|
||
end
|
||
end
|
||
def test_called_from_too_deep
|
||
spec 'called with too deep level then returns nil' do
|
||
assert_nil(f1(100))
|
||
end
|
||
end
|
||
def test_called_from_on_toplevel
|
||
spec 'line number may be nil' do
|
||
a = eval('called_from(0)', TOPLEVEL_BINDING)
|
||
assert_equal(["<main>", nil, "<main>"], a)
|
||
end
|
||
spec 'return nil if stack frame not found' do
|
||
a = eval('called_from', TOPLEVEL_BINDING)
|
||
assert_nil(a)
|
||
end
|
||
end
|
||
end
|