path_coverage 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +316 -177
  3. data/exe/path_coverage_report +4 -0
  4. data/lib/path_coverage/call_trace.rb +88 -0
  5. data/lib/path_coverage/calls.rb +49 -43
  6. data/lib/path_coverage/card_context.rb +104 -0
  7. data/lib/path_coverage/card_forks.rb +44 -0
  8. data/lib/path_coverage/card_gaps.rb +125 -0
  9. data/lib/path_coverage/card_scopes.rb +37 -0
  10. data/lib/path_coverage/card_tree.rb +199 -0
  11. data/lib/path_coverage/card_way_lists.rb +72 -0
  12. data/lib/path_coverage/card_ways.rb +196 -0
  13. data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +38 -25
  14. data/lib/path_coverage/condition.rb +14 -14
  15. data/lib/path_coverage/configuration.rb +7 -4
  16. data/lib/path_coverage/configuration_limits.rb +52 -0
  17. data/lib/path_coverage/configuration_paths.rb +7 -7
  18. data/lib/path_coverage/console_summary.rb +24 -22
  19. data/lib/path_coverage/{site.rb → decision.rb} +2 -2
  20. data/lib/path_coverage/decision_locations.rb +76 -0
  21. data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +18 -8
  22. data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
  23. data/lib/path_coverage/fold_branches.rb +18 -0
  24. data/lib/path_coverage/fold_cells.rb +42 -0
  25. data/lib/path_coverage/fold_rows.rb +140 -0
  26. data/lib/path_coverage/gaps.rb +33 -15
  27. data/lib/path_coverage/json_reader.rb +54 -19
  28. data/lib/path_coverage/json_reports.rb +102 -0
  29. data/lib/path_coverage/json_writer.rb +31 -16
  30. data/lib/path_coverage/kernel.rb +56 -57
  31. data/lib/path_coverage/kinds/base.rb +16 -16
  32. data/lib/path_coverage/kinds/block.rb +16 -15
  33. data/lib/path_coverage/kinds/case.rb +14 -17
  34. data/lib/path_coverage/kinds/chain.rb +40 -11
  35. data/lib/path_coverage/kinds/choice.rb +7 -10
  36. data/lib/path_coverage/kinds/contains.rb +43 -33
  37. data/lib/path_coverage/kinds/safe.rb +44 -46
  38. data/lib/path_coverage/kinds/write.rb +6 -12
  39. data/lib/path_coverage/kinds.rb +2 -2
  40. data/lib/path_coverage/leaving.rb +44 -0
  41. data/lib/path_coverage/loop_forks.rb +57 -0
  42. data/lib/path_coverage/loop_nodes.rb +55 -0
  43. data/lib/path_coverage/manifest.rb +94 -59
  44. data/lib/path_coverage/manifest_blocks.rb +5 -26
  45. data/lib/path_coverage/manifest_case.rb +2 -2
  46. data/lib/path_coverage/manifest_chains.rb +8 -8
  47. data/lib/path_coverage/manifest_choices.rb +52 -29
  48. data/lib/path_coverage/manifest_disabled.rb +97 -0
  49. data/lib/path_coverage/manifest_folding.rb +66 -26
  50. data/lib/path_coverage/manifest_ignoring.rb +6 -6
  51. data/lib/path_coverage/manifest_writes.rb +11 -10
  52. data/lib/path_coverage/measured_by.rb +20 -0
  53. data/lib/path_coverage/merge.rb +7 -7
  54. data/lib/path_coverage/method_loops.rb +128 -0
  55. data/lib/path_coverage/method_statements.rb +147 -139
  56. data/lib/path_coverage/method_ways.rb +142 -0
  57. data/lib/path_coverage/paths_report.rb +16 -16
  58. data/lib/path_coverage/recorder.rb +57 -36
  59. data/lib/path_coverage/recorder_blocks.rb +17 -17
  60. data/lib/path_coverage/recorder_cases.rb +12 -11
  61. data/lib/path_coverage/recorder_chains.rb +12 -12
  62. data/lib/path_coverage/recorder_writes.rb +9 -9
  63. data/lib/path_coverage/render.rb +58 -51
  64. data/lib/path_coverage/result.rb +32 -31
  65. data/lib/path_coverage/rewriter.rb +39 -27
  66. data/lib/path_coverage/rewriter_nested.rb +13 -13
  67. data/lib/path_coverage/runs_report.rb +84 -0
  68. data/lib/path_coverage/source_lines.rb +16 -16
  69. data/lib/path_coverage/source_report.rb +100 -0
  70. data/lib/path_coverage/stable_leaves.rb +162 -0
  71. data/lib/path_coverage/statement_arms.rb +61 -27
  72. data/lib/path_coverage/summary.rb +4 -62
  73. data/lib/path_coverage/totals.rb +10 -8
  74. data/lib/path_coverage/version.rb +1 -1
  75. data/lib/path_coverage/walk.rb +106 -0
  76. data/lib/path_coverage/way_algebras.rb +127 -0
  77. data/lib/path_coverage/ways_report.rb +91 -0
  78. data/lib/path_coverage/ways_totals.rb +46 -0
  79. data/lib/path_coverage/within_methods.rb +184 -44
  80. metadata +36 -29
  81. data/lib/path_coverage/card.rb +0 -14
  82. data/lib/path_coverage/html/file.html +0 -33
  83. data/lib/path_coverage/html/index.html +0 -29
  84. data/lib/path_coverage/html/settings.js +0 -50
  85. data/lib/path_coverage/html/side.js +0 -470
  86. data/lib/path_coverage/html/sort.js +0 -34
  87. data/lib/path_coverage/html/style.css +0 -470
  88. data/lib/path_coverage/html/tabs.js +0 -15
  89. data/lib/path_coverage/html/theme.js +0 -27
  90. data/lib/path_coverage/html_branch.rb +0 -30
  91. data/lib/path_coverage/html_cards.rb +0 -111
  92. data/lib/path_coverage/html_cells.rb +0 -66
  93. data/lib/path_coverage/html_file.rb +0 -123
  94. data/lib/path_coverage/html_index.rb +0 -128
  95. data/lib/path_coverage/html_index_head.rb +0 -29
  96. data/lib/path_coverage/html_labels.rb +0 -23
  97. data/lib/path_coverage/html_methods.rb +0 -66
  98. data/lib/path_coverage/html_nav.rb +0 -67
  99. data/lib/path_coverage/html_report.rb +0 -131
  100. data/lib/path_coverage/html_statements.rb +0 -166
  101. data/lib/path_coverage/html_tabs.rb +0 -26
  102. data/lib/path_coverage/html_template.rb +0 -26
  103. data/lib/path_coverage/html_tree.rb +0 -131
@@ -0,0 +1,55 @@
1
+ module PathCoverage
2
+ # The loops in a method's Prism tree: its blocks, lambdas,
3
+ # whiles, untils and fors, each run 0 or more times, and their
4
+ # bodies, as MethodLoops and LoopForks take them.
5
+ module LoopNodes
6
+ # The types of node that are loops written with a keyword,
7
+ # their body their statements.
8
+ KEYWORDS = %i[while_node until_node for_node].freeze
9
+
10
+ # The types of node that are loops.
11
+ LOOPS = [:block_node, :lambda_node, *KEYWORDS].freeze
12
+
13
+ # Every def under node, outermost first.
14
+ def self.defs(node)
15
+ found = node.type == :def_node ? [node] : []
16
+ found + node.compact_child_nodes.flat_map { defs(_1) }
17
+ end
18
+
19
+ # Every loop under node, in the order they start, but none
20
+ # inside a def under it, whose loops are its own.
21
+ def self.loops_in(node)
22
+ return [] if node.nil? || node.type == :def_node
23
+
24
+ own = LOOPS.include?(node.type) ? [node] : []
25
+ own + node.compact_child_nodes.flat_map { loops_in(_1) }
26
+ end
27
+
28
+ # The body of loop: a block's or lambda's body, a keyword
29
+ # loop's statements; nil for an empty one.
30
+ def self.body_of(loop)
31
+ KEYWORDS.include?(loop.type) ? loop.statements : loop.body
32
+ end
33
+
34
+ # True for a keyword loop written after its body, eg x += 1
35
+ # while x < 9, where a call put first in its body would run
36
+ # once, not each pass, so none goes there.
37
+ def self.modifier?(loop)
38
+ return false unless KEYWORDS.include?(loop.type)
39
+
40
+ body = loop.statements.location
41
+ loop.keyword_loc.start_offset > body.start_offset
42
+ end
43
+
44
+ # The first statement of a loop's body, a do ... rescue ... end
45
+ # block's being its first before the rescue; nil when it has
46
+ # none there, its statements all in its rescue, which run only
47
+ # once a statement raises, so there is no pass to note.
48
+ def self.first_statement(body)
49
+ return body.body.first unless body.type == :begin_node
50
+ return if body.statements.nil?
51
+
52
+ body.statements.body.first
53
+ end
54
+ end
55
+ end
@@ -1,50 +1,78 @@
1
1
  require 'prism'
2
2
  require_relative 'condition'
3
+ require_relative 'leaving'
3
4
  require_relative 'manifest_blocks'
4
5
  require_relative 'manifest_case'
5
6
  require_relative 'manifest_chains'
6
7
  require_relative 'manifest_choices'
8
+ require_relative 'manifest_disabled'
7
9
  require_relative 'manifest_folding'
8
10
  require_relative 'manifest_ignoring'
9
11
  require_relative 'manifest_writes'
10
- require_relative 'site'
12
+ require_relative 'decision'
11
13
 
12
14
  module PathCoverage
13
- # Finds every condition site in source without running it: each
15
+ # Finds every condition decision in source without running it: each
14
16
  # outermost &&, ||, and or or, each ternary, if, unless and case
15
17
  # with a subject, with the conditions nested in it.
16
18
  module Manifest
17
- # { site => Condition } for each site in source, none of
19
+ # { decision => Condition } for each decision in source, none of
18
20
  # them named by an operator in ignored; a call to a method
19
21
  # named in present, as a &.'s receiver, is never nil, and a
20
22
  # call to one named in nonempty, eg any?, is true exactly
21
- # when its receiver has items.
22
- def self.sites(source, path, ignored = [], present = [],
23
- nonempty = [])
23
+ # when its receiver has items. None is on a line not
24
+ # analysed, as ManifestDisabled.lines gives them.
25
+ def self.decisions(source, path, ignored = [], present = [],
26
+ nonempty = [])
24
27
  collector = ConditionSites.new(path, ignored,
25
28
  [present, nonempty])
26
- Prism.parse(source).value.accept(collector)
27
- collector.sites
29
+ parsed = Prism.parse(source)
30
+ parsed.value.accept(collector)
31
+ disabled = ManifestDisabled.lines(source, parsed, path)
32
+ collector.decisions.reject do |name, _|
33
+ disabled.include?(name.split(':')[-2].to_i)
34
+ end
28
35
  end
29
36
 
30
- # The source and sites of the covered file at full_path.
37
+ # The source and decisions of the covered file at full_path.
31
38
  def self.parse(full_path, config)
32
39
  source = File.read(full_path)
33
40
  path = config.relative(full_path)
34
41
  treated = %w[present nonempty].map { config.treated(_1, path) }
35
42
  ignored = config.ignored
36
- { source:, sites: sites(source, path, ignored, *treated) }
43
+ { source:,
44
+ decisions: decisions(source, path, ignored, *treated) }
45
+ end
46
+
47
+ # True for an expression that is no bare argument, so a
48
+ # rewrite wrapping it in another call must parenthesise it: a
49
+ # command call, an and or an or, or a modifier, eg x unless y.
50
+ def self.bare_less?(node)
51
+ return true if command?(node) || modifier?(node)
52
+
53
+ %i[and_node or_node].include?(node.type) &&
54
+ %w[and or].include?(node.operator)
37
55
  end
38
56
 
39
57
  # True for a call to a named method with arguments and no
40
- # parentheses, eg `id.include? 'N'`, but not `id.size == 6`;
41
- # such a call is no bare argument, so a rewrite wrapping it
42
- # in another call must parenthesise it.
58
+ # parentheses, eg `id.include? 'N'`, but not `id.size == 6`.
43
59
  def self.command?(node)
44
60
  node.type == :call_node && node.arguments &&
45
61
  node.opening_loc.nil? && node.name.match?(/\A[a-z_]/i)
46
62
  end
47
63
 
64
+ # True for a modifier if, unless or rescue, eg x if y: one
65
+ # with no end keyword, a ternary being no modifier.
66
+ def self.modifier?(node)
67
+ case node.type
68
+ when :rescue_modifier_node then true
69
+ when :unless_node then node.end_keyword_loc.nil?
70
+ when :if_node
71
+ !node.if_keyword_loc.nil? && node.end_keyword_loc.nil?
72
+ else false
73
+ end
74
+ end
75
+
48
76
  # Literals and lambdas, whose value is truthy by their form;
49
77
  # a constant is not among them, as it can hold nil or false.
50
78
  TRUTHY_TYPES = %i[
@@ -82,7 +110,7 @@ module PathCoverage
82
110
  TRUTHY_GLOBALS.include?(node.name)
83
111
  end
84
112
 
85
- # Collects each site and its Condition.
113
+ # Collects each decision and its Condition.
86
114
  class ConditionSites < Prism::Visitor
87
115
  include ManifestBlocks
88
116
  include ManifestCase
@@ -92,7 +120,7 @@ module PathCoverage
92
120
  include ManifestIgnoring
93
121
  include ManifestWrites
94
122
 
95
- # Starts with no sites found, to find none named by an
123
+ # Starts with no decisions found, to find none named by an
96
124
  # operator in ignored, and to take a call to a method named
97
125
  # in present as never nil, and to one in nonempty as true
98
126
  # exactly when its receiver has items.
@@ -101,72 +129,72 @@ module PathCoverage
101
129
  @ignored = ignored
102
130
  @present = present
103
131
  @nonempty = nonempty
104
- @sites = {}
132
+ @decisions = {}
105
133
  @notes = nil
106
134
  @inner = nil
107
135
  super()
108
136
  end
109
137
 
110
- # The sites found so far.
111
- attr_reader :sites
138
+ # The decisions found so far.
139
+ attr_reader :decisions
112
140
 
113
141
  OPERATOR_TYPES = %i[and_node or_node].freeze
114
142
 
115
- # Notes an && or and site.
143
+ # Notes an && or and decision.
116
144
  def visit_and_node(node)
117
- add_site(node)
145
+ add_decision(node)
118
146
  end
119
147
 
120
- # Notes an || or or site.
148
+ # Notes an || or or decision.
121
149
  def visit_or_node(node)
122
- add_site(node)
150
+ add_decision(node)
123
151
  end
124
152
 
125
- # Notes a ternary or an if site, a modifier if included,
153
+ # Notes a ternary or an if decision, a modifier if included,
126
154
  # its arms included.
127
155
  def visit_if_node(node)
128
- add_site(node)
156
+ add_decision(node)
129
157
  end
130
158
 
131
- # Notes an unless site, a modifier unless included, its
159
+ # Notes an unless decision, a modifier unless included, its
132
160
  # arms included.
133
161
  def visit_unless_node(node)
134
- add_site(node)
162
+ add_decision(node)
135
163
  end
136
164
 
137
- # Notes a case site, when its paths can be seen; any other
138
- # case is no site, though a condition inside it may be.
165
+ # Notes a case decision, when its paths can be seen; any other
166
+ # case is no decision, though a condition inside it may be.
139
167
  def visit_case_node(node)
140
- return super unless case_site?(node)
168
+ return super unless case_decision?(node)
141
169
 
142
- add_site(node)
170
+ add_decision(node)
143
171
  end
144
172
 
145
- # Notes a &. chain as a site, or a block method's call, when
173
+ # Notes a &. chain as a decision, or a block method's call, when
146
174
  # its block's every call can be seen; any other call is no
147
- # site, though a condition inside it may be.
175
+ # decision, though a condition inside it may be.
148
176
  def visit_call_node(node)
149
- return add_site(node) if safe_chain?(node)
150
- return super unless block_site?(node)
177
+ return add_decision(node) if safe_chain?(node)
178
+ return super unless block_decision?(node)
151
179
 
152
- add_site(node)
180
+ add_decision(node)
153
181
  end
154
182
 
155
- # Notes node as a site, named where it starts: an if or an
183
+ # Notes node as a decision, named where it starts: an if or an
156
184
  # unless at its keyword. It claims its place first, so a
157
- # site found inside one of its leaves or arms follows it.
158
- # While a leaf is being visited, any site is collected as
185
+ # decision found inside one of its leaves or arms follows it.
186
+ # While a leaf is being visited, any decision is collected as
159
187
  # part of that leaf instead, as it is part of one expression.
160
- def add_site(node)
188
+ def add_decision(node)
161
189
  return visit_child_nodes(node) if ignored?(node)
162
190
  return @inner << nested_condition_of(node) if @inner
163
191
 
164
- site = Site.at(@path, site_loc(node))
165
- @sites[site] = nil
192
+ decision = Decision.at(@path, decision_loc(node))
193
+ @decisions[decision] = nil
166
194
  outer = @notes
167
195
  @notes = { commands: [], fixed: {} }
168
196
  condition = scoped { condition_of(node) }
169
- @sites[site] = condition.with(**@notes)
197
+ @decisions[decision] = condition.with(**@notes)
170
198
  @notes = outer
171
199
  end
172
200
 
@@ -190,20 +218,26 @@ module PathCoverage
190
218
 
191
219
  # node's operands: a chain of the same operator flattened
192
220
  # into one list, the other operator nested as a Condition,
193
- # as is a &. chain, its value its last leaf, anything else a
194
- # leaf's byte range.
221
+ # as is a &. chain, its value its last leaf, an operand that
222
+ # leaves the method an Arm, eg return t in x or return t,
223
+ # anything else a leaf's byte range.
195
224
  def operands_of(node)
196
- [node.left, node.right].flat_map do |written|
197
- operand = grouped(written)
198
- next [leaf(operand)] if ignored?(operand)
199
- next operands_of(operand) if operand.type == node.type
200
- next [condition_of(operand)] if operator?(operand)
201
- if safe_chain?(operand)
202
- next [chain_condition(operand, value: true)]
203
- end
204
-
205
- [leaf(operand)]
225
+ [node.left, node.right].flat_map { operands_in(_1, node) }
226
+ end
227
+
228
+ # The operands written, one side of node, stands for, as
229
+ # operands_of gives them.
230
+ def operands_in(written, node)
231
+ operand = grouped(written)
232
+ return [leaf(operand)] if ignored?(operand)
233
+ return [Leaving.arm(operand)] if Leaving.word(operand)
234
+ return operands_of(operand) if operand.type == node.type
235
+ return [condition_of(operand)] if operator?(operand)
236
+ if safe_chain?(operand)
237
+ return [chain_condition(operand, value: true)]
206
238
  end
239
+
240
+ [leaf(operand)]
207
241
  end
208
242
 
209
243
  # The byte range of node as written.
@@ -217,15 +251,16 @@ module PathCoverage
217
251
  OPERATOR_TYPES.include?(node.type)
218
252
  end
219
253
 
220
- # The byte range of a leaf operand, noted among its site's
221
- # commands and fixed leaves where it is one; or, when sites
254
+ # The byte range of a leaf operand, noted among its decision's
255
+ # commands and fixed leaves where it is one; or, when decisions
222
256
  # that fold are found inside it, the leaf with them nested.
223
257
  def leaf(operand)
224
- inner = collected { operand.accept(self) }
258
+ inner = leaf_decisions(operand)
225
259
  loc = operand.location
226
260
  range = loc.start_offset...loc.end_offset
227
- @notes[:commands] << range if Manifest.command?(operand)
228
- outcome = Manifest.fixed_outcome(operand)
261
+ @notes[:commands] << range if Manifest.bare_less?(operand)
262
+ outcome = Manifest.fixed_outcome(operand) ||
263
+ given_outcome(operand)
229
264
  @notes[:fixed][range] = outcome if outcome
230
265
  nonempty = nonempty_call?(operand, inner)
231
266
  contained(inner, range, nonempty:)
@@ -1,7 +1,7 @@
1
1
  require_relative 'condition'
2
2
 
3
3
  module PathCoverage
4
- # The part of the manifest's visitor that builds the site of a
4
+ # The part of the manifest's visitor that builds the decision of a
5
5
  # call to a block method, one that calls its block an item at
6
6
  # a time, until it decides, eg any?, or for every item, eg
7
7
  # select: its leaves are its receiver, whose items the block
@@ -14,14 +14,14 @@ module PathCoverage
14
14
  # make it ignore its block, not by &., which skips it for
15
15
  # nil, and with a block of one expression that is a value,
16
16
  # so can be wrapped: not break, next or return.
17
- def block_site?(node)
17
+ def block_decision?(node)
18
18
  return false unless BLOCK_PATHS.key?(node.name.to_s)
19
19
 
20
20
  plain_call?(node) && value?(block_expression(node.block))
21
21
  end
22
22
 
23
23
  # True for a call on a receiver, with no arguments. A call by
24
- # &. never gets here, as it is a site of its own first.
24
+ # &. never gets here, as it is a decision of its own first.
25
25
  def plain_call?(node)
26
26
  !node.receiver.nil? && node.arguments.nil?
27
27
  end
@@ -60,36 +60,15 @@ module PathCoverage
60
60
  )
61
61
  end
62
62
 
63
- # The byte range of block's one expression, any site in it
63
+ # The byte range of block's one expression, any decision in it
64
64
  # visited as one of its own, as the block runs once per item;
65
65
  # noted among the commands when it is no bare argument.
66
66
  def block_body(block)
67
67
  body = block_expression(block)
68
68
  scoped { body.accept(self) }
69
69
  range = extent_of(body)
70
- @notes[:commands] << range if bare_less?(body)
70
+ @notes[:commands] << range if Manifest.bare_less?(body)
71
71
  range
72
72
  end
73
-
74
- # True for an expression that is no bare argument: a command
75
- # call, an and or an or, or a modifier, eg x unless y.
76
- def bare_less?(node)
77
- return true if Manifest.command?(node) || modifier?(node)
78
-
79
- %i[and_node or_node].include?(node.type) &&
80
- %w[and or].include?(node.operator)
81
- end
82
-
83
- # True for a modifier if, unless or rescue, eg x if y: one
84
- # with no end keyword, a ternary being no modifier.
85
- def modifier?(node)
86
- case node.type
87
- when :rescue_modifier_node then true
88
- when :unless_node then node.end_keyword_loc.nil?
89
- when :if_node
90
- !node.if_keyword_loc.nil? && node.end_keyword_loc.nil?
91
- else false
92
- end
93
- end
94
73
  end
95
74
  end
@@ -1,7 +1,7 @@
1
1
  require_relative 'condition'
2
2
 
3
3
  module PathCoverage
4
- # The part of the manifest's visitor that builds a case site,
4
+ # The part of the manifest's visitor that builds a case decision,
5
5
  # named case: its arms are its whens, each holding a when
6
6
  # condition of its patterns, and its else. It relies on its
7
7
  # includer's extent_of and statement_arm.
@@ -10,7 +10,7 @@ module PathCoverage
10
10
  # subject, as each pattern is then matched against it, and
11
11
  # no splat among its patterns, whose items cannot each be
12
12
  # stood in for.
13
- def case_site?(node)
13
+ def case_decision?(node)
14
14
  return false if node.predicate.nil?
15
15
 
16
16
  patterns = node.conditions.flat_map(&:conditions)
@@ -1,7 +1,7 @@
1
1
  require_relative 'condition'
2
2
 
3
3
  module PathCoverage
4
- # The part of the manifest's visitor that builds the sites of
4
+ # The part of the manifest's visitor that builds the decisions of
5
5
  # safe-navigation chains, eg user&.account&.owner. It relies
6
6
  # on its includer's extent_of.
7
7
  module ManifestChains
@@ -23,7 +23,7 @@ module PathCoverage
23
23
  chain_calls(node).any?(&:safe_navigation?)
24
24
  end
25
25
 
26
- # A call that is a site as a Condition: a &. chain's, or a
26
+ # A call that is a decision as a Condition: a &. chain's, or a
27
27
  # block method call's. It relies on its includer's
28
28
  # block_condition.
29
29
  def call_condition(node)
@@ -36,23 +36,23 @@ module PathCoverage
36
36
  # receivers of its &. calls, innermost first, each nil or
37
37
  # not nil, and, with value, the whole chain's value last, as
38
38
  # when it is an operand of an && or an if's predicate. Any
39
- # site in a call's arguments or block, or in the chain's
40
- # first receiver, is a site of its own. A chain that is no
39
+ # decision in a call's arguments or block, or in the chain's
40
+ # first receiver, is a decision of its own. A chain that is no
41
41
  # bare argument, eg a command call, is noted among the
42
- # site's commands, to be parenthesised.
42
+ # decision's commands, to be parenthesised.
43
43
  def chain_condition(node, value:)
44
44
  calls = chain_calls(node)
45
45
  visit_chain(calls)
46
46
  safe = calls.select(&:safe_navigation?).reverse
47
47
  receivers = safe.map { |call| receiver_of(call) }
48
48
  extent = extent_of(node)
49
- @notes[:commands] << extent if Manifest.command?(node)
49
+ @notes[:commands] << extent if Manifest.bare_less?(node)
50
50
  operands = value ? [*receivers, extent] : receivers
51
51
  Condition.new(operator: SAFE, operands:, extent:)
52
52
  end
53
53
 
54
54
  # The byte range of a &. call's receiver, noted among its
55
- # site's fixed leaves as :present, never nil, when it is a
55
+ # decision's fixed leaves as :present, never nil, when it is a
56
56
  # call to a method treated as present.
57
57
  def receiver_of(call)
58
58
  receiver = call.receiver
@@ -63,7 +63,7 @@ module PathCoverage
63
63
  range
64
64
  end
65
65
 
66
- # Visits the sites inside a chain's calls' arguments and
66
+ # Visits the decisions inside a chain's calls' arguments and
67
67
  # blocks, and inside its first receiver, eg (a || b) in
68
68
  # (a || b)&.size, but never a call of the chain itself.
69
69
  def visit_chain(calls)
@@ -1,7 +1,8 @@
1
1
  require_relative 'condition'
2
+ require_relative 'leaving'
2
3
 
3
4
  module PathCoverage
4
- # The part of the manifest's visitor that builds the sites
5
+ # The part of the manifest's visitor that builds the decisions
5
6
  # whose predicate picks an arm: a ternary, named ?:, an if,
6
7
  # named if, and an unless, named unless. It relies on its
7
8
  # includer's condition_of, extent_of, grouped, leaf and
@@ -11,15 +12,16 @@ module PathCoverage
11
12
  # being an if_node with no if keyword.
12
13
  CHOICE_TYPES = %i[if_node unless_node].freeze
13
14
 
14
- # Where node's site starts: a &. chain's start; a block
15
- # method's call at its method; an if's or an unless's
16
- # keyword, which a modifier's statement comes before;
17
- # anything else's own start.
18
- def site_loc(node)
15
+ # Where node's decision is named: a &. chain's start; a block
16
+ # method's call at its method; a ternary at its ?, so it never
17
+ # shares the name of a decision starting where it does, eg its
18
+ # predicate; an if's or an unless's keyword, which a
19
+ # modifier's statement comes before; anything else's own start.
20
+ def decision_loc(node)
19
21
  return node.location if safe_chain?(node)
20
22
  return node.message_loc if node.type == :call_node
21
23
  return node.location unless CHOICE_TYPES.include?(node.type)
22
- return node.location if ternary?(node)
24
+ return node.then_keyword_loc if ternary?(node)
23
25
 
24
26
  keyword_loc(node)
25
27
  end
@@ -71,16 +73,18 @@ module PathCoverage
71
73
  end
72
74
 
73
75
  # An if's arm, statements its code, which never folds into
74
- # the if's site; as a new scope, any site inside it is a site
75
- # of its own. An arm with no statements has no extent.
76
+ # the if's decision; as a new scope, any decision inside it
77
+ # is a decision of its own. An arm with no statements has no
78
+ # extent.
76
79
  def statement_arm(kind, statements)
77
80
  scoped { statements&.accept(self) }
78
81
  extent = statements ? extent_of(statements) : nil
79
- Arm.new(kind:, extent:, body: nil)
82
+ leaves = Leaving.of(statements)
83
+ Arm.new(kind:, extent:, body: nil, leaves:)
80
84
  end
81
85
 
82
86
  # An if's else arm, from subsequent: an else's statements,
83
- # or an elsif, which is a site of its own; none for none.
87
+ # or an elsif, which is a decision of its own; none for none.
84
88
  def else_arm(subsequent)
85
89
  return [] if subsequent.nil?
86
90
  if subsequent.type == :else_node
@@ -89,7 +93,7 @@ module PathCoverage
89
93
 
90
94
  scoped { subsequent.accept(self) }
91
95
  extent = extent_of(subsequent)
92
- [Arm.new(kind: :else, extent:, body: nil)]
96
+ [Arm.new(kind: :else, extent:, body: nil, leaves: nil)]
93
97
  end
94
98
 
95
99
  # A ternary's predicate, then its then and else arms.
@@ -101,27 +105,46 @@ module PathCoverage
101
105
  ]
102
106
  end
103
107
 
104
- # One arm of a ternary, statements its code: a condition
105
- # folds in as its body, being part of the one expression, as
106
- # does a plain arm with sites inside it, its value its last
107
- # leaf; a plain arm with none has no body.
108
+ # One arm of a ternary, statements its code: a ternary, or a
109
+ # block method's call whose value follows from its path, is a
110
+ # decision of its own, as an if's arm holding an if is, so the arm
111
+ # is plain; another condition folds in as its body, being part
112
+ # of the one expression, as does a plain arm with decisions inside
113
+ # it, its value its last leaf; a plain arm with none has no
114
+ # body.
108
115
  def arm_of(kind, statements)
109
116
  written = grouped(statements.body.first)
110
117
  extent = extent_of(statements)
111
- body = if foldable?(written)
112
- condition_of(written)
118
+ body = if ternary?(written) || valued?(written)
119
+ own_decision(statements)
113
120
  else
114
- inner = collected { statements.accept(self) }
115
- inner.empty? ? nil : contained(inner, extent)
121
+ arm_body(written, statements, extent)
116
122
  end
117
- Arm.new(kind:, extent:, body:)
123
+ Arm.new(kind:, extent:, body:, leaves: Leaving.of(statements))
118
124
  end
119
125
 
120
- # True for a condition that folds into a ternary's site: a
121
- # ternary, never ignored, as the ternary it folds into is
122
- # not; or an &&, ||, and or or not ignored.
126
+ # No body, for an arm, statements its code, whose one decision is
127
+ # a decision of its own: visited in a new scope, so noted as one.
128
+ def own_decision(statements)
129
+ scoped { statements.accept(self) }
130
+ nil
131
+ end
132
+
133
+ # The body of a ternary's arm, statements its code, written its
134
+ # one expression, extent its byte range: the condition written
135
+ # is, when it folds; else the decisions inside it, contained, nil
136
+ # for none.
137
+ def arm_body(written, statements, extent)
138
+ return condition_of(written) if foldable?(written)
139
+
140
+ inner = collected { statements.accept(self) }
141
+ inner.empty? ? nil : contained(inner, extent)
142
+ end
143
+
144
+ # True for a condition that folds into a ternary's decision: an
145
+ # &&, ||, and or or not ignored.
123
146
  def foldable?(node)
124
- ternary?(node) || (operator?(node) && !ignored?(node))
147
+ operator?(node) && !ignored?(node)
125
148
  end
126
149
 
127
150
  # True for a ternary, an if with no if keyword.
@@ -129,13 +152,13 @@ module PathCoverage
129
152
  node.type == :if_node && node.if_keyword_loc.nil?
130
153
  end
131
154
 
132
- # A ternary's or an if's predicate as its operand: an && or
133
- # || nested as a Condition, as is a &. chain, its value its
134
- # last leaf, anything else a leaf.
155
+ # A ternary's or an if's predicate as its operand: a &. chain
156
+ # nested as a Condition, its value its last leaf; anything
157
+ # else a leaf, an && or || among them, as a decision of its own,
158
+ # as leaf_decisions has it.
135
159
  def predicate_of(node)
136
160
  predicate = grouped(node.predicate)
137
161
  return leaf(predicate) if ignored?(predicate)
138
- return condition_of(predicate) if operator?(predicate)
139
162
  if safe_chain?(predicate)
140
163
  return chain_condition(predicate, value: true)
141
164
  end