summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraycabta <aycabta@gmail.com>2019-11-23 05:20:23 +0900
committeraycabta <aycabta@gmail.com>2019-11-23 05:43:07 +0900
commit1ee010a3171978007a4550e8077f1e4b646bd80a (patch)
tree38af4fa5262e515c6d8614925589d365439b6a4b
parent8e743fad4e9124bd59bb5f14473cb188db9d3c34 (diff)
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.
-rw-r--r--lib/tracer.rb8
-rw-r--r--test/test_tracer.rb73
2 files changed, 78 insertions, 3 deletions
diff --git a/lib/tracer.rb b/lib/tracer.rb
index e5d2a3479b..cf44db5fcd 100644
--- a/lib/tracer.rb
+++ b/lib/tracer.rb
@@ -142,7 +142,8 @@ class Tracer
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
# })
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
# "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 6622b53663..634501c11a 100644
--- a/test/test_tracer.rb
+++ b/test/test_tracer.rb
@@ -53,4 +53,77 @@ require 'tracer'
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