path_coverage 0.4.0 → 0.6.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 (108) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +260 -300
  3. data/exe/path_coverage_report +4 -0
  4. data/lib/path_coverage/call_trace.rb +109 -0
  5. data/lib/path_coverage/calls.rb +73 -43
  6. data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +35 -33
  7. data/lib/path_coverage/condition.rb +14 -14
  8. data/lib/path_coverage/configuration.rb +7 -4
  9. data/lib/path_coverage/configuration_limits.rb +52 -0
  10. data/lib/path_coverage/configuration_paths.rb +7 -7
  11. data/lib/path_coverage/console_summary.rb +28 -26
  12. data/lib/path_coverage/{site.rb → decision.rb} +2 -2
  13. data/lib/path_coverage/decision_locations.rb +76 -0
  14. data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +13 -11
  15. data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
  16. data/lib/path_coverage/fold_branches.rb +18 -0
  17. data/lib/path_coverage/fold_cells.rb +42 -0
  18. data/lib/path_coverage/{html_tree.rb → fold_rows.rb} +43 -72
  19. data/lib/path_coverage/gaps.rb +33 -15
  20. data/lib/path_coverage/json_reader.rb +54 -19
  21. data/lib/path_coverage/json_reports.rb +109 -0
  22. data/lib/path_coverage/json_writer.rb +29 -14
  23. data/lib/path_coverage/kernel.rb +77 -58
  24. data/lib/path_coverage/kinds/base.rb +16 -16
  25. data/lib/path_coverage/kinds/block.rb +16 -15
  26. data/lib/path_coverage/kinds/case.rb +14 -12
  27. data/lib/path_coverage/kinds/chain.rb +40 -11
  28. data/lib/path_coverage/kinds/choice.rb +4 -3
  29. data/lib/path_coverage/kinds/contains.rb +40 -32
  30. data/lib/path_coverage/kinds/safe.rb +38 -26
  31. data/lib/path_coverage/kinds/write.rb +6 -6
  32. data/lib/path_coverage/kinds.rb +2 -2
  33. data/lib/path_coverage/leaving.rb +52 -0
  34. data/lib/path_coverage/loop_forks.rb +57 -0
  35. data/lib/path_coverage/loop_nodes.rb +91 -0
  36. data/lib/path_coverage/manifest.rb +92 -58
  37. data/lib/path_coverage/manifest_blocks.rb +5 -26
  38. data/lib/path_coverage/manifest_case.rb +2 -2
  39. data/lib/path_coverage/manifest_chains.rb +8 -8
  40. data/lib/path_coverage/manifest_choices.rb +23 -20
  41. data/lib/path_coverage/manifest_disabled.rb +97 -0
  42. data/lib/path_coverage/manifest_folding.rb +43 -40
  43. data/lib/path_coverage/manifest_ignoring.rb +6 -6
  44. data/lib/path_coverage/manifest_writes.rb +11 -10
  45. data/lib/path_coverage/measured_by.rb +20 -0
  46. data/lib/path_coverage/merge.rb +7 -7
  47. data/lib/path_coverage/method_loops.rb +172 -0
  48. data/lib/path_coverage/method_statements.rb +150 -134
  49. data/lib/path_coverage/method_ways.rb +170 -0
  50. data/lib/path_coverage/paths_context.rb +104 -0
  51. data/lib/path_coverage/paths_fitting.rb +31 -0
  52. data/lib/path_coverage/paths_forks.rb +72 -0
  53. data/lib/path_coverage/paths_gaps.rb +137 -0
  54. data/lib/path_coverage/paths_leaving.rb +40 -0
  55. data/lib/path_coverage/paths_report.rb +16 -16
  56. data/lib/path_coverage/paths_rescues.rb +146 -0
  57. data/lib/path_coverage/paths_scopes.rb +37 -0
  58. data/lib/path_coverage/paths_tree.rb +203 -0
  59. data/lib/path_coverage/paths_way_lists.rb +72 -0
  60. data/lib/path_coverage/paths_ways.rb +193 -0
  61. data/lib/path_coverage/recorder.rb +71 -36
  62. data/lib/path_coverage/recorder_blocks.rb +17 -17
  63. data/lib/path_coverage/recorder_cases.rb +12 -11
  64. data/lib/path_coverage/recorder_chains.rb +12 -12
  65. data/lib/path_coverage/recorder_writes.rb +9 -9
  66. data/lib/path_coverage/render.rb +58 -51
  67. data/lib/path_coverage/rescue_forks.rb +174 -0
  68. data/lib/path_coverage/result.rb +32 -31
  69. data/lib/path_coverage/rewriter.rb +116 -35
  70. data/lib/path_coverage/rewriter_nested.rb +13 -13
  71. data/lib/path_coverage/runs_report.rb +84 -0
  72. data/lib/path_coverage/source_lines.rb +16 -16
  73. data/lib/path_coverage/source_report.rb +100 -0
  74. data/lib/path_coverage/stable_leaves.rb +162 -0
  75. data/lib/path_coverage/statement_arms.rb +53 -38
  76. data/lib/path_coverage/summary.rb +4 -62
  77. data/lib/path_coverage/totals.rb +12 -10
  78. data/lib/path_coverage/version.rb +1 -1
  79. data/lib/path_coverage/walk.rb +106 -0
  80. data/lib/path_coverage/way_algebras.rb +127 -0
  81. data/lib/path_coverage/ways_report.rb +91 -0
  82. data/lib/path_coverage/ways_totals.rb +46 -0
  83. data/lib/path_coverage/within_methods.rb +152 -97
  84. metadata +39 -30
  85. data/lib/path_coverage/card.rb +0 -14
  86. data/lib/path_coverage/card_tree.rb +0 -186
  87. data/lib/path_coverage/card_ways.rb +0 -140
  88. data/lib/path_coverage/html/file.html +0 -33
  89. data/lib/path_coverage/html/index.html +0 -29
  90. data/lib/path_coverage/html/settings.js +0 -50
  91. data/lib/path_coverage/html/side.js +0 -599
  92. data/lib/path_coverage/html/sort.js +0 -34
  93. data/lib/path_coverage/html/style.css +0 -484
  94. data/lib/path_coverage/html/tabs.js +0 -15
  95. data/lib/path_coverage/html/theme.js +0 -27
  96. data/lib/path_coverage/html_branch.rb +0 -30
  97. data/lib/path_coverage/html_cards.rb +0 -88
  98. data/lib/path_coverage/html_cells.rb +0 -71
  99. data/lib/path_coverage/html_file.rb +0 -123
  100. data/lib/path_coverage/html_index.rb +0 -128
  101. data/lib/path_coverage/html_index_head.rb +0 -29
  102. data/lib/path_coverage/html_labels.rb +0 -16
  103. data/lib/path_coverage/html_methods.rb +0 -66
  104. data/lib/path_coverage/html_nav.rb +0 -67
  105. data/lib/path_coverage/html_report.rb +0 -131
  106. data/lib/path_coverage/html_statements.rb +0 -186
  107. data/lib/path_coverage/html_tabs.rb +0 -26
  108. data/lib/path_coverage/html_template.rb +0 -26
@@ -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
@@ -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|
@@ -52,20 +52,29 @@ module PathCoverage
52
52
  end
53
53
  end
54
54
 
55
- # The inserts for a &. chain site: each receiver in a present
56
- # call, and the whole chain in a leave call that enters it
57
- # first. Eg user&.account becomes, in effect,
58
- # leave(enter(site, 1), present(site, 0, 0, user)&.account).
59
- 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)
60
61
  leaves = condition.leaves
61
- enter = "PathCoverage.enter(#{site.inspect}, #{leaves.size})"
62
+ name = decision.inspect
63
+ enter = "PathCoverage.enter(#{name}, #{leaves.size})"
62
64
  call = "PathCoverage.leave(#{enter}, "
63
- command = condition.commands.include?(condition.extent)
64
- presents = leaves.each_with_index.flat_map do |range, index|
65
- Rewriter.wrap(present(site, index, 0), range, false, :leaf)
66
- end
67
65
  extent = condition.extent
68
- 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
69
78
  end
70
79
 
71
80
  # Its leaves need present and chained calls.
@@ -74,30 +83,32 @@ module PathCoverage
74
83
  end
75
84
 
76
85
  # { leaf range => the opening of its call } for the leaves of
77
- # chain nested in a site whose leaves are leaves: a
86
+ # chain nested in a decision whose leaves are leaves: a
78
87
  # receiver's present call, and the chain's value's chained
79
88
  # call, each told where its chain's leaves start.
80
- def self.calls(site, chain, leaves)
89
+ def self.calls(decision, chain, leaves)
81
90
  first = leaves.index(chain.operands.first)
82
91
  *receivers, value = chain.operands
83
92
  calls = receivers.to_h do |range|
84
- [range, present(site, leaves.index(range), first)]
93
+ [range, present(decision, leaves.index(range), first)]
85
94
  end
86
95
  at = leaves.index(value)
87
- calls.merge(value => chained(site, at, first))
96
+ calls.merge(value => chained(decision, at, first))
88
97
  end
89
98
 
90
99
  # The opening of the chained call around the index-th leaf
91
- # 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
92
101
  # first.
93
- def self.chained(site, index, first)
94
- "PathCoverage.chained(#{site.inspect}, #{index}, #{first}, "
102
+ def self.chained(decision, index, first)
103
+ "PathCoverage.chained(#{decision.inspect}, #{index}, " \
104
+ "#{first}, "
95
105
  end
96
106
 
97
107
  # The opening of the present call around the index-th leaf
98
- # of site, its chain's leaves starting at first.
99
- def self.present(site, index, first)
100
- "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}, "
101
112
  end
102
113
 
103
114
  # Its leaves' nodes, each after the first showing only what
@@ -130,8 +141,9 @@ module PathCoverage
130
141
  # is reached only when the one before is not nil.
131
142
  def self.rows(chain, indent)
132
143
  chain[:operands].each_with_index.map do |operand, depth|
133
- branch = "#{indent}#{HtmlBranch::GAP * depth}#{HtmlBranch::ELL}"
134
- HtmlTree.operand_row(operand, branch)
144
+ gaps = FoldBranches::GAP * depth
145
+ branch = "#{indent}#{gaps}#{FoldBranches::ELL}"
146
+ FoldRows.operand_row(operand, branch)
135
147
  end
136
148
  end
137
149
  end
@@ -22,21 +22,21 @@ module PathCoverage
22
22
 
23
23
  # { target range => nil }: its target is never read, so
24
24
  # never wrapped.
25
- def self.calls(_site, inner, _leaves)
25
+ def self.calls(_decision, inner, _leaves)
26
26
  { inner.operands.first => nil }
27
27
  end
28
28
 
29
- # 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
30
30
  # in a written_at call, which notes the target's outcome when
31
31
  # it runs, and the whole of it in a settle call, which notes
32
32
  # the other when it does not. A right side among commands,
33
33
  # eg a command call, is parenthesised. Eg x ||= v becomes,
34
- # in effect, settle(site, at, true, x ||= written_at(site,
35
- # at, false, v)).
36
- 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)
37
37
  _target, arm = inner.operands
38
38
  ran = THEN_ON[inner.operator]
39
- name = site.inspect
39
+ name = decision.inspect
40
40
  written = "PathCoverage.written_at(#{name}, #{at}, #{ran}, "
41
41
  settle = "PathCoverage.settle(#{name}, #{at}, #{!ran}, "
42
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,52 @@
1
+ require_relative 'condition'
2
+
3
+ module PathCoverage
4
+ # Whether code leaves where it stands, the method, or, for a next
5
+ # or a break, its pass or its loop, decided once, from Prism's
6
+ # nodes, for every part of the gem that asks.
7
+ module Leaving
8
+ # The methods whose call, with no receiver, leaves the method:
9
+ # raise, fail, its other name, and throw, for its catch.
10
+ CALLS = %i[raise fail throw].freeze
11
+
12
+ # The word a statement leaves by, or nil when it leaves
13
+ # nothing. A return, a next, a break and a retry give their
14
+ # keyword. A next, a break or a retry leaves only its pass or
15
+ # its loop. A call of one of CALLS with no receiver gives the
16
+ # method's name, eg 'fail'.
17
+ def self.word(statement)
18
+ return WORDS[statement.type] if WORDS.key?(statement.type)
19
+
20
+ statement.name.to_s if leaving_call?(statement)
21
+ end
22
+
23
+ # The words of the statements that leave by their keyword, by
24
+ # their node's type.
25
+ WORDS = { return_node: 'return', next_node: 'next',
26
+ break_node: 'break', retry_node: 'retry' }.freeze
27
+
28
+ # The arm a statement that leaves the method is as an operand,
29
+ # eg return t in x or return t: run when the operand before it
30
+ # gives the outcome its operator goes on with, never a value,
31
+ # so never a leaf.
32
+ def self.arm(statement)
33
+ loc = statement.location
34
+ Arm.new(kind: :then, extent: loc.start_offset...loc.end_offset,
35
+ body: nil, leaves: word(statement))
36
+ end
37
+
38
+ # The word of the first of an arm's statements that leaves the
39
+ # method, statements its code; nil for an arm with no
40
+ # statements or none that leaves.
41
+ def self.of(statements)
42
+ statements && statements.body.map { word(_1) }.compact.first
43
+ end
44
+
45
+ # True when statement is a call of one of CALLS with no
46
+ # receiver.
47
+ def self.leaving_call?(statement)
48
+ statement.type == :call_node && statement.receiver.nil? &&
49
+ CALLS.include?(statement.name)
50
+ end
51
+ end
52
+ 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, LoopNodes.at(loop)),
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
@@ -0,0 +1,91 @@
1
+ module PathCoverage
2
+ # The loops in a method's Prism tree, as MethodLoops and
3
+ # LoopForks take them. Its blocks, lambdas, whiles, untils and
4
+ # fors each run 0 or more times. So does each begin whose rescue
5
+ # retries, each attempt a pass.
6
+ module LoopNodes
7
+ # The types of node that are loops written with a keyword,
8
+ # their body their statements.
9
+ KEYWORDS = %i[while_node until_node for_node].freeze
10
+
11
+ # The types of node that are loops.
12
+ LOOPS = [:block_node, :lambda_node, *KEYWORDS].freeze
13
+
14
+ # Every def under node, outermost first.
15
+ def self.defs(node)
16
+ found = node.type == :def_node ? [node] : []
17
+ found + node.compact_child_nodes.flat_map { defs(_1) }
18
+ end
19
+
20
+ # Every loop under node, in the order they start, but none
21
+ # inside a def under it, whose loops are its own.
22
+ def self.loops_in(node)
23
+ return [] if node.nil? || node.type == :def_node
24
+
25
+ own = loop?(node) ? [node] : []
26
+ own + node.compact_child_nodes.flat_map { loops_in(_1) }
27
+ end
28
+
29
+ # True for node when it is a loop: one of LOOPS, or a begin
30
+ # whose rescue retries.
31
+ def self.loop?(node)
32
+ return true if LOOPS.include?(node.type)
33
+
34
+ node.type == :begin_node && retries?(node.rescue_clause)
35
+ end
36
+
37
+ # True when node, a rescue, or what it holds, retries. A begin
38
+ # inside it with a rescue of its own takes its own retries.
39
+ def self.retries?(node)
40
+ return false if node.nil?
41
+ return true if node.type == :retry_node
42
+ return false if node.type == :begin_node && node.rescue_clause
43
+
44
+ node.compact_child_nodes.any? { retries?(_1) }
45
+ end
46
+
47
+ # The location a loop is named by. A begin that starts at its
48
+ # def, as a def's body with a rescue does, is named by its end,
49
+ # the def's end. Its start is the def's own name, and no row
50
+ # starts at an end. Any other loop is named by its whole.
51
+ def self.at(loop)
52
+ implicit?(loop) ? loop.end_keyword_loc : loop.location
53
+ end
54
+
55
+ # True for a begin with no begin keyword, which starts at its
56
+ # def.
57
+ def self.implicit?(loop)
58
+ loop.type == :begin_node && loop.begin_keyword_loc.nil?
59
+ end
60
+
61
+ # The body of loop: a block's or lambda's body, a keyword
62
+ # loop's statements; nil for an empty one. A begin is its own
63
+ # body, its rescues included, as a retry runs it again.
64
+ def self.body_of(loop)
65
+ return loop if loop.type == :begin_node
66
+
67
+ KEYWORDS.include?(loop.type) ? loop.statements : loop.body
68
+ end
69
+
70
+ # True for a keyword loop written after its body, eg x += 1
71
+ # while x < 9, where a call put first in its body would run
72
+ # once, not each pass, so none goes there.
73
+ def self.modifier?(loop)
74
+ return false unless KEYWORDS.include?(loop.type)
75
+
76
+ body = loop.statements.location
77
+ loop.keyword_loc.start_offset > body.start_offset
78
+ end
79
+
80
+ # The first statement of a loop's body, a do ... rescue ... end
81
+ # block's being its first before the rescue; nil when it has
82
+ # none there, its statements all in its rescue, which run only
83
+ # once a statement raises, so there is no pass to note.
84
+ def self.first_statement(body)
85
+ return body.body.first unless body.type == :begin_node
86
+ return if body.statements.nil?
87
+
88
+ body.statements.body.first
89
+ end
90
+ end
91
+ end