diff options
author | Benoit Daloze <eregontp@gmail.com> | 2020-05-31 18:22:49 +0200 |
---|---|---|
committer | Benoit Daloze <eregontp@gmail.com> | 2020-05-31 18:22:49 +0200 |
commit | 34776105c8a6739ca3aad1de4a2c942f4a8f2f29 (patch) | |
tree | 0103cf2cc89c1322d8c3e88fff0a80b54db8320b /spec/ruby/core | |
parent | f4502b001a665109bf776f9037ecbc52cb5f2d88 (diff) | |
download | ruby-34776105c8a6739ca3aad1de4a2c942f4a8f2f29.tar.gz |
Update to ruby/spec@4e486fa
Diffstat (limited to 'spec/ruby/core')
32 files changed, 355 insertions, 87 deletions
diff --git a/spec/ruby/core/array/fixtures/classes.rb b/spec/ruby/core/array/fixtures/classes.rb index 42071ed0cd..2791a2eb5d 100644 --- a/spec/ruby/core/array/fixtures/classes.rb +++ b/spec/ruby/core/array/fixtures/classes.rb @@ -144,7 +144,7 @@ module ArraySpecs end def self.universal_pack_object - obj = mock("string float int") + obj = mock("string float int".freeze) obj.stub!(:to_int).and_return(1) obj.stub!(:to_str).and_return("1") obj.stub!(:to_f).and_return(1.0) diff --git a/spec/ruby/core/array/shared/slice.rb b/spec/ruby/core/array/shared/slice.rb index d6b4547b2b..f36890fa4e 100644 --- a/spec/ruby/core/array/shared/slice.rb +++ b/spec/ruby/core/array/shared/slice.rb @@ -466,6 +466,12 @@ describe :array_slice, shared: true do obj = 8e19 -> { array.send(@method, obj) }.should raise_error(RangeError) + + # boundary value when longs are 64 bits + -> { array.send(@method, 2.0**63) }.should raise_error(RangeError) + + # just under the boundary value when longs are 64 bits + array.send(@method, max_long.to_f.prev_float).should == nil end it "raises a RangeError when the length is out of range of Fixnum" do diff --git a/spec/ruby/core/enumerator/new_spec.rb b/spec/ruby/core/enumerator/new_spec.rb index 15e42d247e..100edc8455 100644 --- a/spec/ruby/core/enumerator/new_spec.rb +++ b/spec/ruby/core/enumerator/new_spec.rb @@ -75,5 +75,36 @@ describe "Enumerator.new" do enum.to_a.should == ["a\n", "b\n", "c"] end end + + describe 'yielded values' do + it 'handles yield arguments properly' do + Enumerator.new { |y| y.yield(1) }.to_a.should == [1] + Enumerator.new { |y| y.yield(1) }.first.should == 1 + + Enumerator.new { |y| y.yield([1]) }.to_a.should == [[1]] + Enumerator.new { |y| y.yield([1]) }.first.should == [1] + + Enumerator.new { |y| y.yield(1, 2) }.to_a.should == [[1, 2]] + Enumerator.new { |y| y.yield(1, 2) }.first.should == [1, 2] + + Enumerator.new { |y| y.yield([1, 2]) }.to_a.should == [[1, 2]] + Enumerator.new { |y| y.yield([1, 2]) }.first.should == [1, 2] + end + + it 'handles << arguments properly' do + Enumerator.new { |y| y.<<(1) }.to_a.should == [1] + Enumerator.new { |y| y.<<(1) }.first.should == 1 + + Enumerator.new { |y| y.<<([1]) }.to_a.should == [[1]] + Enumerator.new { |y| y.<<([1]) }.first.should == [1] + + # << doesn't accept multiple arguments + # Enumerator.new { |y| y.<<(1, 2) }.to_a.should == [[1, 2]] + # Enumerator.new { |y| y.<<(1, 2) }.first.should == [1, 2] + + Enumerator.new { |y| y.<<([1, 2]) }.to_a.should == [[1, 2]] + Enumerator.new { |y| y.<<([1, 2]) }.first.should == [1, 2] + end + end end end diff --git a/spec/ruby/core/enumerator/yielder/append_spec.rb b/spec/ruby/core/enumerator/yielder/append_spec.rb index dac66585a5..7f7059774d 100644 --- a/spec/ruby/core/enumerator/yielder/append_spec.rb +++ b/spec/ruby/core/enumerator/yielder/append_spec.rb @@ -21,4 +21,27 @@ describe "Enumerator::Yielder#<<" do y = Enumerator::Yielder.new {|x| x + 1} (y << 1).should equal(y) end + + context "when multiple arguments passed" do + ruby_version_is '' ... '2.6' do + it "yields the arguments list to the block" do + ary = [] + y = Enumerator::Yielder.new { |*x| ary << x } + y.<<(1, 2) + + ary.should == [[1, 2]] + end + end + + ruby_version_is '2.6' do + it "raises an ArgumentError" do + ary = [] + y = Enumerator::Yielder.new { |*x| ary << x } + + -> { + y.<<(1, 2) + }.should raise_error(ArgumentError, /wrong number of arguments/) + end + end + end end diff --git a/spec/ruby/core/enumerator/yielder/yield_spec.rb b/spec/ruby/core/enumerator/yielder/yield_spec.rb index 58fc8e007a..acfdf114b6 100644 --- a/spec/ruby/core/enumerator/yielder/yield_spec.rb +++ b/spec/ruby/core/enumerator/yielder/yield_spec.rb @@ -20,4 +20,14 @@ describe "Enumerator::Yielder#yield" do y = Enumerator::Yielder.new {|x| x + 1} y.yield(1).should == 2 end + + context "when multiple arguments passed" do + it "yields the arguments list to the block" do + ary = [] + y = Enumerator::Yielder.new { |*x| ary << x } + y.yield(1, 2) + + ary.should == [[1, 2]] + end + end end diff --git a/spec/ruby/core/integer/left_shift_spec.rb b/spec/ruby/core/integer/left_shift_spec.rb index 4b5ef9386e..f9a8c9bbd1 100644 --- a/spec/ruby/core/integer/left_shift_spec.rb +++ b/spec/ruby/core/integer/left_shift_spec.rb @@ -71,8 +71,11 @@ describe "Integer#<< (with n << m)" do it "calls #to_int to convert the argument to an Integer" do obj = mock("4") obj.should_receive(:to_int).and_return(4) - (3 << obj).should == 48 + + obj = mock("to_int_neg_bignum") + obj.should_receive(:to_int).and_return(-bignum_value) + (3 << obj).should == 0 end it "raises a TypeError when #to_int does not return an Integer" do diff --git a/spec/ruby/core/integer/right_shift_spec.rb b/spec/ruby/core/integer/right_shift_spec.rb index 3eeaf3eb2f..1eac6cb5bc 100644 --- a/spec/ruby/core/integer/right_shift_spec.rb +++ b/spec/ruby/core/integer/right_shift_spec.rb @@ -71,8 +71,11 @@ describe "Integer#>> (with n >> m)" do it "calls #to_int to convert the argument to an Integer" do obj = mock("2") obj.should_receive(:to_int).and_return(2) - (8 >> obj).should == 2 + + obj = mock("to_int_bignum") + obj.should_receive(:to_int).and_return(bignum_value) + (8 >> obj).should == 0 end it "raises a TypeError when #to_int does not return an Integer" do diff --git a/spec/ruby/core/kernel/shared/require.rb b/spec/ruby/core/kernel/shared/require.rb index 69977194e9..28fdb5e451 100644 --- a/spec/ruby/core/kernel/shared/require.rb +++ b/spec/ruby/core/kernel/shared/require.rb @@ -242,6 +242,15 @@ describe :kernel_require, shared: true do @object.require("load_fixture").should be_true ScratchPad.recorded.should == [:loaded] end + + ruby_bug "#16926", "2.7"..."2.8" do + it "does not load a feature twice when $LOAD_PATH has been modified" do + $LOAD_PATH.replace [CODE_LOADING_DIR] + @object.require("load_fixture").should be_true + $LOAD_PATH.replace [File.expand_path("b", CODE_LOADING_DIR), CODE_LOADING_DIR] + @object.require("load_fixture").should be_false + end + end end describe "(file extensions)" do diff --git a/spec/ruby/core/module/autoload_spec.rb b/spec/ruby/core/module/autoload_spec.rb index 6f0ca49dcf..85a0d067d1 100644 --- a/spec/ruby/core/module/autoload_spec.rb +++ b/spec/ruby/core/module/autoload_spec.rb @@ -294,6 +294,32 @@ describe "Module#autoload" do ScratchPad.recorded.should == [nil, nil] @check.call.should == ["constant", nil] end + + it "does not raise an error if the autoload constant was not defined" do + module ModuleSpecs::Autoload + autoload :RequiredDirectlyNoConstant, fixture(__FILE__, "autoload_required_directly_no_constant.rb") + end + @path = fixture(__FILE__, "autoload_required_directly_no_constant.rb") + @remove << :RequiredDirectlyNoConstant + @check = -> { + [ + defined?(ModuleSpecs::Autoload::RequiredDirectlyNoConstant), + ModuleSpecs::Autoload.constants(false).include?(:RequiredDirectlyNoConstant), + ModuleSpecs::Autoload.const_defined?(:RequiredDirectlyNoConstant), + ModuleSpecs::Autoload.autoload?(:RequiredDirectlyNoConstant) + ] + } + ScratchPad.record @check + @check.call.should == ["constant", true, true, @path] + $:.push File.dirname(@path) + begin + require "autoload_required_directly_no_constant.rb" + ensure + $:.pop + end + ScratchPad.recorded.should == [nil, true, false, nil] + @check.call.should == [nil, true, false, nil] + end end describe "after the autoload is triggered by require" do diff --git a/spec/ruby/core/module/fixtures/autoload_required_directly_no_constant.rb b/spec/ruby/core/module/fixtures/autoload_required_directly_no_constant.rb new file mode 100644 index 0000000000..25e08c1129 --- /dev/null +++ b/spec/ruby/core/module/fixtures/autoload_required_directly_no_constant.rb @@ -0,0 +1,2 @@ +block = ScratchPad.recorded +ScratchPad.record(block.call) diff --git a/spec/ruby/core/thread/backtrace/location/absolute_path_spec.rb b/spec/ruby/core/thread/backtrace/location/absolute_path_spec.rb index d38659c257..67f9274f32 100644 --- a/spec/ruby/core/thread/backtrace/location/absolute_path_spec.rb +++ b/spec/ruby/core/thread/backtrace/location/absolute_path_spec.rb @@ -29,33 +29,35 @@ describe 'Thread::Backtrace::Location#absolute_path' do end end - platform_is_not :windows do - before :each do - @file = fixture(__FILE__, "absolute_path.rb") - @symlink = tmp("symlink.rb") - File.symlink(@file, @symlink) - ScratchPad.record [] - end - - after :each do - rm_r @symlink - end - - it "returns a canonical path without symlinks, even when __FILE__ does not" do - realpath = File.realpath(@symlink) - realpath.should_not == @symlink - - load @symlink - ScratchPad.recorded.should == [@symlink, realpath] - end - - it "returns a canonical path without symlinks, even when __FILE__ is removed" do - realpath = File.realpath(@symlink) - realpath.should_not == @symlink - - ScratchPad << -> { rm_r(@symlink) } - load @symlink - ScratchPad.recorded.should == [@symlink, realpath] + context "canonicalization" do + platform_is_not :windows do + before :each do + @file = fixture(__FILE__, "absolute_path.rb") + @symlink = tmp("symlink.rb") + File.symlink(@file, @symlink) + ScratchPad.record [] + end + + after :each do + rm_r @symlink + end + + it "returns a canonical path without symlinks, even when __FILE__ does not" do + realpath = File.realpath(@symlink) + realpath.should_not == @symlink + + load @symlink + ScratchPad.recorded.should == [@symlink, realpath] + end + + it "returns a canonical path without symlinks, even when __FILE__ is removed" do + realpath = File.realpath(@symlink) + realpath.should_not == @symlink + + ScratchPad << -> { rm_r(@symlink) } + load @symlink + ScratchPad.recorded.should == [@symlink, realpath] + end end end end diff --git a/spec/ruby/core/thread/backtrace/location/fixtures/path.rb b/spec/ruby/core/thread/backtrace/location/fixtures/path.rb new file mode 100644 index 0000000000..fba34cb0bc --- /dev/null +++ b/spec/ruby/core/thread/backtrace/location/fixtures/path.rb @@ -0,0 +1,2 @@ +ScratchPad << __FILE__ +ScratchPad << caller_locations(0)[0].path diff --git a/spec/ruby/core/thread/backtrace/location/path_spec.rb b/spec/ruby/core/thread/backtrace/location/path_spec.rb index b1a3439747..93eb4982d8 100644 --- a/spec/ruby/core/thread/backtrace/location/path_spec.rb +++ b/spec/ruby/core/thread/backtrace/location/path_spec.rb @@ -86,4 +86,27 @@ describe 'Thread::Backtrace::Location#path' do end end end + + context "canonicalization" do + platform_is_not :windows do + before :each do + @file = fixture(__FILE__, "path.rb") + @symlink = tmp("symlink.rb") + File.symlink(@file, @symlink) + ScratchPad.record [] + end + + after :each do + rm_r @symlink + end + + it "returns a non-canonical path with symlinks, the same as __FILE__" do + realpath = File.realpath(@symlink) + realpath.should_not == @symlink + + load @symlink + ScratchPad.recorded.should == [@symlink, @symlink] + end + end + end end diff --git a/spec/ruby/core/tracepoint/binding_spec.rb b/spec/ruby/core/tracepoint/binding_spec.rb index f37753602e..6a7ef5f85a 100644 --- a/spec/ruby/core/tracepoint/binding_spec.rb +++ b/spec/ruby/core/tracepoint/binding_spec.rb @@ -1,4 +1,5 @@ require_relative '../../spec_helper' +require_relative 'fixtures/classes' describe 'TracePoint#binding' do def test @@ -8,6 +9,7 @@ describe 'TracePoint#binding' do it 'return the generated binding object from event' do bindings = [] TracePoint.new(:return) { |tp| + next unless TracePointSpec.target_thread? bindings << tp.binding }.enable { test diff --git a/spec/ruby/core/tracepoint/callee_id_spec.rb b/spec/ruby/core/tracepoint/callee_id_spec.rb index d340290d8b..cc08a45504 100644 --- a/spec/ruby/core/tracepoint/callee_id_spec.rb +++ b/spec/ruby/core/tracepoint/callee_id_spec.rb @@ -7,6 +7,7 @@ describe "TracePoint#callee_id" do obj = TracePointSpec::ClassWithMethodAlias.new TracePoint.new(:call) do |tp| + next unless TracePointSpec.target_thread? a << tp.callee_id end.enable do obj.m_alias diff --git a/spec/ruby/core/tracepoint/defined_class_spec.rb b/spec/ruby/core/tracepoint/defined_class_spec.rb index 72536e6a56..4593db6d1f 100644 --- a/spec/ruby/core/tracepoint/defined_class_spec.rb +++ b/spec/ruby/core/tracepoint/defined_class_spec.rb @@ -5,6 +5,7 @@ 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| + next unless TracePointSpec.target_thread? last_class_name = tp.defined_class end.enable do TracePointSpec::B.new.foo diff --git a/spec/ruby/core/tracepoint/disable_spec.rb b/spec/ruby/core/tracepoint/disable_spec.rb index 2ab9b987f4..73a31b3b81 100644 --- a/spec/ruby/core/tracepoint/disable_spec.rb +++ b/spec/ruby/core/tracepoint/disable_spec.rb @@ -1,9 +1,11 @@ require_relative '../../spec_helper' +require_relative 'fixtures/classes' describe 'TracePoint#disable' do it 'returns true if trace was enabled' do called = false trace = TracePoint.new(:line) do |tp| + next unless TracePointSpec.target_thread? called = true end @@ -25,6 +27,7 @@ describe 'TracePoint#disable' do it 'returns false if trace was disabled' do called = false trace = TracePoint.new(:line) do |tp| + next unless TracePointSpec.target_thread? called = true end diff --git a/spec/ruby/core/tracepoint/enable_spec.rb b/spec/ruby/core/tracepoint/enable_spec.rb index 15eb2f1616..aa0c3aa0dc 100644 --- a/spec/ruby/core/tracepoint/enable_spec.rb +++ b/spec/ruby/core/tracepoint/enable_spec.rb @@ -1,20 +1,21 @@ require_relative '../../spec_helper' +require_relative 'fixtures/classes' describe 'TracePoint#enable' do - # def test; end - describe 'without a block' do - it 'returns true if trace was enabled' do + it 'returns false if trace was disabled' do called = false trace = TracePoint.new(:line) do |tp| + next unless TracePointSpec.target_thread? called = true end line_event = true called.should == false - trace.enable + ret = trace.enable begin + ret.should == false line_event = true called.should == true ensure @@ -22,30 +23,27 @@ describe 'TracePoint#enable' do end end - it 'returns false if trace was disabled' do + it 'returns true if trace was already enabled' do called = false trace = TracePoint.new(:line) do |tp| + next unless TracePointSpec.target_thread? called = true end - trace.enable.should == false - begin - line_event = true - called.should == true - ensure - trace.disable - end - - called = false line_event = true called.should == false - trace.enable.should == false + ret = trace.enable begin + ret.should == false + + trace.enable.should == true + line_event = true called.should == true ensure trace.disable + trace.should_not.enabled? end end end @@ -54,13 +52,38 @@ describe 'TracePoint#enable' do it 'enables the trace object within a block' do event_name = nil TracePoint.new(:line) do |tp| + next unless TracePointSpec.target_thread? event_name = tp.event end.enable { event_name.should equal(:line) } end + it 'enables the trace object for any thread' do + threads = [] + trace = TracePoint.new(:line) do |tp| + # Runs on purpose on any Thread + threads << Thread.current + end + + thread = nil + trace.enable do + line_event = true + thread = Thread.new do + event_in_other_thread = true + end + thread.join + end + + threads = threads.uniq + threads.should.include?(Thread.current) + threads.should.include?(thread) + end + 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 = TracePoint.new(:line) do |tp| + next unless TracePointSpec.target_thread? + event_name = tp.event + end trace.enable do |*args| event_name.should equal(:line) args.should == [] @@ -87,7 +110,10 @@ describe 'TracePoint#enable' do it 'disables the trace object outside the block' do called = false - trace = TracePoint.new(:line) { called = true } + trace = TracePoint.new(:line) do + next unless TracePointSpec.target_thread? + called = true + end trace.enable { line_event = true } @@ -96,6 +122,35 @@ describe 'TracePoint#enable' do end end + describe "when nested" do + it "enables both TracePoints but only calls the respective callbacks" do + called = false + first = TracePoint.new(:line) do |tp| + next unless TracePointSpec.target_thread? + called = true + end + + all = [] + inspects = [] + second = TracePoint.new(:line) { |tp| + next unless TracePointSpec.target_thread? + all << tp + inspects << tp.inspect + } + + line = nil + first.enable do + second.enable do + line = __LINE__ + end + end + + all.uniq.should == [second] + inspects.uniq.should == ["#<TracePoint:line@#{__FILE__}:#{line}>"] + called.should == true + end + end + ruby_version_is "2.6" do describe 'target: option' do before :each do @@ -104,6 +159,7 @@ describe 'TracePoint#enable' do it 'enables trace point for specific location' do trace = TracePoint.new(:call) do |tp| + next unless TracePointSpec.target_thread? ScratchPad << tp.method_id end @@ -121,6 +177,7 @@ describe 'TracePoint#enable' do it 'traces all the events triggered in specified location' do trace = TracePoint.new(:line, :call, :return, :b_call, :b_return) do |tp| + next unless TracePointSpec.target_thread? ScratchPad << tp.event end @@ -140,6 +197,7 @@ describe 'TracePoint#enable' do it 'does not trace events in nested locations' do trace = TracePoint.new(:call) do |tp| + next unless TracePointSpec.target_thread? ScratchPad << tp.method_id end @@ -177,6 +235,7 @@ describe 'TracePoint#enable' do end trace = TracePoint.new(:b_call) do |tp| + next unless TracePointSpec.target_thread? ScratchPad << tp.lineno end @@ -193,6 +252,7 @@ describe 'TracePoint#enable' do describe 'option value' do it 'accepts Method' do trace = TracePoint.new(:call) do |tp| + next unless TracePointSpec.target_thread? ScratchPad << tp.method_id end @@ -208,6 +268,7 @@ describe 'TracePoint#enable' do it 'accepts UnboundMethod' do trace = TracePoint.new(:call) do |tp| + next unless TracePointSpec.target_thread? ScratchPad << tp.method_id end @@ -225,6 +286,7 @@ describe 'TracePoint#enable' do it 'accepts Proc' do trace = TracePoint.new(:b_call) do |tp| + next unless TracePointSpec.target_thread? ScratchPad << tp.lineno end @@ -242,6 +304,7 @@ describe 'TracePoint#enable' do it "raises ArgumentError if target object cannot trigger specified event" do trace = TracePoint.new(:call) do |tp| + next unless TracePointSpec.target_thread? ScratchPad << tp.method_id end @@ -255,8 +318,7 @@ describe 'TracePoint#enable' do end it "raises ArgumentError if passed not Method/UnboundMethod/Proc" do - trace = TracePoint.new(:call) do |tp| - end + trace = TracePoint.new(:call) {} -> { trace.enable(target: Object.new) do @@ -266,8 +328,7 @@ describe 'TracePoint#enable' do context "nested enabling and disabling" do it "raises ArgumentError if trace point already enabled with target is re-enabled with target" do - trace = TracePoint.new(:b_call) do - end + trace = TracePoint.new(:b_call) {} -> { trace.enable(target: -> {}) do @@ -278,8 +339,7 @@ describe 'TracePoint#enable' do end it "raises ArgumentError if trace point already enabled without target is re-enabled with target" do - trace = TracePoint.new(:b_call) do - end + trace = TracePoint.new(:b_call) {} -> { trace.enable do @@ -290,8 +350,7 @@ describe 'TracePoint#enable' do end it "raises ArgumentError if trace point already enabled with target is re-enabled without target" do - trace = TracePoint.new(:b_call) do - end + trace = TracePoint.new(:b_call) {} -> { trace.enable(target: -> {}) do @@ -302,8 +361,7 @@ describe 'TracePoint#enable' do end it "raises ArgumentError if trace point already enabled with target is disabled with block" do - trace = TracePoint.new(:b_call) do - end + trace = TracePoint.new(:b_call) {} -> { trace.enable(target: -> {}) do @@ -315,10 +373,12 @@ describe 'TracePoint#enable' do it "traces events when trace point with target is enabled in another trace point enabled without target" do trace_outer = TracePoint.new(:b_call) do |tp| + next unless TracePointSpec.target_thread? ScratchPad << :outer end trace_inner = TracePoint.new(:b_call) do |tp| + next unless TracePointSpec.target_thread? ScratchPad << :inner end @@ -335,10 +395,12 @@ describe 'TracePoint#enable' do it "traces events when trace point with target is enabled in another trace point enabled with target" do trace_outer = TracePoint.new(:b_call) do |tp| + next unless TracePointSpec.target_thread? ScratchPad << :outer end trace_inner = TracePoint.new(:b_call) do |tp| + next unless TracePointSpec.target_thread? ScratchPad << :inner end @@ -355,10 +417,12 @@ describe 'TracePoint#enable' do it "traces events when trace point without target is enabled in another trace point enabled with target" do trace_outer = TracePoint.new(:b_call) do |tp| + next unless TracePointSpec.target_thread? ScratchPad << :outer end trace_inner = TracePoint.new(:b_call) do |tp| + next unless TracePointSpec.target_thread? ScratchPad << :inner end @@ -382,6 +446,7 @@ describe 'TracePoint#enable' do it "traces :line events only on specified line of code" do trace = TracePoint.new(:line) do |tp| + next unless TracePointSpec.target_thread? ScratchPad << tp.lineno end @@ -401,8 +466,7 @@ describe 'TracePoint#enable' do end it "raises ArgumentError if :target option isn't specified" do - trace = TracePoint.new(:line) do |tp| - end + trace = TracePoint.new(:line) {} -> { trace.enable(target_line: 67) do @@ -411,8 +475,7 @@ describe 'TracePoint#enable' do end it "raises ArgumentError if :line event isn't registered" do - trace = TracePoint.new(:call) do |tp| - end + trace = TracePoint.new(:call) {} target = -> { x = 1 @@ -429,8 +492,7 @@ describe 'TracePoint#enable' do end it "raises ArgumentError if :target_line value is out of target code lines range" do - trace = TracePoint.new(:line) do |tp| - end + trace = TracePoint.new(:line) {} -> { trace.enable(target_line: 1, target: -> { }) do @@ -439,8 +501,7 @@ describe 'TracePoint#enable' do end it "raises TypeError if :target_line value couldn't be coerced to Integer" do - trace = TracePoint.new(:line) do |tp| - end + trace = TracePoint.new(:line) {} -> { trace.enable(target_line: Object.new, target: -> { }) do @@ -449,8 +510,7 @@ describe 'TracePoint#enable' do end it "raises ArgumentError if :target_line value is negative" do - trace = TracePoint.new(:line) do |tp| - end + trace = TracePoint.new(:line) {} -> { trace.enable(target_line: -2, target: -> { }) do @@ -460,6 +520,7 @@ describe 'TracePoint#enable' do it "accepts value that could be coerced to Integer" do trace = TracePoint.new(:line) do |tp| + next unless TracePointSpec.target_thread? ScratchPad << tp.lineno end diff --git a/spec/ruby/core/tracepoint/enabled_spec.rb b/spec/ruby/core/tracepoint/enabled_spec.rb index dbc3c214c5..0e9566a02c 100644 --- a/spec/ruby/core/tracepoint/enabled_spec.rb +++ b/spec/ruby/core/tracepoint/enabled_spec.rb @@ -1,4 +1,5 @@ require_relative '../../spec_helper' +require_relative 'fixtures/classes' describe 'TracePoint#enabled?' do it 'returns true when current status of the trace is enable' do diff --git a/spec/ruby/core/tracepoint/eval_script_spec.rb b/spec/ruby/core/tracepoint/eval_script_spec.rb index 1d8e425a9a..ccf7fb5975 100644 --- a/spec/ruby/core/tracepoint/eval_script_spec.rb +++ b/spec/ruby/core/tracepoint/eval_script_spec.rb @@ -13,6 +13,7 @@ ruby_version_is "2.6" do CODE TracePoint.new(:script_compiled) do |e| + next unless TracePointSpec.target_thread? ScratchPad << e.eval_script end.enable do eval script diff --git a/spec/ruby/core/tracepoint/event_spec.rb b/spec/ruby/core/tracepoint/event_spec.rb index 019d0c3253..9dea24d125 100644 --- a/spec/ruby/core/tracepoint/event_spec.rb +++ b/spec/ruby/core/tracepoint/event_spec.rb @@ -5,6 +5,7 @@ describe 'TracePoint#event' do it 'returns the type of event' do event_name = nil TracePoint.new(:end, :call) do |tp| + next unless TracePointSpec.target_thread? event_name = tp.event end.enable do TracePointSpec.test diff --git a/spec/ruby/core/tracepoint/fixtures/classes.rb b/spec/ruby/core/tracepoint/fixtures/classes.rb index 49c70e1915..3ab1b00b16 100644 --- a/spec/ruby/core/tracepoint/fixtures/classes.rb +++ b/spec/ruby/core/tracepoint/fixtures/classes.rb @@ -1,4 +1,10 @@ module TracePointSpec + @thread = Thread.current + + def self.target_thread? + Thread.current == @thread + end + class ClassWithMethodAlias def m end diff --git a/spec/ruby/core/tracepoint/inspect_spec.rb b/spec/ruby/core/tracepoint/inspect_spec.rb index 465f01e229..1e48caa215 100644 --- a/spec/ruby/core/tracepoint/inspect_spec.rb +++ b/spec/ruby/core/tracepoint/inspect_spec.rb @@ -10,6 +10,7 @@ describe 'TracePoint#inspect' do inspect = nil line = nil TracePoint.new(:line) { |tp| + next unless TracePointSpec.target_thread? inspect ||= tp.inspect }.enable do line = __LINE__ @@ -22,6 +23,7 @@ describe 'TracePoint#inspect' do inspect = nil line = nil TracePoint.new(:class) { |tp| + next unless TracePointSpec.target_thread? inspect ||= tp.inspect }.enable do line = __LINE__ + 1 diff --git a/spec/ruby/core/tracepoint/lineno_spec.rb b/spec/ruby/core/tracepoint/lineno_spec.rb index a4d7e77e8d..77b3ac8b54 100644 --- a/spec/ruby/core/tracepoint/lineno_spec.rb +++ b/spec/ruby/core/tracepoint/lineno_spec.rb @@ -1,10 +1,20 @@ require_relative '../../spec_helper' +require_relative 'fixtures/classes' 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 == 7 + TracePoint.new(:line) { |tp| + next unless TracePointSpec.target_thread? + lineno = tp.lineno + }.enable do + line_event = true end + lineno.should == __LINE__ - 2 + end + + it 'raises RuntimeError if accessed from outside' do + tp = TracePoint.new(:line) {} + -> { tp.lineno }.should raise_error(RuntimeError, 'access from outside') end end diff --git a/spec/ruby/core/tracepoint/method_id_spec.rb b/spec/ruby/core/tracepoint/method_id_spec.rb index 82254d1299..43e23248b7 100644 --- a/spec/ruby/core/tracepoint/method_id_spec.rb +++ b/spec/ruby/core/tracepoint/method_id_spec.rb @@ -1,12 +1,14 @@ require_relative '../../spec_helper' +require_relative 'fixtures/classes' 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 + TracePoint.new(:call) { |tp| + next unless TracePointSpec.target_thread? + method_name = tp.method_id + }.enable do + TracePointSpec.test method_name.should equal(:test) end end diff --git a/spec/ruby/core/tracepoint/new_spec.rb b/spec/ruby/core/tracepoint/new_spec.rb index 991048514c..e53c2b04a2 100644 --- a/spec/ruby/core/tracepoint/new_spec.rb +++ b/spec/ruby/core/tracepoint/new_spec.rb @@ -8,7 +8,10 @@ describe 'TracePoint.new' do it 'includes :line event when event is not specified' do event_name = nil - TracePoint.new() { |tp| event_name = tp.event }.enable do + TracePoint.new { |tp| + next unless TracePointSpec.target_thread? + event_name = tp.event + }.enable do event_name.should equal(:line) event_name = nil @@ -25,7 +28,10 @@ describe 'TracePoint.new' do event_name = nil (o = mock('line')).should_receive(:to_sym).and_return(:line) - TracePoint.new(o) { |tp| event_name = tp.event }.enable do + TracePoint.new(o) { |tp| + next unless TracePointSpec.target_thread? + event_name = tp.event + }.enable do line_event = true event_name.should == :line end @@ -34,6 +40,7 @@ describe 'TracePoint.new' do it 'includes multiple events when multiple event names are passed as params' do event_name = nil TracePoint.new(:end, :call) do |tp| + next unless TracePointSpec.target_thread? event_name = tp.event end.enable do TracePointSpec.test @@ -49,10 +56,10 @@ describe 'TracePoint.new' do it 'raises a TypeError when the given object is not a string/symbol' do o = mock('123') - -> { TracePoint.new(o) {}}.should raise_error(TypeError) + -> { TracePoint.new(o) {} }.should raise_error(TypeError) o.should_receive(:to_sym).and_return(123) - -> { TracePoint.new(o) {}}.should raise_error(TypeError) + -> { TracePoint.new(o) {} }.should raise_error(TypeError) end it 'expects to be called with a block' do diff --git a/spec/ruby/core/tracepoint/parameters_spec.rb b/spec/ruby/core/tracepoint/parameters_spec.rb index f901c184f4..3a2cdd7750 100644 --- a/spec/ruby/core/tracepoint/parameters_spec.rb +++ b/spec/ruby/core/tracepoint/parameters_spec.rb @@ -1,11 +1,15 @@ require_relative '../../spec_helper' +require_relative 'fixtures/classes' ruby_version_is "2.6" do describe 'TracePoint#parameters' do it 'returns the parameters of block' do f = proc {|x, y, z| } parameters = nil - TracePoint.new(:b_call) {|tp| parameters = tp.parameters }.enable do + TracePoint.new(:b_call) { |tp| + next unless TracePointSpec.target_thread? + parameters = tp.parameters + }.enable do f.call parameters.should == [[:opt, :x], [:opt, :y], [:opt, :z]] end @@ -14,7 +18,10 @@ ruby_version_is "2.6" do it 'returns the parameters of lambda block' do f = -> x, y, z { } parameters = nil - TracePoint.new(:b_call) {|tp| parameters = tp.parameters }.enable do + TracePoint.new(:b_call) { |tp| + next unless TracePointSpec.target_thread? + parameters = tp.parameters + }.enable do f.call(1, 2, 3) parameters.should == [[:req, :x], [:req, :y], [:req, :z]] end diff --git a/spec/ruby/core/tracepoint/path_spec.rb b/spec/ruby/core/tracepoint/path_spec.rb index 1e31c1bb68..5b6c6d4cfc 100644 --- a/spec/ruby/core/tracepoint/path_spec.rb +++ b/spec/ruby/core/tracepoint/path_spec.rb @@ -1,18 +1,26 @@ require_relative '../../spec_helper' +require_relative 'fixtures/classes' 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__}" + TracePoint.new(:line) { |tp| + next unless TracePointSpec.target_thread? + path = tp.path + }.enable do + line_event = true end + path.should == "#{__FILE__}" end it 'equals (eval) inside an eval for :end event' do path = nil - TracePoint.new(:end) { |tp| path = tp.path }.enable do + TracePoint.new(:end) { |tp| + next unless TracePointSpec.target_thread? + path = tp.path + }.enable do eval("module TracePointSpec; end") - path.should == '(eval)' end + path.should == '(eval)' end end diff --git a/spec/ruby/core/tracepoint/raised_exception_spec.rb b/spec/ruby/core/tracepoint/raised_exception_spec.rb index 450717b958..ca2f50abb3 100644 --- a/spec/ruby/core/tracepoint/raised_exception_spec.rb +++ b/spec/ruby/core/tracepoint/raised_exception_spec.rb @@ -1,9 +1,13 @@ require_relative '../../spec_helper' +require_relative 'fixtures/classes' 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 = TracePoint.new(:raise) { |tp| + next unless TracePointSpec.target_thread? + raised_exception = tp.raised_exception + } trace.enable do begin raise StandardError diff --git a/spec/ruby/core/tracepoint/return_value_spec.rb b/spec/ruby/core/tracepoint/return_value_spec.rb index f0ed86bd00..e84c7dd762 100644 --- a/spec/ruby/core/tracepoint/return_value_spec.rb +++ b/spec/ruby/core/tracepoint/return_value_spec.rb @@ -1,11 +1,15 @@ require_relative '../../spec_helper' +require_relative 'fixtures/classes' 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 + TracePoint.new(:return) { |tp| + next unless TracePointSpec.target_thread? + trace_value = tp.return_value + }.enable do test trace_value.should == 'test' end diff --git a/spec/ruby/core/tracepoint/self_spec.rb b/spec/ruby/core/tracepoint/self_spec.rb index 8bfd09301e..2098860e59 100644 --- a/spec/ruby/core/tracepoint/self_spec.rb +++ b/spec/ruby/core/tracepoint/self_spec.rb @@ -4,14 +4,20 @@ require_relative 'fixtures/classes' describe 'TracePoint#self' do it 'return the trace object from event' do trace = nil - TracePoint.new(:line) { |tp| trace = tp.self }.enable do + TracePoint.new(:line) { |tp| + next unless TracePointSpec.target_thread? + trace = tp.self + }.enable do trace.equal?(self).should be_true end end it 'return the class object from a class event' do trace = nil - TracePoint.new(:class) { |tp| trace = tp.self }.enable do + TracePoint.new(:class) { |tp| + next unless TracePointSpec.target_thread? + trace = tp.self + }.enable do class TracePointSpec::C end end diff --git a/spec/ruby/core/tracepoint/trace_spec.rb b/spec/ruby/core/tracepoint/trace_spec.rb index c39721e692..167f594bb9 100644 --- a/spec/ruby/core/tracepoint/trace_spec.rb +++ b/spec/ruby/core/tracepoint/trace_spec.rb @@ -1,4 +1,5 @@ require_relative '../../spec_helper' +require_relative 'fixtures/classes' describe 'TracePoint.trace' do it 'activates the trace automatically' do |