2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2010-11-17 10:14:13 +03:00
|
|
|
require 'test/unit'
|
2011-06-29 17:19:59 +04:00
|
|
|
require 'tmpdir'
|
2010-11-17 10:14:13 +03:00
|
|
|
|
|
|
|
class TestTracer < Test::Unit::TestCase
|
|
|
|
include EnvUtil
|
|
|
|
|
2011-06-29 17:19:59 +04:00
|
|
|
def test_tracer_with_option_r
|
|
|
|
assert_in_out_err(%w[-rtracer -e 1]) do |(*lines),|
|
2010-11-17 10:14:13 +03:00
|
|
|
case lines.size
|
|
|
|
when 1
|
|
|
|
# do nothing
|
|
|
|
else
|
2014-09-14 07:30:08 +04:00
|
|
|
assert_match(%r{rubygems/core_ext/kernel_require\.rb:\d+:Kernel:<:}, lines[0])
|
2010-11-17 10:14:13 +03:00
|
|
|
end
|
2011-01-14 05:02:12 +03:00
|
|
|
assert_equal "#0:-e:1::-: 1", lines.last
|
2010-11-17 10:14:13 +03:00
|
|
|
end
|
|
|
|
end
|
2011-06-29 17:19:59 +04:00
|
|
|
|
|
|
|
def test_tracer_with_option_r_without_gems
|
|
|
|
assert_in_out_err(%w[--disable-gems -rtracer -e 1]) do |(*lines),|
|
2015-06-25 08:26:31 +03:00
|
|
|
assert_equal 1, lines.size, "unexpected output from `ruby --disable-gems -rtracer -e 1`"
|
2011-06-29 17:19:59 +04:00
|
|
|
assert_equal "#0:-e:1::-: 1", lines.last
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_tracer_with_require
|
|
|
|
Dir.mktmpdir("test_ruby_tracer") do |dir|
|
|
|
|
script = File.join(dir, "require_tracer.rb")
|
|
|
|
open(script, "w") do |f|
|
|
|
|
f.print <<-EOF
|
|
|
|
require 'tracer'
|
|
|
|
1
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
assert_in_out_err([script]) do |(*lines),|
|
|
|
|
assert_empty(lines)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_tracer_with_require_without_gems
|
|
|
|
Dir.mktmpdir("test_ruby_tracer") do |dir|
|
|
|
|
script = File.join(dir, "require_tracer.rb")
|
|
|
|
open(script, "w") do |f|
|
|
|
|
f.print <<-EOF
|
|
|
|
require 'tracer'
|
|
|
|
1
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
assert_in_out_err(["--disable-gems", script]) do |(*lines),|
|
|
|
|
assert_empty(lines)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-11-22 23:20:23 +03:00
|
|
|
|
|
|
|
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
|
2019-11-22 23:44:35 +03:00
|
|
|
|
|
|
|
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
|
2019-11-23 05:41:27 +03:00
|
|
|
dummy_script = File.realpath dummy_script
|
2019-11-22 23:44:35 +03:00
|
|
|
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
|
2019-11-23 05:41:27 +03:00
|
|
|
dummy_script = File.realpath dummy_script
|
2019-11-22 23:44:35 +03:00
|
|
|
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
|
2010-11-17 10:14:13 +03:00
|
|
|
end
|