2004-11-17 09:48:35 -05:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestSetTraceFunc < Test::Unit::TestCase
|
2007-11-12 10:38:10 -05:00
|
|
|
def setup
|
2008-06-29 13:26:16 -04:00
|
|
|
@original_compile_option = RubyVM::InstructionSequence.compile_option
|
|
|
|
RubyVM::InstructionSequence.compile_option = {
|
2007-11-12 10:38:10 -05:00
|
|
|
:trace_instruction => true,
|
|
|
|
:specialized_instruction => false
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2008-06-30 09:58:35 -04:00
|
|
|
set_trace_func(nil)
|
2008-06-29 13:26:16 -04:00
|
|
|
RubyVM::InstructionSequence.compile_option = @original_compile_option
|
2007-11-12 10:38:10 -05:00
|
|
|
end
|
2005-04-07 04:38:05 -04:00
|
|
|
|
2007-11-12 10:38:10 -05:00
|
|
|
def test_c_call
|
2005-04-07 04:38:05 -04:00
|
|
|
events = []
|
2007-11-12 10:38:10 -05:00
|
|
|
eval <<-EOF.gsub(/^.*?: /, "")
|
|
|
|
1: set_trace_func(Proc.new { |event, file, lineno, mid, binding, klass|
|
|
|
|
2: events << [event, lineno, mid, klass]
|
|
|
|
3: })
|
|
|
|
4: x = 1 + 1
|
|
|
|
5: set_trace_func(nil)
|
|
|
|
EOF
|
|
|
|
assert_equal(["c-return", 3, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 4, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 4, :+, Fixnum],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-return", 4, :+, Fixnum],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 5, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 5, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
|
|
|
assert_equal([], events)
|
2005-04-07 04:38:05 -04:00
|
|
|
end
|
2004-11-17 09:48:35 -05:00
|
|
|
|
2007-11-12 10:38:10 -05:00
|
|
|
def test_call
|
2004-11-17 09:48:35 -05:00
|
|
|
events = []
|
2007-11-12 10:38:10 -05:00
|
|
|
eval <<-EOF.gsub(/^.*?: /, "")
|
|
|
|
1: set_trace_func(Proc.new { |event, file, lineno, mid, binding, klass|
|
|
|
|
2: events << [event, lineno, mid, klass]
|
|
|
|
3: })
|
|
|
|
4: def add(x, y)
|
|
|
|
5: x + y
|
|
|
|
6: end
|
|
|
|
7: x = add(1, 1)
|
|
|
|
8: set_trace_func(nil)
|
|
|
|
EOF
|
|
|
|
assert_equal(["c-return", 3, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 4, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 4, :method_added, Module],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-return", 4, :method_added, Module],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 7, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["call", 6, :add, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 5, :add, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 5, :+, Fixnum],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-return", 5, :+, Fixnum],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["return", 6, :add, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 8, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 8, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
|
|
|
assert_equal([], events)
|
|
|
|
end
|
2004-11-17 09:48:35 -05:00
|
|
|
|
2007-11-12 10:38:10 -05:00
|
|
|
def test_class
|
|
|
|
events = []
|
|
|
|
eval <<-EOF.gsub(/^.*?: /, "")
|
|
|
|
1: set_trace_func(Proc.new { |event, file, lineno, mid, binding, klass|
|
|
|
|
2: events << [event, lineno, mid, klass]
|
|
|
|
3: })
|
|
|
|
4: class Foo
|
|
|
|
5: def bar
|
|
|
|
6: end
|
|
|
|
7: end
|
|
|
|
8: x = Foo.new.bar
|
|
|
|
9: set_trace_func(nil)
|
|
|
|
EOF
|
|
|
|
assert_equal(["c-return", 3, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 4, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 4, :inherited, Class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-return", 4, :inherited, Class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["class", 7, nil, nil],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 5, nil, nil],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 5, :method_added, Module],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-return", 5, :method_added, Module],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["end", 7, nil, nil],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 8, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 8, :new, Class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 8, :initialize, BasicObject],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-return", 8, :initialize, BasicObject],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-return", 8, :new, Class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["call", 6, :bar, Foo],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["return", 6, :bar, Foo],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 9, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 9, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
2005-03-22 10:43:27 -05:00
|
|
|
assert_equal([], events)
|
2007-11-12 10:38:10 -05:00
|
|
|
end
|
2005-04-07 04:38:05 -04:00
|
|
|
|
2007-11-12 10:38:10 -05:00
|
|
|
def test_raise
|
|
|
|
events = []
|
|
|
|
eval <<-EOF.gsub(/^.*?: /, "")
|
|
|
|
1: set_trace_func(Proc.new { |event, file, lineno, mid, binding, klass|
|
|
|
|
2: events << [event, lineno, mid, klass]
|
|
|
|
3: })
|
2008-06-30 09:58:35 -04:00
|
|
|
4: begin
|
|
|
|
5: raise TypeError, "error"
|
|
|
|
6: rescue TypeError
|
|
|
|
7: end
|
|
|
|
8: set_trace_func(nil)
|
2007-11-12 10:38:10 -05:00
|
|
|
EOF
|
|
|
|
assert_equal(["c-return", 3, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 4, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 4, :raise, Kernel],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 4, :new, Class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 4, :initialize, Exception],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-return", 4, :initialize, Exception],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-return", 4, :new, Class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 4, :backtrace, Exception],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-return", 4, :backtrace, Exception],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 4, :set_backtrace, Exception],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-return", 4, :set_backtrace, Exception],
|
|
|
|
events.shift)
|
2008-02-29 00:16:39 -05:00
|
|
|
assert_equal(["raise", 4, :test_raise, TestSetTraceFunc],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 4, :===, Module],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-return", 4, :===, Module],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 5, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 5, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
2005-04-07 04:38:05 -04:00
|
|
|
assert_equal([], events)
|
2004-11-17 09:48:35 -05:00
|
|
|
end
|
2008-04-23 11:22:13 -04:00
|
|
|
|
|
|
|
def test_invalid_proc
|
|
|
|
assert_raise(TypeError) { set_trace_func(1) }
|
|
|
|
end
|
2004-11-17 09:48:35 -05:00
|
|
|
end
|