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

ruby-changes:47882

From: yui-knk <ko1@a...>
Date: Sun, 24 Sep 2017 03:28:39 +0900 (JST)
Subject: [ruby-changes:47882] yui-knk:r59990 (trunk): Enable to take branch coverages for safe method invocations

yui-knk	2017-09-23 09:17:35 +0900 (Sat, 23 Sep 2017)

  New Revision: 59990

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

  Log:
    Enable to take branch coverages for safe method invocations

  Modified files:
    trunk/compile.c
    trunk/test/coverage/test_coverage.rb
Index: test/coverage/test_coverage.rb
===================================================================
--- test/coverage/test_coverage.rb	(revision 59989)
+++ test/coverage/test_coverage.rb	(revision 59990)
@@ -287,6 +287,27 @@ class TestCoverage < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/coverage/test_coverage.rb#L287
     }
   end
 
+  def test_branch_coverage_for_safe_method_invocation
+    Dir.mktmpdir {|tmp|
+      Dir.chdir(tmp) {
+        File.open("test.rb", "w") do |f|
+          f.puts 'a = 10'
+          f.puts 'b = nil'
+          f.puts 'a&.abs'
+          f.puts 'b&.hoo'
+        end
+
+        assert_in_out_err(%w[-W0 -rcoverage], <<-"end;", ["{:branches=>{[:\"&.\", 0, 3]=>{[:then, 1, 3]=>1, [:else, 2, 3]=>0}, [:\"&.\", 3, 4]=>{[:then, 4, 4]=>0, [:else, 5, 4]=>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
+
   def test_method_coverage
     Dir.mktmpdir {|tmp|
       Dir.chdir(tmp) {
Index: compile.c
===================================================================
--- compile.c	(revision 59989)
+++ compile.c	(revision 59990)
@@ -5581,7 +5581,9 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK https://github.com/ruby/ruby/blob/trunk/compile.c#L5581
 	*/
 	DECL_ANCHOR(recv);
 	DECL_ANCHOR(args);
-	LABEL *lskip = 0;
+	LABEL *else_label = 0;
+	LABEL *end_label = 0;
+	VALUE branches = 0;
 	ID mid = node->nd_mid;
 	VALUE argc;
 	unsigned int flag = 0;
@@ -5660,9 +5662,13 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK https://github.com/ruby/ruby/blob/trunk/compile.c#L5662
 	if (type == NODE_CALL || type == NODE_OPCALL || type == NODE_QCALL) {
 	    CHECK(COMPILE(recv, "recv", node->nd_recv));
 	    if (type == NODE_QCALL) {
-		lskip = NEW_LABEL(line);
+		else_label = NEW_LABEL(line);
+		end_label = NEW_LABEL(line);
+
+		DECL_BRANCH_BASE(branches, nd_line(node), "&.");
 		ADD_INSN(recv, line, dup);
-		ADD_INSNL(recv, line, branchnil, lskip);
+		ADD_INSNL(recv, line, branchnil, else_label);
+		ADD_TRACE_BRANCH_COVERAGE(recv, nd_line(node), "then", branches);
 	    }
 	}
 	else if (type == NODE_FCALL || type == NODE_VCALL) {
@@ -5694,8 +5700,11 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK https://github.com/ruby/ruby/blob/trunk/compile.c#L5700
 
 	ADD_SEND_R(ret, line, mid, argc, parent_block, INT2FIX(flag), keywords);
 
-	if (lskip) {
-	    ADD_LABEL(ret, lskip);
+	if (else_label && end_label) {
+	    ADD_INSNL(ret, line, jump, end_label);
+	    ADD_LABEL(ret, else_label);
+	    ADD_TRACE_BRANCH_COVERAGE(ret, nd_line(node), "else", branches);
+	    ADD_LABEL(ret, end_label);
 	}
 	if (popped) {
 	    ADD_INSN(ret, line, pop);

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

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