ruby-changes:58890
From: aycabta <ko1@a...>
Date: Sat, 23 Nov 2019 06:25:03 +0900 (JST)
Subject: [ruby-changes:58890] b0614a0f96 (master): Tracer.set_get_line_procs should support block and Proc object
https://git.ruby-lang.org/ruby.git/commit/?id=b0614a0f96 From b0614a0f968d40fb1bad31592fc9a607dbac447d Mon Sep 17 00:00:00 2001 From: aycabta <aycabta@g...> Date: Sat, 23 Nov 2019 05:44:35 +0900 Subject: Tracer.set_get_line_procs should support block and Proc object Original Tracer.set_get_line_procs is implemented by "def set_get_line_procs(p = proc)". It means that original Tracer.set_get_line_procs supports block and Proc object. diff --git a/lib/tracer.rb b/lib/tracer.rb index cf44db5..8011ad2 100644 --- a/lib/tracer.rb +++ b/lib/tracer.rb @@ -147,7 +147,8 @@ class Tracer https://github.com/ruby/ruby/blob/trunk/lib/tracer.rb#L147 @filters.push p end - def set_get_line_procs(file, &p) # :nodoc: + def set_get_line_procs(file, p = nil, &b) # :nodoc: + p ||= b @get_line_procs[file] = p end @@ -249,8 +250,9 @@ class Tracer https://github.com/ruby/ruby/blob/trunk/lib/tracer.rb#L250 # puts "line number executed is #{line}" # }) - def Tracer.set_get_line_procs(file_name, &p) - Single.set_get_line_procs(file_name, &p) + def Tracer.set_get_line_procs(file_name, p = nil, &b) + p ||= b + Single.set_get_line_procs(file_name, p) end ## diff --git a/test/test_tracer.rb b/test/test_tracer.rb index 634501c..c79b552 100644 --- a/test/test_tracer.rb +++ b/test/test_tracer.rb @@ -126,4 +126,107 @@ Tracer.off https://github.com/ruby/ruby/blob/trunk/test/test_tracer.rb#L126 end end end + + def test_tracer_by_set_get_line_procs_with_block + Dir.mktmpdir("test_ruby_tracer") do |dir| + dummy_script = File.join(dir, "dummy.rb") + open(dummy_script, "w") do |f| + f.print <<-'EOF' +class Dummy + def initialize + @number = 135 + end + attr :number +end + EOF + end + script = File.join(dir, "require_tracer.rb") + open(script, "w") do |f| + f.print <<-EOF +require 'tracer' + +Tracer.set_get_line_procs('#{dummy_script}') { |line| + str = %{\\n} + str = %{!!\\n} if line >= 3 and line <= 6 + str +} +Tracer.on +require_relative 'dummy' + +dm = Dummy.new +puts dm.number + EOF + end + assert_in_out_err([script]) do |(*lines), err| + expected = [ + "#0:#{script}:9::-: require_relative 'dummy'", + "#0:#{dummy_script}:1::-: ", + "#0:#{dummy_script}:1::C: ", + "#0:#{dummy_script}:2::-: ", + "#0:#{dummy_script}:5::-: !!", + "#0:#{dummy_script}:6::E: !!", + "#0:#{script}:11::-: dm = Dummy.new", + "#0:#{dummy_script}:2:Dummy:>: ", + "#0:#{dummy_script}:3:Dummy:-: !!", + "#0:#{dummy_script}:4:Dummy:<: !!", + "#0:#{script}:12::-: puts dm.number", + "135" + ] + assert_equal(expected, lines) + assert_empty(err) + end + end + end + + def test_tracer_by_set_get_line_procs_with_proc + Dir.mktmpdir("test_ruby_tracer") do |dir| + dummy_script = File.join(dir, "dummy.rb") + open(dummy_script, "w") do |f| + f.print <<-'EOF' +class Dummy + def initialize + @number = 135 + end + attr :number +end + EOF + end + script = File.join(dir, "require_tracer.rb") + open(script, "w") do |f| + f.print <<-EOF +require 'tracer' + +a_proc_to_set_get_line_procs = proc { |line| + str = %{\\n} + str = %{!!\\n} if line >= 3 and line <= 6 + str +} +Tracer.set_get_line_procs('#{dummy_script}', a_proc_to_set_get_line_procs) +Tracer.on +require_relative 'dummy' + +dm = Dummy.new +puts dm.number + EOF + end + assert_in_out_err([script]) do |(*lines), err| + expected = [ + "#0:#{script}:10::-: require_relative 'dummy'", + "#0:#{dummy_script}:1::-: ", + "#0:#{dummy_script}:1::C: ", + "#0:#{dummy_script}:2::-: ", + "#0:#{dummy_script}:5::-: !!", + "#0:#{dummy_script}:6::E: !!", + "#0:#{script}:12::-: dm = Dummy.new", + "#0:#{dummy_script}:2:Dummy:>: ", + "#0:#{dummy_script}:3:Dummy:-: !!", + "#0:#{dummy_script}:4:Dummy:<: !!", + "#0:#{script}:13::-: puts dm.number", + "135" + ] + assert_equal(expected, lines) + assert_empty(err) + end + end + end end -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/