path_coverage 0.1.0 → 0.3.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 (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +315 -107
  3. data/lib/path_coverage/calls.rb +55 -23
  4. data/lib/path_coverage/card.rb +14 -0
  5. data/lib/path_coverage/condition.rb +33 -3
  6. data/lib/path_coverage/configuration.rb +43 -22
  7. data/lib/path_coverage/configuration_paths.rb +58 -0
  8. data/lib/path_coverage/console_summary.rb +75 -0
  9. data/lib/path_coverage/gaps.rb +32 -13
  10. data/lib/path_coverage/html/file.html +5 -5
  11. data/lib/path_coverage/html/index.html +6 -18
  12. data/lib/path_coverage/html/settings.js +50 -0
  13. data/lib/path_coverage/html/side.js +341 -413
  14. data/lib/path_coverage/html/sort.js +24 -21
  15. data/lib/path_coverage/html/style.css +339 -207
  16. data/lib/path_coverage/html/tabs.js +15 -0
  17. data/lib/path_coverage/html/theme.js +27 -0
  18. data/lib/path_coverage/html_branch.rb +30 -0
  19. data/lib/path_coverage/html_cards.rb +111 -0
  20. data/lib/path_coverage/html_cells.rb +66 -0
  21. data/lib/path_coverage/html_file.rb +82 -68
  22. data/lib/path_coverage/html_index.rb +95 -41
  23. data/lib/path_coverage/html_index_head.rb +29 -0
  24. data/lib/path_coverage/html_labels.rb +23 -0
  25. data/lib/path_coverage/html_methods.rb +66 -0
  26. data/lib/path_coverage/html_nav.rb +50 -50
  27. data/lib/path_coverage/html_report.rb +69 -56
  28. data/lib/path_coverage/html_statements.rb +166 -0
  29. data/lib/path_coverage/html_syntax.rb +156 -0
  30. data/lib/path_coverage/html_tabs.rb +26 -0
  31. data/lib/path_coverage/html_template.rb +6 -0
  32. data/lib/path_coverage/html_tree.rb +59 -74
  33. data/lib/path_coverage/json_reader.rb +101 -0
  34. data/lib/path_coverage/json_writer.rb +52 -7
  35. data/lib/path_coverage/kernel.rb +55 -53
  36. data/lib/path_coverage/kinds/base.rb +71 -0
  37. data/lib/path_coverage/kinds/block.rb +137 -0
  38. data/lib/path_coverage/kinds/case.rb +92 -0
  39. data/lib/path_coverage/kinds/chain.rb +45 -0
  40. data/lib/path_coverage/kinds/choice.rb +68 -0
  41. data/lib/path_coverage/kinds/contains.rb +113 -0
  42. data/lib/path_coverage/kinds/safe.rb +153 -0
  43. data/lib/path_coverage/kinds/write.rb +54 -0
  44. data/lib/path_coverage/kinds.rb +37 -0
  45. data/lib/path_coverage/manifest.rb +60 -20
  46. data/lib/path_coverage/manifest_blocks.rb +48 -10
  47. data/lib/path_coverage/manifest_case.rb +2 -3
  48. data/lib/path_coverage/manifest_chains.rb +76 -0
  49. data/lib/path_coverage/manifest_choices.rb +32 -18
  50. data/lib/path_coverage/manifest_folding.rb +91 -0
  51. data/lib/path_coverage/manifest_ignoring.rb +35 -0
  52. data/lib/path_coverage/manifest_writes.rb +67 -0
  53. data/lib/path_coverage/merge.rb +19 -3
  54. data/lib/path_coverage/method_statements.rb +300 -0
  55. data/lib/path_coverage/post.rb +8 -8
  56. data/lib/path_coverage/recorder.rb +20 -79
  57. data/lib/path_coverage/recorder_blocks.rb +68 -0
  58. data/lib/path_coverage/recorder_cases.rb +23 -0
  59. data/lib/path_coverage/recorder_chains.rb +40 -0
  60. data/lib/path_coverage/recorder_writes.rb +26 -0
  61. data/lib/path_coverage/render.rb +56 -47
  62. data/lib/path_coverage/result.rb +19 -0
  63. data/lib/path_coverage/rewriter.rb +69 -107
  64. data/lib/path_coverage/rewriter_nested.rb +62 -0
  65. data/lib/path_coverage/site_paths.rb +9 -58
  66. data/lib/path_coverage/site_tree.rb +21 -57
  67. data/lib/path_coverage/statement_arms.rb +161 -0
  68. data/lib/path_coverage/summary.rb +41 -17
  69. data/lib/path_coverage/totals.rb +6 -12
  70. data/lib/path_coverage/version.rb +1 -1
  71. data/lib/path_coverage/within_methods.rb +92 -0
  72. metadata +40 -11
  73. data/lib/path_coverage/html/toggle.js +0 -12
  74. data/lib/path_coverage/html_paths.rb +0 -138
  75. data/lib/path_coverage/path_gaps.rb +0 -41
  76. data/lib/path_coverage/path_manifest.rb +0 -110
@@ -0,0 +1,113 @@
1
+ require_relative 'base'
2
+ require_relative 'block'
3
+
4
+ module PathCoverage
5
+ module Kinds
6
+ # A leaf with sites inside it, eg ids.select { |i| i.odd? }.any?
7
+ # as an operand of an &&: its operands are those sites'
8
+ # Conditions, which run as it does, then the leaf's own byte
9
+ # range, whose outcome is its value.
10
+ module Contains
11
+ extend Base
12
+
13
+ OPERATORS = [CONTAINS].freeze
14
+
15
+ # The ways node can be evaluated: each way of every one of
16
+ # the sites inside it, then each way of the leaf itself, its
17
+ # value the whole's.
18
+ def self.ways(node, fixed)
19
+ inner = node.operands[0...-1]
20
+ before = inner.reduce([{}]) do |ways, site|
21
+ ways.product(SitePaths.walk(site, fixed))
22
+ .map { |outcomes, (more, _)| outcomes.merge(more) }
23
+ end
24
+ before.flat_map do |outcomes|
25
+ leaf_ways(node, outcomes, fixed).map do |more, value|
26
+ [outcomes.merge(more), value]
27
+ end
28
+ end
29
+ end
30
+
31
+ # The ways the leaf of node can be evaluated after the sites
32
+ # inside it took outcomes: the one outcome it must have then,
33
+ # when known, else true or false.
34
+ def self.leaf_ways(node, outcomes, fixed)
35
+ range = node.operands.last
36
+ known = known_outcome(node, outcomes, fixed)
37
+ return SitePaths.leaf(range, fixed) if known.nil?
38
+
39
+ [[{ range => known }, known]]
40
+ end
41
+
42
+ # The outcome the leaf of node must have after the sites
43
+ # inside it took outcomes, nil when it can have either: for
44
+ # a call treated as nonempty, eg any?, whether its receiver
45
+ # has items; for a leaf that is wholly one site, eg a find,
46
+ # that site's value. Each only when the site's kind can
47
+ # tell. A nonempty call has no arguments, so its receiver is
48
+ # the largest of those sites, any other lying within it.
49
+ def self.known_outcome(node, outcomes, fixed)
50
+ *inner, range = node.operands
51
+ if node.fixed[range] == :nonempty
52
+ receiver = inner.max_by { |site| site.extent.size }
53
+ return Block.items?(receiver, outcomes)
54
+ end
55
+ whole = inner.to_h { |site| [site.extent, site] }[range]
56
+ whole && value_on(whole, outcomes, fixed)
57
+ end
58
+
59
+ # site's value on its way that took outcomes, one of its
60
+ # own, nil when its kind cannot tell. outcomes always hold
61
+ # one of site's ways, so one is always found.
62
+ def self.value_on(site, outcomes, fixed)
63
+ SitePaths.walk(site, fixed).each do |more, value|
64
+ return value if more <= outcomes
65
+ end
66
+ end
67
+
68
+ # Its value in each column: the leaf's own, its last operand.
69
+ def self.value(_operator, operands)
70
+ operands.last[:cells]
71
+ end
72
+
73
+ # nodes, labelled and texted so their rows read as the
74
+ # leaf's code does.
75
+ # Each site inside the leaf is labelled with the leaf's text
76
+ # before it, its operator, and its own opening, eg select {
77
+ # for ids.select { |id| id.odd? }.any?, or ( || for
78
+ # (a || b).any?.
79
+ # The leaf is texted with the last site's own closing, then
80
+ # the leaf's text after that site, eg }.any? or ).any?.
81
+ def self.texted(source, condition, nodes)
82
+ *inner, range = condition.operands
83
+ starts = [range.begin, *inner.map { |site| site.extent.end }]
84
+ labelled = inner.zip(starts, nodes).map do |site, at, node|
85
+ node.merge(label: label(source, site, at))
86
+ end
87
+ text = tail(source, inner.last, range)
88
+ [*labelled, nodes.last.merge(text:)]
89
+ end
90
+
91
+ # site's label inside a leaf: the leaf's text from byte at
92
+ # up to site, then site's operator and its own opening.
93
+ def self.label(source, site, at)
94
+ before = source.byteslice(at...site.extent.begin).strip
95
+ opening, = Kinds.of(site.operator).brackets(source, site)
96
+ [before, site.operator, opening].reject(&:empty?).join(' ')
97
+ end
98
+
99
+ # The text of the leaf at range: the closing of last, the
100
+ # last site inside the leaf, then the leaf's text after it.
101
+ def self.tail(source, last, range)
102
+ _, closing = Kinds.of(last.operator).brackets(source, last)
103
+ closing + source.byteslice(last.extent.end...range.end)
104
+ end
105
+
106
+ # The sites inside it, then the leaf, as siblings, so each
107
+ # site's rows are drawn once, eg select { then }.any?.
108
+ def self.unfold(node)
109
+ node[:operands]
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,153 @@
1
+ require_relative 'base'
2
+
3
+ module PathCoverage
4
+ module Kinds
5
+ # A &. chain, eg user&.account&.owner, each receiver nil or
6
+ # not, a nil one skipping the rest of the chain. As a site of
7
+ # its own its operands are its receivers; nested in another,
8
+ # they are its receivers then its value, its last leaf. Each
9
+ # receiver is wrapped in a present call, which passes it on
10
+ # unchanged, so it is still evaluated just once.
11
+ module Safe
12
+ extend Base
13
+
14
+ OPERATORS = [SAFE].freeze
15
+
16
+ # The ways node, nested in a condition, can be evaluated,
17
+ # its value its last leaf.
18
+ def self.ways(node, fixed)
19
+ chain(node.operands[0...-1], node.operands.last, fixed)
20
+ end
21
+
22
+ # The ways a &. chain site can be evaluated, every operand
23
+ # a receiver.
24
+ def self.root_ways(node, fixed)
25
+ chain(node.operands, nil, fixed)
26
+ end
27
+
28
+ # The ways a &. chain can be evaluated: each receiver not
29
+ # nil going on to the next, a nil one ending the chain, its
30
+ # value false; then, given a last leaf, the chain's value
31
+ # as an operand of a condition, the ways it can be.
32
+ def self.chain(receivers, last, fixed)
33
+ first, *rest = receivers
34
+ on = if !rest.empty? then chain(rest, last, fixed)
35
+ elsif last then SitePaths.walk(last, fixed)
36
+ else [[{}, true]]
37
+ end
38
+ present = on.map do |outcomes, value|
39
+ [outcomes.merge(first => PRESENCE[0]), value]
40
+ end
41
+ return present if fixed[first] == :present
42
+
43
+ [*present, [{ first => PRESENCE[1] }, false]]
44
+ end
45
+
46
+ # Its value in each column: false where a receiver was nil,
47
+ # as the chain then gives nil, else its last leaf's. Only the
48
+ # chain's end, after its last leaf, can be a node only
49
+ # called, so it alone is left out.
50
+ def self.value(_operator, operands)
51
+ leaves = operands.last[:called] ? operands[0...-1] : operands
52
+ columns = leaves.map { |operand| operand[:cells] }.transpose
53
+ columns.map do |column|
54
+ column.include?(PRESENCE[1]) ? false : column.last
55
+ end
56
+ end
57
+
58
+ # The inserts for a &. chain site: each receiver in a present
59
+ # call, and the whole chain in a leave call that enters it
60
+ # first. Eg user&.account becomes, in effect,
61
+ # leave(enter(site, 1), present(site, 0, 0, user)&.account).
62
+ def self.site_inserts(site, condition)
63
+ leaves = condition.leaves
64
+ enter = "PathCoverage.enter(#{site.inspect}, #{leaves.size})"
65
+ call = "PathCoverage.leave(#{enter}, "
66
+ command = condition.commands.include?(condition.extent)
67
+ presents = leaves.each_with_index.flat_map do |range, index|
68
+ Rewriter.wrap(present(site, index, 0), range, false, :leaf)
69
+ end
70
+ extent = condition.extent
71
+ presents + Rewriter.wrap(call, extent, command, :site)
72
+ end
73
+
74
+ # Its leaves need present and chained calls.
75
+ def self.own_calls?
76
+ true
77
+ end
78
+
79
+ # { leaf range => the opening of its call } for the leaves of
80
+ # chain nested in a site whose leaves are leaves: a
81
+ # receiver's present call, and the chain's value's chained
82
+ # call, each told where its chain's leaves start.
83
+ def self.calls(site, chain, leaves)
84
+ first = leaves.index(chain.operands.first)
85
+ *receivers, value = chain.operands
86
+ calls = receivers.to_h do |range|
87
+ [range, present(site, leaves.index(range), first)]
88
+ end
89
+ at = leaves.index(value)
90
+ calls.merge(value => chained(site, at, first))
91
+ end
92
+
93
+ # The opening of the chained call around the index-th leaf
94
+ # of site, a chain's value, its chain's leaves starting at
95
+ # first.
96
+ def self.chained(site, index, first)
97
+ "PathCoverage.chained(#{site.inspect}, #{index}, #{first}, "
98
+ end
99
+
100
+ # The opening of the present call around the index-th leaf
101
+ # of site, its chain's leaves starting at first.
102
+ def self.present(site, index, first)
103
+ "PathCoverage.present(#{site.inspect}, #{index}, #{first}, "
104
+ end
105
+
106
+ # Its leaves' nodes, each after the first showing only what
107
+ # it adds to the one before, eg account after user.
108
+ # A leading &. is left out, as the chain's own row shows it.
109
+ # Any of the chain after its last leaf follows, eg owner in
110
+ # user&.account&.owner, as a node of its own.
111
+ def self.texted(source, condition, nodes)
112
+ ranges = condition.operands
113
+ texted = with_added(source, ranges, nodes)
114
+ rest = shown(source, ranges.last.end...condition.extent.end)
115
+ rest.empty? ? texted : [*texted, called(rest, texted.last)]
116
+ end
117
+
118
+ # nodes, the nodes of a chain's leaves at ranges, each after
119
+ # the first showing only what it adds to the one before.
120
+ def self.with_added(source, ranges, nodes)
121
+ nodes.each_with_index.map do |node, index|
122
+ next node if index.zero?
123
+
124
+ added = ranges[index - 1].end...ranges[index].end
125
+ node.merge(text: shown(source, added))
126
+ end
127
+ end
128
+
129
+ # The text of source at range, less any leading &.
130
+ def self.shown(source, range)
131
+ source.byteslice(range).delete_prefix('&.')
132
+ end
133
+
134
+ # The node of text, the end of a chain after its last
135
+ # leaf, eg owner: no leaf, as it decides no path, but marked
136
+ # called on each path where last, the last leaf, was not
137
+ # nil.
138
+ def self.called(text, last)
139
+ cells = last[:cells].map { |cell| cell == PRESENCE[0] }
140
+ { text:, called: true, cells: }
141
+ end
142
+
143
+ # The rows of its leaves, each under the one before, as each
144
+ # is reached only when the one before is not nil.
145
+ def self.rows(chain, indent)
146
+ chain[:operands].each_with_index.map do |operand, depth|
147
+ branch = "#{indent}#{HtmlBranch::GAP * depth}#{HtmlBranch::ELL}"
148
+ HtmlTree.operand_row(operand, branch)
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,54 @@
1
+ require_relative 'base'
2
+ require_relative 'choice'
3
+
4
+ module PathCoverage
5
+ module Kinds
6
+ # A ||= or &&= write, which decides whether its right side
7
+ # runs as an unless or an if decides its arm: its target is
8
+ # its predicate, its right side its then arm, so its ways and
9
+ # value are a choice's. Its target is never wrapped, so never
10
+ # read again.
11
+ module Write
12
+ extend Base
13
+ extend Arms
14
+
15
+ OPERATORS = WRITES
16
+
17
+ # The name of its arm, of a write named choice: the operator
18
+ # and its right side, eg ||= key.upcase.
19
+ def self.arm_label(arm, choice)
20
+ "#{choice} #{arm[:text]}"
21
+ end
22
+
23
+ # Its target needs no record call, and its right side a
24
+ # written_at call.
25
+ def self.own_calls?
26
+ true
27
+ end
28
+
29
+ # { target range => nil }: its target is never read, so
30
+ # never wrapped.
31
+ def self.calls(_site, inner, _leaves)
32
+ { inner.operands.first => nil }
33
+ end
34
+
35
+ # A write, its target the site's at-th leaf: its right side
36
+ # in a written_at call, which notes the target's outcome when
37
+ # it runs, and the whole of it in a settle call, which notes
38
+ # the other when it does not. A right side among commands,
39
+ # eg a command call, is parenthesised. Eg x ||= v becomes,
40
+ # in effect, settle(site, at, true, x ||= written_at(site,
41
+ # at, false, v)).
42
+ def self.inserts(site, inner, at, commands)
43
+ _target, arm = inner.operands
44
+ ran = THEN_ON[inner.operator]
45
+ name = site.inspect
46
+ written = "PathCoverage.written_at(#{name}, #{at}, #{ran}, "
47
+ settle = "PathCoverage.settle(#{name}, #{at}, #{!ran}, "
48
+ command = commands.include?(arm.extent)
49
+ Rewriter.wrap(settle, inner.extent, false, :kind) +
50
+ Rewriter.wrap(written, arm.extent, command, :leaf)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,37 @@
1
+ require_relative 'condition'
2
+ require_relative 'kinds/block'
3
+ require_relative 'kinds/case'
4
+ require_relative 'kinds/chain'
5
+ require_relative 'kinds/choice'
6
+ require_relative 'kinds/contains'
7
+ require_relative 'kinds/safe'
8
+ require_relative 'kinds/write'
9
+
10
+ module PathCoverage
11
+ # The kinds of condition, each a module that knows, for a
12
+ # condition named by one of its OPERATORS, the ways it can be
13
+ # evaluated, its value in each column of its site's tree, and
14
+ # the inserts that instrument it. Each extends Kinds::Base,
15
+ # whose hooks give what most kinds need.
16
+ module Kinds
17
+ # Every kind, each named by its operators.
18
+ ALL = [Block, Case, Chain, Choice, Contains, Safe, Write].freeze
19
+
20
+ # { operator => kind } for every operator of every kind.
21
+ BY_OPERATOR = ALL.flat_map do |kind|
22
+ kind::OPERATORS.map { |operator| [operator, kind] }
23
+ end.to_h.freeze
24
+
25
+ # The kind of a condition named operator, eg Chain for &&.
26
+ def self.of(operator)
27
+ BY_OPERATOR.fetch(operator)
28
+ end
29
+
30
+ # The names of the kinds measured, eg chain for Chain: a
31
+ # report's fingerprint, as runs measuring different kinds
32
+ # list a site's paths differently.
33
+ def self.names
34
+ ALL.map { |kind| kind.name.split('::').last.downcase }
35
+ end
36
+ end
37
+ end
@@ -2,7 +2,11 @@ require 'prism'
2
2
  require_relative 'condition'
3
3
  require_relative 'manifest_blocks'
4
4
  require_relative 'manifest_case'
5
+ require_relative 'manifest_chains'
5
6
  require_relative 'manifest_choices'
7
+ require_relative 'manifest_folding'
8
+ require_relative 'manifest_ignoring'
9
+ require_relative 'manifest_writes'
6
10
  require_relative 'site'
7
11
 
8
12
  module PathCoverage
@@ -10,9 +14,15 @@ module PathCoverage
10
14
  # outermost &&, ||, and or or, each ternary, if, unless and case
11
15
  # with a subject, with the conditions nested in it.
12
16
  module Manifest
13
- # { site => Condition } for each site in source.
14
- def self.sites(source, path)
15
- collector = ConditionSites.new(path)
17
+ # { site => Condition } for each site in source, none of
18
+ # them named by an operator in ignored; a call to a method
19
+ # named in present, as a &.'s receiver, is never nil, and a
20
+ # 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 = [])
24
+ collector = ConditionSites.new(path, ignored,
25
+ [present, nonempty])
16
26
  Prism.parse(source).value.accept(collector)
17
27
  collector.sites
18
28
  end
@@ -20,7 +30,10 @@ module PathCoverage
20
30
  # The source and sites of the covered file at full_path.
21
31
  def self.parse(full_path, config)
22
32
  source = File.read(full_path)
23
- { source:, sites: sites(source, config.relative(full_path)) }
33
+ path = config.relative(full_path)
34
+ treated = %w[present nonempty].map { config.treated(_1, path) }
35
+ ignored = config.ignored
36
+ { source:, sites: sites(source, path, ignored, *treated) }
24
37
  end
25
38
 
26
39
  # True for a call to a named method with arguments and no
@@ -73,13 +86,24 @@ module PathCoverage
73
86
  class ConditionSites < Prism::Visitor
74
87
  include ManifestBlocks
75
88
  include ManifestCase
89
+ include ManifestChains
76
90
  include ManifestChoices
77
-
78
- # Starts with no sites found.
79
- def initialize(path)
91
+ include ManifestFolding
92
+ include ManifestIgnoring
93
+ include ManifestWrites
94
+
95
+ # Starts with no sites found, to find none named by an
96
+ # operator in ignored, and to take a call to a method named
97
+ # in present as never nil, and to one in nonempty as true
98
+ # exactly when its receiver has items.
99
+ def initialize(path, ignored, (present, nonempty))
80
100
  @path = path
101
+ @ignored = ignored
102
+ @present = present
103
+ @nonempty = nonempty
81
104
  @sites = {}
82
105
  @notes = nil
106
+ @inner = nil
83
107
  super()
84
108
  end
85
109
 
@@ -118,10 +142,11 @@ module PathCoverage
118
142
  add_site(node)
119
143
  end
120
144
 
121
- # Notes a block method's call as a site, when its block's
122
- # every call can be seen; any other call is no site, though
123
- # a condition inside it may be.
145
+ # Notes a &. chain as a site, or a block method's call, when
146
+ # its block's every call can be seen; any other call is no
147
+ # site, though a condition inside it may be.
124
148
  def visit_call_node(node)
149
+ return add_site(node) if safe_chain?(node)
125
150
  return super unless block_site?(node)
126
151
 
127
152
  add_site(node)
@@ -130,12 +155,17 @@ module PathCoverage
130
155
  # Notes node as a site, named where it starts: an if or an
131
156
  # unless at its keyword. It claims its place first, so a
132
157
  # site found inside one of its leaves or arms follows it.
158
+ # While a leaf is being visited, any site is collected as
159
+ # part of that leaf instead, as it is part of one expression.
133
160
  def add_site(node)
161
+ return visit_child_nodes(node) if ignored?(node)
162
+ return @inner << nested_condition_of(node) if @inner
163
+
134
164
  site = Site.at(@path, site_loc(node))
135
165
  @sites[site] = nil
136
166
  outer = @notes
137
167
  @notes = { commands: [], fixed: {} }
138
- condition = condition_of(node)
168
+ condition = scoped { condition_of(node) }
139
169
  @sites[site] = condition.with(**@notes)
140
170
  @notes = outer
141
171
  end
@@ -143,11 +173,14 @@ module PathCoverage
143
173
  # node as a Condition, operands and all, named by its
144
174
  # operator as written; a ternary is named ?:, an if if and
145
175
  # an unless unless, their operands a predicate and arms; a
146
- # case case, its operands its arms.
176
+ # case case, its operands its arms; a write ||= or &&=, its
177
+ # operands its target and its right side; a &. chain &., its
178
+ # operands its receivers.
147
179
  def condition_of(node)
180
+ return write_condition(node) if write?(node)
148
181
  return choice_of(node) if CHOICE_TYPES.include?(node.type)
149
182
  return case_condition(node) if node.type == :case_node
150
- return block_condition(node) if node.type == :call_node
183
+ return call_condition(node) if node.type == :call_node
151
184
 
152
185
  Condition.new(
153
186
  operator: node.operator, operands: operands_of(node),
@@ -157,12 +190,17 @@ module PathCoverage
157
190
 
158
191
  # node's operands: a chain of the same operator flattened
159
192
  # into one list, the other operator nested as a Condition,
160
- # anything else a leaf's byte range.
193
+ # as is a &. chain, its value its last leaf, anything else a
194
+ # leaf's byte range.
161
195
  def operands_of(node)
162
196
  [node.left, node.right].flat_map do |written|
163
197
  operand = grouped(written)
198
+ next [leaf(operand)] if ignored?(operand)
164
199
  next operands_of(operand) if operand.type == node.type
165
200
  next [condition_of(operand)] if operator?(operand)
201
+ if safe_chain?(operand)
202
+ next [chain_condition(operand, value: true)]
203
+ end
166
204
 
167
205
  [leaf(operand)]
168
206
  end
@@ -179,17 +217,18 @@ module PathCoverage
179
217
  OPERATOR_TYPES.include?(node.type)
180
218
  end
181
219
 
182
- # The byte range of a leaf operand, once any site inside
183
- # it is noted too, and the range noted among its site's
184
- # commands and fixed leaves where it is one.
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
222
+ # that fold are found inside it, the leaf with them nested.
185
223
  def leaf(operand)
186
- operand.accept(self)
224
+ inner = collected { operand.accept(self) }
187
225
  loc = operand.location
188
226
  range = loc.start_offset...loc.end_offset
189
227
  @notes[:commands] << range if Manifest.command?(operand)
190
228
  outcome = Manifest.fixed_outcome(operand)
191
229
  @notes[:fixed][range] = outcome if outcome
192
- range
230
+ nonempty = nonempty_call?(operand, inner)
231
+ contained(inner, range, nonempty:)
193
232
  end
194
233
 
195
234
  # The one statement inside a parenthesised operand, or the
@@ -197,7 +236,8 @@ module PathCoverage
197
236
  def grouped(operand)
198
237
  return operand unless operand.type == :parentheses_node
199
238
 
200
- statements = operand.body&.body || []
239
+ body = operand.body
240
+ statements = body ? body.body : []
201
241
  statements.size == 1 ? statements.first : operand
202
242
  end
203
243
  end
@@ -12,13 +12,30 @@ module PathCoverage
12
12
  # True for a call whose block's every call can be seen: to a
13
13
  # block method, on a receiver, with no arguments, which can
14
14
  # make it ignore its block, not by &., which skips it for
15
- # nil, and with a block of one expression.
15
+ # nil, and with a block of one expression that is a value,
16
+ # so can be wrapped: not break, next or return.
16
17
  def block_site?(node)
17
- BLOCK_PATHS.key?(node.name.to_s) && !node.receiver.nil? &&
18
- node.arguments.nil? && !node.safe_navigation? &&
19
- !block_expression(node.block).nil?
18
+ return false unless BLOCK_PATHS.key?(node.name.to_s)
19
+
20
+ plain_call?(node) && value?(block_expression(node.block))
21
+ end
22
+
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.
25
+ def plain_call?(node)
26
+ !node.receiver.nil? && node.arguments.nil?
27
+ end
28
+
29
+ # True for a block's one expression that is a value, so can
30
+ # be wrapped: nil, for no one expression, is not, nor is a
31
+ # jump.
32
+ def value?(body)
33
+ !body.nil? && !JUMPS.include?(body.type)
20
34
  end
21
35
 
36
+ # The expressions that leave a block, so are no value.
37
+ JUMPS = %i[break_node next_node return_node].freeze
38
+
22
39
  # The one expression of block, nil for no block, a block
23
40
  # passed as an argument, or one of other than one expression.
24
41
  def block_expression(block)
@@ -35,23 +52,44 @@ module PathCoverage
35
52
  # among the commands when it is no bare argument.
36
53
  def block_condition(node)
37
54
  node.receiver.accept(self)
38
- body = block_expression(node.block)
39
- body.accept(self)
40
- @notes[:commands] << extent_of(body) if bare_less?(body)
55
+ body = block_body(node.block)
41
56
  Condition.new(
42
57
  operator: node.name.to_s,
43
- operands: [extent_of(node.receiver), extent_of(body)],
58
+ operands: [extent_of(node.receiver), body],
44
59
  extent: extent_of(node)
45
60
  )
46
61
  end
47
62
 
63
+ # The byte range of block's one expression, any site in it
64
+ # visited as one of its own, as the block runs once per item;
65
+ # noted among the commands when it is no bare argument.
66
+ def block_body(block)
67
+ body = block_expression(block)
68
+ scoped { body.accept(self) }
69
+ range = extent_of(body)
70
+ @notes[:commands] << range if bare_less?(body)
71
+ range
72
+ end
73
+
48
74
  # True for an expression that is no bare argument: a command
49
- # call, or an and or an or.
75
+ # call, an and or an or, or a modifier, eg x unless y.
50
76
  def bare_less?(node)
51
- return true if Manifest.command?(node)
77
+ return true if Manifest.command?(node) || modifier?(node)
52
78
 
53
79
  %i[and_node or_node].include?(node.type) &&
54
80
  %w[and or].include?(node.operator)
55
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
56
94
  end
57
95
  end
@@ -13,9 +13,8 @@ module PathCoverage
13
13
  def case_site?(node)
14
14
  return false if node.predicate.nil?
15
15
 
16
- node.conditions.none? do |clause|
17
- clause.conditions.any? { |each| each.type == :splat_node }
18
- end
16
+ patterns = node.conditions.flat_map(&:conditions)
17
+ !patterns.map(&:type).include?(:splat_node)
19
18
  end
20
19
 
21
20
  # A case as a Condition, its extent its case keyword to the