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
@@ -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:
@@ -68,23 +69,19 @@ module PathCoverage
68
69
  end
69
70
  end
70
71
 
71
- # The name of one of its arms: when or else.
72
- def self.arm_label(arm, _choice)
73
- arm[:arm].to_s
74
- end
75
-
76
72
  # Its patterns need pattern calls.
77
73
  def self.own_calls?
78
74
  true
79
75
  end
80
76
 
81
77
  # { pattern range => the opening of its pattern call } for
82
- # the patterns of inner, a case nested in a site whose leaves
83
- # are leaves.
84
- 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)
85
81
  inner.leaves.to_h do |range|
86
82
  index = leaves.index(range)
87
- [range, "PathCoverage.pattern(#{site.inspect}, #{index}, "]
83
+ [range,
84
+ "PathCoverage.pattern(#{decision.inspect}, #{index}, "]
88
85
  end
89
86
  end
90
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.
@@ -52,13 +53,9 @@ module PathCoverage
52
53
  condition.operands.first
53
54
  end
54
55
 
55
- # The name of one of its arms, of a condition named choice:
56
- # then or else for an if or an unless, as its text is
57
- # statements; ? or : for a ternary's, then its condition's
58
- # operator, or its text for a plain arm.
59
- def self.arm_label(arm, choice)
60
- return arm[:arm].to_s if STATEMENT_CHOICES.include?(choice)
61
-
56
+ # The name of one of a ternary's arms: ? or :, then its
57
+ # condition's operator, or its text for a plain arm.
58
+ def self.arm_label(arm)
62
59
  mark = arm[:arm] == :then ? '?' : ':'
63
60
  body = arm[:body]
64
61
  "#{mark} #{body ? body[:operator] : arm[:text]}"
@@ -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,39 +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 && value_on(whole, outcomes, fixed)
56
+ whole = inner.to_h do |decision|
57
+ [decision.extent, decision]
58
+ end[range]
59
+ return nil if whole.nil?
60
+
61
+ value_on(whole, outcomes, fixed)
57
62
  end
58
63
 
59
- # 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
60
65
  # 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|
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|
64
69
  return value if more <= outcomes
65
70
  end
66
71
  end
@@ -72,39 +77,44 @@ module PathCoverage
72
77
 
73
78
  # nodes, labelled and texted so their rows read as the
74
79
  # leaf's code does.
75
- # 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
76
81
  # before it, its operator, and its own opening, eg select {
77
82
  # for ids.select { |id| id.odd? }.any?, or ( || for
78
83
  # (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?.
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?.
81
86
  def self.texted(source, condition, nodes)
82
87
  *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))
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))
86
94
  end
87
95
  text = tail(source, inner.last, range)
88
96
  [*labelled, nodes.last.merge(text:)]
89
97
  end
90
98
 
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(' ')
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(' ')
97
107
  end
98
108
 
99
109
  # 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.
110
+ # last decision inside the leaf, then the leaf's text after it.
101
111
  def self.tail(source, last, range)
102
112
  _, closing = Kinds.of(last.operator).brackets(source, last)
103
113
  closing + source.byteslice(last.extent.end...range.end)
104
114
  end
105
115
 
106
- # The sites inside it, then the leaf, as siblings, so each
107
- # 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?.
108
118
  def self.unfold(node)
109
119
  node[:operands]
110
120
  end
@@ -3,7 +3,7 @@ require_relative 'base'
3
3
  module PathCoverage
4
4
  module Kinds
5
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
6
+ # not, a nil one skipping the rest of the chain. As a decision of
7
7
  # its own its operands are its receivers; nested in another,
8
8
  # they are its receivers then its value, its last leaf. Each
9
9
  # receiver is wrapped in a present call, which passes it on
@@ -19,7 +19,7 @@ module PathCoverage
19
19
  chain(node.operands[0...-1], node.operands.last, fixed)
20
20
  end
21
21
 
22
- # The ways a &. chain site can be evaluated, every operand
22
+ # The ways a &. chain decision can be evaluated, every operand
23
23
  # a receiver.
24
24
  def self.root_ways(node, fixed)
25
25
  chain(node.operands, nil, fixed)
@@ -32,7 +32,7 @@ module PathCoverage
32
32
  def self.chain(receivers, last, fixed)
33
33
  first, *rest = receivers
34
34
  on = if !rest.empty? then chain(rest, last, fixed)
35
- elsif last then SitePaths.walk(last, fixed)
35
+ elsif last then DecisionPaths.walk(last, fixed)
36
36
  else [[{}, true]]
37
37
  end
38
38
  present = on.map do |outcomes, value|
@@ -44,31 +44,37 @@ module PathCoverage
44
44
  end
45
45
 
46
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.
47
+ # as the chain then gives nil, else its last leaf's.
50
48
  def self.value(_operator, operands)
51
- leaves = operands.last[:called] ? operands[0...-1] : operands
52
- columns = leaves.map { |operand| operand[:cells] }.transpose
49
+ columns = operands.map { |operand| operand[:cells] }.transpose
53
50
  columns.map do |column|
54
51
  column.include?(PRESENCE[1]) ? false : column.last
55
52
  end
56
53
  end
57
54
 
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)
55
+ # The inserts for a &. chain decision: each receiver in a
56
+ # present call, and the whole chain in a leave call that
57
+ # enters it first. Eg user&.account becomes, in effect,
58
+ # leave(enter(decision, 1),
59
+ # present(decision, 0, 0, user)&.account).
60
+ def self.decision_inserts(decision, condition)
63
61
  leaves = condition.leaves
64
- enter = "PathCoverage.enter(#{site.inspect}, #{leaves.size})"
62
+ name = decision.inspect
63
+ enter = "PathCoverage.enter(#{name}, #{leaves.size})"
65
64
  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
65
  extent = condition.extent
71
- presents + Rewriter.wrap(call, extent, command, :site)
66
+ command = condition.commands.include?(extent)
67
+ presents(decision, leaves) +
68
+ Rewriter.wrap(call, extent, command, :decision)
69
+ end
70
+
71
+ # The inserts wrapping each of leaves, a &. chain decision's,
72
+ # in its present call.
73
+ def self.presents(decision, leaves)
74
+ leaves.each_with_index.flat_map do |range, index|
75
+ opening = present(decision, index, 0)
76
+ Rewriter.wrap(opening, range, false, :leaf)
77
+ end
72
78
  end
73
79
 
74
80
  # Its leaves need present and chained calls.
@@ -77,42 +83,42 @@ module PathCoverage
77
83
  end
78
84
 
79
85
  # { leaf range => the opening of its call } for the leaves of
80
- # chain nested in a site whose leaves are leaves: a
86
+ # chain nested in a decision whose leaves are leaves: a
81
87
  # receiver's present call, and the chain's value's chained
82
88
  # call, each told where its chain's leaves start.
83
- def self.calls(site, chain, leaves)
89
+ def self.calls(decision, chain, leaves)
84
90
  first = leaves.index(chain.operands.first)
85
91
  *receivers, value = chain.operands
86
92
  calls = receivers.to_h do |range|
87
- [range, present(site, leaves.index(range), first)]
93
+ [range, present(decision, leaves.index(range), first)]
88
94
  end
89
95
  at = leaves.index(value)
90
- calls.merge(value => chained(site, at, first))
96
+ calls.merge(value => chained(decision, at, first))
91
97
  end
92
98
 
93
99
  # The opening of the chained call around the index-th leaf
94
- # of site, a chain's value, its chain's leaves starting at
100
+ # of decision, a chain's value, its chain's leaves starting at
95
101
  # first.
96
- def self.chained(site, index, first)
97
- "PathCoverage.chained(#{site.inspect}, #{index}, #{first}, "
102
+ def self.chained(decision, index, first)
103
+ "PathCoverage.chained(#{decision.inspect}, #{index}, " \
104
+ "#{first}, "
98
105
  end
99
106
 
100
107
  # 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}, "
108
+ # of decision, its chain's leaves starting at first.
109
+ def self.present(decision, index, first)
110
+ "PathCoverage.present(#{decision.inspect}, #{index}, " \
111
+ "#{first}, "
104
112
  end
105
113
 
106
114
  # Its leaves' nodes, each after the first showing only what
107
115
  # it adds to the one before, eg account after user.
108
116
  # 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.
117
+ # The chain after its last leaf, eg owner in
118
+ # user&.account&.owner, has no node: it decides no path, and
119
+ # is called exactly where that leaf is not nil.
111
120
  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)]
121
+ with_added(source, condition.operands, nodes)
116
122
  end
117
123
 
118
124
  # nodes, the nodes of a chain's leaves at ranges, each after
@@ -131,21 +137,13 @@ module PathCoverage
131
137
  source.byteslice(range).delete_prefix('&.')
132
138
  end
133
139
 
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
140
  # The rows of its leaves, each under the one before, as each
144
141
  # is reached only when the one before is not nil.
145
142
  def self.rows(chain, indent)
146
143
  chain[:operands].each_with_index.map do |operand, depth|
147
- branch = "#{indent}#{HtmlBranch::GAP * depth}#{HtmlBranch::ELL}"
148
- HtmlTree.operand_row(operand, branch)
144
+ gaps = FoldBranches::GAP * depth
145
+ branch = "#{indent}#{gaps}#{FoldBranches::ELL}"
146
+ FoldRows.operand_row(operand, branch)
149
147
  end
150
148
  end
151
149
  end
@@ -14,12 +14,6 @@ module PathCoverage
14
14
 
15
15
  OPERATORS = WRITES
16
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
17
  # Its target needs no record call, and its right side a
24
18
  # written_at call.
25
19
  def self.own_calls?
@@ -28,21 +22,21 @@ module PathCoverage
28
22
 
29
23
  # { target range => nil }: its target is never read, so
30
24
  # never wrapped.
31
- def self.calls(_site, inner, _leaves)
25
+ def self.calls(_decision, inner, _leaves)
32
26
  { inner.operands.first => nil }
33
27
  end
34
28
 
35
- # A write, its target the site's at-th leaf: its right side
29
+ # A write, its target the decision's at-th leaf: its right side
36
30
  # in a written_at call, which notes the target's outcome when
37
31
  # it runs, and the whole of it in a settle call, which notes
38
32
  # the other when it does not. A right side among commands,
39
33
  # 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)
34
+ # in effect, settle(decision, at, true,
35
+ # x ||= written_at(decision, at, false, v)).
36
+ def self.inserts(decision, inner, at, commands)
43
37
  _target, arm = inner.operands
44
38
  ran = THEN_ON[inner.operator]
45
- name = site.inspect
39
+ name = decision.inspect
46
40
  written = "PathCoverage.written_at(#{name}, #{at}, #{ran}, "
47
41
  settle = "PathCoverage.settle(#{name}, #{at}, #{!ran}, "
48
42
  command = commands.include?(arm.extent)
@@ -10,7 +10,7 @@ require_relative 'kinds/write'
10
10
  module PathCoverage
11
11
  # The kinds of condition, each a module that knows, for a
12
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
13
+ # evaluated, its value in each column of its decision's tree, and
14
14
  # the inserts that instrument it. Each extends Kinds::Base,
15
15
  # whose hooks give what most kinds need.
16
16
  module Kinds
@@ -29,7 +29,7 @@ module PathCoverage
29
29
 
30
30
  # The names of the kinds measured, eg chain for Chain: a
31
31
  # report's fingerprint, as runs measuring different kinds
32
- # list a site's paths differently.
32
+ # list a decision's paths differently.
33
33
  def self.names
34
34
  ALL.map { |kind| kind.name.split('::').last.downcase }
35
35
  end
@@ -0,0 +1,44 @@
1
+ require_relative 'condition'
2
+
3
+ module PathCoverage
4
+ # Whether code leaves the method where it stands, decided once,
5
+ # from Prism's nodes, for every part of the gem that asks.
6
+ module Leaving
7
+ # The methods whose call, with no receiver, leaves the method:
8
+ # raise, fail, its other name, and throw, for its catch.
9
+ CALLS = %i[raise fail throw].freeze
10
+
11
+ # 'return' for a return, the method's name for a call of one of
12
+ # CALLS with no receiver, eg 'fail'; nil for a statement that
13
+ # does not leave the method.
14
+ def self.word(statement)
15
+ return 'return' if statement.type == :return_node
16
+
17
+ statement.name.to_s if leaving_call?(statement)
18
+ end
19
+
20
+ # The arm a statement that leaves the method is as an operand,
21
+ # eg return t in x or return t: run when the operand before it
22
+ # gives the outcome its operator goes on with, never a value,
23
+ # so never a leaf.
24
+ def self.arm(statement)
25
+ loc = statement.location
26
+ Arm.new(kind: :then, extent: loc.start_offset...loc.end_offset,
27
+ body: nil, leaves: word(statement))
28
+ end
29
+
30
+ # The word of the first of an arm's statements that leaves the
31
+ # method, statements its code; nil for an arm with no
32
+ # statements or none that leaves.
33
+ def self.of(statements)
34
+ statements && statements.body.map { word(_1) }.compact.first
35
+ end
36
+
37
+ # True when statement is a call of one of CALLS with no
38
+ # receiver.
39
+ def self.leaving_call?(statement)
40
+ statement.type == :call_node && statement.receiver.nil? &&
41
+ CALLS.include?(statement.name)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,57 @@
1
+ require 'prism'
2
+ require_relative 'condition'
3
+ require_relative 'decision'
4
+ require_relative 'loop_nodes'
5
+
6
+ module PathCoverage
7
+ # The loops of each method that fork when loops are decisions:
8
+ # 0 items, or 1+ items, its body walked once. Each is one whose
9
+ # passes a pass call put first in its body can show, so a
10
+ # call's trace tells the two apart; but no block of a block
11
+ # method's decision, eg any?'s, whose paths count its items
12
+ # already.
13
+ module LoopForks
14
+ # { def decision => { loop's name => [its bytes, its body's] } }
15
+ # of each def in source, at path, decisions as
16
+ # Manifest.decisions gives them.
17
+ def self.of(source, path, decisions)
18
+ blocks = decision_blocks(decisions)
19
+ LoopNodes.defs(Prism.parse(source).value).to_h do |node|
20
+ [Decision.of(path, node), in_def(node, path, blocks)]
21
+ end
22
+ end
23
+
24
+ # { loop's name => [its bytes, its body's] } of each loop of
25
+ # node, a def, at path, that forks, blocks being where the
26
+ # block of each block method's decision ends.
27
+ def self.in_def(node, path, blocks)
28
+ loops = LoopNodes.loops_in(node.body)
29
+ loops.filter_map { fork(_1, path, blocks) }.to_h
30
+ end
31
+
32
+ # Where the block of each block method's decision among
33
+ # decisions ends, a byte offset, as its call does.
34
+ def self.decision_blocks(decisions)
35
+ by = decisions.values.group_by { BLOCK_PATHS.key?(_1.operator) }
36
+ by.fetch(true, []).to_set { _1.extent.end }
37
+ end
38
+
39
+ # [name, [its bytes, its body's]] of loop, at path, when it is
40
+ # no block method's, its call not ending at one of blocks, and
41
+ # a pass call can go first in its body; else nil.
42
+ def self.fork(loop, path, blocks)
43
+ body = LoopNodes.body_of(loop)
44
+ return if blocks.include?(loop.location.end_offset)
45
+ return if body.nil? || LoopNodes.modifier?(loop)
46
+ return unless LoopNodes.first_statement(body)
47
+
48
+ [Decision.at(path, loop.location),
49
+ [range(loop.location), range(body.location)]]
50
+ end
51
+
52
+ # The bytes of location, as a range.
53
+ def self.range(location)
54
+ location.start_offset...location.end_offset
55
+ end
56
+ end
57
+ end