ruby-changes:58892
From: aycabta <ko1@a...>
Date: Sat, 23 Nov 2019 07:40:04 +0900 (JST)
Subject: [ruby-changes:58892] 1ee010a317 (master): Tracer.add_filter should support block and Proc object
https://git.ruby-lang.org/ruby.git/commit/?id=1ee010a317 From 1ee010a3171978007a4550e8077f1e4b646bd80a Mon Sep 17 00:00:00 2001 From: aycabta <aycabta@g...> Date: Sat, 23 Nov 2019 05:20:23 +0900 Subject: Tracer.add_filter should support block and Proc object Original Tracer.add_filter is implemented by "def add_filter(p = proc)". It means that original Tracer.add_filter supports block and Proc object. diff --git a/lib/tracer.rb b/lib/tracer.rb index e5d2a34..cf44db5 100644 --- a/lib/tracer.rb +++ b/lib/tracer.rb @@ -142,7 +142,8 @@ class Tracer https://github.com/ruby/ruby/blob/trunk/lib/tracer.rb#L142 stdout.print "Trace off\n" if Tracer.verbose? end - def add_filter(&p) # :nodoc: + def add_filter(p = nil, &b) # :nodoc: + p ||= b @filters.push p end @@ -249,7 +250,7 @@ class Tracer https://github.com/ruby/ruby/blob/trunk/lib/tracer.rb#L250 # }) def Tracer.set_get_line_procs(file_name, &p) - Single.set_get_line_procs(file_name, p) + Single.set_get_line_procs(file_name, &p) end ## @@ -261,7 +262,8 @@ class Tracer https://github.com/ruby/ruby/blob/trunk/lib/tracer.rb#L262 # "Kernel" == klass.to_s # end - def Tracer.add_filter(&p) + def Tracer.add_filter(p = nil, &b) + p ||= b Single.add_filter(p) end end diff --git a/test/test_tracer.rb b/test/test_tracer.rb index 6622b53..634501c 100644 --- a/test/test_tracer.rb +++ b/test/test_tracer.rb @@ -53,4 +53,77 @@ require 'tracer' https://github.com/ruby/ruby/blob/trunk/test/test_tracer.rb#L53 end end end + + def test_tracer_by_add_filter_with_block + Dir.mktmpdir("test_ruby_tracer") do |dir| + script = File.join(dir, "require_tracer.rb") + open(script, "w") do |f| + f.print <<-'EOF' +require 'tracer' + +class Hoge + def Hoge.fuga(i) + "fuga #{i}" + end +end + +Tracer.add_filter {|event, file, line, id, binding, klass| + event =~ /line/ and klass.to_s =~ /hoge/i +} +Tracer.on +for i in 0..3 + puts Hoge.fuga(i) if i % 3 == 0 +end +Tracer.off + EOF + end + assert_in_out_err([script]) do |(*lines), err| + expected = [ + "#0:#{script}:5:Hoge:-: \"fuga \#{i}\"", + "fuga 0", + "#0:#{script}:5:Hoge:-: \"fuga \#{i}\"", + "fuga 3" + ] + assert_equal(expected, lines) + assert_empty(err) + end + end + end + + def test_tracer_by_add_filter_with_proc + Dir.mktmpdir("test_ruby_tracer") do |dir| + script = File.join(dir, "require_tracer.rb") + open(script, "w") do |f| + f.print <<-'EOF' +require 'tracer' + +class Hoge + def Hoge.fuga(i) + "fuga #{i}" + end +end + +a_proc_to_add_filter = proc {|event, file, line, id, binding, klass| + event =~ /line/ and klass.to_s =~ /hoge/i +} +Tracer.add_filter(a_proc_to_add_filter) +Tracer.on +for i in 0..3 + puts Hoge.fuga(i) if i % 3 == 0 +end +Tracer.off + EOF + end + assert_in_out_err([script]) do |(*lines), err| + expected = [ + "#0:#{script}:5:Hoge:-: \"fuga \#{i}\"", + "fuga 0", + "#0:#{script}:5:Hoge:-: \"fuga \#{i}\"", + "fuga 3" + ] + 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/