Project

General

Profile

Feature #3917 ยป 0002-add-test-script-for-Kernel-called_from.patch

test script (patch for 1.9.2-p0) - kwatch (makoto kuwata), 10/08/2010 08:45 AM

View differences:

test/ruby/test_called_from.rb
1
require 'test/unit'
2

  
3

  
4
class TestCalledFrom < Test::Unit::TestCase
5

  
6
  def spec(text)
7
    yield
8
  end
9

  
10
  def setup
11
    #@fname = File.basename(__FILE__)
12
    #@fname = File.expand_path(File.basename(__FILE__))
13
    @fname = caller(0).first.split(/:\d+:in/).first
14
  end
15

  
16
  def f1(n)
17
    @f1_linenum = __LINE__ + 1
18
    f2(n)
19
  end
20

  
21
  def f2(n)
22
    @f2_linenum = __LINE__ + 1
23
    f3(n)
24
  end
25

  
26
  def f3(n)
27
    @f3_linenum = __LINE__ + 1
28
    f4(n)
29
  end
30

  
31
  def f4(n)
32
    @f4_linenum = __LINE__ + 1
33
    called_from(n)
34
  end
35

  
36
  def test_called_from_return_value
37
    spec 'returns array of filename, line number, and function name' do
38
      a = f1(nil)
39
      assert_equal([@fname, @f3_linenum, "f3"], a)
40
    end
41
  end
42

  
43
  def test_called_from_with_1
44
    spec 'if level is not specified then returns the same as arg 1 specified' do
45
      e = f1(1)
46
      a = f1(nil)
47
      assert_equal(e, a)
48
    end
49
  end
50

  
51
  def test_called_from_with_n
52
    spec 'level is specified then returns array according to level' do
53
      a = f1(2)
54
      assert_equal([@fname, @f2_linenum, "f2"], a)
55
      a = f1(3)
56
      assert_equal([@fname, @f1_linenum, "f1"], a)
57
    end
58
  end
59

  
60
  def test_called_from_too_deep
61
    spec 'called with too deep level then returns nil' do
62
      assert_nil(f1(100))
63
    end
64
  end
65

  
66
  def test_called_from_on_toplevel
67
    spec 'line number may be nil' do
68
      a = eval('called_from(0)', TOPLEVEL_BINDING)
69
      assert_equal(["<main>", nil, "<main>"], a)
70
    end
71
    spec 'return nil if stack frame not found' do
72
      a = eval('called_from', TOPLEVEL_BINDING)
73
      assert_nil(a)
74
    end
75
  end
76

  
77
end
0
-