From a582a22fa05e4157d85ce64208dfd1fbb64c44a4 Mon Sep 17 00:00:00 2001 From: makoto kuwata Date: Mon, 27 Sep 2010 06:59:22 +0900 Subject: [PATCH 2/2] add test script for Kernel#called_from() --- test/ruby/test_called_from.rb | 77 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 77 insertions(+), 0 deletions(-) create mode 100644 test/ruby/test_called_from.rb diff --git a/test/ruby/test_called_from.rb b/test/ruby/test_called_from.rb new file mode 100644 index 0000000..0dd5a91 --- /dev/null +++ b/test/ruby/test_called_from.rb @@ -0,0 +1,77 @@ +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(["
", nil, "
"], a) + end + spec 'return nil if stack frame not found' do + a = eval('called_from', TOPLEVEL_BINDING) + assert_nil(a) + end + end + +end -- 1.7.2.3