[前][次][番号順一覧][スレッド一覧]

ruby-changes:47767

From: mame <ko1@a...>
Date: Thu, 14 Sep 2017 13:33:03 +0900 (JST)
Subject: [ruby-changes:47767] mame:r59885 (trunk): Add branch coverage for case-when statement

mame	2017-09-14 13:32:58 +0900 (Thu, 14 Sep 2017)

  New Revision: 59885

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59885

  Log:
    Add branch coverage for case-when statement

  Modified files:
    trunk/compile.c
    trunk/test/coverage/test_coverage.rb
Index: test/coverage/test_coverage.rb
===================================================================
--- test/coverage/test_coverage.rb	(revision 59884)
+++ test/coverage/test_coverage.rb	(revision 59885)
@@ -226,4 +226,58 @@ class TestCoverage < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/coverage/test_coverage.rb#L226
       }
     }
   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
Index: compile.c
===================================================================
--- compile.c	(revision 59884)
+++ compile.c	(revision 59885)
@@ -4229,6 +4229,7 @@ compile_case(rb_iseq_t *iseq, LINK_ANCHO https://github.com/ruby/ruby/blob/trunk/compile.c#L4229
     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_ANCHO https://github.com/ruby/ruby/blob/trunk/compile.c#L4243
     }
     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_ANCHO https://github.com/ruby/ruby/blob/trunk/compile.c#L4265
 	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_ANCHO https://github.com/ruby/ruby/blob/trunk/compile.c#L4303
     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_ANCHO https://github.com/ruby/ruby/blob/trunk/compile.c#L4311
 	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_ANCHO https://github.com/ruby/ruby/blob/trunk/compile.c#L4340
     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_ANCHO https://github.com/ruby/ruby/blob/trunk/compile.c#L4351
 	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_ANCHO https://github.com/ruby/ruby/blob/trunk/compile.c#L4383
 	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 https://github.com/ruby/ruby/blob/trunk/compile.c#L6651
 	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);
 	}

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]