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