path_coverage 0.7.1 → 0.8.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 (64) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +45 -19
  3. data/lib/path_coverage/call_trace.rb +1 -1
  4. data/lib/path_coverage/calls.rb +35 -8
  5. data/lib/path_coverage/condition.rb +35 -9
  6. data/lib/path_coverage/current_test.rb +28 -0
  7. data/lib/path_coverage/decision.rb +7 -0
  8. data/lib/path_coverage/decision_sites.rb +40 -0
  9. data/lib/path_coverage/decision_tree.rb +11 -21
  10. data/lib/path_coverage/evaluated.rb +23 -0
  11. data/lib/path_coverage/fold_rows.rb +7 -8
  12. data/lib/path_coverage/gaps.rb +4 -4
  13. data/lib/path_coverage/json_reader.rb +22 -17
  14. data/lib/path_coverage/json_reports.rb +5 -3
  15. data/lib/path_coverage/json_writer.rb +12 -8
  16. data/lib/path_coverage/kernel.rb +11 -3
  17. data/lib/path_coverage/kinds/base.rb +9 -0
  18. data/lib/path_coverage/kinds/block.rb +7 -1
  19. data/lib/path_coverage/kinds/case.rb +63 -20
  20. data/lib/path_coverage/kinds/chain.rb +13 -3
  21. data/lib/path_coverage/kinds/choice.rb +5 -0
  22. data/lib/path_coverage/kinds/contains.rb +6 -0
  23. data/lib/path_coverage/kinds/safe.rb +3 -3
  24. data/lib/path_coverage/kinds/write.rb +3 -3
  25. data/lib/path_coverage/kinds.rb +14 -0
  26. data/lib/path_coverage/loop_nodes.rb +15 -10
  27. data/lib/path_coverage/manifest.rb +61 -35
  28. data/lib/path_coverage/manifest_case.rb +43 -26
  29. data/lib/path_coverage/manifest_folding.rb +20 -0
  30. data/lib/path_coverage/merge.rb +8 -8
  31. data/lib/path_coverage/method_statements.rb +41 -61
  32. data/lib/path_coverage/minitest.rb +7 -0
  33. data/lib/path_coverage/minitest_tests.rb +25 -0
  34. data/lib/path_coverage/paths_context.rb +2 -2
  35. data/lib/path_coverage/paths_forks.rb +6 -3
  36. data/lib/path_coverage/paths_leaving.rb +3 -1
  37. data/lib/path_coverage/paths_regions.rb +78 -0
  38. data/lib/path_coverage/paths_rescues.rb +5 -2
  39. data/lib/path_coverage/paths_scopes.rb +2 -1
  40. data/lib/path_coverage/paths_tree.rb +10 -68
  41. data/lib/path_coverage/paths_way_lists.rb +11 -10
  42. data/lib/path_coverage/recorder.rb +28 -47
  43. data/lib/path_coverage/recorder_blocks.rb +3 -3
  44. data/lib/path_coverage/recorder_cases.rb +70 -17
  45. data/lib/path_coverage/recorder_chains.rb +2 -2
  46. data/lib/path_coverage/render.rb +7 -3
  47. data/lib/path_coverage/report_tallies.rb +79 -0
  48. data/lib/path_coverage/report_text.rb +23 -0
  49. data/lib/path_coverage/rescue_forks.rb +3 -0
  50. data/lib/path_coverage/result.rb +17 -16
  51. data/lib/path_coverage/rewriter.rb +42 -114
  52. data/lib/path_coverage/rewriter_marks.rb +108 -0
  53. data/lib/path_coverage/source_report.rb +5 -6
  54. data/lib/path_coverage/stable_leaves.rb +6 -26
  55. data/lib/path_coverage/statement_arms.rb +23 -25
  56. data/lib/path_coverage/statement_tops.rb +53 -0
  57. data/lib/path_coverage/tally.rb +129 -0
  58. data/lib/path_coverage/testing.rb +13 -0
  59. data/lib/path_coverage/totals.rb +3 -3
  60. data/lib/path_coverage/version.rb +1 -1
  61. data/lib/path_coverage/walk.rb +4 -3
  62. data/lib/path_coverage/ways_report.rb +11 -10
  63. data/lib/path_coverage/within_methods.rb +12 -3
  64. metadata +13 -1
@@ -3,48 +3,65 @@ require_relative 'condition'
3
3
  module PathCoverage
4
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
- # condition of its patterns, and its else. It relies on its
7
- # includer's extent_of and statement_arm.
6
+ # condition of its patterns, and its else. Every case is one,
7
+ # with a subject or none, and with splats among its patterns or
8
+ # none: each pattern is a leaf, a splat one leaf for all its
9
+ # items, and Ruby itself matches or tests it. It relies on its
10
+ # includer's extent_of, statement_arm and known_outcome.
8
11
  module ManifestCase
9
- # True for a case whose paths can be seen: one with a
10
- # subject, as each pattern is then matched against it, and
11
- # no splat among its patterns, whose items cannot each be
12
- # stood in for.
13
- def case_decision?(node)
14
- return false if node.predicate.nil?
15
-
16
- patterns = node.conditions.flat_map(&:conditions)
17
- !patterns.map(&:type).include?(:splat_node)
18
- end
19
-
20
12
  # A case as a Condition, its extent its case keyword to the
21
- # end of its subject, as its arms are statements.
13
+ # end of its subject, or of the keyword when it has none, as
14
+ # its arms are statements; its whole the case to its end.
22
15
  def case_condition(node)
23
- node.predicate.accept(self)
24
- whens = node.conditions.map { |clause| when_arm(clause) }
16
+ subject = node.predicate
17
+ subject&.accept(self)
18
+ whens = node.conditions.map { when_arm(_1, subject) }
25
19
  start = node.case_keyword_loc.start_offset
20
+ head = subject ? subject.location : node.case_keyword_loc
26
21
  Condition.new(
27
22
  operator: 'case',
28
23
  operands: [*whens, *case_else(node.else_clause)],
29
- extent: start...node.predicate.location.end_offset
24
+ extent: start...head.end_offset, whole: extent_of(node)
30
25
  )
31
26
  end
32
27
 
33
28
  # One when as an arm, its body a when condition whose leaves
34
- # are its patterns' byte ranges. A pattern is matched, not
35
- # tested for truth, so none is ever fixed.
36
- def when_arm(clause)
37
- patterns = clause.conditions.map do |pattern|
38
- pattern.accept(self)
39
- extent_of(pattern)
40
- end
29
+ # are its patterns' byte ranges: matched against subject, so
30
+ # never fixed; or, in a case with no subject, a TRUTHY_WHEN,
31
+ # tested for truth, so fixed as a leaf is. Its extent is its
32
+ # patterns as written, parentheses and splats included.
33
+ def when_arm(clause, subject)
34
+ tested = subject.nil?
35
+ written = clause.conditions
36
+ patterns = written.map { pattern_leaf(_1, tested:) }
37
+ first, last = written.values_at(0, -1).map { extent_of(_1) }
41
38
  body = Condition.new(
42
- operator: 'when', operands: patterns,
43
- extent: patterns.first.begin...patterns.last.end
39
+ operator: tested ? TRUTHY_WHEN : 'when', operands: patterns,
40
+ extent: first.begin...last.end
44
41
  )
45
42
  statement_arm(:when, clause.statements).with(body:)
46
43
  end
47
44
 
45
+ # The byte range of pattern as a leaf: a splat's expression,
46
+ # whose items are its patterns, eg xs in *xs; else the pattern
47
+ # inside any parentheses, so a decision that is the whole of it
48
+ # is the whole of the leaf. Either is an expression the case
49
+ # takes as written when a call that gives it back stands in
50
+ # its place. It is noted as a leaf is, tested for truth in a
51
+ # case with no subject; a splat never is, as it tests each of
52
+ # its items, so is never fixed. Decisions inside the pattern
53
+ # are decisions of their own, even in a case in another's leaf,
54
+ # so a decision that is the whole of a pattern gives it its
55
+ # outcome.
56
+ def pattern_leaf(pattern, tested:)
57
+ scoped { pattern.accept(self) }
58
+ splat = pattern.type == :splat_node
59
+ node = splat ? pattern.expression : grouped(pattern)
60
+ range = extent_of(node)
61
+ note_leaf(node, range, tested: tested && !splat)
62
+ range
63
+ end
64
+
48
65
  # A case's else arm, none when it has no else.
49
66
  def case_else(clause)
50
67
  clause ? [statement_arm(:else, clause.statements)] : []
@@ -43,6 +43,26 @@ module PathCoverage
43
43
  # The one outcome each of a decision's values gives a leaf.
44
44
  GIVEN = { [true] => :truthy, [false] => :falsy }.freeze
45
45
 
46
+ # Notes the leaf node, at range, among its decision's commands
47
+ # when it is no bare argument; and, tested for truth, among its
48
+ # flip-flops when it is one, and among its fixed leaves when it
49
+ # can have one outcome only.
50
+ def note_leaf(node, range, tested:)
51
+ @notes[:commands] << range if Manifest.bare_less?(node)
52
+ return unless tested
53
+
54
+ @notes[:flip_flops] << range if Manifest.flip_flop?(node)
55
+ outcome = known_outcome(node)
56
+ @notes[:fixed][range] = outcome if outcome
57
+ end
58
+
59
+ # The one outcome a leaf tested for truth can have, by its
60
+ # form or, for a decision of its own, its values; nil when it
61
+ # can have either.
62
+ def known_outcome(node)
63
+ Manifest.fixed_outcome(node) || given_outcome(node)
64
+ end
65
+
46
66
  # The one outcome the leaf node can have, as the decision it
47
67
  # wholly is gives it on every one of its paths, eg :truthy for
48
68
  # a select, whose array is truthy; nil when its paths give
@@ -2,7 +2,7 @@ module PathCoverage
2
2
  # Combines the operands of several runs of the same source.
3
3
  module Merge
4
4
  # One operands hash, as Result#operands gives them, whose
5
- # counts are the sums of every run's.
5
+ # Tallies are the sums of every run's.
6
6
  def self.operands(runs)
7
7
  runs.reduce do |total, run|
8
8
  total.merge(run) do |_decision, mine, theirs|
@@ -12,9 +12,9 @@ module PathCoverage
12
12
  end
13
13
 
14
14
  # One decision paths hash, as Result#paths gives them, each
15
- # path's count the sum of every run's. Runs that list a
16
- # decision's paths differently raise, as adding their counts by
17
- # position would credit one path's count to another.
15
+ # path's Tally the sum of every run's. Runs that list a
16
+ # decision's paths differently raise, as adding their Tallies by
17
+ # position would credit one path's to another.
18
18
  def self.paths(runs)
19
19
  runs.reduce do |total, run|
20
20
  total.merge(run) do |name, mine, theirs|
@@ -22,14 +22,14 @@ module PathCoverage
22
22
  raise ArgumentError, "runs differ at #{name}" unless same
23
23
 
24
24
  mine.zip(theirs).map do |a, b|
25
- { path: a[:path], count: a[:count] + b[:count] }
25
+ { path: a[:path], tests: a[:tests] + b[:tests] }
26
26
  end
27
27
  end
28
28
  end
29
29
  end
30
30
 
31
- # One listed hash, { decision => { path => count } }, eg the
32
- # paths a treatment rules out, each path's count the sum of
31
+ # One listed hash, { decision => { path => Tally } }, eg the
32
+ # paths a treatment rules out, each path's Tally the sum of
33
33
  # every run's, eg [true, false] taken once in each of two
34
34
  # runs is taken twice.
35
35
  def self.listed(runs)
@@ -40,7 +40,7 @@ module PathCoverage
40
40
  end
41
41
  end
42
42
 
43
- # One operand's counts from two runs, added; a fixed leaf's
43
+ # One operand's Tallies from two runs, added; a fixed leaf's
44
44
  # only comes from the source, so either run's will do.
45
45
  def self.sum(mine, theirs)
46
46
  mine.merge(theirs) do |key, a, b|
@@ -1,12 +1,14 @@
1
1
  require 'prism'
2
+ require_relative 'evaluated'
2
3
  require_relative 'manifest'
3
4
  require_relative 'manifest_chains'
4
5
  require_relative 'manifest_choices'
5
6
  require_relative 'decision'
6
7
  require_relative 'decision_paths'
8
+ require_relative 'decision_sites'
7
9
  require_relative 'decision_tree'
8
- require_relative 'leaving'
9
10
  require_relative 'statement_arms'
11
+ require_relative 'statement_tops'
10
12
  require_relative 'totals'
11
13
 
12
14
  module PathCoverage
@@ -141,23 +143,26 @@ module PathCoverage
141
143
  end
142
144
  end
143
145
 
144
- # How many of a decision row's paths have a count in paths.
146
+ # How many of a decision row's paths some test took, in paths.
145
147
  def self.taken_decision(row, paths)
146
- counts = paths.fetch(row.decision).map { |each| each[:count] }
147
- counts.count(&:positive?)
148
+ paths.fetch(row.decision).count { |each| !each[:tests].empty? }
148
149
  end
149
150
 
150
151
  # Collects each def's top-level statements.
151
152
  class Collector < Prism::Visitor
153
+ include Evaluated::Visitor
152
154
  include ManifestChains
153
155
  include ManifestChoices
154
156
  include StatementArms
157
+ include StatementTops
155
158
 
156
159
  # Starts with no methods found; decisions are decision mode's,
157
- # { decision => Condition }.
160
+ # { decision => Condition }, each known by its site, as
161
+ # DecisionSites has them.
158
162
  def initialize(path, decisions)
159
163
  @path = path
160
164
  @decisions = decisions
165
+ @sites = DecisionSites.new(path, decisions)
161
166
  @methods = {}
162
167
  @names = {}
163
168
  @lasts = {}
@@ -229,19 +234,33 @@ module PathCoverage
229
234
  end
230
235
  end
231
236
 
232
- # The Statement row of node. A decision is where decision mode
233
- # names it, eg a modifier if at its keyword, with as many
234
- # paths as decision mode gives it. Anything else is where
235
- # it starts, with 1 path.
237
+ # The Statement row of node: a decision's row for the site of
238
+ # a decision, a plain row for any other node.
236
239
  def row_of(node)
237
- decision = decision_at(node)
238
- loc = decision ? decision_loc(node) : node.location
239
- name = Decision.at(@path, loc)
240
+ name = @sites.name_of(node)
241
+ name.nil? ? plain_row(node) : decision_row(node, name)
242
+ end
243
+
244
+ # The row of node, the site of the decision named name: at its
245
+ # decision_loc, eg a modifier if at its keyword, with as many
246
+ # paths as decision mode gives the decision.
247
+ def decision_row(node, name)
248
+ loc = decision_loc(node)
249
+ paths = DecisionPaths.possible(@decisions.fetch(name)).size
250
+ Statement.new(
251
+ line: loc.start_line, column: loc.start_column,
252
+ top: top_of(node), paths:, decision: name, name:
253
+ )
254
+ end
255
+
256
+ # The row of node, the site of no decision: named where it
257
+ # starts, with 1 path.
258
+ def plain_row(node)
259
+ loc = node.location
240
260
  Statement.new(
241
261
  line: loc.start_line, column: loc.start_column,
242
- top: top_of(node),
243
- paths: decision ? DecisionPaths.possible(decision).size : 1,
244
- decision: decision ? name : nil, name:
262
+ top: top_of(node), paths: 1, decision: nil,
263
+ name: Decision.at(@path, loc)
245
264
  )
246
265
  end
247
266
 
@@ -261,60 +280,21 @@ module PathCoverage
261
280
  return [node] if decision?(node)
262
281
  return [] if UNSEEN.include?(node.type)
263
282
 
264
- node.compact_child_nodes.flat_map do |child|
283
+ Evaluated.children(node).flat_map do |child|
265
284
  decisions_in(child)
266
285
  end
267
286
  end
268
287
 
269
- # True when decision mode has a decision named where node's
270
- # decision would start.
288
+ # True when node is the site of one of decision mode's
289
+ # decisions.
271
290
  def decision?(node)
272
- !decision_at(node).nil?
291
+ !@sites.name_of(node).nil?
273
292
  end
274
293
 
275
- # Decision mode's Condition for the decision named where
276
- # node's decision would start; nil when it has none there.
294
+ # Decision mode's Condition for the decision node is the site
295
+ # of; nil when node is the site of none.
277
296
  def decision_at(node)
278
- @decisions[decision_name(node)]
279
- end
280
-
281
- # The name node's decision would have, by where it would start.
282
- def decision_name(node)
283
- Decision.at(@path, decision_loc(node))
284
- end
285
-
286
- TOPS = { call_node: '()', def_node: 'def' }.freeze
287
-
288
- # What statement is as written: a decision's operator, eg if,
289
- # when decision mode has a decision where it starts, an elsif
290
- # being elsif, as written, an if or unless with an else
291
- # if/else or unless/else; else = for a write, Leaving's word,
292
- # return or raise, for a statement that leaves the method,
293
- # () for a call, def for a def inside it, value for anything
294
- # else.
295
- def top_of(statement)
296
- decision = decision_at(statement)
297
- return 'elsif' if decision && elsif?(statement)
298
- return with_else(decision.operator, statement) if decision
299
- return '=' if write?(statement)
300
-
301
- word = Leaving.word(statement)
302
- return word if word
303
-
304
- TOPS.fetch(statement.type, 'value')
305
- end
306
-
307
- # True for an if written as an elsif.
308
- def elsif?(statement)
309
- statement.type == :if_node && statement.if_keyword == 'elsif'
310
- end
311
-
312
- # True for a write of any kind: of a variable, eg @x = 1
313
- # or x += 1, or a call Ruby makes of an index or attribute
314
- # write, eg a[i] = x or a.b = x.
315
- def write?(statement)
316
- statement.type.end_with?('_write_node') ||
317
- (statement.type == :call_node && statement.attribute_write?)
297
+ @sites.condition_of(node)
318
298
  end
319
299
  end
320
300
  end
@@ -0,0 +1,7 @@
1
+ require 'minitest'
2
+ require_relative 'minitest_tests'
3
+
4
+ # Required in a test helper, eg require 'path_coverage/minitest',
5
+ # so every Minitest test is marked under way as it runs, and the
6
+ # JSON report tells which tests take which paths.
7
+ Minitest::Test.include(PathCoverage::MinitestTests)
@@ -0,0 +1,25 @@
1
+ require_relative 'current_test'
2
+
3
+ module PathCoverage
4
+ # The hooks that mark each Minitest test under way, so every count
5
+ # is tallied under the test that made it: its setup, body and
6
+ # teardown, and any thread they start. A test's id is its class
7
+ # and name, eg KataTest#test_ok. Once it ends, Tally::NO_TEST is
8
+ # under way again. Each test marks the thread it runs in, so
9
+ # tests run in parallel threads count apart.
10
+ module MinitestTests
11
+ # Marks this test under way before its setup.
12
+ def before_setup
13
+ CurrentTest.testing("#{self.class.name}##{name}")
14
+ super
15
+ end
16
+
17
+ # Marks no test under way after its teardown, even one that
18
+ # raised.
19
+ def after_teardown
20
+ super
21
+ ensure
22
+ CurrentTest.testing(Tally::NO_TEST)
23
+ end
24
+ end
25
+ end
@@ -86,8 +86,8 @@ module PathCoverage
86
86
  decision = row.decision
87
87
  return reached.to_h.key?(row.name) ? [0] : [] if decision.nil?
88
88
 
89
- counts = paths.fetch(decision).map { _1[:count] }
90
- counts.each_with_index.map { |n, at| at if n.positive? }.compact
89
+ taken = paths.fetch(decision).map { _1[:tests] }
90
+ taken.each_index.reject { taken[_1].empty? }
91
91
  end
92
92
 
93
93
  # Example: [["k.rb:5:11", [true]]], a ?:'s true path taken past
@@ -1,3 +1,5 @@
1
+ require_relative 'paths_regions'
2
+
1
3
  module PathCoverage
2
4
  # The loops of a flow. forks is { loop's name => [its bytes, its
3
5
  # body's] }, as LoopForks gives them. Each loop is a scope: a
@@ -14,9 +16,10 @@ module PathCoverage
14
16
  # PathsRescues gives them.
15
17
  def self.regions(forks, rescues)
16
18
  forks.map do |name, (_, body)|
17
- PathsTree::Region.new(owner: PathsTree.key_at(name),
18
- range: body, arm: 'body', on: [1],
19
- leaf: nil, leaves: nil)
19
+ PathsRegions::Region.new(
20
+ owner: PathsTree.key_at(name), range: body, arm: 'body',
21
+ on: [1], leaf: nil, leaves: nil, tested: false
22
+ )
20
23
  end + PathsRescues.regions(rescues)
21
24
  end
22
25
 
@@ -1,3 +1,5 @@
1
+ require_relative 'paths_regions'
2
+
1
3
  module PathCoverage
2
4
  # How ways leave a flow. PathsWays includes it. A return or a
3
5
  # throw returns, and no rescue catches it. A raise or a fail
@@ -14,7 +16,7 @@ module PathCoverage
14
16
  # by, eg return or raise.
15
17
  def leaving(key)
16
18
  decision = @decisions.fetch(@rows[key].decision)
17
- regions = PathsTree.regions_of(@source, key, decision)
19
+ regions = PathsRegions.of(@source, key, decision)
18
20
  regions.select(&:leaves).flat_map do |region|
19
21
  region.on.map { [_1, region.leaves] }
20
22
  end.to_h
@@ -0,0 +1,78 @@
1
+ require_relative 'condition'
2
+ require_relative 'decision_paths'
3
+ require_relative 'decision_tree'
4
+ require_relative 'kinds'
5
+
6
+ module PathCoverage
7
+ # The parts of a decision's code that a row can be inside, each
8
+ # run on some of the decision's paths: its arms of statements,
9
+ # and its leaves, as PathsTree places rows among them.
10
+ module PathsRegions
11
+ # Part of a decision's code that a row can be inside: an arm of
12
+ # statements, or a leaf; owner the key of the decision's row,
13
+ # range its bytes, arm its arm's name, nil for a leaf, on
14
+ # the indices of the decision's possible paths that run it, and
15
+ # leaf a leaf's index among the decision's leaves, nil for an arm,
16
+ # leaves its arm's Leaving word, nil for a leaf or an arm that
17
+ # does not leave the method, and tested true for a leaf whose
18
+ # outcome is its truthiness, so a decision that is the whole of
19
+ # it gives it its value, false for any other region.
20
+ Region = Data.define(:owner, :range, :arm, :on, :leaf, :leaves,
21
+ :tested)
22
+
23
+ # The regions of condition, the decision of the row keyed owner:
24
+ # its arms', and its leaves'.
25
+ def self.of(source, owner, condition)
26
+ possible = DecisionPaths.possible(condition)
27
+ node = DecisionTree.node(source, condition, possible,
28
+ (0..).each)
29
+ arm_regions(owner, condition, node) +
30
+ leaf_regions(owner, condition, possible)
31
+ end
32
+
33
+ # The regions of condition's leaves, in no arm, each run on
34
+ # the paths of possible with an outcome at its index, and tested
35
+ # when its outcome is its truthiness.
36
+ def self.leaf_regions(owner, condition, possible)
37
+ truthy = Kinds.truthy_leaves(condition)
38
+ condition.leaves.each_with_index.map do |range, index|
39
+ skipped = possible.each_index.group_by do |i|
40
+ possible[i][index].nil?
41
+ end
42
+ on = skipped.fetch(false, [])
43
+ Region.new(owner:, range:, arm: nil, on:, leaf: index,
44
+ leaves: nil, tested: truthy.include?(range))
45
+ end
46
+ end
47
+
48
+ # The regions of the arms in condition, node its tree, each
49
+ # operand's node at its own index: of its own arms, and of
50
+ # those of each condition nested in it, in an operand or in
51
+ # an arm's body.
52
+ def self.arm_regions(owner, condition, node)
53
+ built = condition.operands.zip(node[:operands])
54
+ built.flat_map do |operand, operand_node|
55
+ case operand
56
+ when Condition then arm_regions(owner, operand, operand_node)
57
+ when Arm then arm_region(owner, operand, operand_node)
58
+ else []
59
+ end
60
+ end
61
+ end
62
+
63
+ # The regions of arm, node its node: its own, named by its
64
+ # kind and run on the paths that take it, none for an arm
65
+ # with no statements, which holds no row; then its body's.
66
+ def self.arm_region(owner, arm, node)
67
+ body = arm.body ? arm_regions(owner, arm.body, node[:body]) : []
68
+ return body if arm.extent.nil?
69
+
70
+ cells = node[:cells]
71
+ on = cells.each_index.group_by { cells[_1] }.fetch(true, [])
72
+ own = Region.new(owner:, range: arm.extent, arm: arm.kind.to_s,
73
+ on:, leaf: nil, leaves: arm.leaves,
74
+ tested: false)
75
+ [own, *body]
76
+ end
77
+ end
78
+ end
@@ -1,3 +1,5 @@
1
+ require_relative 'paths_regions'
2
+
1
3
  module PathCoverage
2
4
  # How a begin with a rescue forks its flow. PathsWays includes
3
5
  # it. The begin is a scope. Its body either completes, or a
@@ -21,8 +23,9 @@ module PathCoverage
21
23
  owner = PathsTree.key_at(name)
22
24
  bodies = [['body', body], *clauses.map { ['rescue', _1] }]
23
25
  bodies.each_with_index.map do |(arm, range), path|
24
- PathsTree::Region.new(owner:, range:, arm:, on: [path],
25
- leaf: nil, leaves: nil)
26
+ PathsRegions::Region.new(owner:, range:, arm:, on: [path],
27
+ leaf: nil, leaves: nil,
28
+ tested: false)
26
29
  end
27
30
  end
28
31
  end
@@ -1,4 +1,5 @@
1
1
  require 'prism'
2
+ require_relative 'evaluated'
2
3
 
3
4
  module PathCoverage
4
5
  # The scope a top flow is named for: the module or class its
@@ -31,7 +32,7 @@ module PathCoverage
31
32
  # before those nested in it.
32
33
  def self.holders_in(node)
33
34
  own = HOLDERS.include?(node.class) ? [node] : []
34
- own + node.compact_child_nodes.flat_map { holders_in(_1) }
35
+ own + Evaluated.children(node).flat_map { holders_in(_1) }
35
36
  end
36
37
  end
37
38
  end
@@ -1,9 +1,9 @@
1
1
  require_relative 'paths_forks'
2
+ require_relative 'paths_regions'
2
3
  require_relative 'paths_rescues'
3
4
  require_relative 'manifest'
4
5
  require_relative 'method_statements'
5
6
  require_relative 'decision_paths'
6
- require_relative 'decision_tree'
7
7
 
8
8
  module PathCoverage
9
9
  # How the rows of each flow nest: for each row, the decision it
@@ -11,15 +11,6 @@ module PathCoverage
11
11
  # that decision on which it runs, so a reader can tell which paths
12
12
  # reach a row, and count the paths through a flow as a whole.
13
13
  module PathsTree
14
- # Part of a decision's code that a row can be inside: an arm of
15
- # statements, or a leaf; owner the key of the decision's row,
16
- # range its bytes, arm its arm's name, nil for a leaf, on
17
- # the indices of the decision's possible paths that run it, and
18
- # leaf a leaf's index among the decision's leaves, nil for an arm,
19
- # and leaves its arm's Leaving word, nil for a leaf or an arm that
20
- # does not leave the method.
21
- Region = Data.define(:owner, :range, :arm, :on, :leaf, :leaves)
22
-
23
14
  # The place of a row inside no other row's decision.
24
15
  TOP = { 'parent' => nil, 'in' => nil, 'on' => nil,
25
16
  'leaf' => nil, 'gives' => nil }.freeze
@@ -77,7 +68,8 @@ module PathCoverage
77
68
  # { decision => Condition }.
78
69
  def self.row_regions(source, decisions, rows)
79
70
  rows.select(&:decision).flat_map do |row|
80
- regions_of(source, key_of(row), decisions.fetch(row.decision))
71
+ condition = decisions.fetch(row.decision)
72
+ PathsRegions.of(source, key_of(row), condition)
81
73
  end
82
74
  end
83
75
 
@@ -99,15 +91,17 @@ module PathCoverage
99
91
  # The leaf and gives of a row's place, own being the regions
100
92
  # of its parent holding it, decision its Condition: the index of
101
93
  # the innermost leaf holding it; and, when decision is the whole
102
- # of that leaf, decision's value on each of its possible paths,
103
- # as its kind gives them, the leaf's outcome there. Neither
104
- # for a row that is no decision, a return or a raise, which gives
105
- # its parent nothing.
94
+ # of that leaf and the leaf's outcome is its truthiness,
95
+ # decision's value on each of its possible paths, as its kind
96
+ # gives them, the leaf's outcome there. A leaf whose outcome is
97
+ # a match, or a presence, takes none, as a value does not give
98
+ # it. Neither for a row that is no decision, a return or a
99
+ # raise, which gives its parent nothing.
106
100
  def self.leaf_place(own, decision)
107
101
  return { 'leaf' => nil, 'gives' => nil } if decision.nil?
108
102
 
109
103
  leaf = smallest(own.reject(&:arm))
110
- whole = leaf && decision.extent == leaf.range
104
+ whole = leaf&.tested && decision.extent == leaf.range
111
105
  gives = whole ? DecisionPaths.values(decision) : nil
112
106
  { 'leaf' => leaf&.leaf, 'gives' => gives }
113
107
  end
@@ -134,58 +128,6 @@ module PathCoverage
134
128
  # The region of regions with the fewest bytes; nil for none.
135
129
  def self.smallest(regions) = regions.min_by { _1.range.size }
136
130
 
137
- # The regions of condition, the decision of the row keyed owner:
138
- # its arms', and its leaves'.
139
- def self.regions_of(source, owner, condition)
140
- possible = DecisionPaths.possible(condition)
141
- node = DecisionTree.node(source, condition, possible,
142
- (0..).each)
143
- arm_regions(owner, condition, node) +
144
- leaf_regions(owner, condition, possible)
145
- end
146
-
147
- # The regions of condition's leaves, in no arm, each run on
148
- # the paths of possible with an outcome at its index.
149
- def self.leaf_regions(owner, condition, possible)
150
- condition.leaves.each_with_index.map do |range, index|
151
- skipped = possible.each_index.group_by do |i|
152
- possible[i][index].nil?
153
- end
154
- on = skipped.fetch(false, [])
155
- Region.new(owner:, range:, arm: nil, on:, leaf: index,
156
- leaves: nil)
157
- end
158
- end
159
-
160
- # The regions of the arms in condition, node its tree, each
161
- # operand's node at its own index: of its own arms, and of
162
- # those of each condition nested in it, in an operand or in
163
- # an arm's body.
164
- def self.arm_regions(owner, condition, node)
165
- built = condition.operands.zip(node[:operands])
166
- built.flat_map do |operand, operand_node|
167
- case operand
168
- when Condition then arm_regions(owner, operand, operand_node)
169
- when Arm then arm_region(owner, operand, operand_node)
170
- else []
171
- end
172
- end
173
- end
174
-
175
- # The regions of arm, node its node: its own, named by its
176
- # kind and run on the paths that take it, none for an arm
177
- # with no statements, which holds no row; then its body's.
178
- def self.arm_region(owner, arm, node)
179
- body = arm.body ? arm_regions(owner, arm.body, node[:body]) : []
180
- return body if arm.extent.nil?
181
-
182
- cells = node[:cells]
183
- on = cells.each_index.group_by { cells[_1] }.fetch(true, [])
184
- own = Region.new(owner:, range: arm.extent, arm: arm.kind.to_s,
185
- on:, leaf: nil, leaves: arm.leaves)
186
- [own, *body]
187
- end
188
-
189
131
  # The "line:column" key of row, from its name.
190
132
  def self.key_of(row) = key_at(row.name)
191
133