path_coverage 0.5.0 → 0.7.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 (48) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +33 -153
  3. data/lib/path_coverage/call_trace.rb +26 -5
  4. data/lib/path_coverage/calls.rb +24 -0
  5. data/lib/path_coverage/condition.rb +10 -19
  6. data/lib/path_coverage/configuration_limits.rb +1 -1
  7. data/lib/path_coverage/console_summary.rb +13 -13
  8. data/lib/path_coverage/fold_cells.rb +0 -1
  9. data/lib/path_coverage/fold_rows.rb +2 -2
  10. data/lib/path_coverage/gaps.rb +1 -1
  11. data/lib/path_coverage/json_reader.rb +3 -3
  12. data/lib/path_coverage/json_reports.rb +28 -21
  13. data/lib/path_coverage/json_writer.rb +4 -4
  14. data/lib/path_coverage/kernel.rb +41 -20
  15. data/lib/path_coverage/kinds/block.rb +37 -45
  16. data/lib/path_coverage/leaving.rb +14 -6
  17. data/lib/path_coverage/loop_forks.rb +12 -26
  18. data/lib/path_coverage/loop_nodes.rb +41 -5
  19. data/lib/path_coverage/method_loops.rb +57 -13
  20. data/lib/path_coverage/method_statements.rb +25 -12
  21. data/lib/path_coverage/method_ways.rb +49 -21
  22. data/lib/path_coverage/{card_context.rb → paths_context.rb} +16 -16
  23. data/lib/path_coverage/paths_fitting.rb +31 -0
  24. data/lib/path_coverage/paths_forks.rb +72 -0
  25. data/lib/path_coverage/{card_gaps.rb → paths_gaps.rb} +38 -26
  26. data/lib/path_coverage/paths_leaving.rb +40 -0
  27. data/lib/path_coverage/paths_rescues.rb +146 -0
  28. data/lib/path_coverage/{card_scopes.rb → paths_scopes.rb} +6 -6
  29. data/lib/path_coverage/{card_tree.rb → paths_tree.rb} +20 -16
  30. data/lib/path_coverage/{card_way_lists.rb → paths_way_lists.rb} +3 -3
  31. data/lib/path_coverage/paths_ways.rb +193 -0
  32. data/lib/path_coverage/recorder.rb +21 -4
  33. data/lib/path_coverage/recorder_blocks.rb +21 -30
  34. data/lib/path_coverage/render.rb +6 -6
  35. data/lib/path_coverage/rescue_forks.rb +174 -0
  36. data/lib/path_coverage/rewriter.rb +83 -14
  37. data/lib/path_coverage/source_report.rb +4 -4
  38. data/lib/path_coverage/stable_leaves.rb +3 -3
  39. data/lib/path_coverage/statement_arms.rb +23 -9
  40. data/lib/path_coverage/totals.rb +5 -5
  41. data/lib/path_coverage/version.rb +1 -1
  42. data/lib/path_coverage/way_algebras.rb +2 -2
  43. data/lib/path_coverage/ways_report.rb +8 -8
  44. data/lib/path_coverage/ways_totals.rb +7 -7
  45. data/lib/path_coverage/within_methods.rb +62 -62
  46. metadata +12 -8
  47. data/lib/path_coverage/card_forks.rb +0 -44
  48. data/lib/path_coverage/card_ways.rb +0 -196
@@ -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
- 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)]
30
+ flows = MethodStatements.of(source, path, decisions)
31
+ forks = LoopForks.of(source, path)
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
@@ -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