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
@@ -45,7 +45,7 @@ module PathCoverage
45
45
  return if body.nil? || LoopNodes.modifier?(loop)
46
46
  return unless LoopNodes.first_statement(body)
47
47
 
48
- [Decision.at(path, loop.location),
48
+ [Decision.at(path, LoopNodes.at(loop)),
49
49
  [range(loop.location), range(body.location)]]
50
50
  end
51
51
 
@@ -1,7 +1,8 @@
1
1
  module PathCoverage
2
- # The loops in a method's Prism tree: its blocks, lambdas,
3
- # whiles, untils and fors, each run 0 or more times, and their
4
- # bodies, as MethodLoops and LoopForks take them.
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.
5
6
  module LoopNodes
6
7
  # The types of node that are loops written with a keyword,
7
8
  # their body their statements.
@@ -21,13 +22,48 @@ module PathCoverage
21
22
  def self.loops_in(node)
22
23
  return [] if node.nil? || node.type == :def_node
23
24
 
24
- own = LOOPS.include?(node.type) ? [node] : []
25
+ own = loop?(node) ? [node] : []
25
26
  own + node.compact_child_nodes.flat_map { loops_in(_1) }
26
27
  end
27
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
+
28
61
  # The body of loop: a block's or lambda's body, a keyword
29
- # loop's statements; nil for an empty one.
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.
30
64
  def self.body_of(loop)
65
+ return loop if loop.type == :begin_node
66
+
31
67
  KEYWORDS.include?(loop.type) ? loop.statements : loop.body
32
68
  end
33
69
 
@@ -1,10 +1,11 @@
1
1
  require 'prism'
2
- require_relative 'card_tree'
2
+ require_relative 'paths_tree'
3
3
  require_relative 'decision'
4
4
  require_relative 'loop_forks'
5
5
  require_relative 'loop_nodes'
6
6
  require_relative 'manifest'
7
7
  require_relative 'method_statements'
8
+ require_relative 'rescue_forks'
8
9
 
9
10
  module PathCoverage
10
11
  # The loops of each method: the blocks, lambdas, whiles, untils
@@ -14,19 +15,61 @@ module PathCoverage
14
15
  # first run's, as Walk's repeats have them.
15
16
  module MethodLoops
16
17
  # { def decision => { name => the loops holding it, outermost
17
- # first } } for each def in source, at path: each of its
18
- # rows, as MethodStatements gives them, and each loop holding
19
- # one or forking, as LoopForks finds them, by name, as Decision
20
- # names where it starts.
18
+ # first } } for each def in source, at path. It holds each of
19
+ # its rows, as MethodStatements gives them, and each of its
20
+ # rescue forks, as RescueForks gives them. It also holds each
21
+ # loop holding one of those, or forking, as LoopForks finds
22
+ # them. Each is by name, as Decision names where it starts.
21
23
  def self.of(source, path, decisions = nil)
22
24
  decisions ||= Manifest.decisions(source, path)
23
- rows = MethodStatements.of(source, path, decisions)
25
+ spots = spots_in(source, path, decisions)
24
26
  forks = LoopForks.of(source, path, decisions)
25
- starts = CardTree.line_starts(source)
27
+ starts = PathsTree.line_starts(source)
26
28
  LoopNodes.defs(Prism.parse(source).value).to_h do |node|
27
29
  decision = Decision.of(path, node)
28
30
  loops = [named(node, path), forks[decision].keys]
29
- [decision, holding(rows[decision], loops, starts)]
31
+ [decision, holding(spots.fetch(decision), loops, starts)]
32
+ end
33
+ end
34
+
35
+ # A spot a loop can hold, named where it is, like a row.
36
+ Spot = Data.define(:name, :line, :column)
37
+
38
+ # The lines of the defs in source whose body's rescue retries.
39
+ # A retry there restarts the method's frame, so Ruby reports a
40
+ # call of it again, with no return of the first.
41
+ def self.restarts(source)
42
+ defs = LoopNodes.defs(Prism.parse(source).value)
43
+ defs.filter_map do |node|
44
+ node.location.start_line if restarting?(node.body)
45
+ end.to_set
46
+ end
47
+
48
+ # True for body, a def's, when a retry restarts it: a begin of
49
+ # the def's own whose rescue retries. An empty body is nil.
50
+ def self.restarting?(body)
51
+ return false if body.nil?
52
+
53
+ LoopNodes.implicit?(body) && LoopNodes.loop?(body)
54
+ end
55
+
56
+ # { def decision => its spots } for each def in source, at
57
+ # path. Its spots are its rows, as MethodStatements gives them,
58
+ # and the Spot of each of its rescue forks.
59
+ def self.spots_in(source, path, decisions)
60
+ rows = MethodStatements.of(source, path, decisions)
61
+ rescues = RescueForks.of(source, path)
62
+ rows.to_h do |decision, held|
63
+ [decision, held + spots_of(rescues.fetch(decision))]
64
+ end
65
+ end
66
+
67
+ # The Spot of each rescue fork of forks, a def's, as
68
+ # RescueForks gives them.
69
+ def self.spots_of(forks)
70
+ forks.keys.map do |name|
71
+ line, column = name.split(':').last(2).map(&:to_i)
72
+ Spot.new(name:, line:, column:)
30
73
  end
31
74
  end
32
75
 
@@ -44,7 +87,7 @@ module PathCoverage
44
87
  # [name, offset] of node, a loop, where its pass call goes,
45
88
  # when it is among held and its body has a statement; else nil.
46
89
  def self.pass_at(node, path, held)
47
- name = Decision.at(path, node.location)
90
+ name = Decision.at(path, LoopNodes.at(node))
48
91
  return unless held.include?(name) && !LoopNodes.modifier?(node)
49
92
 
50
93
  first = LoopNodes.first_statement(LoopNodes.body_of(node))
@@ -59,7 +102,7 @@ module PathCoverage
59
102
  LoopNodes.loops_in(node.body).filter_map do |loop|
60
103
  keyword = LoopNodes::KEYWORDS.include?(loop.type)
61
104
  held = keyword ? loop.statements : loop
62
- [Decision.at(path, loop.location), held.location] if held
105
+ [Decision.at(path, LoopNodes.at(loop)), held.location] if held
63
106
  end
64
107
  end
65
108
 
@@ -69,7 +112,7 @@ module PathCoverage
69
112
  # no step, as Walk's repeats have them.
70
113
  def self.repeats(source, path, decisions = nil)
71
114
  rows = MethodStatements.of(source, path, decisions)
72
- starts = CardTree.line_starts(source)
115
+ starts = PathsTree.line_starts(source)
73
116
  LoopNodes.defs(Prism.parse(source).value).to_h do |node|
74
117
  decision = Decision.of(path, node)
75
118
  tests = tests_in(node.body, path)
@@ -92,7 +135,8 @@ module PathCoverage
92
135
  LoopNodes.loops_in(node).filter_map do |loop|
93
136
  next unless %i[while_node until_node].include?(loop.type)
94
137
 
95
- [Decision.at(path, loop.location), loop.predicate.location]
138
+ name = Decision.at(path, LoopNodes.at(loop))
139
+ [name, loop.predicate.location]
96
140
  end
97
141
  end
98
142
 
@@ -10,7 +10,7 @@ require_relative 'statement_arms'
10
10
  require_relative 'totals'
11
11
 
12
12
  module PathCoverage
13
- # One row of a method's card, a top-level statement or a
13
+ # One row of a method's flow, a top-level statement or a
14
14
  # decision at its top: where it starts; its top, what it is as
15
15
  # written, eg = for a write; how many paths it has; the name
16
16
  # of its decision, nil when it is no decision; and its own name, by
@@ -19,7 +19,7 @@ module PathCoverage
19
19
  :name)
20
20
 
21
21
  # Finds, without running it, the top-level statements of each
22
- # method in source, one row each in its method's card.
22
+ # method in source, one row each in its method's flow.
23
23
  module MethodStatements
24
24
  # Each function below takes decisions, { decision =>
25
25
  # Condition }, the decisions the run has in source, as
@@ -33,13 +33,13 @@ module PathCoverage
33
33
  end
34
34
 
35
35
  # { def decision => its name } for each def in source, eg
36
- # { "k.rb:4:2" => :ship }, each its card's title.
36
+ # { "k.rb:4:2" => :ship }, each its flow's title.
37
37
  def self.names(source, path, decisions = nil)
38
38
  collect(source, path, decisions).names
39
39
  end
40
40
 
41
41
  # { def decision => its last line } for each def in source, eg
42
- # { "k.rb:4:2" => 6 }, where its card ends.
42
+ # { "k.rb:4:2" => 6 }, where its flow ends.
43
43
  def self.lasts(source, path, decisions = nil)
44
44
  collect(source, path, decisions).lasts
45
45
  end
@@ -53,6 +53,16 @@ module PathCoverage
53
53
  collect(source, path, decisions).reaches
54
54
  end
55
55
 
56
+ # Example: { "k.rb:1:0" => 14 } for a def whose body starts at
57
+ # byte 14.
58
+ # Where each def in source is entered on every call, by the
59
+ # def's own name, so its calls can be counted there. Each is a
60
+ # byte offset. For an endless def it is its body's byte range,
61
+ # to be wrapped.
62
+ def self.entries(source, path, decisions = nil)
63
+ collect(source, path, decisions).entries
64
+ end
65
+
56
66
  # A Collector that has visited each def in source, with
57
67
  # decisions, or, for nil, those of no settings.
58
68
  def self.collect(source, path, decisions)
@@ -65,7 +75,7 @@ module PathCoverage
65
75
  # Example: RATE = x ? 2 : 1 at the top level, its then
66
76
  # taken, gives [1, 2].
67
77
  # [taken, possible] of the paths of the decisions in source on
68
- # no method's card, outside every def, as top_rows gives
78
+ # no method's flow, outside every def, as top_rows gives
69
79
  # them; paths as Result#paths gives them.
70
80
  def self.top_level(source, path, paths, decisions = nil)
71
81
  names = top_rows(source, path, decisions).map(&:decision)
@@ -74,13 +84,13 @@ module PathCoverage
74
84
 
75
85
  # Example: RATE = x ? 2 : 1 gives a ?: row at 1:9.
76
86
  # A Statement row for each decision in source on no method's
77
- # card, outside every def, for the card of its own lines, in
87
+ # flow, outside every def, for the flow of its own lines, in
78
88
  # the order written: where decision mode names it, its
79
89
  # operator, an elsif's elsif, and its paths.
80
90
  def self.top_rows(source, path, decisions = nil)
81
91
  collector = collect(source, path, decisions)
82
- carded = collector.methods.values.flatten.map(&:decision)
83
- loose = collector.decisions.except(*carded)
92
+ in_methods = collector.methods.values.flatten.map(&:decision)
93
+ loose = collector.decisions.except(*in_methods)
84
94
  rows = loose.map do |name, decision|
85
95
  top_row(source, name, decision)
86
96
  end
@@ -152,6 +162,7 @@ module PathCoverage
152
162
  @names = {}
153
163
  @lasts = {}
154
164
  @reaches = {}
165
+ @entries = {}
155
166
  super()
156
167
  end
157
168
 
@@ -159,8 +170,10 @@ module PathCoverage
159
170
  # names and last lines, { def decision => name } and
160
171
  # { def decision => line }; where each row that is no
161
172
  # decision is reached, as MethodStatements.reaches has it;
162
- # and the decisions it was given, { decision => Condition }.
163
- attr_reader :methods, :names, :lasts, :reaches, :decisions
173
+ # where each def is entered, as MethodStatements.entries has
174
+ # it; and the decisions it was given, { decision => Condition }.
175
+ attr_reader :methods, :names, :lasts, :reaches, :entries,
176
+ :decisions
164
177
 
165
178
  # Notes node's name, its last line, its body statements as
166
179
  # its rows, those in its decisions' arms too, but only those
@@ -178,11 +191,11 @@ module PathCoverage
178
191
 
179
192
  # Notes the name and last line of node, the def at decision, and
180
193
  # where it is entered, reached under decision's own name, so its
181
- # card can say how many times it was called.
194
+ # flow can say how many times it was called.
182
195
  def note_def(decision, node)
183
196
  @names[decision] = node.name
184
197
  @lasts[decision] = node.location.end_line
185
- @reaches[decision] = entry_at(node)
198
+ @entries[decision] = entry_at(node)
186
199
  end
187
200
 
188
201
  # Notes where each of tops that is no decision, a return or a
@@ -1,10 +1,11 @@
1
1
  require 'prism'
2
- require_relative 'card_tree'
3
- require_relative 'card_ways'
2
+ require_relative 'paths_tree'
3
+ require_relative 'paths_ways'
4
4
  require_relative 'decision_paths'
5
5
  require_relative 'manifest'
6
6
  require_relative 'loop_forks'
7
7
  require_relative 'method_statements'
8
+ require_relative 'rescue_forks'
8
9
  require_relative 'way_algebras'
9
10
 
10
11
  module PathCoverage
@@ -26,20 +27,24 @@ module PathCoverage
26
27
  # loops, as LoopForks finds them.
27
28
  def self.split(source, path, loops: false)
28
29
  decisions = Manifest.decisions(source, path)
29
- cards = MethodStatements.of(source, path, decisions)
30
+ flows = MethodStatements.of(source, path, decisions)
30
31
  forks = LoopForks.of(source, path, decisions)
31
- cards.to_h do |key, rows|
32
- forked = loops ? forks.fetch(key) : {}
33
- [key, split_card(source, decisions, rows, forked)]
32
+ rescues = RescueForks.of(source, path)
33
+ flows.to_h do |key, rows|
34
+ scopes = [forks.fetch(key), rescues.fetch(key), loops]
35
+ [key, split_flow(source, decisions, rows, scopes)]
34
36
  end
35
37
  end
36
38
 
37
- # [possible, impossible] of the card of rows in source, as
38
- # split gives them, each loop of forks a fork; a way giving no
39
+ # [possible, impossible] of the flow of rows in source, as
40
+ # split gives them. scopes is [forks, rescues, forking]. Each
41
+ # loop of forks, as LoopForks gives a def's, is a scope, and a
42
+ # fork when forking. Each rescue fork of rescues, as
43
+ # RescueForks gives a def's, forks the flow. A way giving no
39
44
  # items to a loop declared never empty is impossible.
40
- def self.split_card(source, decisions, rows, forks = {})
45
+ def self.split_flow(source, decisions, rows, scopes)
41
46
  possible, all = [true, false].map do |stable|
42
- listed(source, decisions, rows, [stable, forks])
47
+ listed(source, decisions, rows, [stable, *scopes])
43
48
  end
44
49
  lines = never_empty(source)
45
50
  by = possible.group_by { empties?(_1, lines) }
@@ -71,22 +76,45 @@ module PathCoverage
71
76
  # The steps of a loop's fork, by its path: no pass, or some.
72
77
  FORKS = ['0 items', '1+ items'].freeze
73
78
 
74
- # The ways through the card of rows, as steps, those no call
75
- # can take too unless stable, each loop of forks a fork.
76
- def self.listed(source, decisions, rows, (stable, forks))
77
- ways = CardWays.card(source, decisions, rows,
78
- WayAlgebras::Listed, [stable, forks])
79
- steps = rows.to_h { [CardTree.key_of(_1), _1] }
80
- loops = forks.keys.to_h { [CardTree.key_at(_1), _1] }
79
+ # The ways through the flow of rows, as steps. Each way's steps
80
+ # are in the order their rows are written. folding is [stable,
81
+ # forks, rescues, forking]. Unless stable, the ways include
82
+ # those no call can take. Each loop in forks is a scope, and
83
+ # also a fork when forking. Each rescue in rescues is a fork.
84
+ def self.listed(source, decisions, rows, folding)
85
+ ways = PathsWays.flow(source, decisions, rows,
86
+ WayAlgebras::Listed, folding)
87
+ steps = rows.to_h { [PathsTree.key_of(_1), _1] }
88
+ named = fork_names(folding)
81
89
  ways.map do |way|
82
- way.map do |key, index|
83
- next [loops[key], [FORKS[index]]] if loops.key?(key)
90
+ ordered(way.map do |key, index|
91
+ next fork_step(named[key], index) if named.key?(key)
84
92
 
85
93
  step(steps, decisions, key, index)
86
- end
94
+ end)
87
95
  end
88
96
  end
89
97
 
98
+ # { key => [kind, name] } of each fork, by key. folding is
99
+ # [stable, forks, rescues, forking]. Each loop of its forks is
100
+ # of kind :loop. Each fork of its rescues is of kind :rescue.
101
+ def self.fork_names((_, forks, rescues))
102
+ loops = forks.keys.to_h { [PathsTree.key_at(_1), [:loop, _1]] }
103
+ loops.merge(rescues.keys.to_h do |name|
104
+ [PathsTree.key_at(name), [:rescue, name]]
105
+ end)
106
+ end
107
+
108
+ # The step of the fork of kind named name on its path at index.
109
+ # A loop's path is no item or some. A rescue fork's path is
110
+ # completed, its body running to its end, or the number of the
111
+ # rescue that ran.
112
+ def self.fork_step((kind, name), index)
113
+ return [name, [FORKS[index]]] if kind == :loop
114
+
115
+ [name, [index.zero? ? 'completed' : "rescue #{index}"]]
116
+ end
117
+
90
118
  # The ways, as of lists them, that way, a call's, as its trace
91
119
  # records it, witnesses: each pass of a loop inlined, one way
92
120
  # for each distinct way its passes took, a loop with no pass
@@ -1,34 +1,34 @@
1
- require_relative 'card_tree'
2
- require_relative 'card_ways'
1
+ require_relative 'paths_tree'
2
+ require_relative 'paths_ways'
3
3
  require_relative 'method_statements'
4
4
  require_relative 'way_algebras'
5
5
 
6
6
  module PathCoverage
7
- # The paths of each def's card rows that its method can take:
8
- # those on some way through the card, as CardWays folds it,
7
+ # The paths of each def's flow rows that its method can take:
8
+ # those on some way through the flow, as PathsWays folds it,
9
9
  # and any taken that it rules out, which are counted, never
10
10
  # dropped, and reported. Paths combined count these, where paths
11
11
  # alone count each decision's paths on its own.
12
12
  # paths is as Result#paths gives it, and reached as
13
13
  # Recorder#reached does, nil when not known.
14
- module CardContext
14
+ module PathsContext
15
15
  # Example: { "k.rb:1:0" => [2, 3] } for ship, its first row's
16
16
  # 2 paths both taken, its second's 1 path, of 2, that its
17
17
  # method can take, not taken.
18
- # Each def's card total in source, [taken, size], its rows'
18
+ # Each def's flow total in source, [taken, size], its rows'
19
19
  # taken as MethodStatements.taken gives them, a row not known
20
20
  # counting as not taken, and its size their paths as rows
21
21
  # gives them.
22
22
  def self.totals(source, path, paths, reached, decisions)
23
- cards = MethodStatements.of(source, path, decisions)
24
- cards.transform_values do |rows|
23
+ flows = MethodStatements.of(source, path, decisions)
24
+ flows.transform_values do |rows|
25
25
  shown = rows(source, decisions, rows, paths, reached)
26
26
  seen = shown.filter_map(&:last).sum
27
27
  [seen, MethodStatements.size(shown.map { _1.first.paths })]
28
28
  end
29
29
  end
30
30
 
31
- # [row, taken] for each of rows, a card's in source: row with
31
+ # [row, taken] for each of rows, a flow's in source: row with
32
32
  # as its paths how many of them its method can take or took,
33
33
  # as of gives them, and taken as MethodStatements.taken gives
34
34
  # it.
@@ -43,15 +43,15 @@ module PathCoverage
43
43
  # Example: [[[1], [1]], [[0, 1], [0]]] for return 0 if a, then
44
44
  # a ?: of b, called once with a false and b true.
45
45
  # [the indices of its paths its method can take, those taken]
46
- # for each of rows, a card's in source: those on some way
47
- # through the card, as WayAlgebras::Labelled folds them; and
46
+ # for each of rows, a flow's in source: those on some way
47
+ # through the flow, as WayAlgebras::Labelled folds them; and
48
48
  # those taken, a return's or a raise's one path index 0.
49
49
  def self.of(source, decisions, rows, paths, reached)
50
50
  labelled = WayAlgebras::Labelled
51
- labels = CardWays.card(source, decisions, rows, labelled)
51
+ labels = PathsWays.flow(source, decisions, rows, labelled)
52
52
  by_key = labels.group_by(&:first)
53
53
  rows.map do |row|
54
- can = by_key.fetch(CardTree.key_of(row), []).map(&:last)
54
+ can = by_key.fetch(PathsTree.key_of(row), []).map(&:last)
55
55
  [can, taken_paths(row, paths, reached)]
56
56
  end
57
57
  end
@@ -77,14 +77,14 @@ module PathCoverage
77
77
  files.flat_map do |full_path, parsed|
78
78
  source, decisions = parsed.values_at(:source, :decisions)
79
79
  path = config.relative(full_path)
80
- cards = MethodStatements.of(source, path, decisions)
81
- cards.values.flat_map do |rows|
80
+ flows = MethodStatements.of(source, path, decisions)
81
+ flows.values.flat_map do |rows|
82
82
  ruled_in(source, decisions, rows, paths, reached)
83
83
  end
84
84
  end
85
85
  end
86
86
 
87
- # [name, path] for each path of rows, a card's in source, taken
87
+ # [name, path] for each path of rows, a flow's in source, taken
88
88
  # though its method rules it out: a decision's name and the
89
89
  # path, as paths has it; a return's or a raise's and nil.
90
90
  def self.ruled_in(source, decisions, rows, paths, reached)
@@ -0,0 +1,31 @@
1
+ module PathCoverage
2
+ # Which paths of a decision row fit the path its parent takes.
3
+ # PathsWays includes it. A row that is the whole of its parent's
4
+ # leaf runs only the paths that give that leaf the outcome its
5
+ # parent's path has.
6
+ module PathsFitting
7
+ # The block's value on each path of the decision row keyed
8
+ # key, by index. A path that does not fit outcomes gets none
9
+ # instead. fits_of decides which paths fit.
10
+ def each_fitting(key, outcomes, none)
11
+ fits = fits_of(key, outcomes)
12
+ possible(@rows[key].decision).each_index.map do |path|
13
+ fits.call(path) ? yield(path) : none
14
+ end
15
+ end
16
+
17
+ # A test of whether a path index of the decision row keyed key
18
+ # fits outcomes, its parent's path's. At the top outcomes is
19
+ # nil, and every path fits. Every path fits a row that gives
20
+ # nothing too. Otherwise a path fits when it gives its leaf the
21
+ # outcome the parent's path has, or gives what is not known.
22
+ def fits_of(key, outcomes)
23
+ place = @places[key]
24
+ gives = place['gives']
25
+ return ->(_) { true } if outcomes.nil? || gives.nil?
26
+
27
+ fits = [nil, outcomes[place['leaf']]]
28
+ ->(path) { fits.include?(gives[path]) }
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,72 @@
1
+ module PathCoverage
2
+ # The loops of a flow. forks is { loop's name => [its bytes, its
3
+ # body's] }, as LoopForks gives them. Each loop is a scope: a
4
+ # next or a break in its body ends a pass, and the way goes on
5
+ # after the loop. When loops are decisions, each loop is also a
6
+ # fork. Its path 0 is no item. Its path 1 is at least one item,
7
+ # and runs its body, which holds its rows. A loop is placed by
8
+ # its bytes, as a row that is no decision is. The module
9
+ # functions place loops, for PathsTree. The instance methods fold
10
+ # them, for PathsWays, which includes this module.
11
+ module PathsForks
12
+ # The region of each loop of forks, its body, run on its path
13
+ # 1. Then the regions of each rescue fork of rescues, as
14
+ # PathsRescues gives them.
15
+ def self.regions(forks, rescues)
16
+ 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)
20
+ end + PathsRescues.regions(rescues)
21
+ end
22
+
23
+ # places, { key => its place }, a flow's rows', with the place
24
+ # of each scope too, among regions. The scopes are each loop of
25
+ # forks and each rescue fork of rescues. All are in the order
26
+ # they start. With no scope it is places alone.
27
+ def self.placed(places, (forks, rescues), regions)
28
+ spans = forks.transform_values(&:first)
29
+ .merge(rescues.transform_values { _1[1] })
30
+ return places if spans.empty?
31
+
32
+ scopes = spans.to_h do |name, span|
33
+ key = PathsTree.key_at(name)
34
+ [key, PathsTree.placed(key, span, regions, nil)]
35
+ end
36
+ merged = places.merge(scopes)
37
+ merged.sort_by { _1[0].split(':').map(&:to_i) }.to_h
38
+ end
39
+
40
+ # The parts of the loop keyed key, which is placed but is no
41
+ # row. Its body's rows run one after another, walked once. A
42
+ # pass ended by a next or a break goes on after the loop. When
43
+ # loops fork, the loop either makes no pass, or makes at least
44
+ # one and runs its body.
45
+ def fork_pair(key)
46
+ body = rejoined(sequence(running(key, 1).map { pair_of(_1) }))
47
+ return body unless @forking
48
+
49
+ own = @ways.unit([key, 1])
50
+ some = body.map { @ways.times(own, _1) }
51
+ added([[@ways.unit([key, 0]), *nothing.drop(1)], some])
52
+ end
53
+
54
+ # The parts of ways through a loop's body, [on, returned, raised,
55
+ # passed], once its scope ends. The ways that ended a pass by a
56
+ # next or a break join those that go on.
57
+ def rejoined((on, returned, raised, passed))
58
+ [@ways.plus(on, passed), returned, raised, @ways.zero]
59
+ end
60
+
61
+ # many, each a fold's parts, added up part by part. No parts
62
+ # add up to no ways.
63
+ def added(many)
64
+ many.reduce(nothing) do |sum, parts|
65
+ sum.zip(parts).map { @ways.plus(*_1) }
66
+ end
67
+ end
68
+
69
+ # The parts, [on, returned, raised, passed], of no ways.
70
+ def nothing = Array.new(4) { @ways.zero }
71
+ end
72
+ end