path_coverage 0.4.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 (102) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +286 -199
  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 +79 -66
  11. data/lib/path_coverage/card_way_lists.rb +72 -0
  12. data/lib/path_coverage/card_ways.rb +123 -67
  13. data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +35 -33
  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} +13 -11
  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/{html_tree.rb → fold_rows.rb} +42 -71
  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 +27 -12
  30. data/lib/path_coverage/kernel.rb +56 -58
  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 -12
  34. data/lib/path_coverage/kinds/chain.rb +40 -11
  35. data/lib/path_coverage/kinds/choice.rb +4 -3
  36. data/lib/path_coverage/kinds/contains.rb +40 -32
  37. data/lib/path_coverage/kinds/safe.rb +38 -26
  38. data/lib/path_coverage/kinds/write.rb +6 -6
  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 +92 -58
  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 +23 -20
  48. data/lib/path_coverage/manifest_disabled.rb +97 -0
  49. data/lib/path_coverage/manifest_folding.rb +43 -40
  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 +130 -127
  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 +31 -30
  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 +135 -80
  80. metadata +33 -28
  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 -599
  86. data/lib/path_coverage/html/sort.js +0 -34
  87. data/lib/path_coverage/html/style.css +0 -484
  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 -88
  92. data/lib/path_coverage/html_cells.rb +0 -71
  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 -16
  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 -186
  101. data/lib/path_coverage/html_tabs.rb +0 -26
  102. data/lib/path_coverage/html_template.rb +0 -26
@@ -1,10 +1,7 @@
1
- require 'fileutils'
2
1
  require_relative 'calls'
3
2
  require_relative 'configuration'
4
- require_relative 'console_summary'
5
- require_relative 'json_writer'
6
3
  require_relative 'manifest'
7
- require_relative 'paths_report'
4
+ require_relative 'method_loops'
8
5
  require_relative 'recorder'
9
6
  require_relative 'render'
10
7
  require_relative 'result'
@@ -17,7 +14,7 @@ require_relative 'within_methods'
17
14
  module PathCoverage
18
15
  @recorder = Recorder.new
19
16
  @config = nil
20
- # { full path => {source:, sites:} } of each covered file
17
+ # { full path => {source:, decisions:} } of each covered file
21
18
  # parsed as it loaded, so the exit report need not read it
22
19
  # again.
23
20
  @parsed = {}
@@ -88,71 +85,70 @@ module PathCoverage
88
85
  end
89
86
 
90
87
  # The compiled, rewritten code of path, or nil for a path
91
- # config does not cover, which Ruby then loads as normal.
88
+ # config does not cover, which Ruby then loads as normal. Each
89
+ # call of a method in it is traced, as its way is recorded.
92
90
  def self.instrumented(path, config)
93
91
  return unless config.covers?(path)
94
92
 
95
93
  parsed = @parsed[path] = Manifest.parse(path, config)
96
- source = parsed[:source]
97
- reaches = WithinMethods.reaches(path, parsed, config)
98
- rewritten = Rewriter.rewrite(source, parsed[:sites], reaches)
99
- RubyVM::InstructionSequence.compile(rewritten, path, path)
94
+ args = [parsed[:source], config.relative(path),
95
+ parsed[:decisions]]
96
+ loops = MethodLoops.of(*args)
97
+ rewritten = rewritten(path, parsed, config, loops)
98
+ iseq = RubyVM::InstructionSequence
99
+ traced(iseq.compile(rewritten, path, path),
100
+ [loops, MethodLoops.repeats(*args)])
100
101
  end
101
102
 
102
- # Writes the JSON report, then runs the chosen reporters, for
103
- # every covered file whether or not the run loaded it, into
104
- # coverage_dir, created first if need be.
105
- def self.report(config)
106
- files = parsed_files(config)
107
- result = collect(files, config)
108
- Render.run(result.operands, files, config,
109
- **result.taken, reached: @recorder.reached)
103
+ # The source of parsed, the file at path, rewritten: each of its
104
+ # decisions recorded, each statement reach counted, and each
105
+ # pass of each of loops, { entry => loops }, noted.
106
+ def self.rewritten(path, parsed, config, loops)
107
+ source = parsed[:source]
108
+ reaches = WithinMethods.reaches(path, parsed, config)
109
+ passes = MethodLoops.passes(source, config.relative(path), loops)
110
+ Rewriter.rewrite(source, parsed[:decisions], reaches, passes)
110
111
  end
111
112
 
112
- # The Result of every covered file, with the counts and site
113
- # paths this run recorded, once its operands and paths are
114
- # written as the JSON report.
115
- def self.collect(files, config)
116
- sites = merged_sites(files)
117
- result = Result.new(sites, @recorder.counts, @recorder.site_paths)
118
- FileUtils.mkdir_p(config.output_dir)
119
- write_json(result, files, config)
120
- result
113
+ # compiled, after noting each of its methods' loops and repeats,
114
+ # [{ entry => loops }, { entry => repeats }], as MethodLoops
115
+ # gives them, on the recorder's trace, and tracing each call of
116
+ # them, and only of them, on the trace of whichever start is
117
+ # current as it runs. Code with no method has no call to
118
+ # trace, and TracePoint refuses to trace it.
119
+ def self.traced(compiled, (loops, repeats))
120
+ return compiled if loops.empty?
121
+
122
+ loops.each do |entry, held|
123
+ @recorder.trace.know(entry, held, repeats[entry])
124
+ end
125
+ hook = method(:on_call_event)
126
+ TracePoint.new(:call, :return, &hook).enable(target: compiled)
127
+ compiled
121
128
  end
122
129
 
123
- # Writes result as the JSON report at config's json_path: its
124
- # operands, site paths, paths a treatment rules out, totals,
125
- # each def's card, how each card's rows nest, the statements
126
- # the recorder reached, so a render from the JSON alone has
127
- # them, and config's fingerprint.
128
- def self.write_json(result, files, config)
129
- report = PathsReport.of(result.paths, files)
130
- reached = @recorder.reached
131
- cards = WithinMethods.listed(files, config, result, reached)
132
- tree = WithinMethods.tree(files, config, result)
133
- parts = {
134
- totals: totals(result, files, config), outside: result.outside,
135
- cards:, tree:, fingerprint: config.fingerprint, reached:
136
- }
137
- path = config.json_path
138
- JsonWriter.write(result.operands, path, report, **parts)
130
+ # Notes point, a TracePoint's :call or :return of a covered
131
+ # method, on the current start's trace.
132
+ def self.on_call_event(point)
133
+ trace = @recorder.trace
134
+ point.event == :call ? trace.call : trace.return
139
135
  end
140
136
 
141
- # The JSON report's totals, each [taken, possible] paths:
142
- # within each decision, and within each method; with groups
143
- # configured, each group's own too, under groups.
144
- def self.totals(result, files, config)
137
+ # Writes the JSON reports, then runs the chosen reporters, for
138
+ # every covered file whether or not the run loaded it, from
139
+ # the counts and decision paths this run recorded.
140
+ def self.report(config)
141
+ files = parsed_files(config)
142
+ decisions = merged_decisions(files)
143
+ result = Result.new(decisions, @recorder.counts,
144
+ @recorder.decision_paths)
145
145
  reached = @recorder.reached
146
- cards = WithinMethods.cards(files, config, result.paths, reached)
147
- totals = ConsoleSummary.totals(result.paths, cards)
148
- groups = config.groups
149
- return totals if groups.empty?
150
-
151
- own = ConsoleSummary.group_totals(result.paths, cards, groups)
152
- totals.merge('groups' => own)
146
+ ways = @recorder.trace.ways
147
+ Render.run(result.operands, files, config,
148
+ **result.taken, reached:, ways:)
153
149
  end
154
150
 
155
- # { full path => {source:, sites:} } for every covered file:
151
+ # { full path => {source:, decisions:} } for every covered file:
156
152
  # as parsed when it loaded, or parsed now for a file the run
157
153
  # never loaded.
158
154
  def self.parsed_files(config)
@@ -161,8 +157,10 @@ module PathCoverage
161
157
  end
162
158
  end
163
159
 
164
- # The sites of every covered file, in one manifest.
165
- def self.merged_sites(files)
166
- files.values.map { |parsed| parsed[:sites] }.reduce({}, :merge)
160
+ # The decisions of every covered file, in one manifest.
161
+ def self.merged_decisions(files)
162
+ files.values.map do |parsed|
163
+ parsed[:decisions]
164
+ end.reduce({}, :merge)
167
165
  end
168
166
  end
@@ -3,48 +3,48 @@ module PathCoverage
3
3
  # The hooks every kind has, each as most kinds need it; a
4
4
  # kind extends Base and defines its own in place of any.
5
5
  module Base
6
- # The ways a site of this kind as a whole can be evaluated:
6
+ # The ways a decision of this kind as a whole can be evaluated:
7
7
  # as it can be nested in another.
8
8
  def root_ways(node, fixed)
9
9
  ways(node, fixed)
10
10
  end
11
11
 
12
- # Whether its leaves, nested in a site, need calls of their
12
+ # Whether its leaves, nested in a decision, need calls of their
13
13
  # own in place of record calls; false for most.
14
14
  def own_calls?
15
15
  false
16
16
  end
17
17
 
18
- # The inserts for a site of this kind: its leaves' records,
18
+ # The inserts for a decision of this kind: its leaves' records,
19
19
  # and its leave around them.
20
- def site_inserts(site, condition)
21
- Rewriter.record_inserts(site, condition) +
22
- Rewriter.trail_inserts(site, condition)
20
+ def decision_inserts(decision, condition)
21
+ Rewriter.record_inserts(decision, condition) +
22
+ Rewriter.trail_inserts(decision, condition)
23
23
  end
24
24
 
25
- # What a site of this kind's leave call wraps: the whole of
25
+ # What a decision of this kind's leave call wraps: the whole of
26
26
  # it, for most.
27
27
  def trailed(condition)
28
28
  condition
29
29
  end
30
30
 
31
- # Its node in a site's tree, each leaf taking the next index
31
+ # Its node in a decision's tree, each leaf taking the next index
32
32
  # of leaves: its operands' nodes, then its arms', if any.
33
33
  def node(source, condition, outcomes, leaves)
34
34
  built = [outcomes, leaves]
35
- operands = SiteTree.operands_of(source, condition, *built)
36
- SiteTree.arms(source, condition, operands, *built)
35
+ operands = DecisionTree.operands_of(source, condition, *built)
36
+ DecisionTree.arms(source, condition, operands, *built)
37
37
  end
38
38
 
39
39
  # The nodes its node stands for among its siblings' in a
40
- # site's tree on a file's page: itself, for most.
40
+ # decision's tree on a file's page: itself, for most.
41
41
  def unfold(node)
42
42
  [node]
43
43
  end
44
44
 
45
45
  # The inserts, besides its leaves' calls, for one nested in
46
- # a site at its at-th leaf: none for most.
47
- def inserts(_site, _inner, _at, _commands)
46
+ # a decision at its at-th leaf: none for most.
47
+ def inserts(_decision, _inner, _at, _commands)
48
48
  []
49
49
  end
50
50
 
@@ -54,14 +54,14 @@ module PathCoverage
54
54
  nodes
55
55
  end
56
56
 
57
- # The text a site of this kind opens and closes with, as
57
+ # The text a decision of this kind opens and closes with, as
58
58
  # [opening, closing], eg ['{', '}'] for a block's braces.
59
59
  # Most kinds have none, so ['', ''].
60
- def brackets(_source, _site)
60
+ def brackets(_source, _decision)
61
61
  ['', '']
62
62
  end
63
63
 
64
- # Its operands' rows in a site's tree on a file's page, nil
64
+ # Its operands' rows in a decision's tree on a file's page, nil
65
65
  # for most, whose operands branch from its operator.
66
66
  def rows(_condition, _indent)
67
67
  nil
@@ -58,12 +58,12 @@ module PathCoverage
58
58
  end
59
59
  }.freeze
60
60
 
61
- # Whether site's result has items on the path that took
62
- # outcomes; nil when its kind cannot tell, eg for a site
61
+ # Whether decision's result has items on the path that took
62
+ # outcomes; nil when its kind cannot tell, eg for a decision
63
63
  # that is no block method's call.
64
- def self.items?(site, outcomes)
65
- receiver, body = site.operands
66
- rule = HAS_ITEMS[site.operator]
64
+ def self.items?(decision, outcomes)
65
+ receiver, body = decision.operands
66
+ rule = HAS_ITEMS[decision.operator]
67
67
  rule&.call(outcomes[receiver], outcomes[body])
68
68
  end
69
69
 
@@ -97,10 +97,10 @@ module PathCoverage
97
97
  # Its block's opening and closing in source, ['{', '}'] or
98
98
  # ['do', 'end']: the opening between its receiver and its
99
99
  # block's expression, the closing after that expression.
100
- def self.brackets(source, site)
101
- receiver, body = site.operands
100
+ def self.brackets(source, decision)
101
+ receiver, body = decision.operands
102
102
  between = source.byteslice(receiver.end...body.begin)
103
- closing = source.byteslice(body.end...site.extent.end)
103
+ closing = source.byteslice(body.end...decision.extent.end)
104
104
  [between[/\{|\bdo\b/], closing.strip]
105
105
  end
106
106
 
@@ -115,19 +115,20 @@ module PathCoverage
115
115
  # item_at call, noting its last answer, or for a method that
116
116
  # calls it with every item an answer_at call, noting every
117
117
  # answer, for its block's expression.
118
- def self.calls(site, inner, leaves)
118
+ def self.calls(decision, inner, leaves)
119
119
  receiver, body = inner.operands
120
120
  at = leaves.index(receiver)
121
121
  every = EVERY_ITEM.include?(inner.operator)
122
122
  noter = every ? 'answer_at' : 'item_at'
123
- { receiver => nil,
124
- body => "PathCoverage.#{noter}(#{site.inspect}, #{at}, " }
123
+ opening = "PathCoverage.#{noter}(#{decision.inspect}, #{at}, "
124
+ { receiver => nil, body => opening }
125
125
  end
126
126
 
127
- # Its call wrapped whole, its receiver the site's at-th leaf,
128
- # so its items are begun before it runs and counted after.
129
- def self.inserts(site, inner, at, _commands)
130
- name = site.inspect
127
+ # Its call wrapped whole, its receiver the decision's at-th
128
+ # leaf, so its items are begun before it runs and counted
129
+ # after.
130
+ def self.inserts(decision, inner, at, _commands)
131
+ name = decision.inspect
131
132
  begun = "PathCoverage.begin_items(#{name}, #{at})"
132
133
  call = "PathCoverage.end_items(#{name}, #{at}, #{begun}, "
133
134
  Rewriter.wrap(call, inner.extent, false, :kind)
@@ -19,20 +19,20 @@ module PathCoverage
19
19
  Chain.ways(node.with(operands: whens), fixed)
20
20
  end
21
21
 
22
- # The inserts that wrap each pattern of a case site in a
22
+ # The inserts that wrap each pattern of a case decision in a
23
23
  # when_match call, its stand-in, which enters and leaves the
24
- # site itself, as the case tries its patterns one by one;
24
+ # decision itself, as the case tries its patterns one by one;
25
25
  # its arms may return or raise, so no leave wraps it.
26
- def self.site_inserts(site, condition)
26
+ def self.decision_inserts(decision, condition)
27
27
  size = condition.leaves.size
28
28
  condition.leaves.each_with_index.flat_map do |range, index|
29
- call = "PathCoverage.when_match(#{site.inspect}, " \
29
+ call = "PathCoverage.when_match(#{decision.inspect}, " \
30
30
  "#{index}, #{size}, "
31
31
  Rewriter.wrap(call, range, false, :leaf)
32
32
  end
33
33
  end
34
34
 
35
- # Its node in a site's tree from its arms, built in order so
35
+ # Its node in a decision's tree from its arms, built in order so
36
36
  # its whens' patterns pair with path entries left to right:
37
37
  # a when is taken on each path one of its patterns matched,
38
38
  # and the else on each none did. A case's value is that of
@@ -52,12 +52,13 @@ module PathCoverage
52
52
  def self.arm_node(source, arm, matched, (outcomes, leaves))
53
53
  condition = arm.body
54
54
  body = if condition
55
- SiteTree.node(source, condition, outcomes, leaves)
55
+ DecisionTree.node(source, condition, outcomes,
56
+ leaves)
56
57
  end
57
58
  matched << body[:cells].map { |cell| cell == true } if body
58
59
  cells = body ? matched.last : unmatched(matched, outcomes)
59
- text = SiteTree.text_of(source, arm)
60
- { arm: arm.kind, text:, cells:, body: }
60
+ text = DecisionTree.text_of(source, arm)
61
+ { arm: arm.kind, text:, leaves: arm.leaves, cells:, body: }
61
62
  end
62
63
 
63
64
  # Whether each column matched none of its whens' patterns:
@@ -74,12 +75,13 @@ module PathCoverage
74
75
  end
75
76
 
76
77
  # { pattern range => the opening of its pattern call } for
77
- # the patterns of inner, a case nested in a site whose leaves
78
- # are leaves.
79
- def self.calls(site, inner, leaves)
78
+ # the patterns of inner, a case nested in a decision whose
79
+ # leaves are leaves.
80
+ def self.calls(decision, inner, leaves)
80
81
  inner.leaves.to_h do |range|
81
82
  index = leaves.index(range)
82
- [range, "PathCoverage.pattern(#{site.inspect}, #{index}, "]
83
+ [range,
84
+ "PathCoverage.pattern(#{decision.inspect}, #{index}, "]
83
85
  end
84
86
  end
85
87
  end
@@ -20,26 +20,55 @@ module PathCoverage
20
20
 
21
21
  # The ways node can be evaluated: each way of its first
22
22
  # operand, going on to the next while each outcome is the
23
- # one its operator goes on with.
23
+ # one its operator goes on with; a way that reaches an
24
+ # operand that leaves the method, eg return t in
25
+ # x or return t, ends there, with no value.
24
26
  def self.ways(node, fixed)
25
27
  first, *rest = node.operands
26
28
  goes_on = CONTINUES_ON.fetch(node.operator)
27
- rest.reduce(SitePaths.walk(first, fixed)) do |ways, operand|
28
- ways.flat_map do |outcomes, value|
29
- next [[outcomes, value]] unless value == goes_on
30
-
31
- SitePaths.walk(operand, fixed).map do |more, after|
32
- [outcomes.merge(more), after]
33
- end
34
- end
29
+ rest.reduce(DecisionPaths.walk(first, fixed)) do |all, later|
30
+ all.flat_map { |way| onward(way, later, goes_on, fixed) }
35
31
  end
36
32
  end
37
33
 
38
- # Its value in each column: the last operand it reached.
34
+ # The ways a way so far goes on to at the operand later: the
35
+ # way as it is, when its value is not the one its operator
36
+ # goes on with; ended with no value, when later leaves the
37
+ # method; else each way of later after it.
38
+ def self.onward((outcomes, value), later, goes_on, fixed)
39
+ return [[outcomes, value]] unless value == goes_on
40
+ return [[outcomes, nil]] if later.is_a?(Arm)
41
+
42
+ DecisionPaths.walk(later, fixed).map do |more, after|
43
+ [outcomes.merge(more), after]
44
+ end
45
+ end
46
+
47
+ # Its value in each column: the last operand it reached, an
48
+ # operand that leaves the method giving none.
39
49
  def self.value(_operator, operands)
40
- operands.map { |operand| operand[:cells] }
50
+ operands.map { _1[:cells] unless _1.key?(:arm) }.compact
41
51
  .transpose.map { |column| column.compact.last }
42
52
  end
53
+
54
+ # What its leave call wraps: the whole of it, or, for one
55
+ # that ends in an operand that leaves the method, eg
56
+ # x or return t, the operands before that one, so its path
57
+ # is counted before the method is left.
58
+ def self.trailed(condition)
59
+ *before, last = condition.operands
60
+ return condition unless last.is_a?(Arm)
61
+
62
+ start = span(before.first).begin
63
+ condition.with(operands: before,
64
+ extent: start...span(before.last).end)
65
+ end
66
+
67
+ # The byte range of operand, a leaf's range or a nested
68
+ # condition's extent.
69
+ def self.span(operand)
70
+ operand.is_a?(Condition) ? operand.extent : operand
71
+ end
43
72
  end
44
73
  end
45
74
  end
@@ -12,11 +12,12 @@ module PathCoverage
12
12
  def ways(node, fixed)
13
13
  predicate, then_arm, else_arm = node.operands
14
14
  then_on = THEN_ON[node.operator]
15
- SitePaths.walk(predicate, fixed).flat_map do |outcomes, value|
15
+ DecisionPaths.walk(predicate,
16
+ fixed).flat_map do |outcomes, value|
16
17
  body = (value == then_on ? then_arm : else_arm)&.body
17
18
  next [[outcomes, nil]] if body.nil?
18
19
 
19
- SitePaths.walk(body, fixed).map do |more, _|
20
+ DecisionPaths.walk(body, fixed).map do |more, _|
20
21
  [outcomes.merge(more), nil]
21
22
  end
22
23
  end
@@ -42,7 +43,7 @@ module PathCoverage
42
43
 
43
44
  OPERATORS = CHOICES
44
45
 
45
- # What its site's leave call wraps: an if's or an unless's
46
+ # What its decision's leave call wraps: an if's or an unless's
46
47
  # predicate, a leaf's byte range or a nested Condition, as
47
48
  # its path is decided there and its arms are statements that
48
49
  # may return or raise; a ternary as a whole.
@@ -3,8 +3,9 @@ require_relative 'block'
3
3
 
4
4
  module PathCoverage
5
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'
6
+ # A leaf with decisions inside it, eg
7
+ # ids.select { |i| i.odd? }.any? as an operand of an &&: its
8
+ # operands are those decisions'
8
9
  # Conditions, which run as it does, then the leaf's own byte
9
10
  # range, whose outcome is its value.
10
11
  module Contains
@@ -13,12 +14,12 @@ module PathCoverage
13
14
  OPERATORS = [CONTAINS].freeze
14
15
 
15
16
  # 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
+ # the decisions inside it, then each way of the leaf itself, its
17
18
  # value the whole's.
18
19
  def self.ways(node, fixed)
19
20
  inner = node.operands[0...-1]
20
- before = inner.reduce([{}]) do |ways, site|
21
- ways.product(SitePaths.walk(site, fixed))
21
+ before = inner.reduce([{}]) do |ways, decision|
22
+ ways.product(DecisionPaths.walk(decision, fixed))
22
23
  .map { |outcomes, (more, _)| outcomes.merge(more) }
23
24
  end
24
25
  before.flat_map do |outcomes|
@@ -28,41 +29,43 @@ module PathCoverage
28
29
  end
29
30
  end
30
31
 
31
- # The ways the leaf of node can be evaluated after the sites
32
+ # The ways the leaf of node can be evaluated after the decisions
32
33
  # inside it took outcomes: the one outcome it must have then,
33
34
  # when known, else true or false.
34
35
  def self.leaf_ways(node, outcomes, fixed)
35
36
  range = node.operands.last
36
37
  known = known_outcome(node, outcomes, fixed)
37
- return SitePaths.leaf(range, fixed) if known.nil?
38
+ return DecisionPaths.leaf(range, fixed) if known.nil?
38
39
 
39
40
  [[{ range => known }, known]]
40
41
  end
41
42
 
42
- # The outcome the leaf of node must have after the sites
43
+ # The outcome the leaf of node must have after the decisions
43
44
  # inside it took outcomes, nil when it can have either: for
44
45
  # 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
46
+ # has items; for a leaf that is wholly one decision, eg a find,
47
+ # that decision's value. Each only when the decision's kind can
47
48
  # tell. A nonempty call has no arguments, so its receiver is
48
- # the largest of those sites, any other lying within it.
49
+ # the largest of those decisions, any other lying within it.
49
50
  def self.known_outcome(node, outcomes, fixed)
50
51
  *inner, range = node.operands
51
52
  if node.fixed[range] == :nonempty
52
- receiver = inner.max_by { |site| site.extent.size }
53
+ receiver = inner.max_by { |decision| decision.extent.size }
53
54
  return Block.items?(receiver, outcomes)
54
55
  end
55
- whole = inner.to_h { |site| [site.extent, site] }[range]
56
+ whole = inner.to_h do |decision|
57
+ [decision.extent, decision]
58
+ end[range]
56
59
  return nil if whole.nil?
57
60
 
58
61
  value_on(whole, outcomes, fixed)
59
62
  end
60
63
 
61
- # site's value on its way that took outcomes, one of its
64
+ # decision's value on its way that took outcomes, one of its
62
65
  # own, nil when its kind cannot tell. outcomes always hold
63
- # one of site's ways, so one is always found.
64
- def self.value_on(site, outcomes, fixed)
65
- SitePaths.walk(site, fixed).each do |more, value|
66
+ # one of decision's ways, so one is always found.
67
+ def self.value_on(decision, outcomes, fixed)
68
+ DecisionPaths.walk(decision, fixed).each do |more, value|
66
69
  return value if more <= outcomes
67
70
  end
68
71
  end
@@ -74,39 +77,44 @@ module PathCoverage
74
77
 
75
78
  # nodes, labelled and texted so their rows read as the
76
79
  # leaf's code does.
77
- # Each site inside the leaf is labelled with the leaf's text
80
+ # Each decision inside the leaf is labelled with the leaf's text
78
81
  # before it, its operator, and its own opening, eg select {
79
82
  # for ids.select { |id| id.odd? }.any?, or ( || for
80
83
  # (a || b).any?.
81
- # The leaf is texted with the last site's own closing, then
82
- # the leaf's text after that site, eg }.any? or ).any?.
84
+ # The leaf is texted with the last decision's own closing, then
85
+ # the leaf's text after that decision, eg }.any? or ).any?.
83
86
  def self.texted(source, condition, nodes)
84
87
  *inner, range = condition.operands
85
- starts = [range.begin, *inner.map { |site| site.extent.end }]
86
- labelled = inner.zip(starts, nodes).map do |site, at, node|
87
- node.merge(label: label(source, site, at))
88
+ starts = [range.begin, *inner.map do |decision|
89
+ decision.extent.end
90
+ end]
91
+ labelled = inner.zip(starts,
92
+ nodes).map do |decision, at, node|
93
+ node.merge(label: label(source, decision, at))
88
94
  end
89
95
  text = tail(source, inner.last, range)
90
96
  [*labelled, nodes.last.merge(text:)]
91
97
  end
92
98
 
93
- # site's label inside a leaf: the leaf's text from byte at
94
- # up to site, then site's operator and its own opening.
95
- def self.label(source, site, at)
96
- before = source.byteslice(at...site.extent.begin).strip
97
- opening, = Kinds.of(site.operator).brackets(source, site)
98
- [before, site.operator, opening].reject(&:empty?).join(' ')
99
+ # decision's label inside a leaf: the leaf's text from byte at
100
+ # up to decision, then decision's operator and its own opening.
101
+ def self.label(source, decision, at)
102
+ before = source.byteslice(at...decision.extent.begin).strip
103
+ opening, = Kinds.of(decision.operator).brackets(source,
104
+ decision)
105
+ [before, decision.operator,
106
+ opening].reject(&:empty?).join(' ')
99
107
  end
100
108
 
101
109
  # The text of the leaf at range: the closing of last, the
102
- # last site inside the leaf, then the leaf's text after it.
110
+ # last decision inside the leaf, then the leaf's text after it.
103
111
  def self.tail(source, last, range)
104
112
  _, closing = Kinds.of(last.operator).brackets(source, last)
105
113
  closing + source.byteslice(last.extent.end...range.end)
106
114
  end
107
115
 
108
- # The sites inside it, then the leaf, as siblings, so each
109
- # site's rows are drawn once, eg select { then }.any?.
116
+ # The decisions inside it, then the leaf, as siblings, so each
117
+ # decision's rows are drawn once, eg select { then }.any?.
110
118
  def self.unfold(node)
111
119
  node[:operands]
112
120
  end