path_coverage 0.5.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 (44) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +7 -134
  3. data/lib/path_coverage/call_trace.rb +26 -5
  4. data/lib/path_coverage/calls.rb +24 -0
  5. data/lib/path_coverage/configuration_limits.rb +1 -1
  6. data/lib/path_coverage/console_summary.rb +13 -13
  7. data/lib/path_coverage/fold_rows.rb +2 -2
  8. data/lib/path_coverage/gaps.rb +1 -1
  9. data/lib/path_coverage/json_reader.rb +3 -3
  10. data/lib/path_coverage/json_reports.rb +28 -21
  11. data/lib/path_coverage/json_writer.rb +4 -4
  12. data/lib/path_coverage/kernel.rb +41 -20
  13. data/lib/path_coverage/leaving.rb +14 -6
  14. data/lib/path_coverage/loop_forks.rb +1 -1
  15. data/lib/path_coverage/loop_nodes.rb +41 -5
  16. data/lib/path_coverage/method_loops.rb +56 -12
  17. data/lib/path_coverage/method_statements.rb +25 -12
  18. data/lib/path_coverage/method_ways.rb +48 -20
  19. data/lib/path_coverage/{card_context.rb → paths_context.rb} +16 -16
  20. data/lib/path_coverage/paths_fitting.rb +31 -0
  21. data/lib/path_coverage/paths_forks.rb +72 -0
  22. data/lib/path_coverage/{card_gaps.rb → paths_gaps.rb} +38 -26
  23. data/lib/path_coverage/paths_leaving.rb +40 -0
  24. data/lib/path_coverage/paths_rescues.rb +146 -0
  25. data/lib/path_coverage/{card_scopes.rb → paths_scopes.rb} +6 -6
  26. data/lib/path_coverage/{card_tree.rb → paths_tree.rb} +19 -15
  27. data/lib/path_coverage/{card_way_lists.rb → paths_way_lists.rb} +3 -3
  28. data/lib/path_coverage/paths_ways.rb +193 -0
  29. data/lib/path_coverage/recorder.rb +14 -0
  30. data/lib/path_coverage/render.rb +6 -6
  31. data/lib/path_coverage/rescue_forks.rb +174 -0
  32. data/lib/path_coverage/rewriter.rb +83 -14
  33. data/lib/path_coverage/source_report.rb +4 -4
  34. data/lib/path_coverage/stable_leaves.rb +3 -3
  35. data/lib/path_coverage/statement_arms.rb +23 -9
  36. data/lib/path_coverage/totals.rb +5 -5
  37. data/lib/path_coverage/version.rb +1 -1
  38. data/lib/path_coverage/way_algebras.rb +2 -2
  39. data/lib/path_coverage/ways_report.rb +8 -8
  40. data/lib/path_coverage/ways_totals.rb +7 -7
  41. data/lib/path_coverage/within_methods.rb +61 -61
  42. metadata +12 -8
  43. data/lib/path_coverage/card_forks.rb +0 -44
  44. data/lib/path_coverage/card_ways.rb +0 -196
@@ -1,10 +1,11 @@
1
1
  require_relative 'gaps'
2
+ require_relative 'method_ways'
2
3
 
3
4
  module PathCoverage
4
5
  # The terminal worklist of the untested paths through each
5
6
  # method: for each method, a heading, then each path no run
6
7
  # took, as the choices a test must make to take it.
7
- module CardGaps
8
+ module PathsGaps
8
9
  # The instruction under a method's untested paths.
9
10
  UNTESTED = 'Add tests that take each of these paths:'.freeze
10
11
  # The instruction under a method with more paths than
@@ -15,40 +16,40 @@ module PathCoverage
15
16
  DECLARED = 'Remove the # path: impossible comment that ' \
16
17
  'declares it:'.freeze
17
18
 
18
- # The lines of cards, { method's key => card }, as a JSON
19
+ # The lines of flows, { method's key => flow }, as a JSON
19
20
  # report has them, each choice's leaves named by their text in
20
21
  # texts, { decision => [text, ...] }; a decision whose parent,
21
22
  # in parents, { decision => its parent decision }, is stepped
22
23
  # on the same path is named there, and the parent left out. A
23
- # method's card listing no gaps, its ways more than max paths
24
+ # method's flow listing no gaps, its ways more than max paths
24
25
  # allows, is named with what to do, so it fails and never
25
- # reads as tested; a top card, run by no call, has no lines.
26
- def self.lines(cards, texts, parents)
27
- cards.flat_map do |key, card|
28
- next [] unless card.key?('taken')
26
+ # reads as tested; a top flow, run by no call, has no lines.
27
+ def self.lines(flows, texts, parents)
28
+ flows.flat_map do |key, flow|
29
+ next [] unless flow.key?('taken')
29
30
 
30
- card_lines(key, card, texts, parents)
31
+ method_lines(key, flow, texts, parents)
31
32
  end
32
33
  end
33
34
 
34
- # The lines of card, the method's at key, as lines gives them.
35
- def self.card_lines(key, card, texts, parents)
36
- where = "#{key.split(':')[..-2].join(':')} #{card['name']}()"
37
- return too_many(where, card) unless card.key?('gaps')
35
+ # The lines of flow, the method's at key, as lines gives them.
36
+ def self.method_lines(key, flow, texts, parents)
37
+ where = "#{key.split(':')[..-2].join(':')} #{flow['name']}()"
38
+ return too_many(where, flow) unless flow.key?('gaps')
38
39
 
39
- named = Choices.new(card, texts, parents)
40
- gaps = card['gaps']
41
- taken = card['impossible_taken']
40
+ named = Choices.new(flow, texts, parents)
41
+ gaps = flow['gaps']
42
+ taken = flow['impossible_taken']
42
43
  section("#{where} has #{paths(gaps.size, 'untested ')}.",
43
44
  UNTESTED, gaps, named) +
44
45
  section("#{where} took #{paths(taken.size, '')} declared " \
45
46
  'impossible.', DECLARED, taken, named)
46
47
  end
47
48
 
48
- # The lines of card, the method's at where, with more paths
49
+ # The lines of flow, the method's at where, with more paths
49
50
  # than --max-paths allows: how many, and what to do.
50
- def self.too_many(where, card)
51
- ["#{where} has #{card['ways']} paths, more than " \
51
+ def self.too_many(where, flow)
52
+ ["#{where} has #{flow['ways']} paths, more than " \
52
53
  '--max-paths allows.', TOO_MANY]
53
54
  end
54
55
 
@@ -69,12 +70,12 @@ module PathCoverage
69
70
  "#{count} #{kind}#{count == 1 ? 'path' : 'paths'}"
70
71
  end
71
72
 
72
- # The choices of a card's ways, as a test must make them.
73
+ # The choices of a flow's ways, as a test must make them.
73
74
  class Choices
74
- # The choices of card's ways, its rows naming each step's
75
- # decision, texts and parents as CardGaps.lines has them.
76
- def initialize(card, texts, parents)
77
- @decisions = card['rows'].to_h { [_1['at'], _1['decision']] }
75
+ # The choices of flow's ways, its rows naming each step's
76
+ # decision, texts and parents as PathsGaps.lines has them.
77
+ def initialize(flow, texts, parents)
78
+ @decisions = flow['rows'].to_h { [_1['at'], _1['decision']] }
78
79
  @texts = texts
79
80
  @parents = parents
80
81
  end
@@ -110,16 +111,27 @@ module PathCoverage
110
111
 
111
112
  private
112
113
 
113
- # One step's choice, after its line: a decision's leaves'
114
- # outcomes, or a loop's items.
114
+ # One step's choice, after its line. For a decision it is its
115
+ # leaves' outcomes. For a fork, a loop or a rescue, it is its
116
+ # path.
115
117
  def choice(step)
116
118
  line = step['at'].split(':').first
117
119
  decision = @decisions[step['at']]
118
120
  path = step['path']
119
- return "line #{line} loop: #{path.first}" if decision.nil?
121
+ return "line #{line} #{forked(path.first)}" if decision.nil?
120
122
 
121
123
  "line #{line} #{Gaps.steps(path, @texts[decision])}"
122
124
  end
125
+
126
+ # A fork's path, from its label, as MethodWays labels it. A
127
+ # label in FORKS is a loop's items. Any other is a rescue's:
128
+ # its body completing, or which of its rescues runs.
129
+ def forked(label)
130
+ return "loop: #{label}" if MethodWays::FORKS.include?(label)
131
+ return 'rescue: body completes' if label == 'completed'
132
+
133
+ "rescue: #{label} runs"
134
+ end
123
135
  end
124
136
  end
125
137
  end
@@ -0,0 +1,40 @@
1
+ module PathCoverage
2
+ # How ways leave a flow. PathsWays includes it. A return or a
3
+ # throw returns, and no rescue catches it. A raise or a fail
4
+ # raises, and a rescue can catch it. A next, a break or a retry
5
+ # ends its pass, and the way goes on after its loop.
6
+ module PathsLeaving
7
+ # Where the ways that leave by each word go, as an index into a
8
+ # fold's parts, [on, returned, raised, passed].
9
+ PARTS = { 'return' => 1, 'throw' => 1, 'raise' => 2, 'fail' => 2,
10
+ 'next' => 3, 'break' => 3, 'retry' => 3 }.freeze
11
+
12
+ # { path index => word } for the decision row keyed key. It
13
+ # holds each path whose arm leaves, with the word it leaves
14
+ # by, eg return or raise.
15
+ def leaving(key)
16
+ decision = @decisions.fetch(@rows[key].decision)
17
+ regions = PathsTree.regions_of(@source, key, decision)
18
+ regions.select(&:leaves).flat_map do |region|
19
+ region.on.map { [_1, region.leaves] }
20
+ end.to_h
21
+ end
22
+
23
+ # The parts of ways, [on, returned, raised, passed]. With word
24
+ # nil they are unchanged. Otherwise every way leaves, and those
25
+ # that went on leave by word.
26
+ def leaving_by((on, *out), word)
27
+ return [on, *out] if word.nil?
28
+
29
+ all = left(word, on).drop(1).zip(out).map { @ways.plus(*_1) }
30
+ [@ways.zero, *all]
31
+ end
32
+
33
+ # The parts holding ways, put where word sends them, as PARTS
34
+ # says. Every other part is empty.
35
+ def left(word, ways)
36
+ part = PARTS.fetch(word)
37
+ (0..3).map { |index| index == part ? ways : @ways.zero }
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,146 @@
1
+ module PathCoverage
2
+ # How a begin with a rescue forks its flow. PathsWays includes
3
+ # it. The begin is a scope. Its body either completes, or a
4
+ # rescue runs. A rescue can run after any prefix of a way through
5
+ # the body, because a raise can come from anywhere. A prefix can
6
+ # end at any row, including one inside an arm or a loop's body.
7
+ module PathsRescues
8
+ # { key => its rescues } of each rescue fork of rescues, as
9
+ # RescueForks gives them.
10
+ def self.counts(rescues)
11
+ rescues.to_h do |name, (count, *)|
12
+ [PathsTree.key_at(name), count]
13
+ end
14
+ end
15
+
16
+ # The regions of each rescue fork of rescues, as RescueForks
17
+ # gives them. Its body is run on its path 0. The body of its
18
+ # rescue numbered n is run on its path n.
19
+ def self.regions(rescues)
20
+ rescues.flat_map do |name, (_, _, body, clauses)|
21
+ owner = PathsTree.key_at(name)
22
+ bodies = [['body', body], *clauses.map { ['rescue', _1] }]
23
+ bodies.each_with_index.map do |(arm, range), path|
24
+ PathsTree::Region.new(owner:, range:, arm:, on: [path],
25
+ leaf: nil, leaves: nil)
26
+ end
27
+ end
28
+ end
29
+
30
+ # The parts of the rescue fork keyed key, [on, returned, raised,
31
+ # passed]. Its body's ways that go on complete, labelled [key,
32
+ # 0]. A prefix of the body, or a way that raises in it, then
33
+ # runs rescue n, labelled [key, n], then that rescue's rows. A
34
+ # raise written in the body is taken as caught there. Ways that
35
+ # return or pass still leave.
36
+ def rescue_pair(key)
37
+ on, returned, _, passed = body = run_on(key, 0)
38
+ completed = [@ways.times(on, @ways.unit([key, 0])), returned,
39
+ @ways.zero, passed]
40
+ ran = (1..@rescues.fetch(key)).map do |path|
41
+ prefix = rescue_prefix(key, body, path)
42
+ run_on(key, path).map { @ways.times(prefix, _1) }
43
+ end
44
+ added([completed, *ran])
45
+ end
46
+
47
+ # The ways up to and including the step of the rescue numbered
48
+ # path of the fork keyed key. They start with each prefix of
49
+ # its body, or with one of body, its parts, that goes through
50
+ # the whole of it or raises in it.
51
+ def rescue_prefix(key, (on, _, raised, _), path)
52
+ whole = @ways.plus(on, raised)
53
+ ended = @ways.plus(opened(running(key, 0)), whole)
54
+ @ways.times(ended, @ways.unit([key, path]))
55
+ end
56
+
57
+ # The parts of the rows the fork keyed key runs on its path at
58
+ # index path, one after another: its body on path 0, else the
59
+ # body of its rescue numbered path.
60
+ def run_on(key, path)
61
+ sequence(running(key, path).map { pair_of(_1) })
62
+ end
63
+
64
+ # The ways that stop partway through the rows keyed keys,
65
+ # before the last of them completes. Each goes on through the
66
+ # rows before one row, then stops before that row or inside
67
+ # it. outcomes is the parent's path's outcomes, or nil at the
68
+ # top. Only a row's paths that fit outcomes are taken.
69
+ def opened(keys, outcomes = nil)
70
+ on = @ways.one
71
+ keys.reduce(@ways.zero) do |cut, key|
72
+ stop = @ways.plus(@ways.one, inside(key, outcomes))
73
+ here = @ways.times(on, stop)
74
+ on = @ways.times(on, fitted_pair(key, outcomes)[0])
75
+ @ways.plus(cut, here)
76
+ end
77
+ end
78
+
79
+ # The ways that stop inside the row keyed key, after its own
80
+ # step. For a decision, each fitting path's step, then a stop
81
+ # partway through the rows its arm runs. For a loop, a stop
82
+ # partway through its body. A return or a raise has none,
83
+ # because it leaves.
84
+ def inside(key, outcomes)
85
+ return rescue_inside(key) if @rescues.key?(key)
86
+ return looped(key) unless @rows.key?(key)
87
+
88
+ decision = @rows[key].decision
89
+ return @ways.zero if decision.nil?
90
+
91
+ cuts = each_fitting(key, outcomes, @ways.zero) do |path|
92
+ inside_on(key, path)
93
+ end
94
+ cuts.reduce(@ways.zero) { |all, cut| @ways.plus(all, cut) }
95
+ end
96
+
97
+ # The ways that stop inside the decision row keyed key, on its
98
+ # path at index path. The path's step comes with the whole of
99
+ # the decisions in its condition. No cut divides those. Then
100
+ # comes a stop partway through the rows its arms run.
101
+ def inside_on(key, path)
102
+ outcomes = possible(@rows[key].decision)[path]
103
+ by = running(key, path).group_by { @places[_1]['leaf'].nil? }
104
+ whole = whole_on(key, path, outcomes, by.fetch(false, []))
105
+ @ways.times(whole, opened(by.fetch(true, []), outcomes))
106
+ end
107
+
108
+ # The ways through the decision row keyed key on its path at
109
+ # index path. They include the whole of leaves, the rows in its
110
+ # condition, one after another.
111
+ def whole_on(key, path, outcomes, leaves)
112
+ pairs = leaves.map { fitted_pair(_1, outcomes) }
113
+ @ways.times(own(key, path, outcomes), sequence(pairs)[0])
114
+ end
115
+
116
+ # The ways that stop inside the loop keyed key, partway through
117
+ # its body. When loops fork, they start with its step of at
118
+ # least one item.
119
+ def looped(key)
120
+ partway = opened(running(key, 1))
121
+ return partway unless @forking
122
+
123
+ @ways.times(@ways.unit([key, 1]), partway)
124
+ end
125
+
126
+ # The ways that stop inside the rescue fork keyed key. They stop
127
+ # partway through its body, or partway through the rows of one
128
+ # of its rescues, after that rescue's step.
129
+ def rescue_inside(key)
130
+ body = run_on(key, 0)
131
+ ran = (1..@rescues.fetch(key)).map do |path|
132
+ prefix = rescue_prefix(key, body, path)
133
+ @ways.times(prefix, opened(running(key, path)))
134
+ end
135
+ ran.reduce(opened(running(key, 0))) do |all, cut|
136
+ @ways.plus(all, cut)
137
+ end
138
+ end
139
+
140
+ # The parts of the row keyed key. outcomes is its parent's
141
+ # path's outcomes, or nil at the top.
142
+ def fitted_pair(key, outcomes)
143
+ outcomes.nil? ? pair_of(key) : child_pair(key, outcomes)
144
+ end
145
+ end
146
+ end
@@ -1,10 +1,10 @@
1
1
  require 'prism'
2
2
 
3
3
  module PathCoverage
4
- # The scope a top card is named for: the module or class its
5
- # lines are in, as a def names a method's card.
6
- module CardScopes
7
- # The kinds of node that are a top card's scope.
4
+ # The scope a top flow is named for: the module or class its
5
+ # lines are in, as a def names a method's flow.
6
+ module PathsScopes
7
+ # The kinds of node that are a top flow's scope.
8
8
  HOLDERS = [Prism::ModuleNode, Prism::ClassNode].freeze
9
9
 
10
10
  # A lambda giving the scope of a line of source, as scope
@@ -17,8 +17,8 @@ module PathCoverage
17
17
  # Example: line 2, inside module Shop on lines 1 to 9, gives
18
18
  # Shop.
19
19
  # The name of the innermost of holders, modules and classes,
20
- # whose lines hold line, a top card's scope, as a def is a
21
- # method card's; file for a line in none.
20
+ # whose lines hold line, a top flow's scope, as a def is a
21
+ # method flow's; file for a line in none.
22
22
  def self.scope(holders, line)
23
23
  holding = holders.select do |node|
24
24
  location = node.location
@@ -1,15 +1,16 @@
1
- require_relative 'card_forks'
1
+ require_relative 'paths_forks'
2
+ require_relative 'paths_rescues'
2
3
  require_relative 'manifest'
3
4
  require_relative 'method_statements'
4
5
  require_relative 'decision_paths'
5
6
  require_relative 'decision_tree'
6
7
 
7
8
  module PathCoverage
8
- # How the rows of each card nest: for each row, the decision it
9
+ # How the rows of each flow nest: for each row, the decision it
9
10
  # runs inside, the arm of that decision it is in, and the paths of
10
11
  # that decision on which it runs, so a reader can tell which paths
11
- # reach a row, and count the paths through a card as a whole.
12
- module CardTree
12
+ # reach a row, and count the paths through a flow as a whole.
13
+ module PathsTree
13
14
  # Part of a decision's code that a row can be inside: an arm of
14
15
  # statements, or a leaf; owner the key of the decision's row,
15
16
  # range its bytes, arm its arm's name, nil for a leaf, on
@@ -37,26 +38,29 @@ module PathCoverage
37
38
  # not known; each nil where it does not apply.
38
39
  def self.of(source, path, decisions = nil)
39
40
  decisions = Manifest.decisions(source, path) if decisions.nil?
40
- cards = MethodStatements.of(source, path, decisions)
41
- cards.transform_values { nested(source, decisions, _1) }
41
+ flows = MethodStatements.of(source, path, decisions)
42
+ flows.transform_values { nested(source, decisions, _1) }
42
43
  end
43
44
 
44
- # { "line:column" => its place } for each of rows, a card's,
45
+ # { "line:column" => its place } for each of rows, a flow's,
45
46
  # each placed in the innermost region of another row's
46
47
  # decision holding all of its own decision's code, or, for a
47
48
  # row that is no decision, where it starts: a decision
48
49
  # starting where its child does, eg an || whose first operand
49
50
  # is an any?, holds that child, and is not held by it. Each of
50
51
  # forks, { loop's name => [its bytes, its body's] }, as
51
- # LoopForks gives them, is placed too, as CardForks places it.
52
- def self.nested(source, decisions, rows, forks = {})
53
- regions = card_regions(source, decisions, rows) +
54
- CardForks.regions(forks)
52
+ # LoopForks gives them, is placed too. So is each of rescues,
53
+ # { fork's name => [its rescues, its bytes, its body's, [each
54
+ # rescue's body's]] }, as RescueForks gives them. scopes is
55
+ # [forks, rescues].
56
+ def self.nested(source, decisions, rows, scopes = [{}, {}])
57
+ regions = row_regions(source, decisions, rows) +
58
+ PathsForks.regions(*scopes)
55
59
  places = rows_placed(source, rows, [decisions, regions])
56
- CardForks.placed(places, forks, regions)
60
+ PathsForks.placed(places, scopes, regions)
57
61
  end
58
62
 
59
- # { "line:column" => its place } for each of rows, a card's,
63
+ # { "line:column" => its place } for each of rows, a flow's,
60
64
  # among regions, as nested places them.
61
65
  def self.rows_placed(source, rows, (decisions, regions))
62
66
  starts = line_starts(source)
@@ -69,9 +73,9 @@ module PathCoverage
69
73
  end
70
74
  end
71
75
 
72
- # The regions of the decisions of rows, a card's, decisions being
76
+ # The regions of the decisions of rows, a flow's, decisions being
73
77
  # { decision => Condition }.
74
- def self.card_regions(source, decisions, rows)
78
+ def self.row_regions(source, decisions, rows)
75
79
  rows.select(&:decision).flat_map do |row|
76
80
  regions_of(source, key_of(row), decisions.fetch(row.decision))
77
81
  end
@@ -2,10 +2,10 @@ require_relative 'method_ways'
2
2
  require_relative 'ways_report'
3
3
 
4
4
  module PathCoverage
5
- # The ways a method's card lists for the JSON report: those its
5
+ # The ways a method's flow lists for the JSON report: those its
6
6
  # calls took, its loops' passes, and of the ways one call can take,
7
7
  # those some call took, covered, and those none did, gaps.
8
- module CardWayLists
8
+ module PathsWayLists
9
9
  # { "taken", "passes" } of the method whose entry is named
10
10
  # entry: the ways its calls took, ways being { unit's name =>
11
11
  # { way => count } }, as a CallTrace counts them; and the passes
@@ -19,7 +19,7 @@ module PathCoverage
19
19
  # the method whose entry is named entry, ways as walked has
20
20
  # them; checked [split, forked]: split [possible, impossible],
21
21
  # the ways one call can take, and those none can, as
22
- # MethodWays.split_card lists them, forked the names of the
22
+ # MethodWays.split_flow lists them, forked the names of the
23
23
  # loops that fork there.
24
24
  def self.checked(entry, ways, ((possible, impossible), forked))
25
25
  seen = witnessed(ways.fetch(entry, {}), forked)
@@ -0,0 +1,193 @@
1
+ require_relative 'paths_fitting'
2
+ require_relative 'paths_leaving'
3
+ require_relative 'paths_rescues'
4
+ require_relative 'paths_tree'
5
+ require_relative 'manifest'
6
+ require_relative 'method_statements'
7
+ require_relative 'decision_paths'
8
+ require_relative 'stable_leaves'
9
+ require_relative 'way_algebras'
10
+
11
+ module PathCoverage
12
+ # The ways one call can go through each flow, folded over its
13
+ # tree, as PathsTree places its rows. Each row gives a pair,
14
+ # [ways that go on, ways that leave the method]: a return or a
15
+ # raise leaves, and so does each path of a decision taking an
16
+ # arm that returns or raises, eg return 0 if a. Rows one after
17
+ # another multiply, but only the ways that go on reach the next
18
+ # row; a decision adds up its paths, each with the rows it runs
19
+ # on that path one after another; and a row that is the whole of
20
+ # a leaf counts only those of its paths that give the leaf the
21
+ # outcome the path has, so no impossible way is counted where
22
+ # gives knows it. A block's body counts once, unless the block
23
+ # forks, as PathsForks folds it.
24
+ # Each way also notes the outcome it gives each leaf that
25
+ # StableLeaves finds, by its local's name, and two ways joined
26
+ # one after the other that give one name both outcomes are no
27
+ # way, as no call can take them.
28
+ # The fold is in one of WayAlgebras: Counted counts the ways,
29
+ # by default; Labelled gives the paths taken on any of them, and
30
+ # Listed lists them.
31
+ class PathsWays
32
+ include PathsFitting
33
+ include PathsForks
34
+ include PathsLeaving
35
+ include PathsRescues
36
+
37
+ # Example: { "k.rb:1:0" => 3 } for def f(a, b), return 0 if a,
38
+ # then a ?: of b: 1 way leaves, 2 go on.
39
+ # { def decision => its ways, in algebra } for each def in
40
+ # source, its decisions those of no settings.
41
+ def self.of(source, path, algebra = WayAlgebras::Counted)
42
+ decisions = Manifest.decisions(source, path)
43
+ flows = MethodStatements.of(source, path, decisions)
44
+ flows.transform_values do |rows|
45
+ flow(source, decisions, rows, algebra)
46
+ end
47
+ end
48
+
49
+ # The ways through a flow of rows in source, in algebra,
50
+ # decisions being { decision => Condition }, as
51
+ # Manifest.decisions gives them. folding is [stable, forks,
52
+ # rescues, forking]. With stable false, the ways include those
53
+ # no call can take, as no leaf is then assigned. Each loop of
54
+ # forks, as LoopForks gives them, is a scope that a next or a
55
+ # break ends a pass of. When forking, each loop is a fork too,
56
+ # labelled [its key, 0] for 0 items and [its key, 1] for 1+
57
+ # items, then its body's rows, walked once. Each rescue fork of
58
+ # rescues, a def's, forks the flow.
59
+ def self.flow(source, decisions, rows,
60
+ algebra = WayAlgebras::Counted,
61
+ folding = [true, {}, {}, false])
62
+ stable, forks, rescues, forking = folding
63
+ places = PathsTree.nested(source, decisions, rows,
64
+ [forks, rescues])
65
+ names = stable ? StableLeaves.of(source, decisions, rows) : {}
66
+ new(source, rows, places, decisions,
67
+ [algebra, names, rescues, forking]).total
68
+ end
69
+
70
+ # The fold of rows, a flow's in source, placed as places has
71
+ # them. Each decision row's Condition is in decisions. folding
72
+ # is [algebra, names, rescues, forking]. Its ways are in
73
+ # algebra, and each step of it tallies them, as
74
+ # WayAlgebras::Tallied does. names assigns leaves, as
75
+ # StableLeaves finds them. rescues are the def's rescue forks,
76
+ # as RescueForks gives them. forking says whether loops fork.
77
+ def initialize(source, rows, places, decisions,
78
+ (algebra, names, rescues, forking))
79
+ @source = source
80
+ @rows = rows.to_h { |row| [PathsTree.key_of(row), row] }
81
+ @places = places
82
+ @decisions = decisions
83
+ @ways = WayAlgebras::Tallied.new(algebra)
84
+ @children = places.keys.group_by { places[_1]['parent'] }
85
+ @names = names
86
+ @rescues = PathsRescues.counts(rescues)
87
+ @forking = forking
88
+ end
89
+
90
+ # The ways through the flow, in its algebra. Its top rows run
91
+ # one after another. Every way counts, whether it goes on or
92
+ # leaves, whatever its assignments.
93
+ def total
94
+ tops = @children.fetch(nil, []).map { |key| pair_of(key) }
95
+ all = sequence(tops)
96
+ @ways.total(all.reduce { |sum, ways| @ways.plus(sum, ways) })
97
+ end
98
+
99
+ # The parts of rows run one after another, from each row's
100
+ # parts. The ways that go on pass through every row. A way
101
+ # that leaves at one row has gone on through each row before
102
+ # it.
103
+ def sequence(many)
104
+ start = [@ways.one, *nothing.drop(1)]
105
+ many.reduce(start) do |(on, *out), (more, *leave)|
106
+ through = leave.map { @ways.times(on, _1) }
107
+ [@ways.times(on, more),
108
+ *out.zip(through).map { @ways.plus(*_1) }]
109
+ end
110
+ end
111
+
112
+ # The parts of the row keyed key. A decision adds up its
113
+ # paths' parts. Any other row is a statement that leaves, eg
114
+ # a return or a next, so it leaves by its word on its one path.
115
+ def pair_of(key)
116
+ return rescue_pair(key) if @rescues.key?(key)
117
+ return fork_pair(key) unless @rows.key?(key)
118
+
119
+ row = @rows[key]
120
+ return left(row.top, @ways.unit([key, 0])) if row.decision.nil?
121
+
122
+ many = possible(row.decision).each_index.map do |path|
123
+ pair_on(key, path)
124
+ end
125
+ added(many)
126
+ end
127
+
128
+ # The parts of the decision row keyed key on its path at index
129
+ # path. The path is taken, then the rows it runs one after
130
+ # another, each given that path's outcomes. When the path's arm
131
+ # leaves, every way of it leaves, by the arm's word.
132
+ def pair_on(key, path)
133
+ outcomes = possible(@rows[key].decision)[path]
134
+ many = running(key, path).map do |child|
135
+ child_pair(child, outcomes)
136
+ end
137
+ own = own(key, path, outcomes)
138
+ ways = sequence(many).map { @ways.times(own, _1) }
139
+ leaving_by(ways, leaving(key)[path])
140
+ end
141
+
142
+ # The tally of the one way that takes the decision row keyed
143
+ # key's path at index path, with outcomes, giving each name
144
+ # the outcome of its leaves on it; no way when it gives a name
145
+ # both outcomes, as eg a && b || a && c does with a true then
146
+ # false.
147
+ def own(key, path, outcomes)
148
+ named = named(key, outcomes)
149
+ assignment = named.to_h
150
+ return @ways.zero if assignment.size < named.size
151
+
152
+ @ways.unit([key, path], assignment)
153
+ end
154
+
155
+ # The distinct [name, outcome] of each leaf, of the decision of
156
+ # the row keyed key, that outcomes reaches and that has a name.
157
+ def named(key, outcomes)
158
+ leaves = @decisions.fetch(@rows[key].decision).leaves
159
+ leaves.zip(outcomes).filter_map do |range, outcome|
160
+ next if outcome.nil? || !@names.key?(range)
161
+
162
+ [@names[range], outcome]
163
+ end.uniq
164
+ end
165
+
166
+ # The keys of the rows the decision row keyed key runs on its path
167
+ # at index path, in the order they are written.
168
+ def running(key, path)
169
+ runs = @children.fetch(key, []).group_by do |child|
170
+ @places[child]['on'].include?(path)
171
+ end
172
+ runs.fetch(true, [])
173
+ end
174
+
175
+ # The parts of the row keyed child. outcomes is its parent's
176
+ # path's outcomes. A row that gives nothing keeps all its
177
+ # paths. Otherwise only the paths that fit outcomes count, as
178
+ # PathsFitting decides.
179
+ def child_pair(child, outcomes)
180
+ return pair_of(child) if @places[child]['gives'].nil?
181
+
182
+ many = each_fitting(child, outcomes, nothing) do |path|
183
+ pair_on(child, path)
184
+ end
185
+ added(many)
186
+ end
187
+
188
+ # The possible paths of the decision named decision.
189
+ def possible(decision)
190
+ DecisionPaths.possible(@decisions.fetch(decision))
191
+ end
192
+ end
193
+ end
@@ -52,6 +52,20 @@ module PathCoverage
52
52
  nil
53
53
  end
54
54
 
55
+ # Notes that the rescue fork named name completed, as a step
56
+ # of the call under way. Returns value unchanged.
57
+ def completed(name, value)
58
+ @trace.decided(name, ['completed'])
59
+ value
60
+ end
61
+
62
+ # Notes that the rescue numbered number of the rescue fork
63
+ # named name ran, as a step of the call under way.
64
+ def rescued(name, number)
65
+ @trace.decided(name, ["rescue #{number}"])
66
+ nil
67
+ end
68
+
55
69
  # The stack of decision's evaluations under way in the current
56
70
  # thread (fiber), innermost last, so another thread's never
57
71
  # mix with its.