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 137 138 139 140 141 142 143 144 145 146 147 148
|
#!/usr/bin/env ruby
require 'test/unit'
require 'ruby-prof'
require 'test_helper'
class C1
def C1.hello
end
def hello
end
end
module M1
def hello
end
end
class C2
include M1
extend M1
end
class C3
def hello
end
end
module M4
def hello
end
end
module M5
include M4
def goodbye
hello
end
end
class C6
include M5
def test
goodbye
end
end
class BasicTest < Test::Unit::TestCase
def test_running
assert(!RubyProf.running?)
RubyProf.start
assert(RubyProf.running?)
RubyProf.stop
assert(!RubyProf.running?)
end
def test_double_profile
RubyProf.start
assert_raise(RuntimeError) do
RubyProf.start
end
assert_raise(RuntimeError) do
RubyProf.profile do
puts 1
end
end
RubyProf.stop
end
def test_no_block
assert_raise(ArgumentError) do
RubyProf.profile
end
end
def test_class_and_instance_methods
result = RubyProf.profile do
C1.hello
C1.new.hello
end
methods = result.threads.values.first
# Length should be 6:
# 1 top level,
# 1 Class.new
# 1 Class:Object allocate
# 1 for Object.initialize
# 1 for Class hello
# 1 for Object hello
assert_equal(6, methods.length)
# Check class method
method1 = methods['<Class::C1>#hello']
assert_not_nil(method1)
# Check instance method
method1 = methods['C1#hello']
assert_not_nil(method1)
end
def test_module_methods
result = RubyProf.profile do
C2.hello
C2.new.hello
end
methods = result.threads.values.first
# Length should be 5:
# 1 top level,
# 1 Class.new
# 1 Class:Object allocate
# 1 for Object.initialize
# 1 for hello
assert_equal(5, methods.length)
# Check class method
method1 = methods['M1#hello']
assert_not_nil(method1)
assert_equal(2, method1.called)
end
def test_singleton
c3 = C3.new
class << c3
def hello
end
end
result = RubyProf.profile do
c3.hello
end
methods = result.threads.values.first
# Length should be 2 - one for top level
# and one for the singleton method.
assert_equal(2, methods.length)
# Check singleton method
method1 = methods['<Object::C3>#hello']
assert_not_nil(method1)
end
end
|