Skip to content

Commit

Permalink
Show missed else branch of case statements even if not declared
Browse files Browse the repository at this point in the history
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
  • Loading branch information
PragTob committed Jan 18, 2020
1 parent db804e2 commit 30f9999
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
23 changes: 5 additions & 18 deletions lib/simplecov/source_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down
11 changes: 8 additions & 3 deletions spec/source_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, "+"]]
Expand Down

0 comments on commit 30f9999

Please sign in to comment.