summaryrefslogtreecommitdiff
path: root/spec/ruby/core/tracepoint/new_spec.rb
blob: d333fd069af4d91dc26f5a561047aabd1af33284 (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
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe 'TracePoint.new' do
  it 'returns a new TracePoint object, not enabled by default' do
    TracePoint.new(:line) {}.enabled?.should be_false
  end

  it 'includes :line event when event is not specified' do
    event_name = nil
    TracePoint.new() { |tp| event_name = tp.event }.enable do
      event_name.should equal(:line)

      event_name = nil
      TracePointSpec.test
      event_name.should equal(:line)

      event_name = nil
      TracePointSpec::B.new.foo
      event_name.should equal(:line)
    end
  end

  it 'converts given event name as string into symbol using to_sym' do
    event_name = nil
    (o = mock('line')).should_receive(:to_sym).and_return(:line)

    TracePoint.new(o) { |tp| event_name = tp.event }.enable do
      line_event = true
      event_name.should == :line
    end
  end

  it 'includes multiple events when multiple event names are passed as params' do
    event_name = nil
    TracePoint.new(:end, :call) do |tp|
      event_name = tp.event
    end.enable do
      TracePointSpec.test
      event_name.should equal(:call)

      TracePointSpec::B.new.foo
      event_name.should equal(:call)

      class TracePointSpec::B; end
      event_name.should equal(:end)
    end
  end

  it 'raises a TypeError when the given object is not a string/symbol' do
    o = mock('123')
    -> { TracePoint.new(o) {}}.should raise_error(TypeError)

    o.should_receive(:to_sym).and_return(123)
    -> { TracePoint.new(o) {}}.should raise_error(TypeError)
  end

  ruby_bug "#140740", ""..."2.5" do
    it 'expects to be called with a block' do
      -> { TracePoint.new(:line) }.should raise_error(ArgumentError, "must be called with a block")
    end
  end

  it "raises a Argument error when the given argument doesn't match an event name" do
    -> { TracePoint.new(:test) }.should raise_error(ArgumentError, "unknown event: test")
  end
end