summaryrefslogtreecommitdiff
path: root/spec/ruby/core/tracepoint/inspect_spec.rb
blob: 6cc2ebe24342803315e2186d47cf4aa2825fca10 (plain)
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
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe 'TracePoint#inspect' do
  before do
    @path_prefix = ' '
  end

  it 'returns a string containing a human-readable TracePoint status' do
    TracePoint.new(:line) {}.inspect.should == '#<TracePoint:disabled>'
  end

  it "shows only whether it's enabled when outside the TracePoint handler" do
    trace = TracePoint.new(:line) {}
    trace.enable

    trace.inspect.should == '#<TracePoint:enabled>'

    trace.disable
  end

  it 'returns a String showing the event, path and line' do
    inspect = nil
    line = nil
    TracePoint.new(:line) { |tp|
      next unless TracePointSpec.target_thread?
      next unless tp.path == __FILE__

      inspect ||= tp.inspect
    }.enable do
      line = __LINE__
    end

    inspect.should == "#<TracePoint:line#{@path_prefix}#{__FILE__}:#{line}>"
  end

  it 'returns a String showing the event, method, path and line for a :call event' do
    inspect = nil
    line = nil
    TracePoint.new(:call) { |tp|
      next unless TracePointSpec.target_thread?
      next unless tp.path == __FILE__

      inspect ||= tp.inspect
    }.enable do
      line = __LINE__ + 1
      def trace_point_spec_test_call; end
      trace_point_spec_test_call
    end

    inspect.should =~ /\A#<TracePoint:call [`']trace_point_spec_test_call'#{@path_prefix}#{__FILE__}:#{line}>\z/
  end

  it 'returns a String showing the event, method, path and line for a :return event' do
    inspect = nil
    line = nil
    TracePoint.new(:return) { |tp|
      next unless TracePointSpec.target_thread?
      next unless tp.path == __FILE__

      inspect ||= tp.inspect
    }.enable do
      line = __LINE__ + 4
      def trace_point_spec_test_return
        a = 1
        return a
      end
      trace_point_spec_test_return
    end
    ruby_version_is("3.4") { line -= 1 }

    inspect.should =~ /\A#<TracePoint:return [`']trace_point_spec_test_return'#{@path_prefix}#{__FILE__}:#{line}>\z/
  end

  it 'returns a String showing the event, method, path and line for a :c_call event' do
    inspect = nil
    tracepoint = TracePoint.new(:c_call) { |tp|
      next unless TracePointSpec.target_thread?
      next unless tp.path == __FILE__

      inspect ||= tp.inspect
    }
    line = __LINE__ + 2
    tracepoint.enable do
      [0, 1].max
    end

    inspect.should =~ /\A#<TracePoint:c_call [`']max'#{@path_prefix}#{__FILE__}:#{line}>\z/
  end

  it 'returns a String showing the event, path and line for a :class event' do
    inspect = nil
    line = nil
    TracePoint.new(:class) { |tp|
      next unless TracePointSpec.target_thread?
      next unless tp.path == __FILE__

      inspect ||= tp.inspect
    }.enable do
      line = __LINE__ + 1
      class TracePointSpec::C
      end
    end

    inspect.should == "#<TracePoint:class#{@path_prefix}#{__FILE__}:#{line}>"
  end

  it 'returns a String showing the event and thread for :thread_begin event' do
    inspect = nil
    thread = nil
    thread_inspection = nil
    TracePoint.new(:thread_begin) { |tp|
      next unless Thread.current == thread

      inspect ||= tp.inspect
    }.enable(target_thread: nil) do
      thread = Thread.new {}
      thread_inspection = thread.inspect
      thread.join
    end

    inspect.should == "#<TracePoint:thread_begin #{thread_inspection}>"
  end

  it 'returns a String showing the event and thread for :thread_end event' do
    inspect = nil
    thread = nil
    thread_inspection = nil
    TracePoint.new(:thread_end) { |tp|
      next unless Thread.current == thread

      inspect ||= tp.inspect
    }.enable(target_thread: nil) do
      thread = Thread.new {}
      thread_inspection = thread.inspect
      thread.join
    end

    inspect.should == "#<TracePoint:thread_end #{thread_inspection}>"
  end
end