path_coverage 0.1.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 (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +315 -107
  3. data/lib/path_coverage/calls.rb +55 -23
  4. data/lib/path_coverage/card.rb +14 -0
  5. data/lib/path_coverage/condition.rb +33 -3
  6. data/lib/path_coverage/configuration.rb +43 -22
  7. data/lib/path_coverage/configuration_paths.rb +58 -0
  8. data/lib/path_coverage/console_summary.rb +75 -0
  9. data/lib/path_coverage/gaps.rb +32 -13
  10. data/lib/path_coverage/html/file.html +5 -5
  11. data/lib/path_coverage/html/index.html +6 -18
  12. data/lib/path_coverage/html/settings.js +50 -0
  13. data/lib/path_coverage/html/side.js +341 -413
  14. data/lib/path_coverage/html/sort.js +24 -21
  15. data/lib/path_coverage/html/style.css +339 -207
  16. data/lib/path_coverage/html/tabs.js +15 -0
  17. data/lib/path_coverage/html/theme.js +27 -0
  18. data/lib/path_coverage/html_branch.rb +30 -0
  19. data/lib/path_coverage/html_cards.rb +111 -0
  20. data/lib/path_coverage/html_cells.rb +66 -0
  21. data/lib/path_coverage/html_file.rb +82 -68
  22. data/lib/path_coverage/html_index.rb +95 -41
  23. data/lib/path_coverage/html_index_head.rb +29 -0
  24. data/lib/path_coverage/html_labels.rb +23 -0
  25. data/lib/path_coverage/html_methods.rb +66 -0
  26. data/lib/path_coverage/html_nav.rb +50 -50
  27. data/lib/path_coverage/html_report.rb +69 -56
  28. data/lib/path_coverage/html_statements.rb +166 -0
  29. data/lib/path_coverage/html_syntax.rb +156 -0
  30. data/lib/path_coverage/html_tabs.rb +26 -0
  31. data/lib/path_coverage/html_template.rb +6 -0
  32. data/lib/path_coverage/html_tree.rb +59 -74
  33. data/lib/path_coverage/json_reader.rb +101 -0
  34. data/lib/path_coverage/json_writer.rb +52 -7
  35. data/lib/path_coverage/kernel.rb +55 -53
  36. data/lib/path_coverage/kinds/base.rb +71 -0
  37. data/lib/path_coverage/kinds/block.rb +137 -0
  38. data/lib/path_coverage/kinds/case.rb +92 -0
  39. data/lib/path_coverage/kinds/chain.rb +45 -0
  40. data/lib/path_coverage/kinds/choice.rb +68 -0
  41. data/lib/path_coverage/kinds/contains.rb +113 -0
  42. data/lib/path_coverage/kinds/safe.rb +153 -0
  43. data/lib/path_coverage/kinds/write.rb +54 -0
  44. data/lib/path_coverage/kinds.rb +37 -0
  45. data/lib/path_coverage/manifest.rb +60 -20
  46. data/lib/path_coverage/manifest_blocks.rb +48 -10
  47. data/lib/path_coverage/manifest_case.rb +2 -3
  48. data/lib/path_coverage/manifest_chains.rb +76 -0
  49. data/lib/path_coverage/manifest_choices.rb +32 -18
  50. data/lib/path_coverage/manifest_folding.rb +91 -0
  51. data/lib/path_coverage/manifest_ignoring.rb +35 -0
  52. data/lib/path_coverage/manifest_writes.rb +67 -0
  53. data/lib/path_coverage/merge.rb +19 -3
  54. data/lib/path_coverage/method_statements.rb +300 -0
  55. data/lib/path_coverage/post.rb +8 -8
  56. data/lib/path_coverage/recorder.rb +20 -79
  57. data/lib/path_coverage/recorder_blocks.rb +68 -0
  58. data/lib/path_coverage/recorder_cases.rb +23 -0
  59. data/lib/path_coverage/recorder_chains.rb +40 -0
  60. data/lib/path_coverage/recorder_writes.rb +26 -0
  61. data/lib/path_coverage/render.rb +56 -47
  62. data/lib/path_coverage/result.rb +19 -0
  63. data/lib/path_coverage/rewriter.rb +69 -107
  64. data/lib/path_coverage/rewriter_nested.rb +62 -0
  65. data/lib/path_coverage/site_paths.rb +9 -58
  66. data/lib/path_coverage/site_tree.rb +21 -57
  67. data/lib/path_coverage/statement_arms.rb +161 -0
  68. data/lib/path_coverage/summary.rb +41 -17
  69. data/lib/path_coverage/totals.rb +6 -12
  70. data/lib/path_coverage/version.rb +1 -1
  71. data/lib/path_coverage/within_methods.rb +92 -0
  72. metadata +40 -11
  73. data/lib/path_coverage/html/toggle.js +0 -12
  74. data/lib/path_coverage/html_paths.rb +0 -138
  75. data/lib/path_coverage/path_gaps.rb +0 -41
  76. 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
@@ -6,18 +6,18 @@ module PathCoverage
6
6
  # for runs whose own files are thrown away, eg a failing
7
7
  # docker build step.
8
8
  module Post
9
- # POSTs operands and paths, as the JSON report, to url,
10
- # never through an http_proxy, which could not reach a
11
- # collector local to the run. A collector that cannot be
12
- # reached, or does not answer within a second, is passed
13
- # over in silence: the run's own output and exit status
14
- # must stay as they are.
15
- def self.deliver(operands, url, paths = {})
9
+ # POSTs operands and paths, as the JSON report with its
10
+ # fingerprint, to url, never through an http_proxy,
11
+ # which could not reach a collector local to the run. A
12
+ # collector that cannot be reached, or does not answer
13
+ # within a second, is passed over in silence: the run's own
14
+ # output and exit status must stay as they are.
15
+ def self.deliver(operands, url, paths = {}, fingerprint: {})
16
16
  uri = URI(url)
17
17
  http = Net::HTTP.new(uri.host, uri.port, nil)
18
18
  http.open_timeout = http.read_timeout = 1
19
19
  headers = { 'Content-Type' => 'application/json' }
20
- body = JsonWriter.json(operands, paths)
20
+ body = JsonWriter.json(operands, paths, fingerprint:)
21
21
  http.post(uri.path, body, headers)
22
22
  rescue StandardError
23
23
  nil
@@ -1,31 +1,43 @@
1
1
  require_relative 'condition'
2
+ require_relative 'recorder_blocks'
3
+ require_relative 'recorder_cases'
4
+ require_relative 'recorder_chains'
5
+ require_relative 'recorder_writes'
2
6
 
3
7
  module PathCoverage
4
8
  # Records the outcome of each instrumented operand.
5
9
  class Recorder
6
- # One call's way through its method: the outcome of each
7
- # decision made so far, of the number of decisions its
8
- # method has.
9
- Path = Struct.new(:site, :decisions, :outcomes)
10
+ include RecorderBlocks
11
+ include RecorderCases
12
+ include RecorderChains
13
+ include RecorderWrites
10
14
 
11
15
  # One evaluation's way through its site: the outcome of
12
16
  # each leaf, nil for one not yet, or never, reached.
13
17
  Trail = Struct.new(:site, :outcomes)
14
18
 
15
- # Starts with no outcomes, paths or site paths counted.
19
+ # Starts with no outcomes, site paths or reaches counted.
16
20
  def initialize
17
21
  @counts = Hash.new do |counts, key|
18
22
  counts[key] = { truthy: 0, falsy: 0 }
19
23
  end
20
- @paths = Hash.new do |paths, site|
21
- paths[site] = Hash.new(0)
22
- end
23
24
  @site_paths = Hash.new do |site_paths, site|
24
25
  site_paths[site] = Hash.new(0)
25
26
  end
27
+ @reached = Hash.new(0)
26
28
  @trails_key = :"path_coverage_trails_#{object_id}"
27
29
  end
28
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
+
29
41
  # The stack of site's evaluations under way in the current
30
42
  # thread (fiber), innermost last, so another thread's never
31
43
  # mix with its.
@@ -61,28 +73,6 @@ module PathCoverage
61
73
  value
62
74
  end
63
75
 
64
- # { method site => { [outcome, ...] => calls } } for every
65
- # path taken all the way through so far.
66
- attr_reader :paths
67
-
68
- # A new call's path through the method at site, which has
69
- # size decisions.
70
- def path(site, size)
71
- Path.new(site, size, [])
72
- end
73
-
74
- # Notes value's outcome as path's index-th decision, counts
75
- # path once that is its last, or once it is exits, the
76
- # outcome whose arm leaves the method, and returns value
77
- # unchanged.
78
- def decide(path, index, value, exits = nil)
79
- outcome = value ? true : false
80
- path.outcomes[index] = outcome
81
- last = index == path.decisions - 1 || outcome == exits
82
- @paths[path.site][path.outcomes] += 1 if last
83
- value
84
- end
85
-
86
76
  # The truthy and falsy counts of every operand recorded so
87
77
  # far, keyed by [site, index]; an operand that never ran
88
78
  # has no entry.
@@ -129,54 +119,5 @@ module PathCoverage
129
119
  leave(trails(site).last, matched) if last
130
120
  matched
131
121
  end
132
-
133
- # Starts an evaluation of a stop-early block method's call
134
- # at site, its block not yet called with any item.
135
- def enter_items(site)
136
- trail = enter(site, 2)
137
- trail.outcomes[0] = ITEMS[0]
138
- trail
139
- end
140
-
141
- # Notes value, the block's answer, as its last, and one more
142
- # item it was called with; returns value unchanged. A block
143
- # called where its call was not entered, eg in another
144
- # fiber, is counted but noted on no path.
145
- def item(site, value)
146
- trail = trails(site).last
147
- one_more(trail) if trail
148
- record(site, 1, value)
149
- end
150
-
151
- # Notes value, the block's answer for one more item, among
152
- # all its answers so far: all true, all false, or both;
153
- # returns value unchanged. A block called where its call
154
- # was not entered is counted but noted on no path.
155
- def answer(site, value)
156
- trail = trails(site).last
157
- if trail
158
- one_more(trail)
159
- now = value ? ANSWERS[0] : ANSWERS[1]
160
- same = [nil, now].include?(trail.outcomes[1])
161
- trail.outcomes[1] = same ? now : ANSWERS[2]
162
- end
163
- @counts[[site, 1]][value ? :truthy : :falsy] += 1
164
- value
165
- end
166
-
167
- # Notes on trail one more item its block was called with.
168
- def one_more(trail)
169
- called = ITEMS.index(trail.outcomes[0]) + 1
170
- trail.outcomes[0] = ITEMS[[called, 2].min]
171
- end
172
-
173
- # Ends a block method's call, counting its receiver truthy
174
- # when its block was called, falsy when not, and its path;
175
- # returns value, the call's, unchanged.
176
- def leave_items(trail, value)
177
- called = trail.outcomes[0] != ITEMS[0]
178
- @counts[[trail.site, 0]][called ? :truthy : :falsy] += 1
179
- leave(trail, value)
180
- end
181
122
  end
182
123
  end
@@ -0,0 +1,68 @@
1
+ require_relative 'condition'
2
+
3
+ module PathCoverage
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 site of its own. It relies
8
+ # on its includer's counts, record and trails.
9
+ module RecorderBlocks
10
+ # Starts a block method's call in site, its receiver the
11
+ # site's at-th leaf, its block not yet called with any item;
12
+ # returns the site's current trail, nil for none.
13
+ def begin_items(site, at)
14
+ trail = trails(site).last
15
+ trail.outcomes[at] = ITEMS[0] if trail
16
+ trail
17
+ end
18
+
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
+ # site'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.
24
+ def item_at(site, at, value)
25
+ trail = trails(site).last
26
+ one_more(trail, at) if trail
27
+ record(site, at + 1, value)
28
+ end
29
+
30
+ # Notes value, the block's answer for one more item, among
31
+ # all its answers so far: all true, all false, or both, for a
32
+ # call whose receiver is site's at-th leaf; returns value
33
+ # unchanged. A block called where its call was not entered
34
+ # is counted but noted on no path.
35
+ def answer_at(site, at, value)
36
+ trail = trails(site).last
37
+ note_answer(trail, at, value) if trail
38
+ @counts[[site, at + 1]][value ? :truthy : :falsy] += 1
39
+ value
40
+ end
41
+
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.
44
+ def note_answer(trail, at, value)
45
+ one_more(trail, at)
46
+ now = value ? ANSWERS[0] : ANSWERS[1]
47
+ same = [nil, now].include?(trail.outcomes[at + 1])
48
+ trail.outcomes[at + 1] = same ? now : ANSWERS[2]
49
+ end
50
+
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
+ # Ends a block method's call whose receiver is site's at-th
59
+ # 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.
62
+ def end_items(site, at, trail, value)
63
+ called = trail.nil? || trail.outcomes[at] != ITEMS[0]
64
+ @counts[[site, at]][called ? :truthy : :falsy] += 1
65
+ value
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,23 @@
1
+ module PathCoverage
2
+ # The part of the recorder that notes a case nested in another
3
+ # site, eg (case a when 1 then b end) && c: the site around it
4
+ # enters and leaves, so its patterns only note whether they
5
+ # matched. It relies on its includer's record.
6
+ module RecorderCases
7
+ # A stand-in for the pattern of a case nested in site, its
8
+ # match noted as the site's index-th leaf.
9
+ NestedMatch = Struct.new(:recorder, :site, :index, :pattern) do
10
+ # Whether pattern matches subject, exactly as pattern says,
11
+ # noted on the site's path.
12
+ def ===(subject)
13
+ recorder.record(site, index, pattern === subject)
14
+ end
15
+ end
16
+
17
+ # The stand-in for pattern, the index-th leaf of site, in a
18
+ # case nested in it.
19
+ def pattern(site, index, pattern)
20
+ NestedMatch.new(self, site, index, pattern)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,40 @@
1
+ require_relative 'condition'
2
+
3
+ module PathCoverage
4
+ # The part of the recorder that notes &. chains. It relies on
5
+ # its includer's counts, record and trails.
6
+ module RecorderChains
7
+ # Notes value, the index-th leaf of site, a &. receiver, as
8
+ # not nil or nil, and returns it unchanged; it counts truthy
9
+ # when not nil. A receiver after a nil one in its chain,
10
+ # whose leaves start at first, was never reached, as Ruby
11
+ # skips the rest of the chain, so is not noted.
12
+ def present(site, index, first, value)
13
+ return value unless reached?(site, first, index)
14
+
15
+ @counts[[site, index]][value.nil? ? :falsy : :truthy] += 1
16
+ trail = trails(site).last
17
+ trail.outcomes[index] = PRESENCE[value.nil? ? 1 : 0] if trail
18
+ value
19
+ end
20
+
21
+ # Notes value, a &. chain's value as the index-th leaf of
22
+ # site, as record does, when every receiver of the chain,
23
+ # whose leaves start at first, was not nil; returns value
24
+ # unchanged.
25
+ def chained(site, index, first, value)
26
+ return value unless reached?(site, first, index)
27
+
28
+ record(site, index, value)
29
+ end
30
+
31
+ # True when site's current evaluation reached its index-th
32
+ # leaf: every leaf of its chain before it, from first, was
33
+ # not nil. An evaluation with no trail, eg in another fiber,
34
+ # is taken to reach it.
35
+ def reached?(site, first, index)
36
+ trail = trails(site).last
37
+ trail.nil? || trail.outcomes[first...index].all?(PRESENCE[0])
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,26 @@
1
+ module PathCoverage
2
+ # The part of the recorder that notes ||= and &&= writes: the
3
+ # target's outcome at its place among its site's leaves, at 0
4
+ # for a write that is a site of its own. It relies on its
5
+ # includer's record and trails.
6
+ module RecorderWrites
7
+ # Notes outcome as the target's, site's at-th leaf, of the
8
+ # write whose right side ran, false for ||=, true for &&=,
9
+ # and returns value, the right side's, unchanged. The target
10
+ # is never read, as reading it could call a method twice.
11
+ def written_at(site, at, outcome, value)
12
+ record(site, at, outcome)
13
+ value
14
+ end
15
+
16
+ # Notes skipped as the outcome of the write's target, site's
17
+ # at-th leaf, when its right side never ran, so noted none;
18
+ # returns value, the write's, unchanged.
19
+ def settle(site, at, skipped, value)
20
+ trail = trails(site).last
21
+ unnoted = trail.nil? || trail.outcomes[at].nil?
22
+ record(site, at, skipped) if unnoted
23
+ value
24
+ end
25
+ end
26
+ end