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,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
@@ -147,7 +151,7 @@ module PathCoverage
147
151
  skipped = possible.each_index.group_by do |i|
148
152
  possible[i][index].nil?
149
153
  end
150
- on = skipped.fetch(false)
154
+ on = skipped.fetch(false, [])
151
155
  Region.new(owner:, range:, arm: nil, on:, leaf: index,
152
156
  leaves: nil)
153
157
  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
@@ -14,8 +14,10 @@ module PathCoverage
14
14
  include RecorderWrites
15
15
 
16
16
  # One evaluation's way through its decision: the outcome of
17
- # each leaf, nil for one not yet, or never, reached.
18
- Trail = Struct.new(:decision, :outcomes)
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)
19
21
 
20
22
  # Starts with no outcomes, decision paths or reaches counted.
21
23
  def initialize
@@ -52,6 +54,20 @@ module PathCoverage
52
54
  nil
53
55
  end
54
56
 
57
+ # Notes that the rescue fork named name completed, as a step
58
+ # of the call under way. Returns value unchanged.
59
+ def completed(name, value)
60
+ @trace.decided(name, ['completed'])
61
+ value
62
+ end
63
+
64
+ # Notes that the rescue numbered number of the rescue fork
65
+ # named name ran, as a step of the call under way.
66
+ def rescued(name, number)
67
+ @trace.decided(name, ["rescue #{number}"])
68
+ nil
69
+ end
70
+
55
71
  # The stack of decision's evaluations under way in the current
56
72
  # thread (fiber), innermost last, so another thread's never
57
73
  # mix with its.
@@ -78,12 +94,13 @@ module PathCoverage
78
94
  # decision still entered above trail, as one that raised
79
95
  # leaves, took trail's later outcomes, so trail's path is
80
96
  # not counted; the stack is cleared down to trail either
81
- # way. A counted path is a step of its call's way.
97
+ # way. An unwitnessed trail's path is not counted either.
98
+ # A counted path is a step of its call's way.
82
99
  def leave(trail, value)
83
100
  stack = trails(trail.decision)
84
101
  clean = stack.last.equal?(trail)
85
102
  nil until stack.empty? || stack.pop.equal?(trail)
86
- decided(trail) if clean
103
+ decided(trail) if clean && !trail.unwitnessed
87
104
  value
88
105
  end
89
106
 
@@ -2,28 +2,24 @@ require_relative 'condition'
2
2
 
3
3
  module PathCoverage
4
4
  # The part of the recorder that notes block method calls, eg
5
- # ids.any? { |id| id.odd? }: how many items the block was called
6
- # with, as the receiver's leaf, at, and its answers, as the next
7
- # leaf; at is 0 for a call that is a decision of its own. It relies
8
- # on its includer's counts, record and trails.
5
+ # ids.any? { |id| id.odd? }: its block's answers, as the leaf
6
+ # after the receiver's, at; at is 0 for a call that is a decision
7
+ # of its own. The receiver's leaf stays nil, as how many items
8
+ # the block was called with is a loop's question. It relies on
9
+ # its includer's counts, record and trails.
9
10
  module RecorderBlocks
10
11
  # Starts a block method's call in decision, its receiver the
11
- # decision's at-th leaf, its block not yet called with any item;
12
- # returns the decision's current trail, nil for none.
13
- def begin_items(decision, at)
14
- trail = trails(decision).last
15
- trail.outcomes[at] = ITEMS[0] if trail
16
- trail
12
+ # decision's at-th leaf; returns the decision's current trail,
13
+ # nil for none.
14
+ def begin_items(decision, _at)
15
+ trails(decision).last
17
16
  end
18
17
 
19
- # Notes value, the block's answer, as its last, and one more
20
- # item it was called with, for a call whose receiver is
21
- # decision's at-th leaf; returns value unchanged. A block called
22
- # where its call was not entered, eg in another fiber, is
23
- # counted but noted on no path.
18
+ # Notes value, the block's answer, as its last, for a call
19
+ # whose receiver is decision's at-th leaf; returns value
20
+ # unchanged. A block called where its call was not entered,
21
+ # eg in another fiber, is counted but noted on no path.
24
22
  def item_at(decision, at, value)
25
- trail = trails(decision).last
26
- one_more(trail, at) if trail
27
23
  record(decision, at + 1, value)
28
24
  end
29
25
 
@@ -39,28 +35,23 @@ module PathCoverage
39
35
  value
40
36
  end
41
37
 
42
- # Notes on trail one more item, and value among the answers
43
- # so far of the block whose receiver is its at-th leaf.
38
+ # Notes on trail value among the answers so far of the block
39
+ # whose receiver is its at-th leaf.
44
40
  def note_answer(trail, at, value)
45
- one_more(trail, at)
46
41
  now = value ? ANSWERS[0] : ANSWERS[1]
47
42
  same = [nil, now].include?(trail.outcomes[at + 1])
48
43
  trail.outcomes[at + 1] = same ? now : ANSWERS[2]
49
44
  end
50
45
 
51
- # Notes on trail one more item the block whose receiver is its
52
- # at-th leaf was called with.
53
- def one_more(trail, at)
54
- called = ITEMS.index(trail.outcomes[at]) + 1
55
- trail.outcomes[at] = ITEMS[[called, 2].min]
56
- end
57
-
58
46
  # Ends a block method's call whose receiver is decision's at-th
59
47
  # leaf, counting the receiver truthy when its block was
60
- # called, falsy when not; returns value unchanged. With no
61
- # trail, as begin_items found none, it is counted as called.
48
+ # called, as its answer leaf shows, falsy when not; returns
49
+ # value unchanged. A call whose block was never called
50
+ # leaves trail unwitnessed. With no trail, as begin_items
51
+ # found none, it is counted as called.
62
52
  def end_items(decision, at, trail, value)
63
- called = trail.nil? || trail.outcomes[at] != ITEMS[0]
53
+ called = trail.nil? || !trail.outcomes[at + 1].nil?
54
+ trail.unwitnessed = true unless called
64
55
  @counts[[decision, at]][called ? :truthy : :falsy] += 1
65
56
  value
66
57
  end
@@ -1,4 +1,4 @@
1
- require_relative 'card_context'
1
+ require_relative 'paths_context'
2
2
  require_relative 'console_summary'
3
3
  require_relative 'gaps'
4
4
  require_relative 'json_reader'
@@ -53,12 +53,12 @@ module PathCoverage
53
53
  end
54
54
 
55
55
  # Prints the summary lines: the paths taken of all possible
56
- # alone, and combined, each def's card of files as decision_paths
56
+ # alone, and combined, each def's flow of files as decision_paths
57
57
  # and reached have it; then any operators ignored.
58
58
  def self.summary(files, config, decision_paths, reached)
59
- cards = WithinMethods.cards(files, config, decision_paths,
59
+ flows = WithinMethods.flows(files, config, decision_paths,
60
60
  reached)
61
- puts ConsoleSummary.lines(decision_paths, cards,
61
+ puts ConsoleSummary.lines(decision_paths, flows,
62
62
  config.groups) +
63
63
  ConsoleSummary.ignored_lines(config.ignored)
64
64
  end
@@ -81,14 +81,14 @@ module PathCoverage
81
81
  # path taken that a treatment rules out, as outside lists
82
82
  # them, each leaf named by its text in files; then each path
83
83
  # taken, or return or raise reached, that its method rules
84
- # out, as CardContext.ruled_out finds them in files under
84
+ # out, as PathsContext.ruled_out finds them in files under
85
85
  # config. recorded is as run has it.
86
86
  def self.gaps(operands, files, config, recorded)
87
87
  texts = PathsReport.texts(files)
88
88
  paths, outside, reached =
89
89
  recorded.values_at(:decision_paths, :outside, :reached)
90
90
  lines = Gaps.lines(operands, paths:, texts:, outside:)
91
- ruled = CardContext.ruled_out(files, config, paths, reached)
91
+ ruled = PathsContext.ruled_out(files, config, paths, reached)
92
92
  lines += Gaps.ruled_by_method(ruled, texts)
93
93
  lines.each { |line| puts line }
94
94
  end
@@ -0,0 +1,174 @@
1
+ require 'prism'
2
+ require_relative 'decision'
3
+ require_relative 'leaving'
4
+
5
+ module PathCoverage
6
+ # The rescue forks of each def. Each begin with a rescue forks
7
+ # there, whether it is a def's body, a block's, or nested in a
8
+ # body. Either its body completes, or one of its rescues runs.
9
+ module RescueForks
10
+ # Example: { "k.rb:1:0" => { "k.rb:3:0" => [1, 0...30, 14...20,
11
+ # [38...40]] } } for a def at line 1 whose one rescue is at
12
+ # line 3.
13
+ # { def decision => { fork's name => [its rescues, its begin's
14
+ # byte range, its body's, [each rescue's body's]] } } for each
15
+ # def in source, at path. A fork is named where its first
16
+ # rescue is.
17
+ def self.of(source, path)
18
+ found(source, path).forks
19
+ end
20
+
21
+ # The Found that has visited each def in source, at path.
22
+ def self.found(source, path)
23
+ found = Found.new(path)
24
+ Prism.parse(source).value.accept(found)
25
+ found
26
+ end
27
+
28
+ # Finds the rescue fork of each def it visits.
29
+ class Found < Prism::Visitor
30
+ # Starts with no def visited. Forks are named as at path.
31
+ def initialize(path)
32
+ @path = path
33
+ @forks = {}
34
+ @completions = {}
35
+ @starts = {}
36
+ @values = {}
37
+ @def = nil
38
+ super()
39
+ end
40
+
41
+ # forks is { def decision => { fork's name => [its rescues,
42
+ # its begin's byte range, its body's, [each rescue's body's]]
43
+ # } }.
44
+ # completions is { fork's name => where its body completes }.
45
+ # starts is { [fork's name, rescue's number] => the byte
46
+ # offset where that rescue starts to run }. values is { fork's
47
+ # name => the byte range of a rescue modifier's rescue, its
48
+ # value }.
49
+ attr_reader :forks, :completions, :starts, :values
50
+
51
+ # Visits inside node, a def, noting the rescue forks in it as
52
+ # its own.
53
+ def visit_def_node(node)
54
+ outer = @def
55
+ @def = Decision.of(@path, node)
56
+ @forks[@def] = {}
57
+ super
58
+ @def = outer
59
+ end
60
+
61
+ # Notes node, a begin, as a rescue fork of the def it is in,
62
+ # when it has a rescue, then visits inside it. A def's body
63
+ # with a rescue is a begin, and so is a block's.
64
+ def visit_begin_node(node)
65
+ note_fork(node) unless @def.nil? || node.rescue_clause.nil?
66
+ super
67
+ end
68
+
69
+ # Notes node, a rescue modifier, x rescue y, as a rescue fork
70
+ # of the def it is in, then visits inside it. Its body is x,
71
+ # and its one rescue is y. Its name is where its rescue is.
72
+ def visit_rescue_modifier_node(node)
73
+ note_modifier(node) unless @def.nil?
74
+ super
75
+ end
76
+
77
+ # Notes the rescue fork of node, a rescue modifier.
78
+ def note_modifier(node)
79
+ name = Decision.at(@path, node.keyword_loc)
80
+ body = span(node.expression.location)
81
+ value = span(node.rescue_expression.location)
82
+ @forks[@def][name] = [1, span(node.location), body, [value]]
83
+ note_value(name, node.expression, value)
84
+ end
85
+
86
+ # Notes where the rescue modifier named name completes and
87
+ # where its rescue runs. It completes at expression, wrapped,
88
+ # unless that leaves. Its rescue runs at value, wrapped.
89
+ def note_value(name, expression, value)
90
+ if Leaving.word(expression).nil?
91
+ @completions[name] = span(expression.location)
92
+ end
93
+ @values[name] = value
94
+ end
95
+
96
+ # The byte range of location.
97
+ def span(location)
98
+ location.start_offset...location.end_offset
99
+ end
100
+
101
+ # Notes the rescue fork of node, a begin with a rescue. Its
102
+ # name is where its first rescue is.
103
+ def note_fork(node)
104
+ clause = node.rescue_clause
105
+ name = Decision.at(@path, clause.location)
106
+ note_completion(name, node.statements, clause)
107
+ count = noted(name, clause, 1)
108
+ @forks[@def][name] = [count, *spans_of(node, clause)]
109
+ end
110
+
111
+ # [the byte range of node, a begin, of its body, and of the
112
+ # body of each rescue from clause, its first].
113
+ def spans_of(node, clause)
114
+ whole = node.location
115
+ body = range_of(node.statements, clause.location.start_offset)
116
+ clauses = []
117
+ until clause.nil?
118
+ clauses << range_of(clause.statements,
119
+ header_end(clause))
120
+ clause = clause.subsequent
121
+ end
122
+ [whole.start_offset...whole.end_offset, body, clauses]
123
+ end
124
+
125
+ # The byte range of statements, or an empty range at offset
126
+ # when there are none.
127
+ def range_of(statements, offset)
128
+ return offset...offset if statements.nil?
129
+
130
+ loc = statements.location
131
+ loc.start_offset...loc.end_offset
132
+ end
133
+
134
+ # Notes where a body of statements completes. name is its
135
+ # fork's name, and clause its first rescue. An empty body
136
+ # completes at clause, by a byte offset, for a statement of
137
+ # its own. Otherwise it completes at its last statement, by
138
+ # its byte range, to wrap. A last statement that leaves is
139
+ # not noted, because that body never completes.
140
+ def note_completion(name, statements, clause)
141
+ if statements.nil?
142
+ return @completions[name] = clause.location.start_offset
143
+ end
144
+
145
+ last = statements.body.last
146
+ return unless Leaving.word(last).nil?
147
+
148
+ loc = last.location
149
+ @completions[name] = loc.start_offset...loc.end_offset
150
+ end
151
+
152
+ # How many rescues the fork named name has, counting from
153
+ # clause, the rescue numbered number. It notes where each
154
+ # rescue starts to run, after its header. Once the rescues
155
+ # run out it gives number less one.
156
+ def noted(name, clause, number)
157
+ return number - 1 if clause.nil?
158
+
159
+ @starts[[name, number]] = header_end(clause)
160
+ noted(name, clause.subsequent, number + 1)
161
+ end
162
+
163
+ # The byte offset where the header of clause, a rescue, ends.
164
+ # The header is its keyword, its exceptions, its => and
165
+ # reference, and its then, whichever of them it has.
166
+ def header_end(clause)
167
+ nodes = [*clause.exceptions, clause.reference].compact
168
+ locs = [clause.keyword_loc, clause.operator_loc,
169
+ clause.then_keyword_loc].compact
170
+ (nodes.map(&:location) + locs).map(&:end_offset).max
171
+ end
172
+ end
173
+ end
174
+ end