path_coverage 0.3.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.
@@ -10,11 +10,19 @@ module PathCoverage
10
10
  # The tree of a site, from SiteTree.of, as its row's fold in a
11
11
  # card shows it: a grid whose columns are the site's paths.
12
12
  module HtmlTree
13
- # A site's row, naming its operator and each path's count,
14
- # then the rows of its operands.
13
+ # A site's row, naming its operator and each path's count, and
14
+ # where its else keyword is, if it has one, then the rows of
15
+ # its operands.
15
16
  def self.site_rows(site)
16
17
  counts = HtmlCells.counts(site[:counts])
17
- [row(at(site), operator(site), counts), *operand_rows(site, '')]
18
+ attrs = at(site) + else_attr(site[:else_at])
19
+ [row(attrs, operator(site), counts), *operand_rows(site, '')]
20
+ end
21
+
22
+ # The data-else attribute of a site whose else keyword is at
23
+ # else_at, [line, column, line, column]; '' for none.
24
+ def self.else_attr(else_at)
25
+ else_at ? %( data-else="#{else_at.join(',')}") : ''
18
26
  end
19
27
 
20
28
  # SPIKE: the data-at attribute of a node located in its
@@ -33,42 +41,47 @@ module PathCoverage
33
41
  own = Kinds.of(condition[:operator]).rows(condition, indent)
34
42
  return own if own
35
43
 
36
- operands = unfolded(condition[:operands])
44
+ operands = unfolded(shown(condition))
37
45
  last = operands.size - 1
38
46
  operands.each_with_index.flat_map do |operand, i|
39
47
  tip = i == last
40
48
  branch = indent + (tip ? HtmlBranch::ELL : HtmlBranch::TEE)
41
49
  under = indent + (tip ? HtmlBranch::GAP : HtmlBranch::BAR)
42
- node_rows(operand, branch, under, condition[:operator])
50
+ node_rows(operand, branch, under)
43
51
  end
44
52
  end
45
53
 
46
- # The rows of one operand of a condition whose operator is
47
- # choice, branching after branch, any operands of its own
48
- # indented to under.
49
- def self.node_rows(operand, branch, under, choice)
50
- if operand.key?(:arm)
51
- return arm_rows(operand, branch, under, choice)
52
- end
54
+ # The rows of one operand of a condition, branching after
55
+ # branch, any operands of its own indented to under.
56
+ def self.node_rows(operand, branch, under)
57
+ return arm_rows(operand, branch, under) if operand.key?(:arm)
58
+ return [leaves_row(operand, branch)] if operand.key?(:leaves)
53
59
  return [operand_row(operand, branch)] unless operand[:operator]
54
60
 
55
61
  [subtree_row(operand, branch), *operand_rows(operand, under)]
56
62
  end
57
63
 
58
- # An arm's row: a ternary's, ? for then and : for else, an
59
- # arm holding a condition showing the condition's operator
60
- # and value, its operands below, a plain arm its text; an
61
- # if's named then or else, as its text is statements; its then
62
- # label marked as a name, as no then is written in its code,
63
- # where its else is. A plain arm marks the paths taking it.
64
- def self.arm_rows(arm, branch, under, choice)
64
+ # The row of an arm that leaves the method, as leaving gives
65
+ # it: return or raise, coloured as code, marking each path
66
+ # that leaves there, placed at its code.
67
+ def self.leaves_row(node, branch)
68
+ word = HtmlSyntax.fragment(node[:leaves])
69
+ lead = HtmlBranch.cell(branch) +
70
+ %(<span class="arm" data-label="">#{word}</span>)
71
+ cells = HtmlCells.of(node[:cells], HtmlCells::LEAVES)
72
+ row(%( data-leaves=""#{at(node)}), lead, cells)
73
+ end
74
+
75
+ # A ternary's arm's row, ? for then and : for else: an arm
76
+ # holding a condition showing the condition's operator and
77
+ # value, its operands below; a plain arm its text, marking
78
+ # the paths taking it.
79
+ def self.arm_rows(arm, branch, under)
65
80
  body = arm[:body]
66
81
  values = arm_cells(arm, body)
67
- named = STATEMENT_CHOICES.include?(choice) && arm[:arm] == :then
68
- text = HtmlLabels.arm(arm, choice, named)
69
- flag = named ? ' data-named=""' : ''
82
+ label = HtmlLabels.arm(arm)
70
83
  lead = HtmlBranch.cell(branch) +
71
- %(<span class="arm" data-label=""#{flag}>#{text}</span>)
84
+ %(<span class="arm" data-label="">#{label}</span>)
72
85
  row = row(%( data-arm=""#{at(arm)}), lead, values)
73
86
  body ? [row, *operand_rows(body, under)] : [row]
74
87
  end
@@ -81,6 +94,32 @@ module PathCoverage
81
94
  HtmlCells.of(arm[:cells], HtmlCells::TAKEN)
82
95
  end
83
96
 
97
+ # The operands of condition a card's fold shows: a ?:'s arms,
98
+ # as its arms are part of its one expression; for any other,
99
+ # eg an if, a ||= or a case, no arm, as which arm a path takes
100
+ # follows from its leaves, but each arm's body, eg a when's
101
+ # patterns, which are leaves of its own, and, for an arm that
102
+ # returns or raises, where paths leave the method.
103
+ def self.shown(condition)
104
+ operands = condition[:operands]
105
+ return operands if condition[:operator] == '?:'
106
+
107
+ operands.flat_map do |operand|
108
+ next [operand] unless operand.key?(:arm)
109
+
110
+ [operand[:body], leaving(operand)].compact
111
+ end
112
+ end
113
+
114
+ # { leaves: return or raise, cells:, at: } for arm when its
115
+ # code returns or raises, cells marking the paths that took
116
+ # it, so left the method, at where its code is, so hovering it
117
+ # lights the row; nil for an arm that does neither.
118
+ def self.leaving(arm)
119
+ word = arm[:text].to_s[LEAVES, 1]
120
+ word && { leaves: word, cells: arm[:cells], at: arm[:at] }
121
+ end
122
+
84
123
  # operands with each node standing for the nodes its kind
85
124
  # unfolds it into, eg a leaf with sites inside it for those
86
125
  # sites, then the leaf; a leaf or an arm for itself.
@@ -110,22 +149,21 @@ module PathCoverage
110
149
  # as the code it is part of.
111
150
  def self.operator(condition)
112
151
  label = condition.fetch(:label) { condition[:operator] }
113
- name = HtmlSyntax.fragment(label)
152
+ name = HtmlSyntax.label(label)
114
153
  %(<span class="operator" data-operator="">#{name}</span>)
115
154
  end
116
155
 
117
156
  # One operand's row: its branch, text, coloured as the code it
118
- # is, and its outcome on each path.
119
- # An operand that is only called, eg owner at the end of
120
- # user&.account&.owner, has a tick on each path that called
121
- # it instead.
157
+ # is, written over more than one line read as one, each line
158
+ # break and the indent around it one space, and its outcome on
159
+ # each path.
122
160
  def self.operand_row(operand, branch)
123
- text = HtmlSyntax.fragment(operand[:text].to_s)
161
+ one_line = operand[:text].to_s.gsub(/\s*\n\s*/, ' ')
162
+ text = HtmlSyntax.fragment(one_line)
124
163
  lead = HtmlBranch.cell(branch) +
125
164
  %(<span class="text" data-text="">#{text}</span>)
126
165
  attrs = %( data-operand=""#{at(operand)})
127
- names = operand[:called] ? HtmlCells::TAKEN : HtmlCells::OUTCOMES
128
- row(attrs, lead, HtmlCells.of(operand[:cells], names))
166
+ row(attrs, lead, HtmlCells.of(operand[:cells]))
129
167
  end
130
168
  end
131
169
  end
@@ -20,9 +20,9 @@ module PathCoverage
20
20
  # Result#outside gives them.
21
21
  LISTED = %i[outside].freeze
22
22
 
23
- # operands, and any paths, and parts' outside:, totals: and
24
- # cards:, as WithinMethods.listed gives them, as the JSON
25
- # report's text, with
23
+ # operands, and any paths, and parts' outside:, totals:,
24
+ # cards:, as WithinMethods.listed gives them, and tree:, as
25
+ # WithinMethods.tree gives it, as the JSON report's text, with
26
26
  # parts' fingerprint:, as Configuration#fingerprint gives
27
27
  # it, the names of the kinds measured, and parts' reached:,
28
28
  # as Recorder#reached gives it, even empty, as a run that
@@ -39,7 +39,7 @@ module PathCoverage
39
39
  end
40
40
 
41
41
  # The keys of parts written as they are, when not empty.
42
- KEPT = %i[totals cards].freeze
42
+ KEPT = %i[totals cards tree].freeze
43
43
 
44
44
  # { key => its value } of each of parts' KEPT keys that has
45
45
  # a value not empty.
@@ -122,16 +122,17 @@ module PathCoverage
122
122
 
123
123
  # Writes result as the JSON report at config's json_path: its
124
124
  # operands, site paths, paths a treatment rules out, totals,
125
- # each def's card, the statements the recorder reached, so a
126
- # render from the JSON alone has them, and config's
127
- # fingerprint.
125
+ # each def's card, how each card's rows nest, the statements
126
+ # the recorder reached, so a render from the JSON alone has
127
+ # them, and config's fingerprint.
128
128
  def self.write_json(result, files, config)
129
129
  report = PathsReport.of(result.paths, files)
130
130
  reached = @recorder.reached
131
131
  cards = WithinMethods.listed(files, config, result, reached)
132
+ tree = WithinMethods.tree(files, config, result)
132
133
  parts = {
133
134
  totals: totals(result, files, config), outside: result.outside,
134
- cards:, fingerprint: config.fingerprint, reached:
135
+ cards:, tree:, fingerprint: config.fingerprint, reached:
135
136
  }
136
137
  path = config.json_path
137
138
  JsonWriter.write(result.operands, path, report, **parts)
@@ -68,11 +68,6 @@ module PathCoverage
68
68
  end
69
69
  end
70
70
 
71
- # The name of one of its arms: when or else.
72
- def self.arm_label(arm, _choice)
73
- arm[:arm].to_s
74
- end
75
-
76
71
  # Its patterns need pattern calls.
77
72
  def self.own_calls?
78
73
  true
@@ -52,13 +52,9 @@ module PathCoverage
52
52
  condition.operands.first
53
53
  end
54
54
 
55
- # The name of one of its arms, of a condition named choice:
56
- # then or else for an if or an unless, as its text is
57
- # statements; ? or : for a ternary's, then its condition's
58
- # operator, or its text for a plain arm.
59
- def self.arm_label(arm, choice)
60
- return arm[:arm].to_s if STATEMENT_CHOICES.include?(choice)
61
-
55
+ # The name of one of a ternary's arms: ? or :, then its
56
+ # condition's operator, or its text for a plain arm.
57
+ def self.arm_label(arm)
62
58
  mark = arm[:arm] == :then ? '?' : ':'
63
59
  body = arm[:body]
64
60
  "#{mark} #{body ? body[:operator] : arm[:text]}"
@@ -53,7 +53,9 @@ module PathCoverage
53
53
  return Block.items?(receiver, outcomes)
54
54
  end
55
55
  whole = inner.to_h { |site| [site.extent, site] }[range]
56
- whole && value_on(whole, outcomes, fixed)
56
+ return nil if whole.nil?
57
+
58
+ value_on(whole, outcomes, fixed)
57
59
  end
58
60
 
59
61
  # site's value on its way that took outcomes, one of its
@@ -44,12 +44,9 @@ module PathCoverage
44
44
  end
45
45
 
46
46
  # Its value in each column: false where a receiver was nil,
47
- # as the chain then gives nil, else its last leaf's. Only the
48
- # chain's end, after its last leaf, can be a node only
49
- # called, so it alone is left out.
47
+ # as the chain then gives nil, else its last leaf's.
50
48
  def self.value(_operator, operands)
51
- leaves = operands.last[:called] ? operands[0...-1] : operands
52
- columns = leaves.map { |operand| operand[:cells] }.transpose
49
+ columns = operands.map { |operand| operand[:cells] }.transpose
53
50
  columns.map do |column|
54
51
  column.include?(PRESENCE[1]) ? false : column.last
55
52
  end
@@ -106,13 +103,11 @@ module PathCoverage
106
103
  # Its leaves' nodes, each after the first showing only what
107
104
  # it adds to the one before, eg account after user.
108
105
  # A leading &. is left out, as the chain's own row shows it.
109
- # Any of the chain after its last leaf follows, eg owner in
110
- # user&.account&.owner, as a node of its own.
106
+ # The chain after its last leaf, eg owner in
107
+ # user&.account&.owner, has no node: it decides no path, and
108
+ # is called exactly where that leaf is not nil.
111
109
  def self.texted(source, condition, nodes)
112
- ranges = condition.operands
113
- texted = with_added(source, ranges, nodes)
114
- rest = shown(source, ranges.last.end...condition.extent.end)
115
- rest.empty? ? texted : [*texted, called(rest, texted.last)]
110
+ with_added(source, condition.operands, nodes)
116
111
  end
117
112
 
118
113
  # nodes, the nodes of a chain's leaves at ranges, each after
@@ -131,15 +126,6 @@ module PathCoverage
131
126
  source.byteslice(range).delete_prefix('&.')
132
127
  end
133
128
 
134
- # The node of text, the end of a chain after its last
135
- # leaf, eg owner: no leaf, as it decides no path, but marked
136
- # called on each path where last, the last leaf, was not
137
- # nil.
138
- def self.called(text, last)
139
- cells = last[:cells].map { |cell| cell == PRESENCE[0] }
140
- { text:, called: true, cells: }
141
- end
142
-
143
129
  # The rows of its leaves, each under the one before, as each
144
130
  # is reached only when the one before is not nil.
145
131
  def self.rows(chain, indent)
@@ -14,12 +14,6 @@ module PathCoverage
14
14
 
15
15
  OPERATORS = WRITES
16
16
 
17
- # The name of its arm, of a write named choice: the operator
18
- # and its right side, eg ||= key.upcase.
19
- def self.arm_label(arm, choice)
20
- "#{choice} #{arm[:text]}"
21
- end
22
-
23
17
  # Its target needs no record call, and its right side a
24
18
  # written_at call.
25
19
  def self.own_calls?
@@ -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:)
@@ -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,27 +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: a
121
- # ternary, never ignored, as the ternary it folds into is
122
- # not; or an &&, ||, and or or not 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.
123
143
  def foldable?(node)
124
- ternary?(node) || (operator?(node) && !ignored?(node))
144
+ operator?(node) && !ignored?(node)
125
145
  end
126
146
 
127
147
  # True for a ternary, an if with no if keyword.
@@ -129,13 +149,13 @@ module PathCoverage
129
149
  node.type == :if_node && node.if_keyword_loc.nil?
130
150
  end
131
151
 
132
- # A ternary's or an if's predicate as its operand: an && or
133
- # || nested as a Condition, as is a &. chain, its value its
134
- # 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.
135
156
  def predicate_of(node)
136
157
  predicate = grouped(node.predicate)
137
158
  return leaf(predicate) if ignored?(predicate)
138
- return condition_of(predicate) if operator?(predicate)
139
159
  if safe_chain?(predicate)
140
160
  return chain_condition(predicate, value: true)
141
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:)
@@ -98,23 +98,16 @@ module PathCoverage
98
98
  # not known counts as not taken, never more than it ran.
99
99
  def self.totals(source, path, paths, reached, sites = nil)
100
100
  of(source, path, sites).transform_values do |rows|
101
- [taken(rows, paths, reached).compact.sum, size(rows)]
101
+ seen = taken(rows, paths, reached).compact.sum
102
+ [seen, size(rows.map(&:paths))]
102
103
  end
103
104
  end
104
105
 
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, :*)
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
118
111
  end
119
112
 
120
113
  # Example: an if row with only its then taken gives 1.
@@ -163,10 +156,12 @@ module PathCoverage
163
156
  attr_reader :methods, :names, :lasts, :reaches, :sites
164
157
 
165
158
  # 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.
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.
168
162
  def visit_def_node(node)
169
- tops = rows_in(statements_of(node.body))
163
+ written = rows_in(statements_of(node.body))
164
+ tops = written.select { shapes?(_1) }
170
165
  site = Site.of(@path, node)
171
166
  @methods[site] = tops.map { |top| row_of(top) }
172
167
  note_def(site, node)
@@ -174,16 +169,22 @@ module PathCoverage
174
169
  super
175
170
  end
176
171
 
177
- # Notes the name and last line of node, the def at site.
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.
178
175
  def note_def(site, node)
179
176
  @names[site] = node.name
180
177
  @lasts[site] = node.location.end_line
178
+ @reaches[site] = entry_at(node)
181
179
  end
182
180
 
183
- # Notes where each of tops that is no site is reached: its
184
- # byte offset, or, endless, its byte range.
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.
185
184
  def note_reaches(tops, endless:)
186
- tops.reject { site?(_1) }.each do |top|
185
+ tops.each do |top|
186
+ next if site?(top)
187
+
187
188
  loc = top.location
188
189
  start = loc.start_offset
189
190
  at = endless ? start...loc.end_offset : start
@@ -193,13 +194,16 @@ module PathCoverage
193
194
 
194
195
  # Example: if a then x = 1 end gives the if, then the write.
195
196
  # 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.
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.
199
201
  def rows_in(statements)
200
202
  statements.flat_map do |statement|
201
203
  tops = top_nodes(statement).flat_map do |top|
202
- [top, *inner_sites(top), *rows_in(arms_of(top))]
204
+ named = [top, *inner_sites(top)]
205
+ ordered = named.sort_by { site_loc(_1).start_offset }
206
+ [*ordered, *rows_in(arms_of(top))]
203
207
  end
204
208
  tops + body_rows(statement)
205
209
  end
@@ -263,14 +267,15 @@ module PathCoverage
263
267
 
264
268
  # What statement is as written: a site's operator, eg if,
265
269
  # when decision mode has a site where it starts, an elsif
266
- # being elsif, as written; else = for a write, () for a
270
+ # being elsif, as written, an if or unless with an else
271
+ # if/else or unless/else; else = for a write, () for a
267
272
  # call, raise for a raise of the method's own, as it leaves
268
273
  # the method, return for a return, def for a def inside it,
269
274
  # value for anything else.
270
275
  def top_of(statement)
271
276
  site = site_at(statement)
272
277
  return 'elsif' if site && elsif?(statement)
273
- return site.operator if site
278
+ return with_else(site.operator, statement) if site
274
279
  return 'raise' if raise?(statement)
275
280
  return '=' if write?(statement)
276
281
 
@@ -16,6 +16,14 @@ module PathCoverage
16
16
  end
17
17
  end
18
18
 
19
+ # Example: [false, true, false, true, false] for an any?.
20
+ # site's value on each of its possible paths, in the order
21
+ # possible gives them, nil where not known.
22
+ def self.values(site)
23
+ ways = Kinds.of(site.operator).root_ways(site, site.fixed)
24
+ ways.map(&:last)
25
+ end
26
+
19
27
  # [[{ leaf range => outcome }, value], ...] for each way
20
28
  # node can be evaluated, value being the outcome of node as
21
29
  # a whole, as its kind gives them; fixed is the site's