path_coverage 0.2.0 → 0.3.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 (57) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +112 -113
  3. data/lib/path_coverage/calls.rb +4 -11
  4. data/lib/path_coverage/card.rb +14 -0
  5. data/lib/path_coverage/condition.rb +8 -1
  6. data/lib/path_coverage/configuration.rb +1 -17
  7. data/lib/path_coverage/configuration_paths.rb +4 -2
  8. data/lib/path_coverage/console_summary.rb +15 -17
  9. data/lib/path_coverage/gaps.rb +12 -9
  10. data/lib/path_coverage/html/file.html +5 -5
  11. data/lib/path_coverage/html/index.html +3 -1
  12. data/lib/path_coverage/html/settings.js +50 -0
  13. data/lib/path_coverage/html/side.js +341 -432
  14. data/lib/path_coverage/html/style.css +330 -225
  15. data/lib/path_coverage/html/theme.js +27 -0
  16. data/lib/path_coverage/html_branch.rb +30 -0
  17. data/lib/path_coverage/html_cards.rb +111 -0
  18. data/lib/path_coverage/html_cells.rb +66 -0
  19. data/lib/path_coverage/html_file.rb +82 -68
  20. data/lib/path_coverage/html_index.rb +28 -29
  21. data/lib/path_coverage/html_index_head.rb +10 -30
  22. data/lib/path_coverage/html_labels.rb +23 -0
  23. data/lib/path_coverage/html_methods.rb +66 -0
  24. data/lib/path_coverage/html_nav.rb +50 -50
  25. data/lib/path_coverage/html_report.rb +45 -49
  26. data/lib/path_coverage/html_statements.rb +166 -0
  27. data/lib/path_coverage/html_syntax.rb +156 -0
  28. data/lib/path_coverage/html_template.rb +6 -0
  29. data/lib/path_coverage/html_tree.rb +36 -72
  30. data/lib/path_coverage/json_reader.rb +21 -14
  31. data/lib/path_coverage/json_writer.rb +26 -13
  32. data/lib/path_coverage/kernel.rb +29 -49
  33. data/lib/path_coverage/kinds/block.rb +2 -1
  34. data/lib/path_coverage/kinds/case.rb +7 -4
  35. data/lib/path_coverage/kinds/contains.rb +6 -4
  36. data/lib/path_coverage/kinds/safe.rb +5 -3
  37. data/lib/path_coverage/manifest.rb +2 -1
  38. data/lib/path_coverage/manifest_blocks.rb +3 -4
  39. data/lib/path_coverage/manifest_case.rb +2 -3
  40. data/lib/path_coverage/manifest_choices.rb +4 -3
  41. data/lib/path_coverage/manifest_folding.rb +1 -1
  42. data/lib/path_coverage/merge.rb +3 -3
  43. data/lib/path_coverage/method_statements.rb +300 -0
  44. data/lib/path_coverage/recorder.rb +12 -31
  45. data/lib/path_coverage/render.rb +19 -22
  46. data/lib/path_coverage/rewriter.rb +36 -55
  47. data/lib/path_coverage/site_tree.rb +9 -0
  48. data/lib/path_coverage/statement_arms.rb +161 -0
  49. data/lib/path_coverage/summary.rb +11 -15
  50. data/lib/path_coverage/totals.rb +6 -12
  51. data/lib/path_coverage/version.rb +1 -1
  52. data/lib/path_coverage/within_methods.rb +92 -0
  53. metadata +16 -11
  54. data/lib/path_coverage/html/toggle.js +0 -12
  55. data/lib/path_coverage/html_paths.rb +0 -138
  56. data/lib/path_coverage/path_gaps.rb +0 -41
  57. data/lib/path_coverage/path_manifest.rb +0 -110
@@ -0,0 +1,300 @@
1
+ require 'prism'
2
+ require_relative 'manifest'
3
+ require_relative 'manifest_chains'
4
+ require_relative 'manifest_choices'
5
+ require_relative 'site'
6
+ require_relative 'site_paths'
7
+ require_relative 'site_tree'
8
+ require_relative 'statement_arms'
9
+ require_relative 'totals'
10
+
11
+ 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
14
+ # 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
+ # where it is, eg "k.rb:2:2".
17
+ Statement = Data.define(:line, :column, :top, :paths, :site, :name)
18
+
19
+ # Finds, without running it, the top-level statements of each
20
+ # method in source, one row each in its method's card.
21
+ 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.
26
+
27
+ # { def site => [Statement, ...] } for each def in source.
28
+ # 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
+ end
32
+
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
37
+ end
38
+
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
43
+ end
44
+
45
+ # Example: { "k.rb:2:2" => 18 } for a write at byte 18.
46
+ # 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
48
+ # starts: its byte offset, or, in an endless def, whose body
49
+ # is one expression, its byte range, to be wrapped.
50
+ def self.reaches(source, path, sites = nil)
51
+ collect(source, path, sites).reaches
52
+ end
53
+
54
+ # 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)
59
+ Prism.parse(source).value.accept(collector)
60
+ collector
61
+ end
62
+
63
+ # Example: RATE = x ? 2 : 1 at the top level, its then
64
+ # 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
67
+ # 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))
71
+ end
72
+
73
+ # 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
76
+ # the order written: where decision mode names it, its
77
+ # 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) }
83
+ rows.sort_by { [_1.line, _1.column] }
84
+ end
85
+
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)
89
+ 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:)
93
+ end
94
+
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
103
+ end
104
+
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, :*)
118
+ end
119
+
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,
122
+ # its paths with a count, paths being Result#paths; for any
123
+ # other row, its 1 path when reached has a count for it, 0
124
+ # when not, nil, not known, without reached.
125
+ def self.taken(rows, paths, reached = nil)
126
+ rows.map do |row|
127
+ next taken_site(row, paths) if row.site
128
+ next nil if reached.nil?
129
+
130
+ reached.key?(row.name) ? 1 : 0
131
+ end
132
+ end
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] }
137
+ counts.count(&:positive?)
138
+ end
139
+
140
+ # Collects each def's top-level statements.
141
+ class Collector < Prism::Visitor
142
+ include ManifestChains
143
+ include ManifestChoices
144
+ include StatementArms
145
+
146
+ # Starts with no methods found; sites are decision mode's,
147
+ # { site => Condition }.
148
+ def initialize(path, sites)
149
+ @path = path
150
+ @sites = sites
151
+ @methods = {}
152
+ @names = {}
153
+ @lasts = {}
154
+ @reaches = {}
155
+ super()
156
+ end
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
164
+
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.
168
+ 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)
173
+ note_reaches(tops, endless: !node.equal_loc.nil?)
174
+ super
175
+ end
176
+
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
181
+ end
182
+
183
+ # Notes where each of tops that is no site is reached: its
184
+ # byte offset, or, endless, its byte range.
185
+ def note_reaches(tops, endless:)
186
+ tops.reject { site?(_1) }.each do |top|
187
+ loc = top.location
188
+ start = loc.start_offset
189
+ at = endless ? start...loc.end_offset : start
190
+ @reaches[Site.at(@path, loc)] = at
191
+ end
192
+ end
193
+
194
+ # Example: if a then x = 1 end gives the if, then the write.
195
+ # 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.
199
+ def rows_in(statements)
200
+ statements.flat_map do |statement|
201
+ tops = top_nodes(statement).flat_map do |top|
202
+ [top, *inner_sites(top), *rows_in(arms_of(top))]
203
+ end
204
+ tops + body_rows(statement)
205
+ end
206
+ end
207
+
208
+ # The Statement row of node. A site is where decision mode
209
+ # names it, eg a modifier if at its keyword, with as many
210
+ # paths as decision mode gives it. Anything else is where
211
+ # it starts, with 1 path.
212
+ def row_of(node)
213
+ site = site_at(node)
214
+ loc = site ? site_loc(node) : node.location
215
+ name = Site.at(@path, loc)
216
+ Statement.new(
217
+ line: loc.start_line, column: loc.start_column,
218
+ top: top_of(node),
219
+ paths: site ? SitePaths.possible(site).size : 1,
220
+ site: site ? name : nil, name:
221
+ )
222
+ end
223
+
224
+ # Example: x = (a ? 1 : 2) + (b ? 3 : 4) gives both ?:s.
225
+ # The nodes statement's rows are for: the outermost sites
226
+ # in it, in the order they run, as what holds them is
227
+ # looked through; statement itself when it holds none.
228
+ def top_nodes(statement)
229
+ tops = sites_in(statement)
230
+ tops.empty? ? [statement] : tops
231
+ end
232
+
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
235
+ # has them.
236
+ def sites_in(node)
237
+ return [node] if site?(node)
238
+ return [] if UNSEEN.include?(node.type)
239
+
240
+ node.compact_child_nodes.flat_map { |child| sites_in(child) }
241
+ end
242
+
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?
247
+ end
248
+
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)]
253
+ end
254
+
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))
258
+ end
259
+
260
+ TOPS = {
261
+ call_node: '()', return_node: 'return', def_node: 'def'
262
+ }.freeze
263
+
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.
270
+ 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)
275
+ return '=' if write?(statement)
276
+
277
+ TOPS.fetch(statement.type, 'value')
278
+ end
279
+
280
+ # True for an if written as an elsif.
281
+ def elsif?(statement)
282
+ statement.type == :if_node && statement.if_keyword == 'elsif'
283
+ end
284
+
285
+ # True for a write of any kind: of a variable, eg @x = 1
286
+ # or x += 1, or a call Ruby makes of an index or attribute
287
+ # write, eg a[i] = x or a.b = x.
288
+ def write?(statement)
289
+ statement.type.end_with?('_write_node') ||
290
+ (statement.type == :call_node && statement.attribute_write?)
291
+ 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
+ end
299
+ end
300
+ end
@@ -12,29 +12,32 @@ module PathCoverage
12
12
  include RecorderChains
13
13
  include RecorderWrites
14
14
 
15
- # One call's way through its method: the outcome of each
16
- # decision made so far, of the number of decisions its
17
- # method has.
18
- Path = Struct.new(:site, :decisions, :outcomes)
19
-
20
15
  # One evaluation's way through its site: the outcome of
21
16
  # each leaf, nil for one not yet, or never, reached.
22
17
  Trail = Struct.new(:site, :outcomes)
23
18
 
24
- # Starts with no outcomes, paths or site paths counted.
19
+ # Starts with no outcomes, site paths or reaches counted.
25
20
  def initialize
26
21
  @counts = Hash.new do |counts, key|
27
22
  counts[key] = { truthy: 0, falsy: 0 }
28
23
  end
29
- @paths = Hash.new do |paths, site|
30
- paths[site] = Hash.new(0)
31
- end
32
24
  @site_paths = Hash.new do |site_paths, site|
33
25
  site_paths[site] = Hash.new(0)
34
26
  end
27
+ @reached = Hash.new(0)
35
28
  @trails_key = :"path_coverage_trails_#{object_id}"
36
29
  end
37
30
 
31
+ # { name => runs } of each statement reached so far, by its
32
+ # name, eg { "k.rb:2:2" => 2 }; none never reached.
33
+ attr_reader :reached
34
+
35
+ # Counts a run of the statement named name.
36
+ def reach(name)
37
+ @reached[name] += 1
38
+ nil
39
+ end
40
+
38
41
  # The stack of site's evaluations under way in the current
39
42
  # thread (fiber), innermost last, so another thread's never
40
43
  # mix with its.
@@ -70,28 +73,6 @@ module PathCoverage
70
73
  value
71
74
  end
72
75
 
73
- # { method site => { [outcome, ...] => calls } } for every
74
- # path taken all the way through so far.
75
- attr_reader :paths
76
-
77
- # A new call's path through the method at site, which has
78
- # size decisions.
79
- def path(site, size)
80
- Path.new(site, size, [])
81
- end
82
-
83
- # Notes value's outcome as path's index-th decision, counts
84
- # path once that is its last, or once it is exits, the
85
- # outcome whose arm leaves the method, and returns value
86
- # unchanged.
87
- def decide(path, index, value, exits = nil)
88
- outcome = value ? true : false
89
- path.outcomes[index] = outcome
90
- last = index == path.decisions - 1 || outcome == exits
91
- @paths[path.site][path.outcomes] += 1 if last
92
- value
93
- end
94
-
95
76
  # The truthy and falsy counts of every operand recorded so
96
77
  # far, keyed by [site, index]; an operand that never ran
97
78
  # has no entry.
@@ -6,21 +6,24 @@ require_relative 'manifest'
6
6
  require_relative 'paths_report'
7
7
  require_relative 'post'
8
8
  require_relative 'site_paths'
9
+ require_relative 'within_methods'
9
10
 
10
11
  module PathCoverage
11
12
  # The reporters, run from operands alone: needing nothing the
12
13
  # run recorded beyond them, they can run in another process.
13
14
  module Render
14
15
  # Runs each reporter chosen in config. operands, and taken's
15
- # site_paths: and outside:, are as Result gives them; files
16
- # is { full path => {source:, sites:} } for each covered file.
17
- def self.run(operands, files, config, paths = nil, **taken)
16
+ # site_paths: and outside:, are as Result gives them, and its
17
+ # reached: as Recorder#reached gives it; files is
18
+ # { full path => {source:, sites:} } for each covered file.
19
+ def self.run(operands, files, config, **taken)
18
20
  site_paths = every_site(taken.fetch(:site_paths, {}), files)
19
21
  reporters = config.reporters
20
22
  if reporters.include?(:gaps)
21
23
  gaps(operands, files, site_paths, taken.fetch(:outside, {}))
22
24
  end
23
- html_and_summary(operands, files, config, [paths, site_paths])
25
+ html_and_summary(operands, files, config,
26
+ [site_paths, taken[:reached]])
24
27
  return unless reporters.include?(:post)
25
28
 
26
29
  post(operands, files, config, site_paths)
@@ -36,29 +39,25 @@ module PathCoverage
36
39
  end
37
40
 
38
41
  # Runs the :html and :summary reporters, each when chosen.
39
- def self.html_and_summary(operands, files, config, paths_pair)
40
- paths, site_paths = paths_pair
42
+ def self.html_and_summary(operands, files, config, recorded)
43
+ site_paths, reached = recorded
41
44
  reporters = config.reporters
42
45
  if reporters.include?(:html)
43
- HtmlReport.write(operands, files, config, paths, site_paths:)
46
+ HtmlReport.write(operands, files, config, recorded)
44
47
  end
45
48
  return unless reporters.include?(:summary)
46
49
 
47
- summary(files, config, paths, site_paths)
50
+ cards = WithinMethods.cards(files, config, site_paths, reached)
51
+ summary(config, site_paths, cards)
48
52
  end
49
53
 
50
54
  # Prints the summary lines: the paths taken of all possible
51
- # within decisions, and, when paths are given, as they are
55
+ # within decisions, and, when cards are given, as they are
52
56
  # when methods are measured, within methods; then any
53
57
  # operators ignored.
54
- def self.summary(files, config, paths, site_paths)
55
- methods = files.map do |full_path, parsed|
56
- path = config.relative(full_path)
57
- PathManifest.methods(parsed[:source], path)
58
- end
59
- all = methods.reduce({}, :merge)
58
+ def self.summary(config, site_paths, cards)
60
59
  groups = config.groups
61
- puts ConsoleSummary.lines(site_paths, all, paths, groups) +
60
+ puts ConsoleSummary.lines(site_paths, cards, groups) +
62
61
  ConsoleSummary.ignored_lines(config.ignored)
63
62
  end
64
63
 
@@ -88,17 +87,15 @@ module PathCoverage
88
87
  end
89
88
 
90
89
  # Runs the reporters chosen in config from the JSON reports
91
- # at paths, their counts merged, parsing each covered file
92
- # afresh; with methods measured, the method paths they hold
93
- # too. paths is config.json_path alone by default.
90
+ # at paths, their counts and reaches merged, parsing each
91
+ # covered file afresh. paths is config.json_path alone by
92
+ # default.
94
93
  def self.from_json(config, paths = [config.json_path])
95
94
  operands = JsonReader.operands(paths, config.fingerprint)
96
95
  files = config.files.to_h do |path|
97
96
  [path, Manifest.parse(path, config)]
98
97
  end
99
- methods = config.measures.include?(:methods)
100
- taken = methods ? JsonReader.method_paths(paths) : nil
101
- run(operands, files, config, taken, **JsonReader.taken(paths))
98
+ run(operands, files, config, **JsonReader.taken(paths))
102
99
  end
103
100
  end
104
101
  end
@@ -7,19 +7,33 @@ module PathCoverage
7
7
  # outcome when the code runs. It inserts text at the byte
8
8
  # ranges the manifest found, and never parses.
9
9
  module Rewriter
10
- PATH_NAME = '__path_coverage_path'.freeze
11
-
12
10
  # Returns source with each operand in sites wrapped in a
13
11
  # record call, each site as a whole in a leave call that
14
- # enters it first, and each method in methods starting a
15
- # path that its decisions are wrapped in decide calls to add
16
- # to. Only text is inserted, never a newline, so backtrace
17
- # line numbers still match the file on disk.
18
- def self.rewrite(source, sites, methods = {})
12
+ # enters it first, and each statement in reaches, { name =>
13
+ # offset }, after a reach call that counts its runs. Only
14
+ # text is inserted, never a newline, so backtrace line
15
+ # numbers still match the file on disk.
16
+ def self.rewrite(source, sites, reaches = {})
19
17
  inserts = sites.flat_map do |site, condition|
20
18
  site_inserts(site, condition)
21
19
  end
22
- apply(source.dup, inserts + path_inserts(methods))
20
+ apply(source.dup, inserts + reach_inserts(reaches))
21
+ end
22
+
23
+ # Example: { "k.rb:2:2" => 18 } puts a reach call at 18.
24
+ # The inserts that put a reach call, a statement of its
25
+ # own, before each statement in reaches, outside all else
26
+ # there. A statement reached by
27
+ # its byte range, eg an endless def's body, is wrapped in
28
+ # parentheses with its call, being one expression.
29
+ def self.reach_inserts(reaches)
30
+ rank = [Float::INFINITY, LAYERS.index(:reach)]
31
+ reaches.flat_map do |name, at|
32
+ call = "PathCoverage.reach(#{name.inspect}); "
33
+ next [[at, call, rank]] unless at.is_a?(Range)
34
+
35
+ [[at.begin, "(#{call}", rank], [at.end, ')', rank, :close]]
36
+ end
23
37
  end
24
38
 
25
39
  # The inserts for one site, as its kind gives them, eg its
@@ -69,53 +83,21 @@ module PathCoverage
69
83
  "PathCoverage.record(#{site.inspect}, #{index}, "
70
84
  end
71
85
 
72
- # The inserts that start each method's path at its entry,
73
- # outermost of all there, and wrap each of its decisions'
74
- # predicates in a decide call, outside any record or
75
- # outcome call.
76
- def self.path_inserts(methods)
77
- methods.flat_map do |site, method|
78
- decisions = method.decisions
79
- decides = decisions.each_with_index.flat_map do |decision, i|
80
- call = "PathCoverage.decide(#{PATH_NAME}, #{i}, "
81
- tail = decision.exits.nil? ? '' : ", #{decision.exits}"
82
- command = decision.command
83
- wrap(call, decision.range, command, :decision, tail)
84
- end
85
- [start_insert(site, method)] + decides
86
- end
87
- end
88
-
89
- # The insert at method's entry that starts a call's path,
90
- # outermost of all there.
91
- def self.start_insert(site, method)
92
- start = path_start(site, method.decisions.size)
93
- [method.entry, start, [Float::INFINITY, LAYERS.index(:path)]]
94
- end
95
-
96
- # The statement that starts a call's path through the
97
- # method at site, which has size decisions.
98
- def self.path_start(site, size)
99
- call = "PathCoverage.path(#{site.inspect}, #{size})"
100
- "#{PATH_NAME} = #{call}; "
101
- end
102
-
103
86
  # The layers of inserts, innermost first, that break a tie
104
87
  # between inserts wrapping the same range: a leaf's call, a
105
88
  # nested kind's call around its whole, eg end_items, a site's
106
- # leave, a method's decide, and a method's path start.
107
- LAYERS = %i[leaf kind site decision path].freeze
89
+ # leave, and a statement's reach.
90
+ LAYERS = %i[leaf kind site reach].freeze
108
91
 
109
- # The two inserts that wrap the code at range in call, with
110
- # tail as the call's further arguments. Their order, [size,
111
- # rank], nests them among inserts at the same offset: the
112
- # longer range outside, the rank of layer in LAYERS breaking
113
- # a tie. A command call is parenthesised, as it is no bare
114
- # argument; nothing else is, since parentheses make a
115
- # statement, which gives its line a line-coverage count of
116
- # its own.
117
- def self.wrap(call, range, command, layer, tail = '')
118
- open, close = command ? ['(', ")#{tail})"] : ['', "#{tail})"]
92
+ # The two inserts that wrap the code at range in call. Their
93
+ # order, [size, rank], nests them among inserts at the same
94
+ # offset: the longer range outside, the rank of layer in
95
+ # LAYERS breaking a tie. A command call is parenthesised, as
96
+ # it is no bare argument; nothing else is, since parentheses
97
+ # make a statement, which gives its line a line-coverage
98
+ # count of its own.
99
+ def self.wrap(call, range, command, layer)
100
+ open, close = command ? ['(', '))'] : ['', ')']
119
101
  order = [range.size, LAYERS.index(layer)]
120
102
  [
121
103
  [range.begin, call + open, order],
@@ -129,10 +111,9 @@ module PathCoverage
129
111
  # offset lands before text already there, so at one offset
130
112
  # opening inserts go in lowest order first, leaving the
131
113
  # highest outermost, then closing ones highest order first,
132
- # so the innermost call closes first and each tail stays
133
- # with its own call. Eg a ternary that is a method's
134
- # decision is a site too: its leave, wrapping the whole
135
- # ternary, opens outside the decide wrapping its predicate.
114
+ # so the innermost call closes first. Eg a ternary whose
115
+ # predicate is a leaf: the site's leave, wrapping the whole
116
+ # ternary, opens outside the record wrapping its predicate.
136
117
  def self.apply(source, inserts)
137
118
  sorted = inserts.sort_by do |offset, _, order, close|
138
119
  size, rank = order
@@ -13,15 +13,24 @@ module PathCoverage
13
13
  # an arm { arm:, text:, cells:, body: } whether the column
14
14
  # took it, body being its condition's tree, nil for a plain
15
15
  # arm. Texts are cut from source at the manifest's ranges.
16
+ # An elsif's tree is labelled elsif, as it is written.
16
17
  def self.of(source, condition, paths)
17
18
  columns = paths.sort_by.with_index do |taken, i|
18
19
  [taken[:count].zero? ? 0 : 1, i]
19
20
  end
20
21
  outcomes = columns.map { |taken| taken[:path] }
21
22
  tree = node(source, condition, outcomes, (0..).each)
23
+ tree = tree.merge(label: 'elsif') if elsif?(source, condition)
22
24
  tree.merge(counts: columns.map { |taken| taken[:count] })
23
25
  end
24
26
 
27
+ # True for an if site written as an elsif: an if's extent
28
+ # starts at its keyword.
29
+ def self.elsif?(source, condition)
30
+ condition.operator == 'if' &&
31
+ source.byteslice(condition.extent.begin, 5) == 'elsif'
32
+ end
33
+
25
34
  # The tree of condition, as its kind builds it, each leaf
26
35
  # taking the next index of leaves, so leaves pair with path
27
36
  # entries left to right.