deep-cover 0.6.2 → 0.6.3.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -1
  3. data/.rspec +2 -1
  4. data/.rspec_all +2 -1
  5. data/.rubocop.yml +8 -9
  6. data/Gemfile +2 -0
  7. data/Rakefile +32 -6
  8. data/bin/cov +3 -3
  9. data/deep_cover.gemspec +3 -16
  10. data/exe/deep-cover +5 -0
  11. data/lib/deep_cover/cli/debugger.rb +1 -1
  12. data/lib/deep_cover/cli/exec.rb +1 -1
  13. data/lib/deep_cover/cli/instrumented_clone_reporter.rb +5 -3
  14. data/lib/deep_cover/cli/runner.rb +2 -2
  15. data/lib/deep_cover/{tools/dump_covered_code.rb → dump_covered_code.rb} +2 -0
  16. metadata +10 -203
  17. data/lib/deep-cover.rb +0 -3
  18. data/lib/deep_cover.rb +0 -22
  19. data/lib/deep_cover/analyser.rb +0 -23
  20. data/lib/deep_cover/analyser/base.rb +0 -104
  21. data/lib/deep_cover/analyser/branch.rb +0 -41
  22. data/lib/deep_cover/analyser/covered_code_source.rb +0 -21
  23. data/lib/deep_cover/analyser/function.rb +0 -14
  24. data/lib/deep_cover/analyser/node.rb +0 -54
  25. data/lib/deep_cover/analyser/per_char.rb +0 -38
  26. data/lib/deep_cover/analyser/per_line.rb +0 -41
  27. data/lib/deep_cover/analyser/ruby25_like_branch.rb +0 -211
  28. data/lib/deep_cover/analyser/statement.rb +0 -33
  29. data/lib/deep_cover/analyser/stats.rb +0 -54
  30. data/lib/deep_cover/analyser/subset.rb +0 -27
  31. data/lib/deep_cover/auto_run.rb +0 -71
  32. data/lib/deep_cover/autoload_tracker.rb +0 -215
  33. data/lib/deep_cover/backports.rb +0 -22
  34. data/lib/deep_cover/base.rb +0 -117
  35. data/lib/deep_cover/basics.rb +0 -22
  36. data/lib/deep_cover/builtin_takeover.rb +0 -10
  37. data/lib/deep_cover/config.rb +0 -104
  38. data/lib/deep_cover/config_setter.rb +0 -33
  39. data/lib/deep_cover/core_ext/autoload_overrides.rb +0 -112
  40. data/lib/deep_cover/core_ext/coverage_replacement.rb +0 -61
  41. data/lib/deep_cover/core_ext/exec_callbacks.rb +0 -27
  42. data/lib/deep_cover/core_ext/instruction_sequence_load_iseq.rb +0 -32
  43. data/lib/deep_cover/core_ext/load_overrides.rb +0 -19
  44. data/lib/deep_cover/core_ext/require_overrides.rb +0 -28
  45. data/lib/deep_cover/coverage.rb +0 -125
  46. data/lib/deep_cover/coverage/analysis.rb +0 -42
  47. data/lib/deep_cover/coverage/persistence.rb +0 -84
  48. data/lib/deep_cover/covered_code.rb +0 -145
  49. data/lib/deep_cover/custom_requirer.rb +0 -187
  50. data/lib/deep_cover/flag_comment_associator.rb +0 -68
  51. data/lib/deep_cover/load.rb +0 -66
  52. data/lib/deep_cover/memoize.rb +0 -48
  53. data/lib/deep_cover/module_override.rb +0 -39
  54. data/lib/deep_cover/node.rb +0 -23
  55. data/lib/deep_cover/node/arguments.rb +0 -51
  56. data/lib/deep_cover/node/assignments.rb +0 -273
  57. data/lib/deep_cover/node/base.rb +0 -155
  58. data/lib/deep_cover/node/begin.rb +0 -27
  59. data/lib/deep_cover/node/block.rb +0 -61
  60. data/lib/deep_cover/node/branch.rb +0 -32
  61. data/lib/deep_cover/node/case.rb +0 -113
  62. data/lib/deep_cover/node/collections.rb +0 -31
  63. data/lib/deep_cover/node/const.rb +0 -12
  64. data/lib/deep_cover/node/def.rb +0 -40
  65. data/lib/deep_cover/node/empty_body.rb +0 -32
  66. data/lib/deep_cover/node/exceptions.rb +0 -79
  67. data/lib/deep_cover/node/if.rb +0 -73
  68. data/lib/deep_cover/node/keywords.rb +0 -86
  69. data/lib/deep_cover/node/literals.rb +0 -100
  70. data/lib/deep_cover/node/loops.rb +0 -74
  71. data/lib/deep_cover/node/mixin/can_augment_children.rb +0 -65
  72. data/lib/deep_cover/node/mixin/check_completion.rb +0 -18
  73. data/lib/deep_cover/node/mixin/child_can_be_empty.rb +0 -27
  74. data/lib/deep_cover/node/mixin/executed_after_children.rb +0 -15
  75. data/lib/deep_cover/node/mixin/execution_location.rb +0 -66
  76. data/lib/deep_cover/node/mixin/filters.rb +0 -47
  77. data/lib/deep_cover/node/mixin/flow_accounting.rb +0 -71
  78. data/lib/deep_cover/node/mixin/has_child.rb +0 -145
  79. data/lib/deep_cover/node/mixin/has_child_handler.rb +0 -75
  80. data/lib/deep_cover/node/mixin/has_tracker.rb +0 -46
  81. data/lib/deep_cover/node/mixin/is_statement.rb +0 -20
  82. data/lib/deep_cover/node/mixin/rewriting.rb +0 -35
  83. data/lib/deep_cover/node/mixin/wrapper.rb +0 -15
  84. data/lib/deep_cover/node/module.rb +0 -66
  85. data/lib/deep_cover/node/root.rb +0 -20
  86. data/lib/deep_cover/node/send.rb +0 -161
  87. data/lib/deep_cover/node/short_circuit.rb +0 -42
  88. data/lib/deep_cover/node/splat.rb +0 -15
  89. data/lib/deep_cover/node/variables.rb +0 -16
  90. data/lib/deep_cover/parser_ext/range.rb +0 -21
  91. data/lib/deep_cover/problem_with_diagnostic.rb +0 -63
  92. data/lib/deep_cover/reporter.rb +0 -10
  93. data/lib/deep_cover/reporter/base.rb +0 -68
  94. data/lib/deep_cover/reporter/html.rb +0 -15
  95. data/lib/deep_cover/reporter/html/base.rb +0 -14
  96. data/lib/deep_cover/reporter/html/index.rb +0 -59
  97. data/lib/deep_cover/reporter/html/site.rb +0 -70
  98. data/lib/deep_cover/reporter/html/source.rb +0 -136
  99. data/lib/deep_cover/reporter/html/template/assets/32px.png +0 -0
  100. data/lib/deep_cover/reporter/html/template/assets/40px.png +0 -0
  101. data/lib/deep_cover/reporter/html/template/assets/deep_cover.css.sass +0 -336
  102. data/lib/deep_cover/reporter/html/template/assets/jquery-3.2.1.min.js +0 -4
  103. data/lib/deep_cover/reporter/html/template/assets/jquery-3.2.1.min.map +0 -1
  104. data/lib/deep_cover/reporter/html/template/assets/jstree.css +0 -1108
  105. data/lib/deep_cover/reporter/html/template/assets/jstree.js +0 -8424
  106. data/lib/deep_cover/reporter/html/template/assets/jstreetable.js +0 -1069
  107. data/lib/deep_cover/reporter/html/template/assets/throbber.gif +0 -0
  108. data/lib/deep_cover/reporter/html/template/index.html.erb +0 -75
  109. data/lib/deep_cover/reporter/html/template/source.html.erb +0 -35
  110. data/lib/deep_cover/reporter/istanbul.rb +0 -184
  111. data/lib/deep_cover/reporter/text.rb +0 -58
  112. data/lib/deep_cover/reporter/tree/util.rb +0 -86
  113. data/lib/deep_cover/tools.rb +0 -22
  114. data/lib/deep_cover/tools/blank.rb +0 -25
  115. data/lib/deep_cover/tools/builtin_coverage.rb +0 -55
  116. data/lib/deep_cover/tools/camelize.rb +0 -13
  117. data/lib/deep_cover/tools/content_tag.rb +0 -11
  118. data/lib/deep_cover/tools/covered.rb +0 -9
  119. data/lib/deep_cover/tools/execute_sample.rb +0 -34
  120. data/lib/deep_cover/tools/format.rb +0 -18
  121. data/lib/deep_cover/tools/format_char_cover.rb +0 -19
  122. data/lib/deep_cover/tools/format_generated_code.rb +0 -27
  123. data/lib/deep_cover/tools/indent_string.rb +0 -26
  124. data/lib/deep_cover/tools/merge.rb +0 -16
  125. data/lib/deep_cover/tools/number_lines.rb +0 -22
  126. data/lib/deep_cover/tools/our_coverage.rb +0 -11
  127. data/lib/deep_cover/tools/profiling.rb +0 -68
  128. data/lib/deep_cover/tools/render_template.rb +0 -13
  129. data/lib/deep_cover/tools/require_relative_dir.rb +0 -12
  130. data/lib/deep_cover/tools/scan_match_datas.rb +0 -10
  131. data/lib/deep_cover/tools/silence_warnings.rb +0 -18
  132. data/lib/deep_cover/tools/slice.rb +0 -9
  133. data/lib/deep_cover/tools/strip_heredoc.rb +0 -18
  134. data/lib/deep_cover/tools/truncate_backtrace.rb +0 -32
  135. data/lib/deep_cover/tracker_bucket.rb +0 -50
  136. data/lib/deep_cover/tracker_hits_per_path.rb +0 -35
  137. data/lib/deep_cover/tracker_storage.rb +0 -76
  138. data/lib/deep_cover/tracker_storage_per_path.rb +0 -34
  139. data/lib/deep_cover/version.rb +0 -5
@@ -1,3 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'deep_cover'
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DeepCover
4
- require_relative 'deep_cover/load'
5
-
6
- load_absolute_basics
7
-
8
- extend Base
9
- extend ConfigSetter
10
- end
11
- DeepCover::GLOBAL_BINDING = binding
12
-
13
- require './.deep_cover.rb' if File.exist?('./.deep_cover.rb')
14
-
15
- if ENV['DEEP_COVER_OPTIONS']
16
- DeepCover.config.set(YAML.load(ENV['DEEP_COVER_OPTIONS']))
17
- end
18
- if %w[1 t true].include?(ENV['DEEP_COVER'])
19
- DeepCover.start
20
- require_relative 'deep_cover/auto_run'
21
- DeepCover::AutoRun.run!('.').report!(**DeepCover.config)
22
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DeepCover
4
- bootstrap
5
-
6
- # An analyser works on a subset of the original Node AST.
7
- # The Root node is always considered part of the subset.
8
- # One can iterate this subset with `each_node`, or ask
9
- # the analyser for information about a node's children
10
- # (i.e. with respect to this subset), or runs for any node
11
- # in this subset.
12
-
13
- # An analyser can summarize information with `results`.
14
- # While CoveredCodeSource is based on a CoveredCode, all
15
- # other analysers are based on another source analyser.
16
-
17
- class Analyser
18
- end
19
-
20
- require_relative_dir 'analyser'
21
-
22
- Analyser.include Analyser::Base
23
- end
@@ -1,104 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DeepCover
4
- module Analyser::Base
5
- include Tools::Covered
6
-
7
- attr_reader :source, :options
8
-
9
- def initialize(source, **options)
10
- @source = to_source(source, **options)
11
- @options = options
12
- end
13
-
14
- # Looking exclusively at our subset of nodes, returns the node's direct descendants
15
- def node_children(node)
16
- @source.node_children(node)
17
- end
18
-
19
- # Returns the number of runs of the node (assumed to be in our subset)
20
- def node_runs(node)
21
- @source.node_runs(node)
22
- end
23
-
24
- def node_covered?(node)
25
- covered?(node_runs(node))
26
- end
27
-
28
- def node_runs_map
29
- each_node.map do |node|
30
- [node, node_runs(node)]
31
- end.to_h
32
- end
33
-
34
- # Analyser-specific output
35
- def results
36
- node_runs_map
37
- end
38
-
39
- def node_stat_type(node)
40
- return :not_executable unless node.executable?
41
- case node_runs(node)
42
- when nil
43
- :ignored
44
- when 0
45
- :not_executed
46
- else
47
- :executed
48
- end
49
- end
50
-
51
- def node_stat_contributions(nodes)
52
- if respond_to? :node_stat_contribution
53
- nodes.sum { |n| node_stat_contribution(n) }
54
- else
55
- nodes.size
56
- end
57
- end
58
-
59
- def stats
60
- st = each_node.group_by { |n| node_stat_type(n) }
61
- .transform_values { |nodes| node_stat_contributions(nodes) }
62
- Analyser::Stats.new(**st)
63
- end
64
-
65
- # Iterates on nodes in the subset.
66
- # Yields the node and it's children (within the subset)
67
- def each_node(from = covered_code.root, &block)
68
- return to_enum(:each_node) unless block_given?
69
- begin
70
- yield from unless from.is_a?(Node::Root)
71
- rescue ProblemWithDiagnostic
72
- raise
73
- rescue StandardError, SystemStackError => e
74
- raise ProblemWithDiagnostic.new(covered_code, from.diagnostic_expression, e)
75
- end
76
- node_children(from).each do |child|
77
- each_node(child, &block)
78
- end
79
- end
80
-
81
- def covered_code
82
- @source.covered_code
83
- end
84
-
85
- protected
86
-
87
- def convert(covered_code, **options)
88
- Analyser::Node.new(covered_code, **options)
89
- end
90
-
91
- def to_source(source, **options)
92
- case source
93
- when Analyser
94
- source
95
- when CoveredCode
96
- convert(source, **options)
97
- when Node
98
- convert(source.covered_code, **options)
99
- else
100
- raise ArgumentError, "expected Analyser, Node or CoveredCode, got #{source.class}"
101
- end
102
- end
103
- end
104
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'subset'
4
-
5
- module DeepCover
6
- class Analyser::Branch < Analyser
7
- def self.human_name
8
- 'Branches'
9
- end
10
- include Analyser::Subset
11
- SUBSET_CLASSES = [Node::Branch].freeze
12
-
13
- def node_runs(node)
14
- runs = super
15
- if node.is_a?(Node::Branch) && covered?(runs)
16
- worst = worst_branch_runs(node)
17
- runs = worst unless covered?(worst)
18
- end
19
- runs
20
- end
21
-
22
- def results
23
- each_node.map do |node|
24
- branches_runs = node.branches.map { |jump| [jump, branch_runs(jump)] }.to_h
25
- [node, branches_runs]
26
- end.to_h
27
- end
28
-
29
- private
30
-
31
- def worst_branch_runs(fork)
32
- fork.branches.map { |jump| branch_runs(jump) }
33
- .sort_by { |runs| runs == 0 ? -2 : runs || -1 }
34
- .first
35
- end
36
-
37
- def branch_runs(branch)
38
- branch.flow_entry_count.nonzero? || source.node_runs(branch)
39
- end
40
- end
41
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DeepCover
4
- class Analyser::CoveredCodeSource < Analyser
5
- attr_reader :covered_code
6
-
7
- def initialize(covered_code)
8
- @covered_code = covered_code.freeze
9
- end
10
-
11
- # Looking exclusively at our subset of nodes, returns the node's direct descendants
12
- def node_children(node)
13
- node.children_nodes
14
- end
15
-
16
- # Returns the number of runs of the node (assumed to be in our subset)
17
- def node_runs(node)
18
- node.execution_count if node.executable?
19
- end
20
- end
21
- end
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'subset'
4
-
5
- module DeepCover
6
- class Analyser::Function < Analyser
7
- include Analyser::Subset
8
- SUBSET_CLASSES = [Node::Block, Node::Defs, Node::Def].freeze
9
-
10
- def node_runs(node)
11
- super(node.body)
12
- end
13
- end
14
- end
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DeepCover
4
- class Analyser::Node < Analyser
5
- include Analyser::Subset
6
-
7
- def self.human_name
8
- 'Nodes'
9
- end
10
-
11
- def initialize(source, ignore_uncovered: [], **options)
12
- @cache = {}.compare_by_identity
13
- super
14
- @allow_filters = Array(ignore_uncovered).map { |kind| Node.filter_to_method_name(kind) }
15
- @nocov_ranges = FlagCommentAssociator.new(covered_code)
16
- end
17
-
18
- def node_runs(node)
19
- @cache.fetch(node) do
20
- runs = super
21
- runs = nil if runs == 0 && should_be_ignored?(node)
22
- @cache[node] = runs
23
- end
24
- end
25
-
26
- def in_subset?(node, _parent)
27
- node.executable?
28
- end
29
-
30
- protected
31
-
32
- def convert(node, **) # rubocop:disable Naming/UncommunicativeMethodParamName [#5436]
33
- Analyser::CoveredCodeSource.new(node)
34
- end
35
-
36
- private
37
-
38
- def should_be_ignored?(node)
39
- @nocov_ranges.include?(node) ||
40
- @allow_filters.any? { |f| node.public_send(f) } ||
41
- is_ignored?(node.parent)
42
- end
43
-
44
- def is_ignored?(node)
45
- if node == nil
46
- false
47
- elsif node.executable?
48
- node_runs(node).nil?
49
- else
50
- is_ignored?(node.parent)
51
- end
52
- end
53
- end
54
- end
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DeepCover
4
- class Analyser::PerChar < Analyser
5
- def self.human_name
6
- 'Chars'
7
- end
8
-
9
- # Returns an array of characters for each line of code.
10
- # Each character is either ' ' (executed), '-' (not executable) or 'x' (not covered)
11
- def results
12
- bc = buffer.source_lines.map { |line| '-' * line.size }
13
- each_node do |node|
14
- runs = node_runs(node)
15
- next if runs == nil
16
- node.proper_range.each do |pos|
17
- bc[buffer.line_for_position(pos) - buffer.first_line][buffer.column_for_position(pos)] = runs > 0 ? ' ' : 'x'
18
- end
19
- end
20
- bc.zip(buffer.source_lines) { |cov, line| cov[line.size..-1] = '' } # remove extraneous character for end lines, in any
21
- bc
22
- end
23
-
24
- def node_stat_contribution(node)
25
- node.executed_locs.sum(&:size)
26
- end
27
-
28
- def stats
29
- s = super
30
- actual_total = buffer.source.size
31
- s.with not_executable: actual_total - s.total
32
- end
33
-
34
- def buffer
35
- covered_code.buffer
36
- end
37
- end
38
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DeepCover
4
- class Analyser::PerLine < Analyser
5
- # Returns an array of runs, one per line.
6
- # allow_partial can be one of:
7
- # true: Allow any partial covering. Basically ruby's line coverage,
8
- # if any thing is executed, it is considered executed
9
- # branch: Only allow branches to be partially covered.
10
- # if a node is not executed, the line has to be marked as not executed, even if part of it was.
11
- # false: Allow nothing to be partially covered.
12
- # same as :branch, but also:
13
- # if an empty branch is not executed, the line has to be marked as not executed.
14
- # This is only for empty branches because, if they are not empty, there will already
15
- # be some red from the partial node covering. We don't everything to become red,
16
- # just want 100% coverage to be as hard as branch + node coverage.
17
- def results
18
- allow_partial = options.fetch(:allow_partial, true)
19
- line_hits = Array.new(covered_code.nb_lines + covered_code.lineno - 1)
20
- disallowed_lines = Set.new
21
- each_node do |node|
22
- next unless (runs = node_runs(node))
23
- node.executed_locs.each do |loc|
24
- line_index = loc.line - 1
25
-
26
- next if disallowed_lines.include?(line_index)
27
- disallowed_lines << line_index if [nil, false, :branch].include?(allow_partial) && runs == 0
28
- disallowed_lines << line_index if !allow_partial && missed_empty_branch?(node)
29
-
30
- line_hits[line_index] = [line_hits[line_index] || 0, runs].max
31
- end
32
- end
33
- disallowed_lines.each { |line_index| line_hits[line_index] = 0 }
34
- line_hits
35
- end
36
-
37
- def missed_empty_branch?(node)
38
- node.is_a?(Node::Branch) && node.branches.any? { |b| b.is_a?(Node::EmptyBody) && !Tools.covered?(node_runs(b)) }
39
- end
40
- end
41
- end
@@ -1,211 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'subset'
4
-
5
- module DeepCover
6
- class Analyser::Ruby25LikeBranch < Analyser
7
- def self.human_name
8
- 'Ruby25 branches'
9
- end
10
- include Analyser::Subset
11
- SUBSET_CLASSES = [Node::Case, Node::Csend, Node::If, Node::ShortCircuit,
12
- Node::Until, Node::UntilPost, Node::While, Node::WhilePost,
13
- ].freeze
14
-
15
- def initialize(*args)
16
- super
17
- @loc_index = 0
18
- end
19
-
20
- def results
21
- extractor = NodeCoverageExtrator.new
22
- each_node.map do |node|
23
- extractor.branch_coverage(node)
24
- end.to_h
25
- end
26
-
27
- # This is the class doing the work. Since everything is about the node, the class delegates
28
- # missing methods to the node, simplifying the code.
29
- class NodeCoverageExtrator < SimpleDelegator
30
- def initialize(node = nil)
31
- self.node = node
32
- @loc_index = 0
33
- end
34
-
35
- alias_method :node=, :__setobj__
36
- alias_method :node, :__getobj__
37
-
38
- def branch_coverage(node)
39
- self.node = node
40
- case node
41
- when Node::Case
42
- handle_case
43
- when Node::Csend
44
- handle_csend
45
- when Node::If
46
- handle_if
47
- when Node::ShortCircuit
48
- handle_short_circuit
49
- when Node::Until, Node::While, Node::UntilPost, Node::WhilePost
50
- handle_until_while
51
- end
52
- end
53
-
54
- def handle_case
55
- cond_info = [:case, *node_loc_infos]
56
-
57
- sub_keys = [:when] * (branches.size - 1) + [:else]
58
- empty_fallbacks = whens.map { |w| (w.loc_hash[:begin] || w.loc_hash[:expression]).wrap_rwhitespace_and_comments.end }
59
- empty_fallbacks.map!(&:begin)
60
-
61
- if loc_hash[:else]
62
- empty_fallbacks << loc_hash[:end].begin
63
- else
64
- # DeepCover manually inserts a `else` for Case when there isn't one for tracker purposes.
65
- # The normal behavior of ruby25's branch coverage when there is no else is to return the loc of the node
66
- # So we sent that fallback.
67
- empty_fallbacks << expression
68
- end
69
-
70
- branches_locs = whens.map do |when_node|
71
- next when_node.body if when_node.body.is_a?(Node::EmptyBody)
72
-
73
- start_at = when_node.loc_hash[:begin]
74
- start_at = start_at.wrap_rwhitespace_and_comments.end if start_at
75
- start_at ||= when_node.body.expression.begin
76
-
77
- end_at = when_node.body.expression.end
78
- start_at.with(end_pos: end_at.end_pos)
79
- end
80
-
81
- branches_locs << node.else
82
- clauses_infos = infos_for_branches(branches_locs, sub_keys, empty_fallbacks, execution_counts: branches.map(&:execution_count))
83
-
84
- [cond_info, clauses_infos]
85
- end
86
-
87
- def handle_csend
88
- # csend wraps the comment but not the newlines
89
- node_range = node.expression.wrap_rwhitespace_and_comments(whitespaces: /\A[ \t\r\f]+/)
90
- cond_info = [:"&.", *node_loc_infos(node_range)]
91
- false_branch, true_branch = branches
92
- [cond_info, {[:then, *node_loc_infos(node_range)] => true_branch.execution_count,
93
- [:else, *node_loc_infos(node_range)] => false_branch.execution_count,
94
- },
95
- ]
96
- end
97
-
98
- def handle_if
99
- key = style == :unless ? :unless : :if
100
-
101
- node_range = extend_elsif_range
102
- cond_info = [key, *node_loc_infos(node_range)]
103
-
104
- sub_keys = [:then, :else]
105
- if style == :ternary
106
- empty_fallback_locs = [nil, nil]
107
- else
108
- else_loc = loc_hash[:else]
109
-
110
- first_clause_fallback = loc_hash[:begin]
111
- if first_clause_fallback
112
- first_clause_fallback = first_clause_fallback.wrap_rwhitespace_and_comments.end
113
- elsif else_loc
114
- first_clause_fallback = else_loc.begin
115
- end
116
-
117
- if else_loc
118
- second_clause_fallback = else_loc.wrap_rwhitespace_and_comments.end
119
- end
120
- end_loc = root_if_node.loc_hash[:end]
121
- end_loc = end_loc.begin if end_loc
122
-
123
- empty_fallback_locs = [first_clause_fallback || end_loc, second_clause_fallback || end_loc]
124
- end
125
- # loc can be nil if the clause can't be empty, such as ternary and modifer if/unless
126
-
127
- if key == :unless
128
- sub_keys.reverse!
129
- empty_fallback_locs.reverse!
130
- end
131
-
132
- branches_locs = branches
133
- execution_counts = branches_locs.map(&:execution_count)
134
- branches_locs[1] = extend_elsif_range(branches_locs[1])
135
-
136
- clauses_infos = infos_for_branches(branches_locs, sub_keys, empty_fallback_locs, execution_counts: execution_counts, node_range: node_range)
137
- [cond_info, clauses_infos]
138
- end
139
-
140
- def handle_short_circuit
141
- cond_info = [operator, *node_loc_infos]
142
- sub_keys = [:then, :else]
143
- sub_keys.reverse! if node.is_a?(Node::Or)
144
-
145
- [cond_info, infos_for_branches(branches, sub_keys, [nil, nil])]
146
- end
147
-
148
- def handle_until_while
149
- key = loc_hash[:keyword].source.to_sym
150
- base_info = [key, *node_loc_infos]
151
- body_node = if node.is_a?(Node::WhilePost) || node.is_a?(Node::UntilPost)
152
- if !body.instructions.empty?
153
- end_pos = body.instructions.last.expression.end_pos
154
- body.instructions.first.expression.with(end_pos: end_pos)
155
- else
156
- body.loc_hash[:end].begin
157
- end
158
- elsif body.is_a?(Node::Begin) && !node.body.expressions.empty?
159
- end_pos = body.expressions.last.expression.end_pos
160
- body.expressions.first.expression.with(end_pos: end_pos)
161
- else
162
- body
163
- end
164
-
165
- [base_info, {[:body, *node_loc_infos(body_node)] => body.execution_count}]
166
- end
167
-
168
- protected
169
-
170
- # If the actual else clause (final one) of an if...elsif...end is empty, then Ruby makes the
171
- # node reach the `end` in its branch coverage output
172
- def extend_elsif_range(possible_elsif = node)
173
- return possible_elsif unless possible_elsif.is_a?(Node::If) && possible_elsif.style == :elsif
174
- deepest_if = possible_elsif.deepest_elsif_node
175
- if deepest_if.false_branch.is_a?(Node::EmptyBody)
176
- return possible_elsif.expression.with(end_pos: possible_elsif.root_if_node.loc_hash[:end].begin_pos)
177
- end
178
- possible_elsif
179
- end
180
-
181
- def infos_for_branch(branch, key, empty_fallback_loc, execution_count: nil, node_range: node)
182
- if !branch.is_a?(Node::EmptyBody)
183
- loc = branch
184
- elsif branch.expression
185
- # There is clause, but it is empty
186
- loc = empty_fallback_loc
187
- else
188
- # There is no clause
189
- loc = node_range
190
- end
191
-
192
- execution_count ||= branch.execution_count
193
- [[key, *node_loc_infos(loc)], execution_count]
194
- end
195
-
196
- def infos_for_branches(branches, keys, empty_fallback_locs, execution_counts: [], node_range: node)
197
- branches_infos = branches.map.with_index do |branch, i|
198
- infos_for_branch(branch, keys[i], empty_fallback_locs[i], execution_count: execution_counts[i], node_range: node_range)
199
- end
200
- branches_infos.to_h
201
- end
202
-
203
- def node_loc_infos(node_or_range = node)
204
- source_range = node_or_range.is_a?(Node) ? node_or_range.expression : node_or_range
205
-
206
- @loc_index += 1
207
- [@loc_index, source_range.line, source_range.column, source_range.last_line, source_range.last_column]
208
- end
209
- end
210
- end
211
- end