From 30f9999e0ff79cf7bff3dd4a3336fac2e96cf0e2 Mon Sep 17 00:00:00 2001 From: Tobias Pfeiffer Date: Sat, 18 Jan 2020 14:46:43 +0100 Subject: [PATCH] Show missed else branch of case statements even if not declared I "flip flopped" a lot on this, the initial PR had it implemented as omitting this. You can make an argument either way. You can say your case statement already handles the entire range of input values given your domain. And then testing another value seems too much as it's not realistic (or caught elsewhere). However, who are we to hide additional branch coverage data from users? It's also more consistent as we also display the else branch for if statement without an else. I also think it might help show lapses in coverage. ```ruby var = case arg when "functional" 55 when "something" 42 end var += 1 ``` When we never hit a branch we get an error down there, it's something peple might reliably forget. On top of that it's actually more code to ignore it. So to summarize it why should we display the else branch for case statements even if we don't have them: * show all available coverage data * consistency with other constructs * actually less code --- lib/simplecov/source_file.rb | 23 +++++------------------ spec/source_file_spec.rb | 11 ++++++++--- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/lib/simplecov/source_file.rb b/lib/simplecov/source_file.rb index b6142f7a..57bb7e82 100644 --- a/lib/simplecov/source_file.rb +++ b/lib/simplecov/source_file.rb @@ -211,19 +211,18 @@ def build_branches_from(condition, branches) # [:then, 4, 6, 6, 6, 10] # # which is [type, id, start_line, start_col, end_line, end_col] - condition_type, condition_id, condition_start_line, * = restore_ruby_data_structure(condition) + _condition_type, condition_id, condition_start_line, * = restore_ruby_data_structure(condition) - branches - .map { |branch_data, hit_count| [restore_ruby_data_structure(branch_data), hit_count] } - .reject { |branch_data, _hit_count| ignore_branch?(branch_data, condition_type, condition_start_line) } - .map { |branch_data, hit_count| build_branch(branch_data, hit_count, condition_start_line, condition_id) } + branches.map do |branch_data, hit_count| + branch_data = restore_ruby_data_structure(branch_data) + build_branch(branch_data, hit_count, condition_start_line, condition_id) + end end def build_branch(branch_data, hit_count, condition_start_line, condition_id) type, id, start_line, _start_col, end_line, _end_col = branch_data SourceFile::Branch.new( - # rubocop these are keyword args please let me keep them, thank you start_line: start_line, end_line: end_line, coverage: hit_count, @@ -232,18 +231,6 @@ def build_branch(branch_data, hit_count, condition_start_line, condition_id) ) end - def ignore_branch?(branch_data, condition_type, condition_start_line) - branch_type = branch_data[0] - branch_start_line = branch_data[2] - - # branch coverage always reports case to be with an else branch even when - # there is no else branch to be covered, it's noticable by the reported start - # line being the same as that of the condition/case - condition_type == :case && - branch_type == :else && - condition_start_line == branch_start_line - end - # # Branch is positive or negative. # For `case` conditions, `when` always supposed as positive branch. diff --git a/spec/source_file_spec.rb b/spec/source_file_spec.rb index 13040ced..7e744379 100644 --- a/spec/source_file_spec.rb +++ b/spec/source_file_spec.rb @@ -489,14 +489,19 @@ end describe "branch coverage" do - it "covers 1/3" do - expect(subject.total_branches.size).to eq 3 + it "covers 1/4 (counting the else branch)" do + expect(subject.total_branches.size).to eq 4 expect(subject.covered_branches.size).to eq 1 - expect(subject.missed_branches.size).to eq 2 + expect(subject.missed_branches.size).to eq 3 + end + + it "marks the non declared else branch as missing at the point of the case" do + expect(subject.branches_for_line(3)).to eq [[0, "-"]] end it "covers the branch that includes 42" do expect(subject.branches_report).to eq( + 3 => [[0, "-"]], 4 => [[0, "+"]], 6 => [[1, "+"]], 8 => [[0, "+"]]