path_coverage 0.7.2 → 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 (61) 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 +24 -6
  6. data/lib/path_coverage/current_test.rb +28 -0
  7. data/lib/path_coverage/decision_tree.rb +11 -21
  8. data/lib/path_coverage/evaluated.rb +23 -0
  9. data/lib/path_coverage/fold_rows.rb +7 -8
  10. data/lib/path_coverage/gaps.rb +4 -4
  11. data/lib/path_coverage/json_reader.rb +22 -17
  12. data/lib/path_coverage/json_reports.rb +5 -3
  13. data/lib/path_coverage/json_writer.rb +12 -8
  14. data/lib/path_coverage/kernel.rb +11 -3
  15. data/lib/path_coverage/kinds/base.rb +9 -0
  16. data/lib/path_coverage/kinds/block.rb +7 -1
  17. data/lib/path_coverage/kinds/case.rb +63 -20
  18. data/lib/path_coverage/kinds/chain.rb +13 -3
  19. data/lib/path_coverage/kinds/choice.rb +5 -0
  20. data/lib/path_coverage/kinds/contains.rb +6 -0
  21. data/lib/path_coverage/kinds/safe.rb +3 -3
  22. data/lib/path_coverage/kinds/write.rb +3 -3
  23. data/lib/path_coverage/kinds.rb +14 -0
  24. data/lib/path_coverage/loop_nodes.rb +5 -3
  25. data/lib/path_coverage/manifest.rb +57 -33
  26. data/lib/path_coverage/manifest_case.rb +43 -26
  27. data/lib/path_coverage/manifest_folding.rb +20 -0
  28. data/lib/path_coverage/merge.rb +8 -8
  29. data/lib/path_coverage/method_statements.rb +5 -4
  30. data/lib/path_coverage/minitest.rb +7 -0
  31. data/lib/path_coverage/minitest_tests.rb +25 -0
  32. data/lib/path_coverage/paths_context.rb +2 -2
  33. data/lib/path_coverage/paths_forks.rb +6 -3
  34. data/lib/path_coverage/paths_leaving.rb +3 -1
  35. data/lib/path_coverage/paths_regions.rb +78 -0
  36. data/lib/path_coverage/paths_rescues.rb +5 -2
  37. data/lib/path_coverage/paths_scopes.rb +2 -1
  38. data/lib/path_coverage/paths_tree.rb +10 -68
  39. data/lib/path_coverage/paths_way_lists.rb +11 -10
  40. data/lib/path_coverage/recorder.rb +28 -47
  41. data/lib/path_coverage/recorder_blocks.rb +3 -3
  42. data/lib/path_coverage/recorder_cases.rb +70 -17
  43. data/lib/path_coverage/recorder_chains.rb +2 -2
  44. data/lib/path_coverage/render.rb +7 -3
  45. data/lib/path_coverage/report_tallies.rb +79 -0
  46. data/lib/path_coverage/report_text.rb +23 -0
  47. data/lib/path_coverage/rescue_forks.rb +3 -0
  48. data/lib/path_coverage/result.rb +17 -16
  49. data/lib/path_coverage/rewriter.rb +42 -114
  50. data/lib/path_coverage/rewriter_marks.rb +108 -0
  51. data/lib/path_coverage/source_report.rb +5 -6
  52. data/lib/path_coverage/stable_leaves.rb +6 -26
  53. data/lib/path_coverage/statement_arms.rb +18 -5
  54. data/lib/path_coverage/tally.rb +129 -0
  55. data/lib/path_coverage/testing.rb +13 -0
  56. data/lib/path_coverage/totals.rb +3 -3
  57. data/lib/path_coverage/version.rb +1 -1
  58. data/lib/path_coverage/walk.rb +4 -3
  59. data/lib/path_coverage/ways_report.rb +11 -10
  60. data/lib/path_coverage/within_methods.rb +12 -3
  61. metadata +11 -1
@@ -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,4 +1,5 @@
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'
@@ -142,14 +143,14 @@ module PathCoverage
142
143
  end
143
144
  end
144
145
 
145
- # 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.
146
147
  def self.taken_decision(row, paths)
147
- counts = paths.fetch(row.decision).map { |each| each[:count] }
148
- counts.count(&:positive?)
148
+ paths.fetch(row.decision).count { |each| !each[:tests].empty? }
149
149
  end
150
150
 
151
151
  # Collects each def's top-level statements.
152
152
  class Collector < Prism::Visitor
153
+ include Evaluated::Visitor
153
154
  include ManifestChains
154
155
  include ManifestChoices
155
156
  include StatementArms
@@ -279,7 +280,7 @@ module PathCoverage
279
280
  return [node] if decision?(node)
280
281
  return [] if UNSEEN.include?(node.type)
281
282
 
282
- node.compact_child_nodes.flat_map do |child|
283
+ Evaluated.children(node).flat_map do |child|
283
284
  decisions_in(child)
284
285
  end
285
286
  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
 
@@ -1,4 +1,5 @@
1
1
  require_relative 'method_ways'
2
+ require_relative 'tally'
2
3
  require_relative 'ways_report'
3
4
 
4
5
  module PathCoverage
@@ -49,23 +50,23 @@ module PathCoverage
49
50
  end
50
51
  end
51
52
 
52
- # { way => how many calls witnessed it } of walked, { way =>
53
- # count }, each way's steps ordered, as MethodWays.witnessed
54
- # gives them, each loop of forked a fork.
53
+ # { way => Tally of the calls that witnessed it } of walked,
54
+ # { way => Tally }, each way's steps ordered, as
55
+ # MethodWays.witnessed gives them, each loop of forked a fork.
55
56
  def self.witnessed(walked, forked)
56
- walked.each_with_object(Hash.new(0)) do |(way, count), seen|
57
- MethodWays.witnessed(way, forked).each { seen[_1] += count }
57
+ walked.each_with_object(Tally.per_key) do |(way, tests), found|
58
+ MethodWays.witnessed(way, forked).each { found[_1] += tests }
58
59
  end
59
60
  end
60
61
 
61
- # [{ "way", "count" }] of each of possible some call took, as
62
- # seen counts them.
62
+ # [{ "way", "tests" }] of each of possible some call took, as
63
+ # seen tallies them.
63
64
  def self.covered(possible, seen)
64
65
  possible.filter_map do |way|
65
- count = seen.fetch(MethodWays.matched(way), 0)
66
- next unless count.positive?
66
+ tests = seen.fetch(MethodWays.matched(way)) { Tally.new }
67
+ next if tests.empty?
67
68
 
68
- { 'way' => WaysReport.way(way), 'count' => count }
69
+ { 'way' => WaysReport.way(way), 'tests' => tests }
69
70
  end
70
71
  end
71
72
  end
@@ -1,5 +1,6 @@
1
1
  require_relative 'call_trace'
2
2
  require_relative 'condition'
3
+ require_relative 'current_test'
3
4
  require_relative 'recorder_blocks'
4
5
  require_relative 'recorder_cases'
5
6
  require_relative 'recorder_chains'
@@ -14,26 +15,28 @@ module PathCoverage
14
15
  include RecorderWrites
15
16
 
16
17
  # One evaluation's way through its decision: the outcome of
17
- # each leaf, nil for one not yet, or never, reached; and
18
- # whether it witnesses no path, as when a block method's
19
- # block was never called.
20
- Trail = Struct.new(:decision, :outcomes, :unwitnessed)
18
+ # each leaf, nil for one not yet, or never, reached; whether it
19
+ # witnesses no path, as when a block method's block was never
20
+ # called; and the index of a case pattern reached whose
21
+ # outcome is not yet known, nil for none.
22
+ Trail = Struct.new(:decision, :outcomes, :unwitnessed, :pending)
21
23
 
22
24
  # Starts with no outcomes, decision paths or reaches counted.
25
+ # Every count is a Tally, by the test under way as it happens.
23
26
  def initialize
24
27
  @counts = Hash.new do |counts, key|
25
- counts[key] = { truthy: 0, falsy: 0 }
28
+ counts[key] = { truthy: Tally.new, falsy: Tally.new }
26
29
  end
27
30
  @decision_paths = Hash.new do |decision_paths, decision|
28
- decision_paths[decision] = Hash.new(0)
31
+ decision_paths[decision] = Tally.per_key
29
32
  end
30
- @reached = Hash.new(0)
33
+ @reached = Tally.per_key
31
34
  @trails_key = :"path_coverage_trails_#{object_id}"
32
35
  @trace = CallTrace.new
33
36
  end
34
37
 
35
- # { name => runs } of each statement reached so far, by its
36
- # name, eg { "k.rb:2:2" => 2 }; none never reached.
38
+ # { name => Tally } of the runs of each statement reached so
39
+ # far, by its name, eg "k.rb:2:2"; none never reached.
37
40
  attr_reader :reached
38
41
 
39
42
  # The ways each call of a covered method has taken so far.
@@ -42,7 +45,7 @@ module PathCoverage
42
45
  # Counts a run of the statement named name, a step of its
43
46
  # call's way.
44
47
  def reach(name)
45
- @reached[name] += 1
48
+ @reached[name].add(CurrentTest.id)
46
49
  @trace.reach(name)
47
50
  nil
48
51
  end
@@ -95,11 +98,14 @@ module PathCoverage
95
98
  # leaves, took trail's later outcomes, so trail's path is
96
99
  # not counted; the stack is cleared down to trail either
97
100
  # way. An unwitnessed trail's path is not counted either.
98
- # A counted path is a step of its call's way.
101
+ # A counted path is a step of its call's way. A trail already
102
+ # left, as a case is when its arm is taken, is left once only.
99
103
  def leave(trail, value)
100
104
  stack = trails(trail.decision)
105
+ return value unless stack.include?(trail)
106
+
101
107
  clean = stack.last.equal?(trail)
102
- nil until stack.empty? || stack.pop.equal?(trail)
108
+ nil until stack.pop.equal?(trail)
103
109
  decided(trail) if clean && !trail.unwitnessed
104
110
  value
105
111
  end
@@ -107,55 +113,30 @@ module PathCoverage
107
113
  # Counts trail's path, its decision's, and steps its call's
108
114
  # way with it.
109
115
  def decided(trail)
110
- @decision_paths[trail.decision][trail.outcomes] += 1
116
+ @decision_paths[trail.decision][trail.outcomes]
117
+ .add(CurrentTest.id)
111
118
  @trace.decided(trail.decision, trail.outcomes)
112
119
  end
113
120
 
114
- # The truthy and falsy counts of every operand recorded so
121
+ # The truthy and falsy Tallies of every operand recorded so
115
122
  # far, keyed by [decision, index]; an operand that never ran
116
123
  # has no entry.
117
124
  attr_reader :counts
118
125
 
126
+ # Counts one more outcome, :truthy or :falsy, of the operand
127
+ # keyed key, [decision, index], under the test under way.
128
+ def count(key, outcome)
129
+ @counts[key][outcome].add(CurrentTest.id)
130
+ end
131
+
119
132
  # Counts value's outcome and returns it unchanged, so the
120
133
  # condition around it behaves exactly as uninstrumented.
121
134
  def record(decision, index, value)
122
135
  outcome = value ? :truthy : :falsy
123
- @counts[[decision, index]][outcome] += 1
136
+ count([decision, index], outcome)
124
137
  trail = trails(decision).last
125
138
  trail.outcomes[index] = outcome == :truthy if trail
126
139
  value
127
140
  end
128
-
129
- # A stand-in for a case's index-th pattern of total: the
130
- # case matches its subject against the stand-in, which
131
- # matches it against the pattern, once, as the case would
132
- # have.
133
- WhenMatch = Struct.new(
134
- :recorder, :decision, :index, :total, :pattern
135
- ) do
136
- # Whether pattern matches subject, exactly as pattern says,
137
- # noted on the decision's path.
138
- def ===(subject)
139
- recorder.matched(decision, index, total, pattern === subject)
140
- end
141
- end
142
-
143
- # The stand-in for pattern, the index-th of size in the case
144
- # at decision.
145
- def when_match(decision, index, size, pattern)
146
- WhenMatch.new(self, decision, index, size, pattern)
147
- end
148
-
149
- # Notes whether the index-th of size patterns matched, the
150
- # first entering the case's decision, and a match or the last
151
- # leaving it, as no more patterns are then tried; returns
152
- # matched unchanged.
153
- def matched(decision, index, size, matched)
154
- enter(decision, size) if index.zero?
155
- record(decision, index, matched)
156
- last = matched || index == size - 1
157
- leave(trails(decision).last, matched) if last
158
- matched
159
- end
160
141
  end
161
142
  end
@@ -6,7 +6,7 @@ module PathCoverage
6
6
  # after the receiver's, at; at is 0 for a call that is a decision
7
7
  # of its own. The receiver's leaf stays nil, as how many items
8
8
  # the block was called with is a loop's question. It relies on
9
- # its includer's counts, record and trails.
9
+ # its includer's count, record and trails.
10
10
  module RecorderBlocks
11
11
  # Starts a block method's call in decision, its receiver the
12
12
  # decision's at-th leaf; returns the decision's current trail,
@@ -31,7 +31,7 @@ module PathCoverage
31
31
  def answer_at(decision, at, value)
32
32
  trail = trails(decision).last
33
33
  note_answer(trail, at, value) if trail
34
- @counts[[decision, at + 1]][value ? :truthy : :falsy] += 1
34
+ count([decision, at + 1], value ? :truthy : :falsy)
35
35
  value
36
36
  end
37
37
 
@@ -52,7 +52,7 @@ module PathCoverage
52
52
  def end_items(decision, at, trail, value)
53
53
  called = trail.nil? || !trail.outcomes[at + 1].nil?
54
54
  trail.unwitnessed = true unless called
55
- @counts[[decision, at]][called ? :truthy : :falsy] += 1
55
+ count([decision, at], called ? :truthy : :falsy)
56
56
  value
57
57
  end
58
58
  end