path_coverage 0.4.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 (108) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +260 -300
  3. data/exe/path_coverage_report +4 -0
  4. data/lib/path_coverage/call_trace.rb +109 -0
  5. data/lib/path_coverage/calls.rb +73 -43
  6. data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +35 -33
  7. data/lib/path_coverage/condition.rb +14 -14
  8. data/lib/path_coverage/configuration.rb +7 -4
  9. data/lib/path_coverage/configuration_limits.rb +52 -0
  10. data/lib/path_coverage/configuration_paths.rb +7 -7
  11. data/lib/path_coverage/console_summary.rb +28 -26
  12. data/lib/path_coverage/{site.rb → decision.rb} +2 -2
  13. data/lib/path_coverage/decision_locations.rb +76 -0
  14. data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +13 -11
  15. data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
  16. data/lib/path_coverage/fold_branches.rb +18 -0
  17. data/lib/path_coverage/fold_cells.rb +42 -0
  18. data/lib/path_coverage/{html_tree.rb → fold_rows.rb} +43 -72
  19. data/lib/path_coverage/gaps.rb +33 -15
  20. data/lib/path_coverage/json_reader.rb +54 -19
  21. data/lib/path_coverage/json_reports.rb +109 -0
  22. data/lib/path_coverage/json_writer.rb +29 -14
  23. data/lib/path_coverage/kernel.rb +77 -58
  24. data/lib/path_coverage/kinds/base.rb +16 -16
  25. data/lib/path_coverage/kinds/block.rb +16 -15
  26. data/lib/path_coverage/kinds/case.rb +14 -12
  27. data/lib/path_coverage/kinds/chain.rb +40 -11
  28. data/lib/path_coverage/kinds/choice.rb +4 -3
  29. data/lib/path_coverage/kinds/contains.rb +40 -32
  30. data/lib/path_coverage/kinds/safe.rb +38 -26
  31. data/lib/path_coverage/kinds/write.rb +6 -6
  32. data/lib/path_coverage/kinds.rb +2 -2
  33. data/lib/path_coverage/leaving.rb +52 -0
  34. data/lib/path_coverage/loop_forks.rb +57 -0
  35. data/lib/path_coverage/loop_nodes.rb +91 -0
  36. data/lib/path_coverage/manifest.rb +92 -58
  37. data/lib/path_coverage/manifest_blocks.rb +5 -26
  38. data/lib/path_coverage/manifest_case.rb +2 -2
  39. data/lib/path_coverage/manifest_chains.rb +8 -8
  40. data/lib/path_coverage/manifest_choices.rb +23 -20
  41. data/lib/path_coverage/manifest_disabled.rb +97 -0
  42. data/lib/path_coverage/manifest_folding.rb +43 -40
  43. data/lib/path_coverage/manifest_ignoring.rb +6 -6
  44. data/lib/path_coverage/manifest_writes.rb +11 -10
  45. data/lib/path_coverage/measured_by.rb +20 -0
  46. data/lib/path_coverage/merge.rb +7 -7
  47. data/lib/path_coverage/method_loops.rb +172 -0
  48. data/lib/path_coverage/method_statements.rb +150 -134
  49. data/lib/path_coverage/method_ways.rb +170 -0
  50. data/lib/path_coverage/paths_context.rb +104 -0
  51. data/lib/path_coverage/paths_fitting.rb +31 -0
  52. data/lib/path_coverage/paths_forks.rb +72 -0
  53. data/lib/path_coverage/paths_gaps.rb +137 -0
  54. data/lib/path_coverage/paths_leaving.rb +40 -0
  55. data/lib/path_coverage/paths_report.rb +16 -16
  56. data/lib/path_coverage/paths_rescues.rb +146 -0
  57. data/lib/path_coverage/paths_scopes.rb +37 -0
  58. data/lib/path_coverage/paths_tree.rb +203 -0
  59. data/lib/path_coverage/paths_way_lists.rb +72 -0
  60. data/lib/path_coverage/paths_ways.rb +193 -0
  61. data/lib/path_coverage/recorder.rb +71 -36
  62. data/lib/path_coverage/recorder_blocks.rb +17 -17
  63. data/lib/path_coverage/recorder_cases.rb +12 -11
  64. data/lib/path_coverage/recorder_chains.rb +12 -12
  65. data/lib/path_coverage/recorder_writes.rb +9 -9
  66. data/lib/path_coverage/render.rb +58 -51
  67. data/lib/path_coverage/rescue_forks.rb +174 -0
  68. data/lib/path_coverage/result.rb +32 -31
  69. data/lib/path_coverage/rewriter.rb +116 -35
  70. data/lib/path_coverage/rewriter_nested.rb +13 -13
  71. data/lib/path_coverage/runs_report.rb +84 -0
  72. data/lib/path_coverage/source_lines.rb +16 -16
  73. data/lib/path_coverage/source_report.rb +100 -0
  74. data/lib/path_coverage/stable_leaves.rb +162 -0
  75. data/lib/path_coverage/statement_arms.rb +53 -38
  76. data/lib/path_coverage/summary.rb +4 -62
  77. data/lib/path_coverage/totals.rb +12 -10
  78. data/lib/path_coverage/version.rb +1 -1
  79. data/lib/path_coverage/walk.rb +106 -0
  80. data/lib/path_coverage/way_algebras.rb +127 -0
  81. data/lib/path_coverage/ways_report.rb +91 -0
  82. data/lib/path_coverage/ways_totals.rb +46 -0
  83. data/lib/path_coverage/within_methods.rb +152 -97
  84. metadata +39 -30
  85. data/lib/path_coverage/card.rb +0 -14
  86. data/lib/path_coverage/card_tree.rb +0 -186
  87. data/lib/path_coverage/card_ways.rb +0 -140
  88. data/lib/path_coverage/html/file.html +0 -33
  89. data/lib/path_coverage/html/index.html +0 -29
  90. data/lib/path_coverage/html/settings.js +0 -50
  91. data/lib/path_coverage/html/side.js +0 -599
  92. data/lib/path_coverage/html/sort.js +0 -34
  93. data/lib/path_coverage/html/style.css +0 -484
  94. data/lib/path_coverage/html/tabs.js +0 -15
  95. data/lib/path_coverage/html/theme.js +0 -27
  96. data/lib/path_coverage/html_branch.rb +0 -30
  97. data/lib/path_coverage/html_cards.rb +0 -88
  98. data/lib/path_coverage/html_cells.rb +0 -71
  99. data/lib/path_coverage/html_file.rb +0 -123
  100. data/lib/path_coverage/html_index.rb +0 -128
  101. data/lib/path_coverage/html_index_head.rb +0 -29
  102. data/lib/path_coverage/html_labels.rb +0 -16
  103. data/lib/path_coverage/html_methods.rb +0 -66
  104. data/lib/path_coverage/html_nav.rb +0 -67
  105. data/lib/path_coverage/html_report.rb +0 -131
  106. data/lib/path_coverage/html_statements.rb +0 -186
  107. data/lib/path_coverage/html_tabs.rb +0 -26
  108. data/lib/path_coverage/html_template.rb +0 -26
@@ -2,105 +2,122 @@ require 'prism'
2
2
  require_relative 'manifest'
3
3
  require_relative 'manifest_chains'
4
4
  require_relative 'manifest_choices'
5
- require_relative 'site'
6
- require_relative 'site_paths'
7
- require_relative 'site_tree'
5
+ require_relative 'decision'
6
+ require_relative 'decision_paths'
7
+ require_relative 'decision_tree'
8
+ require_relative 'leaving'
8
9
  require_relative 'statement_arms'
9
10
  require_relative 'totals'
10
11
 
11
12
  module PathCoverage
12
- # One row of a method's card, a top-level statement or a
13
- # site at its top: where it starts; its top, what it is as
13
+ # One row of a method's flow, a top-level statement or a
14
+ # decision at its top: where it starts; its top, what it is as
14
15
  # written, eg = for a write; how many paths it has; the name
15
- # of its site, nil when it is no site; and its own name, by
16
+ # of its decision, nil when it is no decision; and its own name, by
16
17
  # where it is, eg "k.rb:2:2".
17
- Statement = Data.define(:line, :column, :top, :paths, :site, :name)
18
+ Statement = Data.define(:line, :column, :top, :paths, :decision,
19
+ :name)
18
20
 
19
21
  # Finds, without running it, the top-level statements of each
20
- # method in source, one row each in its method's card.
22
+ # method in source, one row each in its method's flow.
21
23
  module MethodStatements
22
- # Each function below takes sites, { site => Condition }, the
23
- # sites the run has in source, as Manifest.parse gives them
24
- # with the run's ignore and treat settings; nil for those of
25
- # no settings.
24
+ # Each function below takes decisions, { decision =>
25
+ # Condition }, the decisions the run has in source, as
26
+ # Manifest.parse gives them with the run's ignore and treat
27
+ # settings; nil for those of no settings.
26
28
 
27
- # { def site => [Statement, ...] } for each def in source.
29
+ # { def decision => [Statement, ...] } for each def in source.
28
30
  # Example: def f; x = 1; x; end gives = and value rows.
29
- def self.of(source, path, sites = nil)
30
- collect(source, path, sites).methods
31
+ def self.of(source, path, decisions = nil)
32
+ collect(source, path, decisions).methods
31
33
  end
32
34
 
33
- # { def site => its name } for each def in source, eg
34
- # { "k.rb:4:2" => :ship }, each its card's title.
35
- def self.names(source, path, sites = nil)
36
- collect(source, path, sites).names
35
+ # { def decision => its name } for each def in source, eg
36
+ # { "k.rb:4:2" => :ship }, each its flow's title.
37
+ def self.names(source, path, decisions = nil)
38
+ collect(source, path, decisions).names
37
39
  end
38
40
 
39
- # { def site => its last line } for each def in source, eg
40
- # { "k.rb:4:2" => 6 }, where its card ends.
41
- def self.lasts(source, path, sites = nil)
42
- collect(source, path, sites).lasts
41
+ # { def decision => its last line } for each def in source, eg
42
+ # { "k.rb:4:2" => 6 }, where its flow ends.
43
+ def self.lasts(source, path, decisions = nil)
44
+ collect(source, path, decisions).lasts
43
45
  end
44
46
 
45
47
  # Example: { "k.rb:2:2" => 18 } for a write at byte 18.
46
48
  # Where a call can count the runs of each row that is no
47
- # site, in every def in source, by the row's name, where it
49
+ # decision, in every def in source, by the row's name, where it
48
50
  # starts: its byte offset, or, in an endless def, whose body
49
51
  # is one expression, its byte range, to be wrapped.
50
- def self.reaches(source, path, sites = nil)
51
- collect(source, path, sites).reaches
52
+ def self.reaches(source, path, decisions = nil)
53
+ collect(source, path, decisions).reaches
54
+ end
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
52
64
  end
53
65
 
54
66
  # A Collector that has visited each def in source, with
55
- # sites, or, for nil, those of no settings.
56
- def self.collect(source, path, sites)
57
- sites = Manifest.sites(source, path) if sites.nil?
58
- collector = Collector.new(path, sites)
67
+ # decisions, or, for nil, those of no settings.
68
+ def self.collect(source, path, decisions)
69
+ decisions = Manifest.decisions(source, path) if decisions.nil?
70
+ collector = Collector.new(path, decisions)
59
71
  Prism.parse(source).value.accept(collector)
60
72
  collector
61
73
  end
62
74
 
63
75
  # Example: RATE = x ? 2 : 1 at the top level, its then
64
76
  # taken, gives [1, 2].
65
- # [taken, possible] of the paths of the sites in source on
66
- # no method's card, outside every def, as top_rows gives
77
+ # [taken, possible] of the paths of the decisions in source on
78
+ # no method's flow, outside every def, as top_rows gives
67
79
  # them; paths as Result#paths gives them.
68
- def self.top_level(source, path, paths, sites = nil)
69
- names = top_rows(source, path, sites).map(&:site)
70
- Totals.decisions(paths.slice(*names))
80
+ def self.top_level(source, path, paths, decisions = nil)
81
+ names = top_rows(source, path, decisions).map(&:decision)
82
+ Totals.alone(paths.slice(*names))
71
83
  end
72
84
 
73
85
  # Example: RATE = x ? 2 : 1 gives a ?: row at 1:9.
74
- # A Statement row for each site in source on no method's
75
- # card, outside every def, for the card of its own lines, in
86
+ # A Statement row for each decision in source on no method's
87
+ # flow, outside every def, for the flow of its own lines, in
76
88
  # the order written: where decision mode names it, its
77
89
  # operator, an elsif's elsif, and its paths.
78
- def self.top_rows(source, path, sites = nil)
79
- collector = collect(source, path, sites)
80
- carded = collector.methods.values.flatten.map(&:site)
81
- loose = collector.sites.except(*carded)
82
- rows = loose.map { |name, site| top_row(source, name, site) }
90
+ def self.top_rows(source, path, decisions = nil)
91
+ collector = collect(source, path, decisions)
92
+ in_methods = collector.methods.values.flatten.map(&:decision)
93
+ loose = collector.decisions.except(*in_methods)
94
+ rows = loose.map do |name, decision|
95
+ top_row(source, name, decision)
96
+ end
83
97
  rows.sort_by { [_1.line, _1.column] }
84
98
  end
85
99
 
86
- # The Statement row of the site named name, its Condition
87
- # site, at the top level of source.
88
- def self.top_row(source, name, site)
100
+ # The Statement row of the decision named name, its Condition
101
+ # decision, at the top level of source.
102
+ def self.top_row(source, name, decision)
89
103
  line, column = name.split(':').last(2).map { Integer(_1) }
90
- top = SiteTree.elsif?(source, site) ? 'elsif' : site.operator
91
- paths = SitePaths.possible(site).size
92
- Statement.new(line:, column:, top:, paths:, site: name, name:)
104
+ top = if DecisionTree.elsif?(source,
105
+ decision)
106
+ 'elsif'
107
+ else
108
+ decision.operator
109
+ end
110
+ paths = DecisionPaths.possible(decision).size
111
+ Statement.new(line:, column:, top:, paths:, decision: name,
112
+ name:)
93
113
  end
94
114
 
95
- # Example: { "k.rb:1:0" => [2, 4] }, 2 of ship's 4 taken.
96
- # Each def's card total in source, [taken, size], its rows'
97
- # taken as taken gives them from paths and reached; a row
98
- # not known counts as not taken, never more than it ran.
99
- def self.totals(source, path, paths, reached, sites = nil)
100
- of(source, path, sites).transform_values do |rows|
101
- seen = taken(rows, paths, reached).compact.sum
102
- [seen, size(rows.map(&:paths))]
103
- end
115
+ # Example: 19:04, a statement on line 19 at column 4.
116
+ # statement's line:column, the column two digits so they
117
+ # line up.
118
+ def self.at(statement)
119
+ format('%<line>d:%<column>02d',
120
+ line: statement.line, column: statement.column)
104
121
  end
105
122
 
106
123
  # Example: rows of 1, 2 and 2 paths, [1, 2, 2], have size 5.
@@ -111,22 +128,22 @@ module PathCoverage
111
128
  end
112
129
 
113
130
  # Example: an if row with only its then taken gives 1.
114
- # How many paths of each row were taken: for a site's row,
131
+ # How many paths of each row were taken: for a decision's row,
115
132
  # its paths with a count, paths being Result#paths; for any
116
133
  # other row, its 1 path when reached has a count for it, 0
117
134
  # when not, nil, not known, without reached.
118
135
  def self.taken(rows, paths, reached = nil)
119
136
  rows.map do |row|
120
- next taken_site(row, paths) if row.site
137
+ next taken_decision(row, paths) if row.decision
121
138
  next nil if reached.nil?
122
139
 
123
140
  reached.key?(row.name) ? 1 : 0
124
141
  end
125
142
  end
126
143
 
127
- # How many of a site row's paths have a count in paths.
128
- def self.taken_site(row, paths)
129
- counts = paths.fetch(row.site).map { |each| each[:count] }
144
+ # How many of a decision row's paths have a count in paths.
145
+ def self.taken_decision(row, paths)
146
+ counts = paths.fetch(row.decision).map { |each| each[:count] }
130
147
  counts.count(&:positive?)
131
148
  end
132
149
 
@@ -136,149 +153,154 @@ module PathCoverage
136
153
  include ManifestChoices
137
154
  include StatementArms
138
155
 
139
- # Starts with no methods found; sites are decision mode's,
140
- # { site => Condition }.
141
- def initialize(path, sites)
156
+ # Starts with no methods found; decisions are decision mode's,
157
+ # { decision => Condition }.
158
+ def initialize(path, decisions)
142
159
  @path = path
143
- @sites = sites
160
+ @decisions = decisions
144
161
  @methods = {}
145
162
  @names = {}
146
163
  @lasts = {}
147
164
  @reaches = {}
165
+ @entries = {}
148
166
  super()
149
167
  end
150
168
 
151
- # The methods found so far, { def site => rows }; their
152
- # names and last lines, { def site => name } and { def site
153
- # => line }; where each row that is no site is reached, as
154
- # MethodStatements.reaches has it; and the sites it was
155
- # given, { site => Condition }.
156
- attr_reader :methods, :names, :lasts, :reaches, :sites
169
+ # The methods found so far, { def decision => rows }; their
170
+ # names and last lines, { def decision => name } and
171
+ # { def decision => line }; where each row that is no
172
+ # decision is reached, as MethodStatements.reaches has it;
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
157
177
 
158
178
  # Notes node's name, its last line, its body statements as
159
- # its rows, those in its sites' arms too, but only those that
160
- # shape its paths, as shapes? has it, none for an empty body,
161
- # and where each row that is no site is reached.
179
+ # its rows, those in its decisions' arms too, but only those
180
+ # that shape its paths, as shapes? has it, none for an empty
181
+ # body, and where each row that is no decision is reached.
162
182
  def visit_def_node(node)
163
183
  written = rows_in(statements_of(node.body))
164
184
  tops = written.select { shapes?(_1) }
165
- site = Site.of(@path, node)
166
- @methods[site] = tops.map { |top| row_of(top) }
167
- note_def(site, node)
185
+ decision = Decision.of(@path, node)
186
+ @methods[decision] = tops.map { |top| row_of(top) }
187
+ note_def(decision, node)
168
188
  note_reaches(tops, endless: !node.equal_loc.nil?)
169
189
  super
170
190
  end
171
191
 
172
- # Notes the name and last line of node, the def at site, and
173
- # where it is entered, reached under site's own name, so its
174
- # card can say how many times it was called.
175
- def note_def(site, node)
176
- @names[site] = node.name
177
- @lasts[site] = node.location.end_line
178
- @reaches[site] = entry_at(node)
192
+ # Notes the name and last line of node, the def at decision, and
193
+ # where it is entered, reached under decision's own name, so its
194
+ # flow can say how many times it was called.
195
+ def note_def(decision, node)
196
+ @names[decision] = node.name
197
+ @lasts[decision] = node.location.end_line
198
+ @entries[decision] = entry_at(node)
179
199
  end
180
200
 
181
- # Notes where each of tops that is no site, a return or a
201
+ # Notes where each of tops that is no decision, a return or a
182
202
  # raise, is reached: its byte offset, or, endless, its byte
183
203
  # range.
184
204
  def note_reaches(tops, endless:)
185
205
  tops.each do |top|
186
- next if site?(top)
206
+ next if decision?(top)
187
207
 
188
208
  loc = top.location
189
209
  start = loc.start_offset
190
210
  at = endless ? start...loc.end_offset : start
191
- @reaches[Site.at(@path, loc)] = at
211
+ @reaches[Decision.at(@path, loc)] = at
192
212
  end
193
213
  end
194
214
 
195
215
  # Example: if a then x = 1 end gives the if, then the write.
196
216
  # The nodes statements' rows are for, flat, in the order
197
217
  # they are written: each statement's tops, each with the
198
- # sites of their own inside it, in the order they are named,
218
+ # decisions of their own inside it, in the order they are named,
199
219
  # eg a || before the ?: whose predicate it is, then the rows
200
220
  # of its arms' statements, when it has any.
201
221
  def rows_in(statements)
202
222
  statements.flat_map do |statement|
203
223
  tops = top_nodes(statement).flat_map do |top|
204
- named = [top, *inner_sites(top)]
205
- ordered = named.sort_by { site_loc(_1).start_offset }
224
+ named = [top, *inner_decisions(top)]
225
+ ordered = named.sort_by { decision_loc(_1).start_offset }
206
226
  [*ordered, *rows_in(arms_of(top))]
207
227
  end
208
228
  tops + body_rows(statement)
209
229
  end
210
230
  end
211
231
 
212
- # The Statement row of node. A site is where decision mode
232
+ # The Statement row of node. A decision is where decision mode
213
233
  # names it, eg a modifier if at its keyword, with as many
214
234
  # paths as decision mode gives it. Anything else is where
215
235
  # it starts, with 1 path.
216
236
  def row_of(node)
217
- site = site_at(node)
218
- loc = site ? site_loc(node) : node.location
219
- name = Site.at(@path, loc)
237
+ decision = decision_at(node)
238
+ loc = decision ? decision_loc(node) : node.location
239
+ name = Decision.at(@path, loc)
220
240
  Statement.new(
221
241
  line: loc.start_line, column: loc.start_column,
222
242
  top: top_of(node),
223
- paths: site ? SitePaths.possible(site).size : 1,
224
- site: site ? name : nil, name:
243
+ paths: decision ? DecisionPaths.possible(decision).size : 1,
244
+ decision: decision ? name : nil, name:
225
245
  )
226
246
  end
227
247
 
228
248
  # Example: x = (a ? 1 : 2) + (b ? 3 : 4) gives both ?:s.
229
- # The nodes statement's rows are for: the outermost sites
249
+ # The nodes statement's rows are for: the outermost decisions
230
250
  # in it, in the order they run, as what holds them is
231
251
  # looked through; statement itself when it holds none.
232
252
  def top_nodes(statement)
233
- tops = sites_in(statement)
253
+ tops = decisions_in(statement)
234
254
  tops.empty? ? [statement] : tops
235
255
  end
236
256
 
237
- # node when it is a site, else the outermost sites in its
238
- # children, in order; none in a body or a def, as UNSEEN
257
+ # node when it is a decision, else the outermost decisions in
258
+ # its children, in order; none in a body or a def, as UNSEEN
239
259
  # has them.
240
- def sites_in(node)
241
- return [node] if site?(node)
260
+ def decisions_in(node)
261
+ return [node] if decision?(node)
242
262
  return [] if UNSEEN.include?(node.type)
243
263
 
244
- node.compact_child_nodes.flat_map { |child| sites_in(child) }
264
+ node.compact_child_nodes.flat_map do |child|
265
+ decisions_in(child)
266
+ end
245
267
  end
246
268
 
247
- # True when decision mode has a site named where node's
248
- # site would start.
249
- def site?(node)
250
- !site_at(node).nil?
269
+ # True when decision mode has a decision named where node's
270
+ # decision would start.
271
+ def decision?(node)
272
+ !decision_at(node).nil?
251
273
  end
252
274
 
253
- # Decision mode's Condition for the site named where
254
- # node's site would start; nil when it has none there.
255
- def site_at(node)
256
- @sites[site_name(node)]
275
+ # Decision mode's Condition for the decision named where
276
+ # node's decision would start; nil when it has none there.
277
+ def decision_at(node)
278
+ @decisions[decision_name(node)]
257
279
  end
258
280
 
259
- # The name node's site would have, by where it would start.
260
- def site_name(node)
261
- Site.at(@path, site_loc(node))
281
+ # The name node's decision would have, by where it would start.
282
+ def decision_name(node)
283
+ Decision.at(@path, decision_loc(node))
262
284
  end
263
285
 
264
- TOPS = {
265
- call_node: '()', return_node: 'return', def_node: 'def'
266
- }.freeze
286
+ TOPS = { call_node: '()', def_node: 'def' }.freeze
267
287
 
268
- # What statement is as written: a site's operator, eg if,
269
- # when decision mode has a site where it starts, an elsif
288
+ # What statement is as written: a decision's operator, eg if,
289
+ # when decision mode has a decision where it starts, an elsif
270
290
  # being elsif, as written, an if or unless with an else
271
- # if/else or unless/else; else = for a write, () for a
272
- # call, raise for a raise of the method's own, as it leaves
273
- # the method, return for a return, def for a def inside it,
274
- # value for anything else.
291
+ # if/else or unless/else; else = for a write, Leaving's word,
292
+ # return or raise, for a statement that leaves the method,
293
+ # () for a call, def for a def inside it, value for anything
294
+ # else.
275
295
  def top_of(statement)
276
- site = site_at(statement)
277
- return 'elsif' if site && elsif?(statement)
278
- return with_else(site.operator, statement) if site
279
- return 'raise' if raise?(statement)
296
+ decision = decision_at(statement)
297
+ return 'elsif' if decision && elsif?(statement)
298
+ return with_else(decision.operator, statement) if decision
280
299
  return '=' if write?(statement)
281
300
 
301
+ word = Leaving.word(statement)
302
+ return word if word
303
+
282
304
  TOPS.fetch(statement.type, 'value')
283
305
  end
284
306
 
@@ -294,12 +316,6 @@ module PathCoverage
294
316
  statement.type.end_with?('_write_node') ||
295
317
  (statement.type == :call_node && statement.attribute_write?)
296
318
  end
297
-
298
- # True when statement is a call of raise with no receiver.
299
- def raise?(statement)
300
- statement.type == :call_node && statement.receiver.nil? &&
301
- statement.name == :raise
302
- end
303
319
  end
304
320
  end
305
321
  end
@@ -0,0 +1,170 @@
1
+ require 'prism'
2
+ require_relative 'paths_tree'
3
+ require_relative 'paths_ways'
4
+ require_relative 'decision_paths'
5
+ require_relative 'manifest'
6
+ require_relative 'loop_forks'
7
+ require_relative 'method_statements'
8
+ require_relative 'rescue_forks'
9
+ require_relative 'way_algebras'
10
+
11
+ module PathCoverage
12
+ # The ways one call can take through each method, each listed as
13
+ # its steps, as a call's trace records them: a decision's
14
+ # [name, path], or any other row's [name], eg a return's.
15
+ module MethodWays
16
+ # { def decision => [way, ...] } for each def in source, at
17
+ # path, its decisions those of no settings: the ways some call
18
+ # can take, each loop a fork when loops.
19
+ def self.of(source, path, loops: false)
20
+ split(source, path, loops:).transform_values(&:first)
21
+ end
22
+
23
+ # { def decision => [possible, impossible] } for each def in
24
+ # source, at path, its decisions those of no settings: the
25
+ # ways some call can take, and those none can, as giving a
26
+ # leaf StableLeaves finds both outcomes; each loop a fork when
27
+ # loops, as LoopForks finds them.
28
+ def self.split(source, path, loops: false)
29
+ decisions = Manifest.decisions(source, path)
30
+ flows = MethodStatements.of(source, path, decisions)
31
+ forks = LoopForks.of(source, path, decisions)
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)]
36
+ end
37
+ end
38
+
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
44
+ # items to a loop declared never empty is impossible.
45
+ def self.split_flow(source, decisions, rows, scopes)
46
+ possible, all = [true, false].map do |stable|
47
+ listed(source, decisions, rows, [stable, *scopes])
48
+ end
49
+ lines = never_empty(source)
50
+ by = possible.group_by { empties?(_1, lines) }
51
+ possible = by.fetch(false, [])
52
+ [possible, all - possible]
53
+ end
54
+
55
+ # The comment on a loop's line declaring it is never given no
56
+ # items.
57
+ NEVER_EMPTY = '# path: impossible 0 items'.freeze
58
+
59
+ # The numbers of the lines of source with a comment holding
60
+ # NEVER_EMPTY.
61
+ def self.never_empty(source)
62
+ Prism.parse(source).comments.filter_map do |comment|
63
+ location = comment.location
64
+ location.start_line if location.slice.include?(NEVER_EMPTY)
65
+ end.to_set
66
+ end
67
+
68
+ # True for a way giving no items to a loop on one of lines.
69
+ def self.empties?(way, lines)
70
+ way.any? do |name, path|
71
+ line = Integer(name.split(':')[-2])
72
+ path == [FORKS[0]] && lines.include?(line)
73
+ end
74
+ end
75
+
76
+ # The steps of a loop's fork, by its path: no pass, or some.
77
+ FORKS = ['0 items', '1+ items'].freeze
78
+
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)
89
+ ways.map do |way|
90
+ ordered(way.map do |key, index|
91
+ next fork_step(named[key], index) if named.key?(key)
92
+
93
+ step(steps, decisions, key, index)
94
+ end)
95
+ end
96
+ end
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
+
118
+ # The ways, as of lists them, that way, a call's, as its trace
119
+ # records it, witnesses: each pass of a loop inlined, one way
120
+ # for each distinct way its passes took, a loop with no pass
121
+ # adding nothing; each way's steps in the order their rows are
122
+ # written, as ordered gives them. A loop of forked, the names
123
+ # of loops that fork, that made a pass steps 1+ items first.
124
+ def self.witnessed(way, forked = [])
125
+ found = way.reduce([[]]) do |ways, step|
126
+ next ways.map { _1 + [step] } if step.size < 3
127
+
128
+ ways.product(passes(step, forked)).map { _1.sum([]) }
129
+ end
130
+ found.to_set { ordered(_1) }
131
+ end
132
+
133
+ # The ways a loop's step, [name, items, pass ways], inlines:
134
+ # each way its passes witness, after a step of 1+ items when
135
+ # the loop is among forked; or the one way of no steps when it
136
+ # made no pass.
137
+ def self.passes(step, forked)
138
+ taken = step[2].flat_map { witnessed(_1, forked).to_a }
139
+ return [[]] if taken.empty?
140
+ return taken unless forked.include?(step[0])
141
+
142
+ taken.map { [[step[0], [FORKS[1]]], *_1] }
143
+ end
144
+
145
+ # way, a static way, as the way witnessed gives a call that
146
+ # took it: with no step of 0 items, as a loop that made no
147
+ # pass is no step of a call's trace, its steps ordered.
148
+ def self.matched(way)
149
+ by = way.group_by { _1[1] == [FORKS[0]] }
150
+ ordered(by.fetch(false, []))
151
+ end
152
+
153
+ # way's steps in the order their rows are written, by line
154
+ # then column, so a way is compared by its steps alone.
155
+ def self.ordered(way)
156
+ way.sort_by { |step| step[0].split(':').last(2).map(&:to_i) }
157
+ end
158
+
159
+ # The step of the path at index of the row keyed key, among
160
+ # rows, { key => row }: a decision's [name, path], decisions
161
+ # being { decision => Condition }; any other's [name].
162
+ def self.step(rows, decisions, key, index)
163
+ row = rows[key]
164
+ return [row.name] if row.decision.nil?
165
+
166
+ possible = DecisionPaths.possible(decisions[row.decision])
167
+ [row.decision, possible[index]]
168
+ end
169
+ end
170
+ end