path_coverage 0.2.0 → 0.4.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 (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +172 -121
  3. data/lib/path_coverage/calls.rb +4 -11
  4. data/lib/path_coverage/card.rb +14 -0
  5. data/lib/path_coverage/card_tree.rb +186 -0
  6. data/lib/path_coverage/card_ways.rb +140 -0
  7. data/lib/path_coverage/condition.rb +11 -4
  8. data/lib/path_coverage/configuration.rb +1 -17
  9. data/lib/path_coverage/configuration_paths.rb +4 -2
  10. data/lib/path_coverage/console_summary.rb +15 -17
  11. data/lib/path_coverage/gaps.rb +12 -9
  12. data/lib/path_coverage/html/file.html +5 -5
  13. data/lib/path_coverage/html/index.html +3 -1
  14. data/lib/path_coverage/html/settings.js +50 -0
  15. data/lib/path_coverage/html/side.js +470 -432
  16. data/lib/path_coverage/html/style.css +345 -226
  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 +88 -0
  20. data/lib/path_coverage/html_cells.rb +71 -0
  21. data/lib/path_coverage/html_file.rb +82 -68
  22. data/lib/path_coverage/html_index.rb +28 -29
  23. data/lib/path_coverage/html_index_head.rb +10 -30
  24. data/lib/path_coverage/html_labels.rb +16 -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 +45 -49
  28. data/lib/path_coverage/html_statements.rb +186 -0
  29. data/lib/path_coverage/html_syntax.rb +167 -0
  30. data/lib/path_coverage/html_template.rb +6 -0
  31. data/lib/path_coverage/html_tree.rb +90 -88
  32. data/lib/path_coverage/json_reader.rb +21 -14
  33. data/lib/path_coverage/json_writer.rb +26 -13
  34. data/lib/path_coverage/kernel.rb +32 -51
  35. data/lib/path_coverage/kinds/block.rb +2 -1
  36. data/lib/path_coverage/kinds/case.rb +7 -9
  37. data/lib/path_coverage/kinds/choice.rb +3 -7
  38. data/lib/path_coverage/kinds/contains.rb +9 -5
  39. data/lib/path_coverage/kinds/safe.rb +6 -18
  40. data/lib/path_coverage/kinds/write.rb +0 -6
  41. data/lib/path_coverage/manifest.rb +5 -3
  42. data/lib/path_coverage/manifest_blocks.rb +3 -4
  43. data/lib/path_coverage/manifest_case.rb +2 -3
  44. data/lib/path_coverage/manifest_choices.rb +41 -20
  45. data/lib/path_coverage/manifest_folding.rb +48 -11
  46. data/lib/path_coverage/merge.rb +3 -3
  47. data/lib/path_coverage/method_statements.rb +305 -0
  48. data/lib/path_coverage/recorder.rb +12 -31
  49. data/lib/path_coverage/render.rb +19 -22
  50. data/lib/path_coverage/rewriter.rb +36 -55
  51. data/lib/path_coverage/site_paths.rb +8 -0
  52. data/lib/path_coverage/site_tree.rb +9 -0
  53. data/lib/path_coverage/statement_arms.rb +194 -0
  54. data/lib/path_coverage/summary.rb +11 -15
  55. data/lib/path_coverage/totals.rb +6 -12
  56. data/lib/path_coverage/version.rb +1 -1
  57. data/lib/path_coverage/within_methods.rb +177 -0
  58. metadata +19 -12
  59. data/lib/path_coverage/html/toggle.js +0 -12
  60. data/lib/path_coverage/html_paths.rb +0 -138
  61. data/lib/path_coverage/path_gaps.rb +0 -41
  62. data/lib/path_coverage/path_manifest.rb +0 -110
@@ -221,11 +221,12 @@ module PathCoverage
221
221
  # commands and fixed leaves where it is one; or, when sites
222
222
  # that fold are found inside it, the leaf with them nested.
223
223
  def leaf(operand)
224
- inner = collected { operand.accept(self) }
224
+ inner = leaf_sites(operand)
225
225
  loc = operand.location
226
226
  range = loc.start_offset...loc.end_offset
227
227
  @notes[:commands] << range if Manifest.command?(operand)
228
- outcome = Manifest.fixed_outcome(operand)
228
+ outcome = Manifest.fixed_outcome(operand) ||
229
+ given_outcome(operand)
229
230
  @notes[:fixed][range] = outcome if outcome
230
231
  nonempty = nonempty_call?(operand, inner)
231
232
  contained(inner, range, nonempty:)
@@ -236,7 +237,8 @@ module PathCoverage
236
237
  def grouped(operand)
237
238
  return operand unless operand.type == :parentheses_node
238
239
 
239
- statements = operand.body&.body || []
240
+ body = operand.body
241
+ statements = body ? body.body : []
240
242
  statements.size == 1 ? statements.first : operand
241
243
  end
242
244
  end
@@ -20,11 +20,10 @@ module PathCoverage
20
20
  plain_call?(node) && value?(block_expression(node.block))
21
21
  end
22
22
 
23
- # True for a call on a receiver, with no arguments, and not
24
- # by &.
23
+ # True for a call on a receiver, with no arguments. A call by
24
+ # &. never gets here, as it is a site of its own first.
25
25
  def plain_call?(node)
26
- !node.receiver.nil? && node.arguments.nil? &&
27
- !node.safe_navigation?
26
+ !node.receiver.nil? && node.arguments.nil?
28
27
  end
29
28
 
30
29
  # True for a block's one expression that is a value, so can
@@ -13,9 +13,8 @@ module PathCoverage
13
13
  def case_site?(node)
14
14
  return false if node.predicate.nil?
15
15
 
16
- node.conditions.none? do |clause|
17
- clause.conditions.any? { |each| each.type == :splat_node }
18
- end
16
+ patterns = node.conditions.flat_map(&:conditions)
17
+ !patterns.map(&:type).include?(:splat_node)
19
18
  end
20
19
 
21
20
  # A case as a Condition, its extent its case keyword to the
@@ -11,15 +11,16 @@ module PathCoverage
11
11
  # being an if_node with no if keyword.
12
12
  CHOICE_TYPES = %i[if_node unless_node].freeze
13
13
 
14
- # Where node's site starts: a &. chain's start; a block
15
- # method's call at its method; an if's or an unless's
16
- # keyword, which a modifier's statement comes before;
17
- # anything else's own start.
14
+ # Where node's site is named: a &. chain's start; a block
15
+ # method's call at its method; a ternary at its ?, so it never
16
+ # shares the name of a site starting where it does, eg its
17
+ # predicate; an if's or an unless's keyword, which a
18
+ # modifier's statement comes before; anything else's own start.
18
19
  def site_loc(node)
19
20
  return node.location if safe_chain?(node)
20
21
  return node.message_loc if node.type == :call_node
21
22
  return node.location unless CHOICE_TYPES.include?(node.type)
22
- return node.location if ternary?(node)
23
+ return node.then_keyword_loc if ternary?(node)
23
24
 
24
25
  keyword_loc(node)
25
26
  end
@@ -101,26 +102,46 @@ module PathCoverage
101
102
  ]
102
103
  end
103
104
 
104
- # One arm of a ternary, statements its code: a condition
105
- # folds in as its body, being part of the one expression, as
106
- # does a plain arm with sites inside it, its value its last
107
- # leaf; a plain arm with none has no body.
105
+ # One arm of a ternary, statements its code: a ternary, or a
106
+ # block method's call whose value follows from its path, is a
107
+ # site of its own, as an if's arm holding an if is, so the arm
108
+ # is plain; another condition folds in as its body, being part
109
+ # of the one expression, as does a plain arm with sites inside
110
+ # it, its value its last leaf; a plain arm with none has no
111
+ # body.
108
112
  def arm_of(kind, statements)
109
113
  written = grouped(statements.body.first)
110
114
  extent = extent_of(statements)
111
- body = if foldable?(written)
112
- condition_of(written)
115
+ body = if ternary?(written) || valued?(written)
116
+ own_site(statements)
113
117
  else
114
- inner = collected { statements.accept(self) }
115
- inner.empty? ? nil : contained(inner, extent)
118
+ arm_body(written, statements, extent)
116
119
  end
117
120
  Arm.new(kind:, extent:, body:)
118
121
  end
119
122
 
120
- # True for a condition that folds into a ternary's site:
121
- # an &&, ||, and, or, or a ternary, none of them ignored.
123
+ # No body, for an arm, statements its code, whose one site is
124
+ # a site of its own: visited in a new scope, so noted as one.
125
+ def own_site(statements)
126
+ scoped { statements.accept(self) }
127
+ nil
128
+ end
129
+
130
+ # The body of a ternary's arm, statements its code, written its
131
+ # one expression, extent its byte range: the condition written
132
+ # is, when it folds; else the sites inside it, contained, nil
133
+ # for none.
134
+ def arm_body(written, statements, extent)
135
+ return condition_of(written) if foldable?(written)
136
+
137
+ inner = collected { statements.accept(self) }
138
+ inner.empty? ? nil : contained(inner, extent)
139
+ end
140
+
141
+ # True for a condition that folds into a ternary's site: an
142
+ # &&, ||, and or or not ignored.
122
143
  def foldable?(node)
123
- (operator?(node) || ternary?(node)) && !ignored?(node)
144
+ operator?(node) && !ignored?(node)
124
145
  end
125
146
 
126
147
  # True for a ternary, an if with no if keyword.
@@ -128,13 +149,13 @@ module PathCoverage
128
149
  node.type == :if_node && node.if_keyword_loc.nil?
129
150
  end
130
151
 
131
- # A ternary's or an if's predicate as its operand: an && or
132
- # || nested as a Condition, as is a &. chain, its value its
133
- # last leaf, anything else a leaf.
152
+ # A ternary's or an if's predicate as its operand: a &. chain
153
+ # nested as a Condition, its value its last leaf; anything
154
+ # else a leaf, an && or || among them, as a site of its own,
155
+ # as leaf_sites has it.
134
156
  def predicate_of(node)
135
157
  predicate = grouped(node.predicate)
136
158
  return leaf(predicate) if ignored?(predicate)
137
- return condition_of(predicate) if operator?(predicate)
138
159
  if safe_chain?(predicate)
139
160
  return chain_condition(predicate, value: true)
140
161
  end
@@ -1,4 +1,6 @@
1
1
  require_relative 'condition'
2
+ require_relative 'site'
3
+ require_relative 'site_paths'
2
4
 
3
5
  module PathCoverage
4
6
  # The part of the manifest's visitor that folds every site in one
@@ -7,9 +9,50 @@ module PathCoverage
7
9
  # Condition of that leaf, rather than noted as a site of its own.
8
10
  # A new scope, eg a block's body or an if's arm, stops the
9
11
  # collecting, so a site in it is a site of its own. It relies on
10
- # its includer's condition_of, chain_condition, safe_chain? and
11
- # extent_of.
12
+ # its includer's condition_of, chain_condition, safe_chain?,
13
+ # block_site? and extent_of.
12
14
  module ManifestFolding
15
+ # The sites that fold into the leaf operand, as collected
16
+ # finds them; none for a leaf that is wholly a site of its own,
17
+ # as own_site? has it, visited as one, as the leaf's outcome is
18
+ # its value.
19
+ def leaf_sites(operand)
20
+ unless own_site?(operand)
21
+ return collected { operand.accept(self) }
22
+ end
23
+
24
+ scoped { operand.accept(self) }
25
+ []
26
+ end
27
+
28
+ # True for a leaf that is wholly a site of its own: a block
29
+ # method's call whose value follows from its path, or an && or
30
+ # ||, eg a predicate, not ignored.
31
+ def own_site?(node)
32
+ valued?(node) || (operator?(node) && !ignored?(node))
33
+ end
34
+
35
+ # True for a block method's call that is a site, not ignored,
36
+ # whose value follows from its path, eg any?, but not find.
37
+ def valued?(node)
38
+ node.type == :call_node && block_site?(node) &&
39
+ VALUED.include?(node.name.to_s) && !ignored?(node)
40
+ end
41
+
42
+ # The one outcome each of a site's values gives a leaf.
43
+ GIVEN = { [true] => :truthy, [false] => :falsy }.freeze
44
+
45
+ # The one outcome the leaf node can have, as the site it wholly
46
+ # is gives it on every one of its paths, eg :truthy for a
47
+ # select, whose array is truthy; nil when its paths give both,
48
+ # or node is no such site.
49
+ def given_outcome(node)
50
+ return nil unless own_site?(node)
51
+
52
+ site = @sites.fetch(Site.at(@path, site_loc(node)))
53
+ GIVEN[SitePaths.values(site).uniq]
54
+ end
55
+
13
56
  # node's Condition as a site nested in a leaf: a &. chain's
14
57
  # with its value as its last leaf, as its leaf's outcome
15
58
  # follows from it.
@@ -43,17 +86,11 @@ module PathCoverage
43
86
  # range as a leaf, or, with inner sites found in it, a Condition
44
87
  # named contains: those sites, then range, the leaf's outcome,
45
88
  # fixed as :nonempty when nonempty. A leaf that is wholly one
46
- # block method's call whose value follows from its path is
47
- # that call alone, needing no outcome of its own.
89
+ # block method's call whose value follows from its path never
90
+ # comes here: it is a site of its own, as leaf_sites has it.
48
91
  def contained(inner, range, nonempty: false)
49
92
  return range if inner.empty?
50
93
 
51
- only = inner.first
52
- if inner.size == 1 && only.extent == range &&
53
- VALUED.include?(only.operator)
54
- return only
55
- end
56
-
57
94
  fixed = nonempty ? { range => :nonempty } : {}
58
95
  Condition.new(operator: CONTAINS, operands: [*inner, range],
59
96
  extent: range, fixed:)
@@ -70,7 +107,7 @@ module PathCoverage
70
107
 
71
108
  receiver = extent_of(grouped(node.receiver))
72
109
  @nonempty.include?(node.name.to_s) &&
73
- inner.any? { |site| site.extent == receiver }
110
+ inner.map(&:extent).include?(receiver)
74
111
  end
75
112
 
76
113
  # A block's body is a new scope: it runs once per item.
@@ -28,11 +28,11 @@ module PathCoverage
28
28
  end
29
29
  end
30
30
 
31
- # One method paths hash, { site => { path => count } }, as
32
- # the recorder counts them, each path's count the sum of
31
+ # One listed hash, { site => { path => count } }, eg the
32
+ # paths a treatment rules out, each path's count the sum of
33
33
  # every run's, eg [true, false] taken once in each of two
34
34
  # runs is taken twice.
35
- def self.method_paths(runs)
35
+ def self.listed(runs)
36
36
  runs.reduce do |total, run|
37
37
  total.merge(run) do |_site, mine, theirs|
38
38
  mine.merge(theirs) { |_path, a, b| a + b }
@@ -0,0 +1,305 @@
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
+ seen = taken(rows, paths, reached).compact.sum
102
+ [seen, size(rows.map(&:paths))]
103
+ end
104
+ end
105
+
106
+ # Example: rows of 1, 2 and 2 paths, [1, 2, 2], have size 5.
107
+ # The paths a method's tests are to take: paths, its rows'
108
+ # paths, added up, each row's taken on its own.
109
+ def self.size(paths)
110
+ paths.sum
111
+ end
112
+
113
+ # 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,
115
+ # its paths with a count, paths being Result#paths; for any
116
+ # other row, its 1 path when reached has a count for it, 0
117
+ # when not, nil, not known, without reached.
118
+ def self.taken(rows, paths, reached = nil)
119
+ rows.map do |row|
120
+ next taken_site(row, paths) if row.site
121
+ next nil if reached.nil?
122
+
123
+ reached.key?(row.name) ? 1 : 0
124
+ end
125
+ end
126
+
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] }
130
+ counts.count(&:positive?)
131
+ end
132
+
133
+ # Collects each def's top-level statements.
134
+ class Collector < Prism::Visitor
135
+ include ManifestChains
136
+ include ManifestChoices
137
+ include StatementArms
138
+
139
+ # Starts with no methods found; sites are decision mode's,
140
+ # { site => Condition }.
141
+ def initialize(path, sites)
142
+ @path = path
143
+ @sites = sites
144
+ @methods = {}
145
+ @names = {}
146
+ @lasts = {}
147
+ @reaches = {}
148
+ super()
149
+ end
150
+
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
157
+
158
+ # 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.
162
+ def visit_def_node(node)
163
+ written = rows_in(statements_of(node.body))
164
+ 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)
168
+ note_reaches(tops, endless: !node.equal_loc.nil?)
169
+ super
170
+ end
171
+
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)
179
+ end
180
+
181
+ # Notes where each of tops that is no site, a return or a
182
+ # raise, is reached: its byte offset, or, endless, its byte
183
+ # range.
184
+ def note_reaches(tops, endless:)
185
+ tops.each do |top|
186
+ next if site?(top)
187
+
188
+ loc = top.location
189
+ start = loc.start_offset
190
+ at = endless ? start...loc.end_offset : start
191
+ @reaches[Site.at(@path, loc)] = at
192
+ end
193
+ end
194
+
195
+ # Example: if a then x = 1 end gives the if, then the write.
196
+ # The nodes statements' rows are for, flat, in the order
197
+ # they are written: each statement's tops, each with the
198
+ # sites of their own inside it, in the order they are named,
199
+ # eg a || before the ?: whose predicate it is, then the rows
200
+ # of its arms' statements, when it has any.
201
+ def rows_in(statements)
202
+ statements.flat_map do |statement|
203
+ tops = top_nodes(statement).flat_map do |top|
204
+ named = [top, *inner_sites(top)]
205
+ ordered = named.sort_by { site_loc(_1).start_offset }
206
+ [*ordered, *rows_in(arms_of(top))]
207
+ end
208
+ tops + body_rows(statement)
209
+ end
210
+ end
211
+
212
+ # The Statement row of node. A site is where decision mode
213
+ # names it, eg a modifier if at its keyword, with as many
214
+ # paths as decision mode gives it. Anything else is where
215
+ # it starts, with 1 path.
216
+ def row_of(node)
217
+ site = site_at(node)
218
+ loc = site ? site_loc(node) : node.location
219
+ name = Site.at(@path, loc)
220
+ Statement.new(
221
+ line: loc.start_line, column: loc.start_column,
222
+ top: top_of(node),
223
+ paths: site ? SitePaths.possible(site).size : 1,
224
+ site: site ? name : nil, name:
225
+ )
226
+ end
227
+
228
+ # Example: x = (a ? 1 : 2) + (b ? 3 : 4) gives both ?:s.
229
+ # The nodes statement's rows are for: the outermost sites
230
+ # in it, in the order they run, as what holds them is
231
+ # looked through; statement itself when it holds none.
232
+ def top_nodes(statement)
233
+ tops = sites_in(statement)
234
+ tops.empty? ? [statement] : tops
235
+ end
236
+
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
239
+ # has them.
240
+ def sites_in(node)
241
+ return [node] if site?(node)
242
+ return [] if UNSEEN.include?(node.type)
243
+
244
+ node.compact_child_nodes.flat_map { |child| sites_in(child) }
245
+ end
246
+
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?
251
+ end
252
+
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)]
257
+ end
258
+
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))
262
+ end
263
+
264
+ TOPS = {
265
+ call_node: '()', return_node: 'return', def_node: 'def'
266
+ }.freeze
267
+
268
+ # What statement is as written: a site's operator, eg if,
269
+ # when decision mode has a site where it starts, an elsif
270
+ # 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.
275
+ 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)
280
+ return '=' if write?(statement)
281
+
282
+ TOPS.fetch(statement.type, 'value')
283
+ end
284
+
285
+ # True for an if written as an elsif.
286
+ def elsif?(statement)
287
+ statement.type == :if_node && statement.if_keyword == 'elsif'
288
+ end
289
+
290
+ # True for a write of any kind: of a variable, eg @x = 1
291
+ # or x += 1, or a call Ruby makes of an index or attribute
292
+ # write, eg a[i] = x or a.b = x.
293
+ def write?(statement)
294
+ statement.type.end_with?('_write_node') ||
295
+ (statement.type == :call_node && statement.attribute_write?)
296
+ 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
+ end
304
+ end
305
+ 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.