Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show missed else branch of case statements even if not declared #825

Merged
merged 1 commit into from
Jan 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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