diff --git a/core/tracepoint/binding_spec.rb b/core/tracepoint/binding_spec.rb new file mode 100644 index 0000000000..afdcd75671 --- /dev/null +++ b/core/tracepoint/binding_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +ruby_version_is '2.0' do + describe 'TracePoint#binding' do + def test + secret = 42 + end + + it 'return the generated binding object from event' do + bindings = [] + TracePoint.new(:return) { |tp| + bindings << tp.binding + }.enable { + test + } + bindings.size.should == 1 + bindings[0].should be_kind_of(Binding) + bindings[0].local_variables.should == [:secret] + end + end +end diff --git a/core/tracepoint/defined_class_spec.rb b/core/tracepoint/defined_class_spec.rb new file mode 100644 index 0000000000..84005bbcc7 --- /dev/null +++ b/core/tracepoint/defined_class_spec.rb @@ -0,0 +1,28 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +ruby_version_is '2.0' do + describe 'TracePoint#defined_class' do + it 'returns class or module of the method being called' do + last_class_name = nil + TracePoint.new(:call) do |tp| + last_class_name = tp.defined_class + end.enable do + TracePointSpec::B.new.foo + last_class_name.should equal(TracePointSpec::B) + + TracePointSpec::B.new.bar + last_class_name.should equal(TracePointSpec::A) + + c = TracePointSpec::C.new + last_class_name.should equal(TracePointSpec::C) + + c.foo + last_class_name.should equal(TracePointSpec::B) + + c.bar + last_class_name.should equal(TracePointSpec::A) + end + end + end +end diff --git a/core/tracepoint/disable_spec.rb b/core/tracepoint/disable_spec.rb new file mode 100644 index 0000000000..60d5a659e0 --- /dev/null +++ b/core/tracepoint/disable_spec.rb @@ -0,0 +1,66 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +ruby_version_is '2.0' do + describe 'TracePoint#disable' do + def test; end + it 'returns true if trace was enabled' do + event_name, method_name = nil + trace = TracePoint.new(:call) do |tp| + event_name = tp.event + method_name = tp.method_id + end + + trace.enable + trace.disable.should be_true + event_name, method_name = nil + test + method_name.equal?(:test).should be_false + event_name.should equal(nil) + end + + it 'returns false if trace was disabled' do + event_name, method_name = nil + trace = TracePoint.new(:call) do |tp| + event_name = tp.event + method_name = tp.method_id + end + + trace.disable.should be_false + event_name, method_name = nil + test + method_name.equal?(:test).should be_false + event_name.should equal(nil) + end + + it 'is disabled within a block & is enabled outside the block' do + enabled = nil + trace = TracePoint.new(:line) {} + trace.enable + trace.disable { enabled = trace.enabled? } + enabled.should be_false + trace.enabled?.should be_true + trace.disable + end + + it 'is disabled within a block & also returns false when its called with a block' do + trace = TracePoint.new(:line) {} + trace.enable + trace.disable { trace.enabled? }.should == false + trace.enabled?.should equal(true) + trace.disable + end + + ruby_bug "#14057", "2.0"..."2.5" do + it 'can accept param within a block but it should not yield arguments' do + event_name = nil + trace = TracePoint.new(:line) {} + trace.enable + trace.disable do |*args| + args.should == [] + end + trace.enabled?.should be_true + trace.disable + end + end + end +end diff --git a/core/tracepoint/enable_spec.rb b/core/tracepoint/enable_spec.rb new file mode 100644 index 0000000000..1c510bcbb0 --- /dev/null +++ b/core/tracepoint/enable_spec.rb @@ -0,0 +1,94 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +ruby_version_is '2.0' do + describe 'TracePoint#enable' do + def test; end + + describe 'without a block' do + it 'returns true if trace was enabled' do + event_name, method_name = nil, nil + method_name = [] + trace = TracePoint.new(:call) do |tp| + event_name = tp.event + method_name << tp.method_id + end + + test + event_name.should == nil + + trace.enable + test + event_name.should equal(:call) + trace.disable + end + + it 'returns false if trace was disabled' do + event_name, method_name = nil, nil + trace = TracePoint.new(:call) do |tp| + event_name = tp.event + method_name = tp.method_id + end + + trace.enable.should be_false + event_name.should equal(:call) + test + method_name.equal?(:test).should be_true + + trace.disable + event_name, method_name = nil + test + method_name.equal?(:test).should be_false + event_name.should equal(nil) + + trace.enable.should be_false + event_name.should equal(:call) + test + method_name.equal?(:test).should be_true + trace.disable + end + end + + describe 'with a block' do + it 'enables the trace object within a block' do + event_name = nil + TracePoint.new(:line) do |tp| + event_name = tp.event + end.enable { event_name.should equal(:line) } + end + + ruby_bug "#14057", "2.0"..."2.5" do + it 'can accept arguments within a block but it should not yield arguments' do + event_name = nil + trace = TracePoint.new(:line) { |tp| event_name = tp.event } + trace.enable do |*args| + event_name.should equal(:line) + args.should == [] + end + trace.enabled?.should be_false + end + end + + it 'enables trace object on calling with a block if it was already enabled' do + enabled = nil + trace = TracePoint.new(:line) {} + trace.enable + trace.enable { enabled = trace.enabled? } + enabled.should == true + trace.disable + end + + it 'returns value returned by the block' do + trace = TracePoint.new(:line) {} + trace.enable { true; 'test' }.should == 'test' + end + + it 'disables the trace object outside the block' do + event_name = nil + trace = TracePoint.new(:line) { |tp|event_name = tp.event } + trace.enable { '2 + 2' } + event_name.should equal(:line) + trace.enabled?.should be_false + end + end + end +end diff --git a/core/tracepoint/enabled_spec.rb b/core/tracepoint/enabled_spec.rb new file mode 100644 index 0000000000..bed2d1ba0c --- /dev/null +++ b/core/tracepoint/enabled_spec.rb @@ -0,0 +1,16 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +ruby_version_is '2.0' do + describe 'TracePoint#enabled?' do + it 'returns true when current status of the trace is enable' do + trace = TracePoint.new(:call) {} + trace.enable do + trace.enabled?.should be_true + end + end + + it 'returns false when current status of the trace is disabled' do + TracePoint.new(:call) {}.enabled?.should be_false + end + end +end diff --git a/core/tracepoint/event_spec.rb b/core/tracepoint/event_spec.rb new file mode 100644 index 0000000000..1e31ccb7d8 --- /dev/null +++ b/core/tracepoint/event_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +ruby_version_is '2.0' do + describe 'TracePoint#event' do + it 'returns the type of event' 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 B; end + event_name.should equal(:end) + end + + end + end +end diff --git a/core/tracepoint/fixtures/classes.rb b/core/tracepoint/fixtures/classes.rb index fd22ac319d..49c70e1915 100644 --- a/core/tracepoint/fixtures/classes.rb +++ b/core/tracepoint/fixtures/classes.rb @@ -4,5 +4,31 @@ def m end alias_method :m_alias, :m end -end + module A + def bar; end + end + + class B + include A + + def foo; end; + end + + class C < B + def initialize + end + + def foo + super + end + + def bar + super + end + end + + def self.test + 'test' + end +end diff --git a/core/tracepoint/inspect_spec.rb b/core/tracepoint/inspect_spec.rb new file mode 100644 index 0000000000..97d77eedd8 --- /dev/null +++ b/core/tracepoint/inspect_spec.rb @@ -0,0 +1,10 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +ruby_version_is '2.0' do + describe 'TracePoint#inspect' do + it 'returns a string containing a human-readable TracePoint status' do + TracePoint.new(:call) {}.inspect.should == + '#' + end + end +end diff --git a/core/tracepoint/lineno_spec.rb b/core/tracepoint/lineno_spec.rb new file mode 100644 index 0000000000..574765ee5d --- /dev/null +++ b/core/tracepoint/lineno_spec.rb @@ -0,0 +1,12 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +ruby_version_is '2.0' do + describe 'TracePoint#lineno' do + it 'returns the line number of the event' do + lineno = nil + TracePoint.new(:line) { |tp| lineno = tp.lineno }.enable do + lineno.should equal(8) + end + end + end +end diff --git a/core/tracepoint/method_id_spec.rb b/core/tracepoint/method_id_spec.rb new file mode 100644 index 0000000000..62cf2f4044 --- /dev/null +++ b/core/tracepoint/method_id_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +ruby_version_is '2.0' do + describe 'TracePoint#method_id' do + def test; end + + it 'returns the name at the definition of the method being called' do + method_name = nil + TracePoint.new(:call) { |tp| method_name = tp.method_id}.enable do + test + method_name.should equal(:test) + end + end + end +end diff --git a/core/tracepoint/new_spec.rb b/core/tracepoint/new_spec.rb new file mode 100644 index 0000000000..5f8dca344e --- /dev/null +++ b/core/tracepoint/new_spec.rb @@ -0,0 +1,70 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +ruby_version_is '2.0' do + describe 'TracePoint.new' do + it 'returns a new TracePoint object, not enabled by default' do + TracePoint.new(:call) {}.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('return')).should_receive(:to_sym).and_return(:return) + + TracePoint.new(o) { |tp| event_name = tp.event}.enable do + event_name.should equal(nil) + TracePointSpec.test + event_name.should equal(:return) + 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 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.0"..."2.5" do + it 'expects to be called with a block' do + -> { TracePoint.new(:line) }.should raise_error(ArgumentError) + end + end + + it "raises a Argument error when the give argument doesn't match an event name" do + -> { TracePoint.new(:test) }.should raise_error(ArgumentError) + end + end +end diff --git a/core/tracepoint/path_spec.rb b/core/tracepoint/path_spec.rb new file mode 100644 index 0000000000..39d1a0063b --- /dev/null +++ b/core/tracepoint/path_spec.rb @@ -0,0 +1,20 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +ruby_version_is '2.0' do + describe 'TracePoint#path' do + it 'returns the path of the file being run' do + path = nil + TracePoint.new(:line) { |tp| path = tp.path }.enable do + path.should == "#{__FILE__}" + end + end + + it 'equals (eval) inside an eval for :end event' do + path = nil + TracePoint.new(:end) { |tp| path = tp.path }.enable do + eval("class A; end") + path.should == '(eval)' + end + end + end +end diff --git a/core/tracepoint/raised_exception_spec.rb b/core/tracepoint/raised_exception_spec.rb new file mode 100644 index 0000000000..2b07878cd7 --- /dev/null +++ b/core/tracepoint/raised_exception_spec.rb @@ -0,0 +1,18 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +ruby_version_is '2.0' do + describe 'TracePoint#raised_exception' do + it 'returns value from exception raised on the :raise event' do + raised_exception, error_result = nil + trace = TracePoint.new(:raise) { |tp| raised_exception = tp.raised_exception } + trace.enable do + begin + raise StandardError + rescue => e + error_result = e + end + raised_exception.should equal(error_result) + end + end + end +end diff --git a/core/tracepoint/return_value_spec.rb b/core/tracepoint/return_value_spec.rb new file mode 100644 index 0000000000..cffb021d20 --- /dev/null +++ b/core/tracepoint/return_value_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +ruby_version_is '2.0' do + describe 'TracePoint#return_value' do + def test; 'test' end + + it 'returns value from :return event' do + trace_value = nil + TracePoint.new(:return) { |tp| trace_value = tp.return_value}.enable do + test + trace_value.should == 'test' + end + end + end +end diff --git a/core/tracepoint/self_spec.rb b/core/tracepoint/self_spec.rb new file mode 100644 index 0000000000..638f9711f7 --- /dev/null +++ b/core/tracepoint/self_spec.rb @@ -0,0 +1,12 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +ruby_version_is '2.0' do + describe 'TracePoint#self' do + it 'return the trace object from event' do + trace = nil + TracePoint.new(:line) { |tp| trace = tp.self }.enable do + trace.equal?(self).should be_true + end + end + end +end diff --git a/core/tracepoint/trace_spec.rb b/core/tracepoint/trace_spec.rb new file mode 100644 index 0000000000..2bb0e0897a --- /dev/null +++ b/core/tracepoint/trace_spec.rb @@ -0,0 +1,11 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +ruby_version_is '2.0' do + describe 'TracePoint.trace' do + it 'activates the trace automatically' do + trace = TracePoint.trace(:call) {} + trace.enabled?.should be_true + trace.disable + end + end +end