2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2010-08-14 07:59:35 -04:00
|
|
|
require "test/unit"
|
|
|
|
require "coverage"
|
2011-06-13 12:12:52 -04:00
|
|
|
require "tmpdir"
|
2017-04-25 15:13:26 -04:00
|
|
|
require "envutil"
|
2010-08-14 07:59:35 -04:00
|
|
|
|
|
|
|
class TestCoverage < Test::Unit::TestCase
|
|
|
|
def test_result_without_start
|
2017-09-02 09:55:12 -04:00
|
|
|
assert_in_out_err(%w[-rcoverage], <<-"end;", [], /coverage measurement is not enabled/)
|
|
|
|
Coverage.result
|
|
|
|
p :NG
|
|
|
|
end;
|
2010-08-14 07:59:35 -04:00
|
|
|
end
|
2015-11-25 19:48:56 -05:00
|
|
|
|
|
|
|
def test_peek_result_without_start
|
2017-09-02 09:55:12 -04:00
|
|
|
assert_in_out_err(%w[-rcoverage], <<-"end;", [], /coverage measurement is not enabled/)
|
|
|
|
Coverage.peek_result
|
|
|
|
p :NG
|
|
|
|
end;
|
2015-11-25 19:48:56 -05:00
|
|
|
end
|
|
|
|
|
2010-08-14 07:59:35 -04:00
|
|
|
def test_result_with_nothing
|
2017-09-02 09:55:12 -04:00
|
|
|
assert_in_out_err(%w[-rcoverage], <<-"end;", ["{}"], [])
|
|
|
|
Coverage.start
|
|
|
|
p Coverage.result
|
|
|
|
end;
|
2010-08-14 07:59:35 -04:00
|
|
|
end
|
2011-07-04 11:55:06 -04:00
|
|
|
|
2017-09-01 10:58:52 -04:00
|
|
|
def test_coverage_running?
|
2017-09-02 09:55:12 -04:00
|
|
|
assert_in_out_err(%w[-rcoverage], <<-"end;", ["false", "true", "true", "false"], [])
|
|
|
|
p Coverage.running?
|
|
|
|
Coverage.start
|
|
|
|
p Coverage.running?
|
|
|
|
Coverage.peek_result
|
|
|
|
p Coverage.running?
|
|
|
|
Coverage.result
|
|
|
|
p Coverage.running?
|
|
|
|
end;
|
2017-09-01 10:58:52 -04:00
|
|
|
end
|
|
|
|
|
2015-02-13 10:53:47 -05:00
|
|
|
def test_coverage_snapshot
|
|
|
|
Dir.mktmpdir {|tmp|
|
|
|
|
Dir.chdir(tmp) {
|
|
|
|
File.open("test.rb", "w") do |f|
|
|
|
|
f.puts <<-EOS
|
2017-09-02 09:55:12 -04:00
|
|
|
def coverage_test_snapshot
|
2015-02-13 10:53:47 -05:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2017-09-02 09:55:12 -04:00
|
|
|
assert_in_out_err(%w[-rcoverage], <<-"end;", ["[1, 0, nil]", "[1, 1, nil]", "[1, 1, nil]"], [])
|
|
|
|
Coverage.start
|
|
|
|
tmp = Dir.pwd
|
|
|
|
require tmp + "/test.rb"
|
|
|
|
cov = Coverage.peek_result[tmp + "/test.rb"]
|
|
|
|
coverage_test_snapshot
|
|
|
|
cov2 = Coverage.peek_result[tmp + "/test.rb"]
|
|
|
|
p cov
|
|
|
|
p cov2
|
|
|
|
p Coverage.result[tmp + "/test.rb"]
|
|
|
|
end;
|
2015-02-13 10:53:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-07-04 11:55:06 -04:00
|
|
|
def test_restarting_coverage
|
|
|
|
Dir.mktmpdir {|tmp|
|
|
|
|
Dir.chdir(tmp) {
|
2017-09-02 20:02:23 -04:00
|
|
|
tmp = Dir.pwd
|
2011-07-04 11:55:06 -04:00
|
|
|
File.open("test.rb", "w") do |f|
|
|
|
|
f.puts <<-EOS
|
2017-09-02 09:55:12 -04:00
|
|
|
def coverage_test_restarting
|
2011-07-04 11:55:06 -04:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2015-11-25 19:51:03 -05:00
|
|
|
File.open("test2.rb", "w") do |f|
|
|
|
|
f.puts <<-EOS
|
2016-04-11 01:13:31 -04:00
|
|
|
itself
|
2015-11-25 19:51:03 -05:00
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2017-09-02 09:55:12 -04:00
|
|
|
exp1 = { "#{tmp}/test.rb" => [1, 0, nil] }.inspect
|
|
|
|
exp2 = {}.inspect
|
|
|
|
exp3 = { "#{tmp}/test2.rb" => [1] }.inspect
|
|
|
|
assert_in_out_err(%w[-rcoverage], <<-"end;", [exp1, exp2, exp3], [])
|
|
|
|
Coverage.start
|
|
|
|
tmp = Dir.pwd
|
|
|
|
require tmp + "/test.rb"
|
|
|
|
p Coverage.result
|
|
|
|
|
|
|
|
# Restart coverage but '/test.rb' is required before restart,
|
|
|
|
# so coverage is not recorded.
|
|
|
|
Coverage.start
|
|
|
|
coverage_test_restarting
|
|
|
|
p Coverage.result
|
|
|
|
|
|
|
|
# Restart coverage and '/test2.rb' is required after restart,
|
|
|
|
# so coverage is recorded.
|
|
|
|
Coverage.start
|
|
|
|
require tmp + "/test2.rb"
|
|
|
|
p Coverage.result
|
|
|
|
end;
|
2011-08-23 11:44:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_big_code
|
|
|
|
Dir.mktmpdir {|tmp|
|
|
|
|
Dir.chdir(tmp) {
|
|
|
|
File.open("test.rb", "w") do |f|
|
2013-08-08 12:27:08 -04:00
|
|
|
f.puts "__id__\n" * 10000
|
2011-08-23 11:44:26 -04:00
|
|
|
f.puts "def ignore(x); end"
|
|
|
|
f.puts "ignore([1"
|
|
|
|
f.puts "])"
|
|
|
|
end
|
|
|
|
|
2017-09-02 09:55:12 -04:00
|
|
|
assert_in_out_err(%w[-rcoverage], <<-"end;", ["10003"], [])
|
|
|
|
Coverage.start
|
|
|
|
tmp = Dir.pwd
|
|
|
|
require tmp + '/test.rb'
|
|
|
|
p Coverage.result[tmp + '/test.rb'].size
|
|
|
|
end;
|
2011-07-04 11:55:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2016-06-26 19:56:57 -04:00
|
|
|
|
|
|
|
def test_nonpositive_linenumber
|
|
|
|
bug12517 = '[ruby-core:76141] [Bug #12517]'
|
2017-09-02 20:13:15 -04:00
|
|
|
assert_in_out_err(%w[-W0 -rcoverage], <<-"end;", ['{"<compiled>"=>[nil]}'], [], bug12517)
|
2017-09-02 09:55:12 -04:00
|
|
|
Coverage.start
|
|
|
|
RubyVM::InstructionSequence.compile(":ok", nil, "<compiled>", 0)
|
|
|
|
p Coverage.result
|
|
|
|
end;
|
2016-06-26 19:56:57 -04:00
|
|
|
end
|
2017-03-14 07:39:58 -04:00
|
|
|
|
|
|
|
def test_eval
|
|
|
|
bug13305 = '[ruby-core:80079] [Bug #13305]'
|
|
|
|
|
|
|
|
Dir.mktmpdir {|tmp|
|
|
|
|
Dir.chdir(tmp) {
|
|
|
|
File.open("test.rb", "w") do |f|
|
|
|
|
f.puts 'REPEATS = 400'
|
|
|
|
f.puts 'def add_method(target)'
|
|
|
|
f.puts ' REPEATS.times do'
|
|
|
|
f.puts ' target.class_eval(<<~RUBY, __FILE__, __LINE__ + 1)'
|
|
|
|
f.puts ' def foo'
|
|
|
|
f.puts ' #{"\n" * rand(REPEATS)}'
|
|
|
|
f.puts ' end'
|
|
|
|
f.puts ' 1'
|
|
|
|
f.puts ' RUBY'
|
|
|
|
f.puts ' end'
|
|
|
|
f.puts 'end'
|
|
|
|
end
|
|
|
|
|
2017-09-02 20:13:15 -04:00
|
|
|
assert_in_out_err(%w[-W0 -rcoverage], <<-"end;", ["[1, 1, 1, 400, nil, nil, nil, nil, nil, nil, nil]"], [], bug13305)
|
2017-09-02 09:55:12 -04:00
|
|
|
Coverage.start
|
|
|
|
tmp = Dir.pwd
|
|
|
|
require tmp + '/test.rb'
|
2017-04-25 15:13:26 -04:00
|
|
|
add_method(Class.new)
|
2017-09-02 09:55:12 -04:00
|
|
|
p Coverage.result[tmp + "/test.rb"]
|
|
|
|
end;
|
2017-03-14 07:39:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2017-09-10 12:19:40 -04:00
|
|
|
|
|
|
|
def test_nocoverage_optimized_line
|
|
|
|
assert_ruby_status(%w[], "#{<<-"begin;"}\n#{<<-'end;'}")
|
|
|
|
begin;
|
|
|
|
def foo(x)
|
|
|
|
x # optimized away
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
end
|
2017-09-13 23:25:36 -04:00
|
|
|
|
2017-09-23 19:58:24 -04:00
|
|
|
def assert_coverage(code, opt, stdout)
|
|
|
|
stdout = [stdout] unless stdout.is_a?(Array)
|
|
|
|
stdout = stdout.map {|s| s.to_s }
|
2017-09-13 23:25:36 -04:00
|
|
|
Dir.mktmpdir {|tmp|
|
|
|
|
Dir.chdir(tmp) {
|
2017-09-23 19:58:24 -04:00
|
|
|
File.write("test.rb", code)
|
2017-09-13 23:25:36 -04:00
|
|
|
|
2017-09-23 19:58:24 -04:00
|
|
|
assert_in_out_err(%w[-W0 -rcoverage], <<-"end;", stdout, [])
|
2017-09-13 23:25:36 -04:00
|
|
|
ENV["COVERAGE_EXPERIMENTAL_MODE"] = "true"
|
2017-09-23 19:58:24 -04:00
|
|
|
Coverage.start(#{ opt })
|
2017-09-13 23:25:36 -04:00
|
|
|
tmp = Dir.pwd
|
|
|
|
require tmp + '/test.rb'
|
|
|
|
p Coverage.result[tmp + "/test.rb"]
|
|
|
|
end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2017-09-13 23:36:05 -04:00
|
|
|
|
2017-09-23 19:58:24 -04:00
|
|
|
def test_branch_coverage_for_if_statement
|
|
|
|
result = {
|
|
|
|
:branches => {
|
2017-10-22 10:13:50 -04:00
|
|
|
[:if , 0, 2, 2] => {[:then, 1, 3, 4]=>2, [:else, 2, 5, 4]=>1},
|
|
|
|
[:unless, 3, 8, 2] => {[:else, 4, 11, 4]=>2, [:then, 5, 9, 4]=>1},
|
|
|
|
[:if , 6, 14, 2] => {[:then, 7, 15, 4]=>2, [:else, 8, 14, 2]=>1},
|
|
|
|
[:unless, 9, 18, 2] => {[:else, 10, 18, 2]=>2, [:then, 11, 19, 4]=>1},
|
|
|
|
[:if , 12, 22, 2] => {[:then, 13, 22, 2]=>2, [:else, 14, 22, 2]=>1},
|
|
|
|
[:unless, 15, 23, 2] => {[:else, 16, 23, 2]=>2, [:then, 17, 23, 2]=>1},
|
|
|
|
[:if , 18, 25, 2] => {[:then, 19, 25, 11]=>2, [:else, 20, 25, 15]=>1},
|
2017-09-23 19:58:24 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-22 09:34:24 -04:00
|
|
|
assert_coverage(<<~"end;", { branches: true }, result)
|
2017-09-23 19:58:24 -04:00
|
|
|
def foo(x)
|
|
|
|
if x == 0
|
|
|
|
0
|
|
|
|
else
|
|
|
|
1
|
2017-09-13 23:36:05 -04:00
|
|
|
end
|
|
|
|
|
2017-09-23 19:58:24 -04:00
|
|
|
unless x == 0
|
|
|
|
0
|
|
|
|
else
|
|
|
|
1
|
|
|
|
end
|
2017-10-22 09:49:41 -04:00
|
|
|
|
|
|
|
if x == 0
|
|
|
|
0
|
|
|
|
end
|
|
|
|
|
|
|
|
unless x == 0
|
|
|
|
0
|
|
|
|
end
|
2017-10-22 09:58:55 -04:00
|
|
|
|
|
|
|
0 if x == 0
|
|
|
|
0 unless x == 0
|
2017-10-22 10:13:50 -04:00
|
|
|
|
|
|
|
x == 0 ? 0 : 1
|
2017-09-23 19:58:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
foo(0)
|
|
|
|
foo(0)
|
|
|
|
foo(1)
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_branch_coverage_for_while_statement
|
|
|
|
result = {
|
|
|
|
:branches => {
|
2017-10-23 09:25:59 -04:00
|
|
|
[:while, 0, 2, 0] => {[:body, 1, 3, 2]=> 3},
|
|
|
|
[:until, 2, 5, 0] => {[:body, 3, 6, 2]=>10},
|
|
|
|
[:while, 4, 10, 0] => {[:body, 5, 10, 0]=> 3},
|
|
|
|
[:until, 6, 11, 0] => {[:body, 7, 11, 0]=>10},
|
2017-09-13 23:36:05 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-22 09:34:24 -04:00
|
|
|
assert_coverage(<<~"end;", { branches: true }, result)
|
2017-09-23 19:58:24 -04:00
|
|
|
x = 3
|
|
|
|
while x > 0
|
|
|
|
x -= 1
|
|
|
|
end
|
|
|
|
until x == 10
|
|
|
|
x += 1
|
|
|
|
end
|
2017-10-23 09:25:59 -04:00
|
|
|
|
|
|
|
y = 3
|
|
|
|
y -= 1 while y > 0
|
|
|
|
y += 1 until y == 10
|
2017-09-23 19:58:24 -04:00
|
|
|
end;
|
2017-09-13 23:36:05 -04:00
|
|
|
end
|
2017-09-14 00:32:58 -04:00
|
|
|
|
|
|
|
def test_branch_coverage_for_case_statement
|
2017-09-23 19:58:24 -04:00
|
|
|
result = {
|
|
|
|
:branches => {
|
2017-10-22 09:34:24 -04:00
|
|
|
[:case, 0, 2, 2] => {[:when, 1, 4, 4]=>2, [:when, 2, 6, 4]=>0, [:else, 3, 2, 2]=>1},
|
|
|
|
[:case, 4, 9, 2] => {[:when, 5, 11, 4]=>2, [:when, 6, 13, 4]=>0, [:else, 7, 9, 2]=>1},
|
|
|
|
[:case, 8, 16, 2] => {[:when, 9, 18, 4]=>2, [:when, 10, 20, 4]=>0, [:else, 11, 22, 4]=>1},
|
|
|
|
[:case, 12, 25, 2] => {[:when, 13, 27, 4]=>2, [:when, 14, 29, 4]=>0, [:else, 15, 31, 4]=>1},
|
2017-09-14 00:32:58 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-22 09:34:24 -04:00
|
|
|
assert_coverage(<<~"end;", { branches: true }, result)
|
2017-09-23 19:58:24 -04:00
|
|
|
def foo(x)
|
|
|
|
case x
|
|
|
|
when 0
|
|
|
|
0
|
|
|
|
when 1
|
|
|
|
1
|
|
|
|
end
|
2017-09-14 01:12:34 -04:00
|
|
|
|
2017-09-23 19:58:24 -04:00
|
|
|
case
|
|
|
|
when x == 0
|
|
|
|
0
|
|
|
|
when x == 1
|
|
|
|
1
|
2017-09-22 20:17:35 -04:00
|
|
|
end
|
|
|
|
|
2017-09-23 19:58:24 -04:00
|
|
|
case x
|
|
|
|
when 0
|
|
|
|
0
|
|
|
|
when 1
|
|
|
|
1
|
|
|
|
else
|
|
|
|
:other
|
|
|
|
end
|
|
|
|
|
|
|
|
case
|
|
|
|
when x == 0
|
|
|
|
0
|
|
|
|
when x == 1
|
|
|
|
1
|
|
|
|
else
|
|
|
|
:other
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
foo(0)
|
|
|
|
foo(0)
|
|
|
|
foo(2)
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_branch_coverage_for_safe_method_invocation
|
|
|
|
result = {
|
|
|
|
:branches=>{
|
2017-10-22 09:34:24 -04:00
|
|
|
[:"&.", 0, 3, 0] => {[:then, 1, 3, 0]=>1, [:else, 2, 3, 0]=>0},
|
|
|
|
[:"&.", 3, 4, 0] => {[:then, 4, 4, 0]=>0, [:else, 5, 4, 0]=>1},
|
2017-09-22 20:17:35 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-22 09:34:24 -04:00
|
|
|
assert_coverage(<<~"end;", { branches: true }, result)
|
2017-09-23 19:58:24 -04:00
|
|
|
a = 10
|
|
|
|
b = nil
|
|
|
|
a&.abs
|
|
|
|
b&.hoo
|
|
|
|
end;
|
2017-09-22 20:17:35 -04:00
|
|
|
end
|
|
|
|
|
2017-09-14 01:12:34 -04:00
|
|
|
def test_method_coverage
|
2017-09-23 19:58:24 -04:00
|
|
|
result = {
|
|
|
|
:methods => {
|
|
|
|
[:foo, 0, 1] => 2,
|
|
|
|
[:bar, 1, 2] => 1,
|
|
|
|
[:baz, 2, 4] => 0,
|
2017-09-14 01:12:34 -04:00
|
|
|
}
|
|
|
|
}
|
2017-09-23 19:58:24 -04:00
|
|
|
assert_coverage(<<-"end;", { methods: true }, result)
|
|
|
|
def foo; end
|
|
|
|
def bar
|
|
|
|
end
|
|
|
|
def baz; end
|
|
|
|
|
|
|
|
foo
|
|
|
|
foo
|
|
|
|
bar
|
|
|
|
end;
|
2017-09-14 01:12:34 -04:00
|
|
|
end
|
2017-09-02 09:55:12 -04:00
|
|
|
end
|