Add branch coverage for case-when statement

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59885 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mame 2017-09-14 04:32:58 +00:00
Родитель c4a64b734c
Коммит 1f7abf7283
2 изменённых файлов: 67 добавлений и 1 удалений

Просмотреть файл

@ -4229,6 +4229,7 @@ compile_case(rb_iseq_t *iseq, LINK_ANCHOR *const ret, NODE *node, int popped)
VALUE literals = rb_hash_new();
int line;
enum node_type type;
VALUE branches;
INIT_ANCHOR(head);
INIT_ANCHOR(body_seq);
@ -4242,6 +4243,8 @@ compile_case(rb_iseq_t *iseq, LINK_ANCHOR *const ret, NODE *node, int popped)
}
CHECK(COMPILE(head, "case base", node->nd_head));
DECL_BRANCH_BASE(branches, nd_line(node), "case");
node = node->nd_body;
type = nd_type(node);
line = nd_line(node);
@ -4262,6 +4265,7 @@ compile_case(rb_iseq_t *iseq, LINK_ANCHOR *const ret, NODE *node, int popped)
l1 = NEW_LABEL(line);
ADD_LABEL(body_seq, l1);
ADD_INSN(body_seq, line, pop);
ADD_TRACE_BRANCH_COVERAGE(body_seq, node->nd_body ? nd_line(node->nd_body) : line, "when", branches);
CHECK(COMPILE_(body_seq, "when body", node->nd_body, popped));
ADD_INSNL(body_seq, line, jump, endlabel);
@ -4299,6 +4303,7 @@ compile_case(rb_iseq_t *iseq, LINK_ANCHOR *const ret, NODE *node, int popped)
if (node) {
ADD_LABEL(cond_seq, elselabel);
ADD_INSN(cond_seq, line, pop);
ADD_TRACE_BRANCH_COVERAGE(cond_seq, nd_line(node), "else", branches);
CHECK(COMPILE_(cond_seq, "else", node, popped));
ADD_INSNL(cond_seq, line, jump, endlabel);
}
@ -4306,6 +4311,7 @@ compile_case(rb_iseq_t *iseq, LINK_ANCHOR *const ret, NODE *node, int popped)
debugs("== else (implicit)\n");
ADD_LABEL(cond_seq, elselabel);
ADD_INSN(cond_seq, nd_line(tempnode), pop);
ADD_TRACE_BRANCH_COVERAGE(cond_seq, nd_line(tempnode), "else", branches);
if (!popped) {
ADD_INSN(cond_seq, nd_line(tempnode), putnil);
}
@ -4334,6 +4340,9 @@ compile_when(rb_iseq_t *iseq, LINK_ANCHOR *const ret, NODE *node, int popped)
NODE *orig_node = node;
LABEL *endlabel;
DECL_ANCHOR(body_seq);
VALUE branches;
DECL_BRANCH_BASE(branches, nd_line(node), "case");
INIT_ANCHOR(body_seq);
endlabel = NEW_LABEL(nd_line(node));
@ -4342,6 +4351,7 @@ compile_when(rb_iseq_t *iseq, LINK_ANCHOR *const ret, NODE *node, int popped)
const int line = nd_line(node);
LABEL *l1 = NEW_LABEL(line);
ADD_LABEL(body_seq, l1);
ADD_TRACE_BRANCH_COVERAGE(body_seq, node->nd_body ? nd_line(node->nd_body) : line, "when", branches);
CHECK(COMPILE_(body_seq, "when", node->nd_body, popped));
ADD_INSNL(body_seq, line, jump, endlabel);
@ -4373,6 +4383,7 @@ compile_when(rb_iseq_t *iseq, LINK_ANCHOR *const ret, NODE *node, int popped)
node = node->nd_next;
}
/* else */
ADD_TRACE_BRANCH_COVERAGE(ret, node ? nd_line(node) : nd_line(orig_node), "else", branches);
CHECK(COMPILE_(ret, "else", node, popped));
ADD_INSNL(ret, nd_line(orig_node), jump, endlabel);
@ -6640,7 +6651,8 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, NODE *node, int popp
POP_ELEMENT(ret);
/* remove trace(coverage) */
if (IS_INSN_ID(ret->last, trace2) &&
(FIX2LONG(OPERAND_AT(ret->last, 0)) & RUBY_EVENT_COVERAGE)) {
(FIX2LONG(OPERAND_AT(ret->last, 0)) & RUBY_EVENT_COVERAGE) &&
(FIX2LONG(OPERAND_AT(ret->last, 1)) == COVERAGE_INDEX_LINES)) {
POP_ELEMENT(ret);
RARRAY_ASET(ISEQ_LINE_COVERAGE(iseq), line - 1, Qnil);
}

Просмотреть файл

@ -226,4 +226,58 @@ class TestCoverage < Test::Unit::TestCase
}
}
end
def test_branch_coverage_for_case_statement
Dir.mktmpdir {|tmp|
Dir.chdir(tmp) {
File.open("test.rb", "w") do |f|
f.puts 'def foo(x)'
f.puts ' case x'
f.puts ' when 0'
f.puts ' 0'
f.puts ' when 1'
f.puts ' 1'
f.puts ' end'
f.puts ''
f.puts ' case'
f.puts ' when x == 0'
f.puts ' 0'
f.puts ' when x == 1'
f.puts ' 1'
f.puts ' end'
f.puts ''
f.puts ' case x'
f.puts ' when 0'
f.puts ' 0'
f.puts ' when 1'
f.puts ' 1'
f.puts ' else'
f.puts ' :other'
f.puts ' end'
f.puts ''
f.puts ' case'
f.puts ' when x == 0'
f.puts ' 0'
f.puts ' when x == 1'
f.puts ' 1'
f.puts ' else'
f.puts ' :other'
f.puts ' end'
f.puts 'end'
f.puts ''
f.puts 'foo(0)'
f.puts 'foo(0)'
f.puts 'foo(2)'
end
assert_in_out_err(%w[-W0 -rcoverage], <<-"end;", ["{:branches=>{[:case, 0, 2]=>{[:when, 1, 4]=>2, [:when, 2, 6]=>0, [:else, 3, 2]=>1}, [:case, 4, 14]=>{[:when, 5, 11]=>2, [:when, 6, 13]=>0, [:else, 7, 14]=>1}, [:case, 8, 16]=>{[:when, 9, 18]=>2, [:when, 10, 20]=>0, [:else, 11, 22]=>1}, [:case, 12, 32]=>{[:when, 13, 27]=>2, [:when, 14, 29]=>0, [:else, 15, 31]=>1}}}"], [])
ENV["COVERAGE_EXPERIMENTAL_MODE"] = "true"
Coverage.start(branches: true)
tmp = Dir.pwd
require tmp + '/test.rb'
p Coverage.result[tmp + "/test.rb"]
end;
}
}
end
end