summaryrefslogtreecommitdiff
path: root/test/test_tracer.rb
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 /test/test_tracer.rb
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.
Diffstat (limited to 'test/test_tracer.rb')
-rw-r--r--test/test_tracer.rb73
1 files changed, 73 insertions, 0 deletions
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'
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