1require 'dtrace/helper'
2
3module DTrace
4  class TestFunctionEntry < TestCase
5    def test_function_entry
6      probe = <<-eoprobe
7ruby$target:::method-entry
8/arg0 && arg1 && arg2/
9{
10  printf("%s %s %s %d\\n", copyinstr(arg0), copyinstr(arg1), copyinstr(arg2), arg3);
11}
12      eoprobe
13
14      trap_probe(probe, ruby_program) { |d_file, rb_file, probes|
15	foo_calls = probes.map { |line| line.split }.find_all { |row|
16	  row.first == 'Foo'  && row[1] == 'foo'
17	}
18
19	assert_equal 10, foo_calls.length
20	line = '2'
21	foo_calls.each { |f| assert_equal line, f[3] }
22	foo_calls.each { |f| assert_equal rb_file, f[2] }
23      }
24    end
25
26    def test_function_return
27      probe = <<-eoprobe
28ruby$target:::method-return
29/arg0 && arg1 && arg2/
30{
31  printf("%s %s %s %d\\n", copyinstr(arg0), copyinstr(arg1), copyinstr(arg2), arg3);
32}
33      eoprobe
34
35      trap_probe(probe, ruby_program) { |d_file, rb_file, probes|
36	foo_calls = probes.map { |line| line.split }.find_all { |row|
37	  row.first == 'Foo'  && row[1] == 'foo'
38	}
39
40	assert_equal 10, foo_calls.length
41	line = '2'
42	foo_calls.each { |f| assert_equal line, f[3] }
43	foo_calls.each { |f| assert_equal rb_file, f[2] }
44      }
45    end
46
47    def test_return_from_raise
48      program = <<-eoruby
49      class Foo
50        def bar; raise; end
51        def baz
52          bar
53        rescue
54        end
55      end
56
57      Foo.new.baz
58      eoruby
59
60      probe = <<-eoprobe
61ruby$target:::method-return
62/arg0 && arg1 && arg2/
63{
64  printf("%s %s %s %d\\n", copyinstr(arg0), copyinstr(arg1), copyinstr(arg2), arg3);
65}
66      eoprobe
67
68      trap_probe(probe, program) { |d_file, rb_file, probes|
69	foo_calls = probes.map { |line| line.split }.find_all { |row|
70	  row.first == 'Foo'  && row[1] == 'bar'
71	}
72        assert foo_calls.any?
73      }
74    end
75
76    private
77    def ruby_program
78      <<-eoruby
79      class Foo
80	def foo; end
81      end
82      x = Foo.new
83      10.times { x.foo }
84      eoruby
85    end
86  end
87end if defined?(DTrace::TestCase)
88