path_coverage 0.0.2 → 0.2.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.
- checksums.yaml +4 -4
- data/README.md +330 -84
- data/exe/path_coverage_collector +12 -1
- data/lib/path_coverage/calls.rb +106 -0
- data/lib/path_coverage/collector.rb +88 -22
- data/lib/path_coverage/condition.rb +77 -3
- data/lib/path_coverage/configuration.rb +52 -12
- data/lib/path_coverage/configuration_paths.rb +56 -0
- data/lib/path_coverage/console_summary.rb +77 -0
- data/lib/path_coverage/gaps.rb +25 -9
- data/lib/path_coverage/html/index.html +3 -14
- data/lib/path_coverage/html/side.js +36 -6
- data/lib/path_coverage/html/sort.js +27 -21
- data/lib/path_coverage/html/style.css +32 -1
- data/lib/path_coverage/html/tabs.js +15 -0
- data/lib/path_coverage/html_index.rb +105 -19
- data/lib/path_coverage/html_index_head.rb +49 -0
- data/lib/path_coverage/html_report.rb +44 -12
- data/lib/path_coverage/html_tabs.rb +26 -0
- data/lib/path_coverage/html_tree.rb +47 -17
- data/lib/path_coverage/json_reader.rb +94 -0
- data/lib/path_coverage/json_writer.rb +44 -8
- data/lib/path_coverage/kernel.rb +53 -45
- data/lib/path_coverage/kinds/base.rb +71 -0
- data/lib/path_coverage/kinds/block.rb +136 -0
- data/lib/path_coverage/kinds/case.rb +89 -0
- data/lib/path_coverage/kinds/chain.rb +45 -0
- data/lib/path_coverage/kinds/choice.rb +68 -0
- data/lib/path_coverage/kinds/contains.rb +111 -0
- data/lib/path_coverage/kinds/safe.rb +151 -0
- data/lib/path_coverage/kinds/write.rb +54 -0
- data/lib/path_coverage/kinds.rb +37 -0
- data/lib/path_coverage/manifest.rb +93 -26
- data/lib/path_coverage/manifest_blocks.rb +96 -0
- data/lib/path_coverage/manifest_case.rb +54 -0
- data/lib/path_coverage/manifest_chains.rb +76 -0
- data/lib/path_coverage/manifest_choices.rb +64 -32
- data/lib/path_coverage/manifest_folding.rb +91 -0
- data/lib/path_coverage/manifest_ignoring.rb +35 -0
- data/lib/path_coverage/manifest_writes.rb +67 -0
- data/lib/path_coverage/merge.rb +19 -3
- data/lib/path_coverage/post.rb +8 -8
- data/lib/path_coverage/recorder.rb +43 -0
- data/lib/path_coverage/recorder_blocks.rb +68 -0
- data/lib/path_coverage/recorder_cases.rb +23 -0
- data/lib/path_coverage/recorder_chains.rb +40 -0
- data/lib/path_coverage/recorder_writes.rb +26 -0
- data/lib/path_coverage/render.rb +58 -46
- data/lib/path_coverage/result.rb +19 -0
- data/lib/path_coverage/rewriter.rb +64 -37
- data/lib/path_coverage/rewriter_nested.rb +62 -0
- data/lib/path_coverage/site.rb +6 -1
- data/lib/path_coverage/site_paths.rb +9 -46
- data/lib/path_coverage/site_tree.rb +31 -36
- data/lib/path_coverage/summary.rb +64 -17
- data/lib/path_coverage/totals.rb +29 -0
- data/lib/path_coverage/version.rb +1 -1
- metadata +35 -7
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
require_relative 'base'
|
|
2
|
+
|
|
3
|
+
module PathCoverage
|
|
4
|
+
module Kinds
|
|
5
|
+
# The ways and value of a condition whose predicate picks an
|
|
6
|
+
# arm, which Choice and Write share.
|
|
7
|
+
module Arms
|
|
8
|
+
# The ways node can be evaluated: each way of its predicate,
|
|
9
|
+
# then each way of the condition the arm it picks is, if
|
|
10
|
+
# that arm is one; one with no else arm picks none. Its
|
|
11
|
+
# value is that of no choice, so is nil.
|
|
12
|
+
def ways(node, fixed)
|
|
13
|
+
predicate, then_arm, else_arm = node.operands
|
|
14
|
+
then_on = THEN_ON[node.operator]
|
|
15
|
+
SitePaths.walk(predicate, fixed).flat_map do |outcomes, value|
|
|
16
|
+
body = (value == then_on ? then_arm : else_arm)&.body
|
|
17
|
+
next [[outcomes, nil]] if body.nil?
|
|
18
|
+
|
|
19
|
+
SitePaths.walk(body, fixed).map do |more, _|
|
|
20
|
+
[outcomes.merge(more), nil]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Its value in each column: its taken arm's body's, nil for
|
|
26
|
+
# a plain arm.
|
|
27
|
+
def value(_operator, operands)
|
|
28
|
+
_, *arms = operands
|
|
29
|
+
bodies = arms.map { |arm| arm[:body]&.fetch(:cells) }
|
|
30
|
+
arms.first[:cells].each_index.map do |column|
|
|
31
|
+
taken = arms.index { |arm| arm[:cells][column] }
|
|
32
|
+
taken && bodies[taken]&.fetch(column)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# A condition whose predicate picks an arm: a ternary, an if
|
|
38
|
+
# or an unless.
|
|
39
|
+
module Choice
|
|
40
|
+
extend Base
|
|
41
|
+
extend Arms
|
|
42
|
+
|
|
43
|
+
OPERATORS = CHOICES
|
|
44
|
+
|
|
45
|
+
# What its site's leave call wraps: an if's or an unless's
|
|
46
|
+
# predicate, a leaf's byte range or a nested Condition, as
|
|
47
|
+
# its path is decided there and its arms are statements that
|
|
48
|
+
# may return or raise; a ternary as a whole.
|
|
49
|
+
def self.trailed(condition)
|
|
50
|
+
return condition if condition.operator == '?:'
|
|
51
|
+
|
|
52
|
+
condition.operands.first
|
|
53
|
+
end
|
|
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
|
+
|
|
62
|
+
mark = arm[:arm] == :then ? '?' : ':'
|
|
63
|
+
body = arm[:body]
|
|
64
|
+
"#{mark} #{body ? body[:operator] : arm[:text]}"
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
require_relative 'base'
|
|
2
|
+
require_relative 'block'
|
|
3
|
+
|
|
4
|
+
module PathCoverage
|
|
5
|
+
module Kinds
|
|
6
|
+
# A leaf with sites inside it, eg ids.select { |i| i.odd? }.any?
|
|
7
|
+
# as an operand of an &&: its operands are those sites'
|
|
8
|
+
# Conditions, which run as it does, then the leaf's own byte
|
|
9
|
+
# range, whose outcome is its value.
|
|
10
|
+
module Contains
|
|
11
|
+
extend Base
|
|
12
|
+
|
|
13
|
+
OPERATORS = [CONTAINS].freeze
|
|
14
|
+
|
|
15
|
+
# The ways node can be evaluated: each way of every one of
|
|
16
|
+
# the sites inside it, then each way of the leaf itself, its
|
|
17
|
+
# value the whole's.
|
|
18
|
+
def self.ways(node, fixed)
|
|
19
|
+
inner = node.operands[0...-1]
|
|
20
|
+
before = inner.reduce([{}]) do |ways, site|
|
|
21
|
+
ways.product(SitePaths.walk(site, fixed))
|
|
22
|
+
.map { |outcomes, (more, _)| outcomes.merge(more) }
|
|
23
|
+
end
|
|
24
|
+
before.flat_map do |outcomes|
|
|
25
|
+
leaf_ways(node, outcomes, fixed).map do |more, value|
|
|
26
|
+
[outcomes.merge(more), value]
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# The ways the leaf of node can be evaluated after the sites
|
|
32
|
+
# inside it took outcomes: the one outcome it must have then,
|
|
33
|
+
# when known, else true or false.
|
|
34
|
+
def self.leaf_ways(node, outcomes, fixed)
|
|
35
|
+
range = node.operands.last
|
|
36
|
+
known = known_outcome(node, outcomes, fixed)
|
|
37
|
+
return SitePaths.leaf(range, fixed) if known.nil?
|
|
38
|
+
|
|
39
|
+
[[{ range => known }, known]]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# The outcome the leaf of node must have after the sites
|
|
43
|
+
# inside it took outcomes, nil when it can have either: for
|
|
44
|
+
# a call treated as nonempty, eg any?, whether its receiver
|
|
45
|
+
# has items; for a leaf that is wholly one site, eg a find,
|
|
46
|
+
# that site's value. Each only when the site's kind can
|
|
47
|
+
# tell. A nonempty call has no arguments, so its receiver is
|
|
48
|
+
# the largest of those sites, any other lying within it.
|
|
49
|
+
def self.known_outcome(node, outcomes, fixed)
|
|
50
|
+
*inner, range = node.operands
|
|
51
|
+
if node.fixed[range] == :nonempty
|
|
52
|
+
receiver = inner.max_by { |site| site.extent.size }
|
|
53
|
+
return Block.items?(receiver, outcomes)
|
|
54
|
+
end
|
|
55
|
+
whole = inner.find { |site| site.extent == range }
|
|
56
|
+
whole && value_on(whole, outcomes, fixed)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# site's value on its way that took outcomes, one of its
|
|
60
|
+
# own, nil when its kind cannot tell.
|
|
61
|
+
def self.value_on(site, outcomes, fixed)
|
|
62
|
+
ways = SitePaths.walk(site, fixed)
|
|
63
|
+
ways.find { |more, _| more <= outcomes }.last
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Its value in each column: the leaf's own, its last operand.
|
|
67
|
+
def self.value(_operator, operands)
|
|
68
|
+
operands.last[:cells]
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# nodes, labelled and texted so their rows read as the
|
|
72
|
+
# leaf's code does.
|
|
73
|
+
# Each site inside the leaf is labelled with the leaf's text
|
|
74
|
+
# before it, its operator, and its own opening, eg select {
|
|
75
|
+
# for ids.select { |id| id.odd? }.any?, or ( || for
|
|
76
|
+
# (a || b).any?.
|
|
77
|
+
# The leaf is texted with the last site's own closing, then
|
|
78
|
+
# the leaf's text after that site, eg }.any? or ).any?.
|
|
79
|
+
def self.texted(source, condition, nodes)
|
|
80
|
+
*inner, range = condition.operands
|
|
81
|
+
starts = [range.begin, *inner.map { |site| site.extent.end }]
|
|
82
|
+
labelled = inner.zip(starts, nodes).map do |site, at, node|
|
|
83
|
+
node.merge(label: label(source, site, at))
|
|
84
|
+
end
|
|
85
|
+
text = tail(source, inner.last, range)
|
|
86
|
+
[*labelled, nodes.last.merge(text:)]
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# site's label inside a leaf: the leaf's text from byte at
|
|
90
|
+
# up to site, then site's operator and its own opening.
|
|
91
|
+
def self.label(source, site, at)
|
|
92
|
+
before = source.byteslice(at...site.extent.begin).strip
|
|
93
|
+
opening, = Kinds.of(site.operator).brackets(source, site)
|
|
94
|
+
[before, site.operator, opening].reject(&:empty?).join(' ')
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# The text of the leaf at range: the closing of last, the
|
|
98
|
+
# last site inside the leaf, then the leaf's text after it.
|
|
99
|
+
def self.tail(source, last, range)
|
|
100
|
+
_, closing = Kinds.of(last.operator).brackets(source, last)
|
|
101
|
+
closing + source.byteslice(last.extent.end...range.end)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# The sites inside it, then the leaf, as siblings, so each
|
|
105
|
+
# site's rows are drawn once, eg select { then }.any?.
|
|
106
|
+
def self.unfold(node)
|
|
107
|
+
node[:operands]
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
require_relative 'base'
|
|
2
|
+
|
|
3
|
+
module PathCoverage
|
|
4
|
+
module Kinds
|
|
5
|
+
# A &. chain, eg user&.account&.owner, each receiver nil or
|
|
6
|
+
# not, a nil one skipping the rest of the chain. As a site of
|
|
7
|
+
# its own its operands are its receivers; nested in another,
|
|
8
|
+
# they are its receivers then its value, its last leaf. Each
|
|
9
|
+
# receiver is wrapped in a present call, which passes it on
|
|
10
|
+
# unchanged, so it is still evaluated just once.
|
|
11
|
+
module Safe
|
|
12
|
+
extend Base
|
|
13
|
+
|
|
14
|
+
OPERATORS = [SAFE].freeze
|
|
15
|
+
|
|
16
|
+
# The ways node, nested in a condition, can be evaluated,
|
|
17
|
+
# its value its last leaf.
|
|
18
|
+
def self.ways(node, fixed)
|
|
19
|
+
chain(node.operands[0...-1], node.operands.last, fixed)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# The ways a &. chain site can be evaluated, every operand
|
|
23
|
+
# a receiver.
|
|
24
|
+
def self.root_ways(node, fixed)
|
|
25
|
+
chain(node.operands, nil, fixed)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# The ways a &. chain can be evaluated: each receiver not
|
|
29
|
+
# nil going on to the next, a nil one ending the chain, its
|
|
30
|
+
# value false; then, given a last leaf, the chain's value
|
|
31
|
+
# as an operand of a condition, the ways it can be.
|
|
32
|
+
def self.chain(receivers, last, fixed)
|
|
33
|
+
first, *rest = receivers
|
|
34
|
+
on = if !rest.empty? then chain(rest, last, fixed)
|
|
35
|
+
elsif last then SitePaths.walk(last, fixed)
|
|
36
|
+
else [[{}, true]]
|
|
37
|
+
end
|
|
38
|
+
present = on.map do |outcomes, value|
|
|
39
|
+
[outcomes.merge(first => PRESENCE[0]), value]
|
|
40
|
+
end
|
|
41
|
+
return present if fixed[first] == :present
|
|
42
|
+
|
|
43
|
+
[*present, [{ first => PRESENCE[1] }, false]]
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Its value in each column: false where a receiver was nil,
|
|
47
|
+
# as the chain then gives nil, else its last leaf's.
|
|
48
|
+
def self.value(_operator, operands)
|
|
49
|
+
leaves = operands.reject { |operand| operand[:called] }
|
|
50
|
+
columns = leaves.map { |operand| operand[:cells] }.transpose
|
|
51
|
+
columns.map do |column|
|
|
52
|
+
column.include?(PRESENCE[1]) ? false : column.last
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# The inserts for a &. chain site: each receiver in a present
|
|
57
|
+
# call, and the whole chain in a leave call that enters it
|
|
58
|
+
# first. Eg user&.account becomes, in effect,
|
|
59
|
+
# leave(enter(site, 1), present(site, 0, 0, user)&.account).
|
|
60
|
+
def self.site_inserts(site, condition)
|
|
61
|
+
leaves = condition.leaves
|
|
62
|
+
enter = "PathCoverage.enter(#{site.inspect}, #{leaves.size})"
|
|
63
|
+
call = "PathCoverage.leave(#{enter}, "
|
|
64
|
+
command = condition.commands.include?(condition.extent)
|
|
65
|
+
presents = leaves.each_with_index.flat_map do |range, index|
|
|
66
|
+
Rewriter.wrap(present(site, index, 0), range, false, :leaf)
|
|
67
|
+
end
|
|
68
|
+
extent = condition.extent
|
|
69
|
+
presents + Rewriter.wrap(call, extent, command, :site)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Its leaves need present and chained calls.
|
|
73
|
+
def self.own_calls?
|
|
74
|
+
true
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# { leaf range => the opening of its call } for the leaves of
|
|
78
|
+
# chain nested in a site whose leaves are leaves: a
|
|
79
|
+
# receiver's present call, and the chain's value's chained
|
|
80
|
+
# call, each told where its chain's leaves start.
|
|
81
|
+
def self.calls(site, chain, leaves)
|
|
82
|
+
first = leaves.index(chain.operands.first)
|
|
83
|
+
*receivers, value = chain.operands
|
|
84
|
+
calls = receivers.to_h do |range|
|
|
85
|
+
[range, present(site, leaves.index(range), first)]
|
|
86
|
+
end
|
|
87
|
+
at = leaves.index(value)
|
|
88
|
+
calls.merge(value => chained(site, at, first))
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# The opening of the chained call around the index-th leaf
|
|
92
|
+
# of site, a chain's value, its chain's leaves starting at
|
|
93
|
+
# first.
|
|
94
|
+
def self.chained(site, index, first)
|
|
95
|
+
"PathCoverage.chained(#{site.inspect}, #{index}, #{first}, "
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# The opening of the present call around the index-th leaf
|
|
99
|
+
# of site, its chain's leaves starting at first.
|
|
100
|
+
def self.present(site, index, first)
|
|
101
|
+
"PathCoverage.present(#{site.inspect}, #{index}, #{first}, "
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Its leaves' nodes, each after the first showing only what
|
|
105
|
+
# it adds to the one before, eg account after user.
|
|
106
|
+
# A leading &. is left out, as the chain's own row shows it.
|
|
107
|
+
# Any of the chain after its last leaf follows, eg owner in
|
|
108
|
+
# user&.account&.owner, as a node of its own.
|
|
109
|
+
def self.texted(source, condition, nodes)
|
|
110
|
+
ranges = condition.operands
|
|
111
|
+
texted = with_added(source, ranges, nodes)
|
|
112
|
+
rest = shown(source, ranges.last.end...condition.extent.end)
|
|
113
|
+
rest.empty? ? texted : [*texted, called(rest, texted.last)]
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# nodes, the nodes of a chain's leaves at ranges, each after
|
|
117
|
+
# the first showing only what it adds to the one before.
|
|
118
|
+
def self.with_added(source, ranges, nodes)
|
|
119
|
+
nodes.each_with_index.map do |node, index|
|
|
120
|
+
next node if index.zero?
|
|
121
|
+
|
|
122
|
+
added = ranges[index - 1].end...ranges[index].end
|
|
123
|
+
node.merge(text: shown(source, added))
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# The text of source at range, less any leading &.
|
|
128
|
+
def self.shown(source, range)
|
|
129
|
+
source.byteslice(range).delete_prefix('&.')
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# The node of text, the end of a chain after its last
|
|
133
|
+
# leaf, eg owner: no leaf, as it decides no path, but marked
|
|
134
|
+
# called on each path where last, the last leaf, was not
|
|
135
|
+
# nil.
|
|
136
|
+
def self.called(text, last)
|
|
137
|
+
cells = last[:cells].map { |cell| cell == PRESENCE[0] }
|
|
138
|
+
{ text:, called: true, cells: }
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# The rows of its leaves, each under the one before, as each
|
|
142
|
+
# is reached only when the one before is not nil.
|
|
143
|
+
def self.rows(chain, indent)
|
|
144
|
+
chain[:operands].each_with_index.map do |operand, depth|
|
|
145
|
+
branch = "#{indent}#{' ' * depth}└─ "
|
|
146
|
+
HtmlTree.operand_row(operand, branch)
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
require_relative 'base'
|
|
2
|
+
require_relative 'choice'
|
|
3
|
+
|
|
4
|
+
module PathCoverage
|
|
5
|
+
module Kinds
|
|
6
|
+
# A ||= or &&= write, which decides whether its right side
|
|
7
|
+
# runs as an unless or an if decides its arm: its target is
|
|
8
|
+
# its predicate, its right side its then arm, so its ways and
|
|
9
|
+
# value are a choice's. Its target is never wrapped, so never
|
|
10
|
+
# read again.
|
|
11
|
+
module Write
|
|
12
|
+
extend Base
|
|
13
|
+
extend Arms
|
|
14
|
+
|
|
15
|
+
OPERATORS = WRITES
|
|
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
|
+
# Its target needs no record call, and its right side a
|
|
24
|
+
# written_at call.
|
|
25
|
+
def self.own_calls?
|
|
26
|
+
true
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# { target range => nil }: its target is never read, so
|
|
30
|
+
# never wrapped.
|
|
31
|
+
def self.calls(_site, inner, _leaves)
|
|
32
|
+
{ inner.operands.first => nil }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# A write, its target the site's at-th leaf: its right side
|
|
36
|
+
# in a written_at call, which notes the target's outcome when
|
|
37
|
+
# it runs, and the whole of it in a settle call, which notes
|
|
38
|
+
# the other when it does not. A right side among commands,
|
|
39
|
+
# eg a command call, is parenthesised. Eg x ||= v becomes,
|
|
40
|
+
# in effect, settle(site, at, true, x ||= written_at(site,
|
|
41
|
+
# at, false, v)).
|
|
42
|
+
def self.inserts(site, inner, at, commands)
|
|
43
|
+
_target, arm = inner.operands
|
|
44
|
+
ran = THEN_ON[inner.operator]
|
|
45
|
+
name = site.inspect
|
|
46
|
+
written = "PathCoverage.written_at(#{name}, #{at}, #{ran}, "
|
|
47
|
+
settle = "PathCoverage.settle(#{name}, #{at}, #{!ran}, "
|
|
48
|
+
command = commands.include?(arm.extent)
|
|
49
|
+
Rewriter.wrap(settle, inner.extent, false, :kind) +
|
|
50
|
+
Rewriter.wrap(written, arm.extent, command, :leaf)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
require_relative 'condition'
|
|
2
|
+
require_relative 'kinds/block'
|
|
3
|
+
require_relative 'kinds/case'
|
|
4
|
+
require_relative 'kinds/chain'
|
|
5
|
+
require_relative 'kinds/choice'
|
|
6
|
+
require_relative 'kinds/contains'
|
|
7
|
+
require_relative 'kinds/safe'
|
|
8
|
+
require_relative 'kinds/write'
|
|
9
|
+
|
|
10
|
+
module PathCoverage
|
|
11
|
+
# The kinds of condition, each a module that knows, for a
|
|
12
|
+
# condition named by one of its OPERATORS, the ways it can be
|
|
13
|
+
# evaluated, its value in each column of its site's tree, and
|
|
14
|
+
# the inserts that instrument it. Each extends Kinds::Base,
|
|
15
|
+
# whose hooks give what most kinds need.
|
|
16
|
+
module Kinds
|
|
17
|
+
# Every kind, each named by its operators.
|
|
18
|
+
ALL = [Block, Case, Chain, Choice, Contains, Safe, Write].freeze
|
|
19
|
+
|
|
20
|
+
# { operator => kind } for every operator of every kind.
|
|
21
|
+
BY_OPERATOR = ALL.flat_map do |kind|
|
|
22
|
+
kind::OPERATORS.map { |operator| [operator, kind] }
|
|
23
|
+
end.to_h.freeze
|
|
24
|
+
|
|
25
|
+
# The kind of a condition named operator, eg Chain for &&.
|
|
26
|
+
def self.of(operator)
|
|
27
|
+
BY_OPERATOR.fetch(operator)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# The names of the kinds measured, eg chain for Chain: a
|
|
31
|
+
# report's fingerprint, as runs measuring different kinds
|
|
32
|
+
# list a site's paths differently.
|
|
33
|
+
def self.names
|
|
34
|
+
ALL.map { |kind| kind.name.split('::').last.downcase }
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -1,16 +1,28 @@
|
|
|
1
1
|
require 'prism'
|
|
2
2
|
require_relative 'condition'
|
|
3
|
+
require_relative 'manifest_blocks'
|
|
4
|
+
require_relative 'manifest_case'
|
|
5
|
+
require_relative 'manifest_chains'
|
|
3
6
|
require_relative 'manifest_choices'
|
|
7
|
+
require_relative 'manifest_folding'
|
|
8
|
+
require_relative 'manifest_ignoring'
|
|
9
|
+
require_relative 'manifest_writes'
|
|
4
10
|
require_relative 'site'
|
|
5
11
|
|
|
6
12
|
module PathCoverage
|
|
7
13
|
# Finds every condition site in source without running it: each
|
|
8
|
-
# outermost &&, ||, and or or, each ternary
|
|
9
|
-
# conditions nested in it.
|
|
14
|
+
# outermost &&, ||, and or or, each ternary, if, unless and case
|
|
15
|
+
# with a subject, with the conditions nested in it.
|
|
10
16
|
module Manifest
|
|
11
|
-
# { site => Condition } for each site in source
|
|
12
|
-
|
|
13
|
-
|
|
17
|
+
# { site => Condition } for each site in source, none of
|
|
18
|
+
# them named by an operator in ignored; a call to a method
|
|
19
|
+
# named in present, as a &.'s receiver, is never nil, and a
|
|
20
|
+
# call to one named in nonempty, eg any?, is true exactly
|
|
21
|
+
# when its receiver has items.
|
|
22
|
+
def self.sites(source, path, ignored = [], present = [],
|
|
23
|
+
nonempty = [])
|
|
24
|
+
collector = ConditionSites.new(path, ignored,
|
|
25
|
+
[present, nonempty])
|
|
14
26
|
Prism.parse(source).value.accept(collector)
|
|
15
27
|
collector.sites
|
|
16
28
|
end
|
|
@@ -18,7 +30,10 @@ module PathCoverage
|
|
|
18
30
|
# The source and sites of the covered file at full_path.
|
|
19
31
|
def self.parse(full_path, config)
|
|
20
32
|
source = File.read(full_path)
|
|
21
|
-
|
|
33
|
+
path = config.relative(full_path)
|
|
34
|
+
treated = %w[present nonempty].map { config.treated(_1, path) }
|
|
35
|
+
ignored = config.ignored
|
|
36
|
+
{ source:, sites: sites(source, path, ignored, *treated) }
|
|
22
37
|
end
|
|
23
38
|
|
|
24
39
|
# True for a call to a named method with arguments and no
|
|
@@ -69,13 +84,26 @@ module PathCoverage
|
|
|
69
84
|
|
|
70
85
|
# Collects each site and its Condition.
|
|
71
86
|
class ConditionSites < Prism::Visitor
|
|
87
|
+
include ManifestBlocks
|
|
88
|
+
include ManifestCase
|
|
89
|
+
include ManifestChains
|
|
72
90
|
include ManifestChoices
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
91
|
+
include ManifestFolding
|
|
92
|
+
include ManifestIgnoring
|
|
93
|
+
include ManifestWrites
|
|
94
|
+
|
|
95
|
+
# Starts with no sites found, to find none named by an
|
|
96
|
+
# operator in ignored, and to take a call to a method named
|
|
97
|
+
# in present as never nil, and to one in nonempty as true
|
|
98
|
+
# exactly when its receiver has items.
|
|
99
|
+
def initialize(path, ignored, (present, nonempty))
|
|
76
100
|
@path = path
|
|
101
|
+
@ignored = ignored
|
|
102
|
+
@present = present
|
|
103
|
+
@nonempty = nonempty
|
|
77
104
|
@sites = {}
|
|
78
105
|
@notes = nil
|
|
106
|
+
@inner = nil
|
|
79
107
|
super()
|
|
80
108
|
end
|
|
81
109
|
|
|
@@ -94,32 +122,65 @@ module PathCoverage
|
|
|
94
122
|
add_site(node)
|
|
95
123
|
end
|
|
96
124
|
|
|
97
|
-
# Notes a ternary or an if site,
|
|
98
|
-
#
|
|
125
|
+
# Notes a ternary or an if site, a modifier if included,
|
|
126
|
+
# its arms included.
|
|
99
127
|
def visit_if_node(node)
|
|
100
|
-
|
|
128
|
+
add_site(node)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Notes an unless site, a modifier unless included, its
|
|
132
|
+
# arms included.
|
|
133
|
+
def visit_unless_node(node)
|
|
134
|
+
add_site(node)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Notes a case site, when its paths can be seen; any other
|
|
138
|
+
# case is no site, though a condition inside it may be.
|
|
139
|
+
def visit_case_node(node)
|
|
140
|
+
return super unless case_site?(node)
|
|
141
|
+
|
|
142
|
+
add_site(node)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Notes a &. chain as a site, or a block method's call, when
|
|
146
|
+
# its block's every call can be seen; any other call is no
|
|
147
|
+
# site, though a condition inside it may be.
|
|
148
|
+
def visit_call_node(node)
|
|
149
|
+
return add_site(node) if safe_chain?(node)
|
|
150
|
+
return super unless block_site?(node)
|
|
101
151
|
|
|
102
152
|
add_site(node)
|
|
103
153
|
end
|
|
104
154
|
|
|
105
|
-
# Notes node as a site
|
|
106
|
-
#
|
|
107
|
-
#
|
|
155
|
+
# Notes node as a site, named where it starts: an if or an
|
|
156
|
+
# unless at its keyword. It claims its place first, so a
|
|
157
|
+
# site found inside one of its leaves or arms follows it.
|
|
158
|
+
# While a leaf is being visited, any site is collected as
|
|
159
|
+
# part of that leaf instead, as it is part of one expression.
|
|
108
160
|
def add_site(node)
|
|
109
|
-
|
|
161
|
+
return visit_child_nodes(node) if ignored?(node)
|
|
162
|
+
return @inner << nested_condition_of(node) if @inner
|
|
163
|
+
|
|
164
|
+
site = Site.at(@path, site_loc(node))
|
|
110
165
|
@sites[site] = nil
|
|
111
166
|
outer = @notes
|
|
112
167
|
@notes = { commands: [], fixed: {} }
|
|
113
|
-
condition = condition_of(node)
|
|
168
|
+
condition = scoped { condition_of(node) }
|
|
114
169
|
@sites[site] = condition.with(**@notes)
|
|
115
170
|
@notes = outer
|
|
116
171
|
end
|
|
117
172
|
|
|
118
173
|
# node as a Condition, operands and all, named by its
|
|
119
|
-
# operator as written; a ternary is named
|
|
120
|
-
#
|
|
174
|
+
# operator as written; a ternary is named ?:, an if if and
|
|
175
|
+
# an unless unless, their operands a predicate and arms; a
|
|
176
|
+
# case case, its operands its arms; a write ||= or &&=, its
|
|
177
|
+
# operands its target and its right side; a &. chain &., its
|
|
178
|
+
# operands its receivers.
|
|
121
179
|
def condition_of(node)
|
|
122
|
-
return
|
|
180
|
+
return write_condition(node) if write?(node)
|
|
181
|
+
return choice_of(node) if CHOICE_TYPES.include?(node.type)
|
|
182
|
+
return case_condition(node) if node.type == :case_node
|
|
183
|
+
return call_condition(node) if node.type == :call_node
|
|
123
184
|
|
|
124
185
|
Condition.new(
|
|
125
186
|
operator: node.operator, operands: operands_of(node),
|
|
@@ -129,12 +190,17 @@ module PathCoverage
|
|
|
129
190
|
|
|
130
191
|
# node's operands: a chain of the same operator flattened
|
|
131
192
|
# into one list, the other operator nested as a Condition,
|
|
132
|
-
#
|
|
193
|
+
# as is a &. chain, its value its last leaf, anything else a
|
|
194
|
+
# leaf's byte range.
|
|
133
195
|
def operands_of(node)
|
|
134
196
|
[node.left, node.right].flat_map do |written|
|
|
135
197
|
operand = grouped(written)
|
|
198
|
+
next [leaf(operand)] if ignored?(operand)
|
|
136
199
|
next operands_of(operand) if operand.type == node.type
|
|
137
200
|
next [condition_of(operand)] if operator?(operand)
|
|
201
|
+
if safe_chain?(operand)
|
|
202
|
+
next [chain_condition(operand, value: true)]
|
|
203
|
+
end
|
|
138
204
|
|
|
139
205
|
[leaf(operand)]
|
|
140
206
|
end
|
|
@@ -151,17 +217,18 @@ module PathCoverage
|
|
|
151
217
|
OPERATOR_TYPES.include?(node.type)
|
|
152
218
|
end
|
|
153
219
|
|
|
154
|
-
# The byte range of a leaf operand,
|
|
155
|
-
#
|
|
156
|
-
#
|
|
220
|
+
# The byte range of a leaf operand, noted among its site's
|
|
221
|
+
# commands and fixed leaves where it is one; or, when sites
|
|
222
|
+
# that fold are found inside it, the leaf with them nested.
|
|
157
223
|
def leaf(operand)
|
|
158
|
-
operand.accept(self)
|
|
224
|
+
inner = collected { operand.accept(self) }
|
|
159
225
|
loc = operand.location
|
|
160
226
|
range = loc.start_offset...loc.end_offset
|
|
161
227
|
@notes[:commands] << range if Manifest.command?(operand)
|
|
162
228
|
outcome = Manifest.fixed_outcome(operand)
|
|
163
229
|
@notes[:fixed][range] = outcome if outcome
|
|
164
|
-
|
|
230
|
+
nonempty = nonempty_call?(operand, inner)
|
|
231
|
+
contained(inner, range, nonempty:)
|
|
165
232
|
end
|
|
166
233
|
|
|
167
234
|
# The one statement inside a parenthesised operand, or the
|