File: unique_call_path_test.rb

package info (click to toggle)
ruby-prof 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,048 kB
  • sloc: ruby: 9,805; ansic: 2,968; makefile: 7
file content (136 lines) | stat: -rw-r--r-- 3,833 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env ruby
# encoding: UTF-8

require File.expand_path('../test_helper', __FILE__)

class UniqueCallPath
  def method_a(i)
    if i==1
      method_b
    else
      method_c
    end
  end

  def method_b
    method_c
  end

  def method_c
  end

  def method_k(i)
    method_a(i)
  end
end


# --  Tests ----
class UniqueCallPathTest < TestCase
  def test_root
    unique_call_path = UniqueCallPath.new

    result = RubyProf::Profile.profile do
      unique_call_path.method_a(1)
    end

    root_call_info = result.threads.first.call_tree
    assert_equal("UniqueCallPathTest#test_root", root_call_info.target.full_name)
  end

  def test_root_children
    unique_call_path = UniqueCallPath.new

    result = RubyProf::Profile.profile do
      unique_call_path.method_a(1)
      unique_call_path.method_k(2)
    end

    root_call_info = result.threads.first.call_tree
    children = root_call_info.children.sort do |c1, c2|
      c1.target.full_name <=> c2.target.full_name
    end

    assert_equal(2, children.length)
    assert_equal("UniqueCallPath#method_a", children[0].target.full_name)
    assert_equal("UniqueCallPath#method_k", children[1].target.full_name)
  end

  def test_children_of
    unique_call_path = UniqueCallPath.new

    result = RubyProf::Profile.profile do
      unique_call_path.method_a(1)
      unique_call_path.method_k(2)
    end

    root_call_info = result.threads.first.call_tree
    assert_equal("UniqueCallPathTest#test_children_of", root_call_info.target.full_name)

    call_info_a = root_call_info.children.detect do |call_tree|
      call_tree.target.full_name == "UniqueCallPath#method_a"
    end
    refute_nil(call_info_a)

    _children_of_a = call_info_a.children.inject(Array.new) do |array, c|
      if c.parent.eql?(call_info_a)
        array << c
      end
      array
    end

    if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.1')
      assert_equal(1, call_info_a.children.length)
      assert_equal("UniqueCallPath#method_b", call_info_a.children[0].target.full_name)
    else
      assert_equal(2, call_info_a.children.length)
      assert_equal("Integer#==", call_info_a.children[0].target.full_name)
      assert_equal("UniqueCallPath#method_b", call_info_a.children[1].target.full_name)
    end
  end

  def test_unique_path
    unique_call_path = UniqueCallPath.new

    result = RubyProf::Profile.profile do
      unique_call_path.method_a(1)
      unique_call_path.method_k(1)
    end

    root_call_info = result.threads.first.call_tree
    assert_equal("UniqueCallPathTest#test_unique_path", root_call_info.target.full_name)

    call_info_a = root_call_info.children.detect do |call_tree|
      call_tree.target.full_name == "UniqueCallPath#method_a"
    end
    refute_nil(call_info_a)

    children_of_a = call_info_a.children.reduce(Array.new) do |array, c|
      if c.parent.eql?(call_info_a)
        array << c
      end
      array
    end

    children_of_a = children_of_a.sort do |c1, c2|
      c1.target.full_name <=> c2.target.full_name
    end

    if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.1')
      assert_equal(1, call_info_a.children.length)
      assert_equal(1, children_of_a.length)

      assert_equal(1, children_of_a[0].called)
      assert_equal("UniqueCallPath#method_b", children_of_a[0].target.full_name)
    else
      assert_equal(2, call_info_a.children.length)
      assert_equal(2, children_of_a.length)

      assert_equal(1, children_of_a[0].called)
      assert_equal("Integer#==", children_of_a[0].target.full_name)

      assert_equal(1, children_of_a[1].called)
      assert_equal("UniqueCallPath#method_b", children_of_a[1].target.full_name)
    end
  end
end