Module: SimpleCov::Combine::BranchesCombiner
- Defined in:
- lib/simplecov/combine/branches_combiner.rb
Overview
Combine different branch coverage results on single file.
Should be called through SimpleCov.combine.
Class Method Summary collapse
-
.combine(coverage_a, coverage_b) ⇒ Hash
Return merged branches or the existed branch if other is missing.
- .merge_branches(target, source) ⇒ Object
- .tuple_identity(tuple) ⇒ Object
Class Method Details
.combine(coverage_a, coverage_b) ⇒ Hash
Return merged branches or the existed branch if other is missing.
Branches inside files are always same if they exist, the difference only in coverage count. Branch coverage report for any conditional case is built from hash, it's key is a condition and it's body is a hash << keys from condition and value is coverage rate >>. ex: branches => { [:if, 3, 8, 6, 8, 36] => 4, 8, 6, 8, 12] => 1, [:else, 5, 8, 6, 8, 36] => 2, ... } We create copy of result and update it values depending on the combined branches coverage values.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/simplecov/combine/branches_combiner.rb', line 26 def combine(coverage_a, coverage_b) merged = {} #: Hash[untyped, [untyped, Hash[untyped, untyped]]] combined = [coverage_a, coverage_b].each_with_object(merged) do |coverage, memo| coverage.each do |condition, branches_inside| condition_key = tuple_identity(condition) condition_tuple, merged_branches = memo[condition_key] ||= [condition, {}] merge_branches(merged_branches, branches_inside) memo[condition_key] = [condition_tuple, merged_branches] end end combined.values.to_h { |condition, branches| [condition, branches.values.to_h] } end |
.merge_branches(target, source) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/simplecov/combine/branches_combiner.rb', line 40 def merge_branches(target, source) source.each do |branch, count| branch_key = tuple_identity(branch) branch_tuple, existing_count = target[branch_key] target[branch_key] = [branch_tuple || branch, existing_count ? existing_count + count : count] end end |
.tuple_identity(tuple) ⇒ Object
48 49 50 51 52 |
# File 'lib/simplecov/combine/branches_combiner.rb', line 48 def tuple_identity(tuple) tuple = SourceFile::RubyDataParser.call(tuple) type, _id, start_line, start_column, end_line, end_column = tuple [type, start_line, start_column, end_line, end_column] end |