path_coverage 0.7.2 → 0.8.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 +45 -19
- data/lib/path_coverage/call_trace.rb +1 -1
- data/lib/path_coverage/calls.rb +35 -8
- data/lib/path_coverage/condition.rb +24 -6
- data/lib/path_coverage/current_test.rb +28 -0
- data/lib/path_coverage/decision_tree.rb +11 -21
- data/lib/path_coverage/evaluated.rb +23 -0
- data/lib/path_coverage/fold_rows.rb +7 -8
- data/lib/path_coverage/gaps.rb +4 -4
- data/lib/path_coverage/json_reader.rb +22 -17
- data/lib/path_coverage/json_reports.rb +5 -3
- data/lib/path_coverage/json_writer.rb +12 -8
- data/lib/path_coverage/kernel.rb +11 -3
- data/lib/path_coverage/kinds/base.rb +9 -0
- data/lib/path_coverage/kinds/block.rb +7 -1
- data/lib/path_coverage/kinds/case.rb +63 -20
- data/lib/path_coverage/kinds/chain.rb +13 -3
- data/lib/path_coverage/kinds/choice.rb +5 -0
- data/lib/path_coverage/kinds/contains.rb +6 -0
- data/lib/path_coverage/kinds/safe.rb +3 -3
- data/lib/path_coverage/kinds/write.rb +3 -3
- data/lib/path_coverage/kinds.rb +14 -0
- data/lib/path_coverage/loop_nodes.rb +5 -3
- data/lib/path_coverage/manifest.rb +57 -33
- data/lib/path_coverage/manifest_case.rb +43 -26
- data/lib/path_coverage/manifest_folding.rb +20 -0
- data/lib/path_coverage/merge.rb +8 -8
- data/lib/path_coverage/method_statements.rb +5 -4
- data/lib/path_coverage/minitest.rb +7 -0
- data/lib/path_coverage/minitest_tests.rb +25 -0
- data/lib/path_coverage/paths_context.rb +2 -2
- data/lib/path_coverage/paths_forks.rb +6 -3
- data/lib/path_coverage/paths_leaving.rb +3 -1
- data/lib/path_coverage/paths_regions.rb +78 -0
- data/lib/path_coverage/paths_rescues.rb +5 -2
- data/lib/path_coverage/paths_scopes.rb +2 -1
- data/lib/path_coverage/paths_tree.rb +10 -68
- data/lib/path_coverage/paths_way_lists.rb +11 -10
- data/lib/path_coverage/recorder.rb +28 -47
- data/lib/path_coverage/recorder_blocks.rb +3 -3
- data/lib/path_coverage/recorder_cases.rb +70 -17
- data/lib/path_coverage/recorder_chains.rb +2 -2
- data/lib/path_coverage/render.rb +7 -3
- data/lib/path_coverage/report_tallies.rb +79 -0
- data/lib/path_coverage/report_text.rb +23 -0
- data/lib/path_coverage/rescue_forks.rb +3 -0
- data/lib/path_coverage/result.rb +17 -16
- data/lib/path_coverage/rewriter.rb +42 -114
- data/lib/path_coverage/rewriter_marks.rb +108 -0
- data/lib/path_coverage/source_report.rb +5 -6
- data/lib/path_coverage/stable_leaves.rb +6 -26
- data/lib/path_coverage/statement_arms.rb +18 -5
- data/lib/path_coverage/tally.rb +129 -0
- data/lib/path_coverage/testing.rb +13 -0
- data/lib/path_coverage/totals.rb +3 -3
- data/lib/path_coverage/version.rb +1 -1
- data/lib/path_coverage/walk.rb +4 -3
- data/lib/path_coverage/ways_report.rb +11 -10
- data/lib/path_coverage/within_methods.rb +12 -3
- metadata +11 -1
data/lib/path_coverage/kernel.rb
CHANGED
|
@@ -7,6 +7,7 @@ require_relative 'render'
|
|
|
7
7
|
require_relative 'rescue_forks'
|
|
8
8
|
require_relative 'result'
|
|
9
9
|
require_relative 'rewriter'
|
|
10
|
+
require_relative 'testing'
|
|
10
11
|
require_relative 'within_methods'
|
|
11
12
|
|
|
12
13
|
# Path coverage for Ruby. This file is the kernel: it loads
|
|
@@ -85,11 +86,11 @@ module PathCoverage
|
|
|
85
86
|
end
|
|
86
87
|
end
|
|
87
88
|
|
|
88
|
-
# The compiled, rewritten code of path, or nil for a path
|
|
89
|
-
#
|
|
89
|
+
# The compiled, rewritten code of path, or nil for a path that
|
|
90
|
+
# is not instrumentable, which Ruby then loads as normal. Each
|
|
90
91
|
# call of a method in it is traced, as its way is recorded.
|
|
91
92
|
def self.instrumented(path, config)
|
|
92
|
-
return unless
|
|
93
|
+
return unless instrumentable?(path, config)
|
|
93
94
|
|
|
94
95
|
parsed = @parsed[path] = Manifest.parse(path, config)
|
|
95
96
|
args = [parsed[:source], config.relative(path),
|
|
@@ -101,6 +102,13 @@ module PathCoverage
|
|
|
101
102
|
traced(iseq.compile(rewritten, path, path), known(args, loops))
|
|
102
103
|
end
|
|
103
104
|
|
|
105
|
+
# True for a path config covers whose source parses. A source
|
|
106
|
+
# that does not parse is left to Ruby, to raise its own
|
|
107
|
+
# SyntaxError, since a rewrite of broken code could make it valid.
|
|
108
|
+
def self.instrumentable?(path, config)
|
|
109
|
+
config.covers?(path) && Prism.parse_file(path).errors.empty?
|
|
110
|
+
end
|
|
111
|
+
|
|
104
112
|
# [loops, repeats, restarts] of the file of args, [source, path,
|
|
105
113
|
# decisions], as traced takes them, loops as MethodLoops gives
|
|
106
114
|
# them.
|
|
@@ -9,6 +9,15 @@ module PathCoverage
|
|
|
9
9
|
ways(node, fixed)
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
+
# The leaves among condition's own operands whose outcome is
|
|
13
|
+
# their truthiness, so a decision that is the whole of one
|
|
14
|
+
# gives it its value, and two reads of one unwritten local
|
|
15
|
+
# give one outcome: none for most, eg a &. receiver's
|
|
16
|
+
# presence, a block's receiver's items, or a write's target.
|
|
17
|
+
def truthy_operands(_condition)
|
|
18
|
+
[]
|
|
19
|
+
end
|
|
20
|
+
|
|
12
21
|
# Whether its leaves, nested in a decision, need calls of their
|
|
13
22
|
# own in place of record calls; false for most.
|
|
14
23
|
def own_calls?
|
|
@@ -12,6 +12,12 @@ module PathCoverage
|
|
|
12
12
|
|
|
13
13
|
OPERATORS = BLOCK_PATHS.keys.freeze
|
|
14
14
|
|
|
15
|
+
# Its block's body, whose truthiness is each answer; not its
|
|
16
|
+
# receiver, whose leaf counts items.
|
|
17
|
+
def self.truthy_operands(condition)
|
|
18
|
+
[condition.operands.last]
|
|
19
|
+
end
|
|
20
|
+
|
|
15
21
|
# A value that holds whatever the path, eg select's array.
|
|
16
22
|
ALWAYS = ->(*) { true }
|
|
17
23
|
|
|
@@ -123,7 +129,7 @@ module PathCoverage
|
|
|
123
129
|
name = decision.inspect
|
|
124
130
|
begun = "PathCoverage.begin_items(#{name}, #{at})"
|
|
125
131
|
call = "PathCoverage.end_items(#{name}, #{at}, #{begun}, "
|
|
126
|
-
Rewriter.wrap(call, inner.extent,
|
|
132
|
+
Rewriter.wrap(call, inner.extent, :bare, :kind)
|
|
127
133
|
end
|
|
128
134
|
end
|
|
129
135
|
end
|
|
@@ -4,9 +4,11 @@ require_relative 'chain'
|
|
|
4
4
|
module PathCoverage
|
|
5
5
|
module Kinds
|
|
6
6
|
# A case, its operands its whens' and else's arms, each when
|
|
7
|
-
# trying its patterns in turn until one matches.
|
|
8
|
-
#
|
|
9
|
-
#
|
|
7
|
+
# trying its patterns in turn until one matches. Ruby itself
|
|
8
|
+
# matches each pattern, or tests it for truth in a case with no
|
|
9
|
+
# subject, in the code's own frame, so $~ and a private ===
|
|
10
|
+
# behave as written: the instrumentation only notes which
|
|
11
|
+
# pattern each try reached and which arm the case took.
|
|
10
12
|
module Case
|
|
11
13
|
extend Base
|
|
12
14
|
|
|
@@ -19,17 +21,10 @@ module PathCoverage
|
|
|
19
21
|
Chain.ways(node.with(operands: whens), fixed)
|
|
20
22
|
end
|
|
21
23
|
|
|
22
|
-
#
|
|
23
|
-
#
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
def self.decision_inserts(decision, condition)
|
|
27
|
-
size = condition.leaves.size
|
|
28
|
-
condition.leaves.each_with_index.flat_map do |range, index|
|
|
29
|
-
call = "PathCoverage.when_match(#{decision.inspect}, " \
|
|
30
|
-
"#{index}, #{size}, "
|
|
31
|
-
Rewriter.wrap(call, range, false, :leaf)
|
|
32
|
-
end
|
|
24
|
+
# What a case decision's leave call wraps: the whole case, to
|
|
25
|
+
# its end, as its arms run inside it.
|
|
26
|
+
def self.trailed(condition)
|
|
27
|
+
condition.whole
|
|
33
28
|
end
|
|
34
29
|
|
|
35
30
|
# Its node in a decision's tree from its arms, built in order so
|
|
@@ -52,8 +47,8 @@ module PathCoverage
|
|
|
52
47
|
def self.arm_node(source, arm, matched, (outcomes, leaves))
|
|
53
48
|
condition = arm.body
|
|
54
49
|
body = if condition
|
|
55
|
-
DecisionTree.node(source, condition,
|
|
56
|
-
|
|
50
|
+
labelled(DecisionTree.node(source, condition,
|
|
51
|
+
outcomes, leaves))
|
|
57
52
|
end
|
|
58
53
|
matched << body[:cells].map { |cell| cell == true } if body
|
|
59
54
|
cells = body ? matched.last : unmatched(matched, outcomes)
|
|
@@ -61,6 +56,14 @@ module PathCoverage
|
|
|
61
56
|
{ arm: arm.kind, text:, leaves: arm.leaves, cells:, body: }
|
|
62
57
|
end
|
|
63
58
|
|
|
59
|
+
# body, a when's node, labelled when, as it is written, when
|
|
60
|
+
# its operator is TRUTHY_WHEN.
|
|
61
|
+
def self.labelled(body)
|
|
62
|
+
return body unless body[:operator] == TRUTHY_WHEN
|
|
63
|
+
|
|
64
|
+
body.merge(label: 'when')
|
|
65
|
+
end
|
|
66
|
+
|
|
64
67
|
# Whether each column matched none of its whens' patterns:
|
|
65
68
|
# a when's cell is true where it matched.
|
|
66
69
|
def self.unmatched(matched, outcomes)
|
|
@@ -74,16 +77,56 @@ module PathCoverage
|
|
|
74
77
|
true
|
|
75
78
|
end
|
|
76
79
|
|
|
77
|
-
# { pattern range => the opening of its
|
|
78
|
-
#
|
|
79
|
-
#
|
|
80
|
+
# { pattern range => the opening of its tried call } for the
|
|
81
|
+
# patterns of inner, a case whose patterns are among leaves,
|
|
82
|
+
# decision's. The call notes its pattern reached and gives
|
|
83
|
+
# back what it is passed, so the case takes the pattern, or a
|
|
84
|
+
# splat's items, as written.
|
|
80
85
|
def self.calls(decision, inner, leaves)
|
|
81
86
|
inner.leaves.to_h do |range|
|
|
82
87
|
index = leaves.index(range)
|
|
83
88
|
[range,
|
|
84
|
-
"PathCoverage.
|
|
89
|
+
"PathCoverage.tried(#{decision.inspect}, #{index}, "]
|
|
85
90
|
end
|
|
86
91
|
end
|
|
92
|
+
|
|
93
|
+
# The inserts besides its patterns' calls for inner, a case
|
|
94
|
+
# in decision: a took first in each when arm with statements,
|
|
95
|
+
# noting the pattern last reached matched before the arm
|
|
96
|
+
# runs; a missed after the last pattern, which gives no
|
|
97
|
+
# patterns, noting the pattern last reached did not match;
|
|
98
|
+
# and a settled around the whole, noting the match of a when
|
|
99
|
+
# arm with no statements, which neither notes. A case that
|
|
100
|
+
# is decision itself decides it in took and missed, as its
|
|
101
|
+
# arms may leave; one nested in a decision's leaf only notes
|
|
102
|
+
# its patterns' outcomes, for that decision to decide.
|
|
103
|
+
def self.inserts(decision, inner, _at, _commands)
|
|
104
|
+
called = [decision.inspect, inner.site ? '' : '_within']
|
|
105
|
+
whens = inner.operands.select(&:body)
|
|
106
|
+
settled = "PathCoverage.settled(#{decision.inspect}, "
|
|
107
|
+
[*took_inserts(whens, called),
|
|
108
|
+
missed_insert(whens.last, called),
|
|
109
|
+
*Rewriter.wrap(settled, inner.whole, :bare, :kind)]
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# A took call, a statement of its own, first in each of whens
|
|
113
|
+
# with statements, called being [the decision's name, the
|
|
114
|
+
# calls' suffix]. It is outside the reach of the arm's first
|
|
115
|
+
# statement, as the case is decided before its arm runs.
|
|
116
|
+
def self.took_inserts(whens, (name, suffix))
|
|
117
|
+
rank = [Float::INFINITY, Rewriter::LAYERS.index(:arm)]
|
|
118
|
+
whens.select(&:extent).map do |arm|
|
|
119
|
+
call = "PathCoverage.took#{suffix}(#{name}); "
|
|
120
|
+
[arm.extent.begin, call, rank]
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# A missed call after the last pattern of last, a case's last
|
|
125
|
+
# when, called being [the decision's name, the calls' suffix].
|
|
126
|
+
def self.missed_insert(last, (name, suffix))
|
|
127
|
+
call = ", *PathCoverage.missed#{suffix}(#{name})"
|
|
128
|
+
[last.body.extent.end, call, [0, Rewriter::LAYERS.index(:leaf)]]
|
|
129
|
+
end
|
|
87
130
|
end
|
|
88
131
|
end
|
|
89
132
|
end
|
|
@@ -8,16 +8,26 @@ module PathCoverage
|
|
|
8
8
|
module Chain
|
|
9
9
|
extend Base
|
|
10
10
|
|
|
11
|
-
OPERATORS =
|
|
11
|
+
OPERATORS = ['&&', 'and', '||', 'or', 'when',
|
|
12
|
+
TRUTHY_WHEN].freeze
|
|
12
13
|
|
|
13
14
|
# The outcome of an operand on which each operator goes on
|
|
14
15
|
# to its next operand; any other ends the chain there. A
|
|
15
|
-
# when's patterns, and a case's whens, stop at a match
|
|
16
|
+
# when's patterns, and a case's whens, stop at a match, or,
|
|
17
|
+
# tested for truth, at a truthy one.
|
|
16
18
|
CONTINUES_ON = {
|
|
17
19
|
'&&' => true, 'and' => true, '||' => false, 'or' => false,
|
|
18
|
-
'when' => false, 'case' => false
|
|
20
|
+
'when' => false, TRUTHY_WHEN => false, 'case' => false
|
|
19
21
|
}.freeze
|
|
20
22
|
|
|
23
|
+
# Its leaf operands, each tested for truth; none for a when
|
|
24
|
+
# in a case with a subject, whose patterns are matched.
|
|
25
|
+
def self.truthy_operands(condition)
|
|
26
|
+
return [] if condition.operator == 'when'
|
|
27
|
+
|
|
28
|
+
condition.operands.grep(Range)
|
|
29
|
+
end
|
|
30
|
+
|
|
21
31
|
# The ways node can be evaluated: each way of its first
|
|
22
32
|
# operand, going on to the next while each outcome is the
|
|
23
33
|
# one its operator goes on with; a way that reaches an
|
|
@@ -43,6 +43,11 @@ module PathCoverage
|
|
|
43
43
|
|
|
44
44
|
OPERATORS = CHOICES
|
|
45
45
|
|
|
46
|
+
# Its predicate, when a leaf, tested for truth.
|
|
47
|
+
def self.truthy_operands(condition)
|
|
48
|
+
condition.operands.take(1).grep(Range)
|
|
49
|
+
end
|
|
50
|
+
|
|
46
51
|
# What its decision's leave call wraps: an if's or an unless's
|
|
47
52
|
# predicate, a leaf's byte range or a nested Condition, as
|
|
48
53
|
# its path is decided there and its arms are statements that
|
|
@@ -13,6 +13,12 @@ module PathCoverage
|
|
|
13
13
|
|
|
14
14
|
OPERATORS = [CONTAINS].freeze
|
|
15
15
|
|
|
16
|
+
# Its leaf, the last operand, whose outcome is its value's
|
|
17
|
+
# truthiness.
|
|
18
|
+
def self.truthy_operands(condition)
|
|
19
|
+
[condition.operands.last]
|
|
20
|
+
end
|
|
21
|
+
|
|
16
22
|
# The ways node can be evaluated: each way of every one of
|
|
17
23
|
# the decisions inside it, then each way of the leaf itself, its
|
|
18
24
|
# value the whole's.
|
|
@@ -63,9 +63,9 @@ module PathCoverage
|
|
|
63
63
|
enter = "PathCoverage.enter(#{name}, #{leaves.size})"
|
|
64
64
|
call = "PathCoverage.leave(#{enter}, "
|
|
65
65
|
extent = condition.extent
|
|
66
|
-
|
|
66
|
+
form = condition.commands.include?(extent) ? :grouped : :bare
|
|
67
67
|
presents(decision, leaves) +
|
|
68
|
-
Rewriter.wrap(call, extent,
|
|
68
|
+
Rewriter.wrap(call, extent, form, :decision)
|
|
69
69
|
end
|
|
70
70
|
|
|
71
71
|
# The inserts wrapping each of leaves, a &. chain decision's,
|
|
@@ -73,7 +73,7 @@ module PathCoverage
|
|
|
73
73
|
def self.presents(decision, leaves)
|
|
74
74
|
leaves.each_with_index.flat_map do |range, index|
|
|
75
75
|
opening = present(decision, index, 0)
|
|
76
|
-
Rewriter.wrap(opening, range,
|
|
76
|
+
Rewriter.wrap(opening, range, :bare, :leaf)
|
|
77
77
|
end
|
|
78
78
|
end
|
|
79
79
|
|
|
@@ -39,9 +39,9 @@ module PathCoverage
|
|
|
39
39
|
name = decision.inspect
|
|
40
40
|
written = "PathCoverage.written_at(#{name}, #{at}, #{ran}, "
|
|
41
41
|
settle = "PathCoverage.settle(#{name}, #{at}, #{!ran}, "
|
|
42
|
-
|
|
43
|
-
Rewriter.wrap(settle, inner.extent,
|
|
44
|
-
Rewriter.wrap(written, arm.extent,
|
|
42
|
+
form = commands.include?(arm.extent) ? :grouped : :bare
|
|
43
|
+
Rewriter.wrap(settle, inner.extent, :bare, :kind) +
|
|
44
|
+
Rewriter.wrap(written, arm.extent, form, :leaf)
|
|
45
45
|
end
|
|
46
46
|
end
|
|
47
47
|
end
|
data/lib/path_coverage/kinds.rb
CHANGED
|
@@ -27,6 +27,20 @@ module PathCoverage
|
|
|
27
27
|
BY_OPERATOR.fetch(operator)
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
+
# The byte range of every leaf of condition whose outcome is its
|
|
31
|
+
# truthiness, as each kind tells of its own operands, those of
|
|
32
|
+
# its nested conditions and of its arms' bodies included.
|
|
33
|
+
def self.truthy_leaves(condition)
|
|
34
|
+
own = of(condition.operator).truthy_operands(condition)
|
|
35
|
+
own + condition.operands.flat_map do |operand|
|
|
36
|
+
case operand
|
|
37
|
+
when Condition then truthy_leaves(operand)
|
|
38
|
+
when Arm then operand.body ? truthy_leaves(operand.body) : []
|
|
39
|
+
else []
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
30
44
|
# The names of the kinds measured, eg chain for Chain: a
|
|
31
45
|
# report's fingerprint, as runs measuring different kinds
|
|
32
46
|
# list a decision's paths differently.
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
require_relative 'evaluated'
|
|
2
|
+
|
|
1
3
|
module PathCoverage
|
|
2
4
|
# The loops in a method's Prism tree, as MethodLoops and
|
|
3
5
|
# LoopForks take them. Its blocks, lambdas, whiles, untils and
|
|
@@ -14,7 +16,7 @@ module PathCoverage
|
|
|
14
16
|
# Every def under node, outermost first.
|
|
15
17
|
def self.defs(node)
|
|
16
18
|
found = node.type == :def_node ? [node] : []
|
|
17
|
-
found + node.
|
|
19
|
+
found + Evaluated.children(node).flat_map { defs(_1) }
|
|
18
20
|
end
|
|
19
21
|
|
|
20
22
|
# Every loop under node, in the order they start, but none
|
|
@@ -23,7 +25,7 @@ module PathCoverage
|
|
|
23
25
|
return [] if node.nil? || node.type == :def_node
|
|
24
26
|
|
|
25
27
|
own = loop?(node) ? [node] : []
|
|
26
|
-
own + node.
|
|
28
|
+
own + Evaluated.children(node).flat_map { loops_in(_1) }
|
|
27
29
|
end
|
|
28
30
|
|
|
29
31
|
# True for node when it is a loop: one of LOOPS, or a begin
|
|
@@ -41,7 +43,7 @@ module PathCoverage
|
|
|
41
43
|
return true if node.type == :retry_node
|
|
42
44
|
return false if node.type == :begin_node && node.rescue_clause
|
|
43
45
|
|
|
44
|
-
node.
|
|
46
|
+
Evaluated.children(node).any? { retries?(_1) }
|
|
45
47
|
end
|
|
46
48
|
|
|
47
49
|
# The location a loop is named by. A begin that starts at its
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
require 'prism'
|
|
2
2
|
require_relative 'condition'
|
|
3
|
+
require_relative 'evaluated'
|
|
3
4
|
require_relative 'leaving'
|
|
4
5
|
require_relative 'manifest_blocks'
|
|
5
6
|
require_relative 'manifest_case'
|
|
@@ -13,8 +14,8 @@ require_relative 'decision'
|
|
|
13
14
|
|
|
14
15
|
module PathCoverage
|
|
15
16
|
# Finds every condition decision in source without running it: each
|
|
16
|
-
# outermost &&, ||, and or or, each ternary, if, unless and case
|
|
17
|
-
# with
|
|
17
|
+
# outermost &&, ||, and or or, each ternary, if, unless and case,
|
|
18
|
+
# with the conditions nested in it.
|
|
18
19
|
module Manifest
|
|
19
20
|
# { decision => Condition } for each decision in source, none of
|
|
20
21
|
# them named by an operator in ignored; a call to a method
|
|
@@ -36,7 +37,7 @@ module PathCoverage
|
|
|
36
37
|
|
|
37
38
|
# The source and decisions of the covered file at full_path.
|
|
38
39
|
def self.parse(full_path, config)
|
|
39
|
-
source =
|
|
40
|
+
source = source_of(full_path)
|
|
40
41
|
path = config.relative(full_path)
|
|
41
42
|
treated = %w[present nonempty].map { config.treated(_1, path) }
|
|
42
43
|
ignored = config.ignored
|
|
@@ -44,35 +45,63 @@ module PathCoverage
|
|
|
44
45
|
decisions: decisions(source, path, ignored, *treated) }
|
|
45
46
|
end
|
|
46
47
|
|
|
47
|
-
#
|
|
48
|
-
#
|
|
49
|
-
#
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
48
|
+
# The text of the file at full_path, its bytes as written, in
|
|
49
|
+
# the encoding Ruby reads it in, its magic comment's, else
|
|
50
|
+
# UTF-8, as Prism finds it: so byte offsets index it, and its
|
|
51
|
+
# characters count as Ruby counts them. Bytes that encoding
|
|
52
|
+
# cannot read, as in a file Ruby could not load, are binary
|
|
53
|
+
# text, each byte a character of its own.
|
|
54
|
+
def self.source_of(full_path)
|
|
55
|
+
bytes = File.binread(full_path)
|
|
56
|
+
encoding = Prism.parse(bytes).source.encoding
|
|
57
|
+
text = bytes.dup.force_encoding(encoding)
|
|
58
|
+
text.valid_encoding? ? text : bytes
|
|
55
59
|
end
|
|
56
60
|
|
|
57
|
-
# True for a
|
|
58
|
-
#
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
61
|
+
# True for a leaf tested for truth that is a flip-flop, eg
|
|
62
|
+
# (i == 3)..(i == 5), or one through parentheses, a ! or not,
|
|
63
|
+
# an && or an ||, each of which a condition passes through: so
|
|
64
|
+
# its condition alone makes it a flip-flop, which, passed to a
|
|
65
|
+
# call, would build a Range. A flip-flop under an if or ?:
|
|
66
|
+
# inside the leaf, or in a block, has a condition of its own.
|
|
67
|
+
def self.flip_flop?(node)
|
|
68
|
+
node.type == :flip_flop_node ||
|
|
69
|
+
conditioned(node).any? { flip_flop?(_1) }
|
|
62
70
|
end
|
|
63
71
|
|
|
64
|
-
#
|
|
65
|
-
#
|
|
66
|
-
|
|
72
|
+
# The expressions a condition on node passes into: the
|
|
73
|
+
# statements in parentheses, both sides of an && or ||, and the
|
|
74
|
+
# receiver of a ! or not; none for any other node.
|
|
75
|
+
def self.conditioned(node)
|
|
67
76
|
case node.type
|
|
68
|
-
when
|
|
69
|
-
when :
|
|
70
|
-
|
|
71
|
-
!node.if_keyword_loc.nil? && node.end_keyword_loc.nil?
|
|
72
|
-
else false
|
|
77
|
+
when *PASSING then node.compact_child_nodes
|
|
78
|
+
when :call_node then negated(node)
|
|
79
|
+
else []
|
|
73
80
|
end
|
|
74
81
|
end
|
|
75
82
|
|
|
83
|
+
# The nodes a condition passes into each child of.
|
|
84
|
+
PASSING = %i[
|
|
85
|
+
parentheses_node statements_node and_node or_node
|
|
86
|
+
].freeze
|
|
87
|
+
|
|
88
|
+
# [the receiver of node, a call, when it is a ! or not], none
|
|
89
|
+
# for any other call.
|
|
90
|
+
def self.negated(node)
|
|
91
|
+
node.name == :! ? [node.receiver].compact : []
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# True for an expression that is no bare argument, so a
|
|
95
|
+
# rewrite wrapping it in another call must parenthesise it.
|
|
96
|
+
# Prism decides: the expression is bare only when, as a call's
|
|
97
|
+
# argument after another, it parses whole, eg `id.size == 6`,
|
|
98
|
+
# but not `id.include? 'N'`, `not x`, `x in y` or `x if y`.
|
|
99
|
+
# After another, since a rewrite's call passes it last, and a
|
|
100
|
+
# lone argument may be a command call where a later one may not.
|
|
101
|
+
def self.bare_less?(node)
|
|
102
|
+
Prism.parse("f(0, #{node.slice})").errors.any?
|
|
103
|
+
end
|
|
104
|
+
|
|
76
105
|
# Literals and lambdas, whose value is truthy by their form;
|
|
77
106
|
# a constant is not among them, as it can hold nil or false.
|
|
78
107
|
TRUTHY_TYPES = %i[
|
|
@@ -112,6 +141,7 @@ module PathCoverage
|
|
|
112
141
|
|
|
113
142
|
# Collects each decision and its Condition.
|
|
114
143
|
class ConditionSites < Prism::Visitor
|
|
144
|
+
include Evaluated::Visitor
|
|
115
145
|
include ManifestBlocks
|
|
116
146
|
include ManifestCase
|
|
117
147
|
include ManifestChains
|
|
@@ -162,11 +192,8 @@ module PathCoverage
|
|
|
162
192
|
add_decision(node)
|
|
163
193
|
end
|
|
164
194
|
|
|
165
|
-
# Notes a case decision
|
|
166
|
-
# case is no decision, though a condition inside it may be.
|
|
195
|
+
# Notes a case decision: every case is one.
|
|
167
196
|
def visit_case_node(node)
|
|
168
|
-
return super unless case_decision?(node)
|
|
169
|
-
|
|
170
197
|
add_decision(node)
|
|
171
198
|
end
|
|
172
199
|
|
|
@@ -193,7 +220,7 @@ module PathCoverage
|
|
|
193
220
|
decision = Decision.at(@path, decision_loc(node))
|
|
194
221
|
@decisions[decision] = nil
|
|
195
222
|
outer = @notes
|
|
196
|
-
@notes = { commands: [], fixed: {} }
|
|
223
|
+
@notes = { commands: [], flip_flops: [], fixed: {} }
|
|
197
224
|
condition = scoped { condition_of(node) }
|
|
198
225
|
@decisions[decision] =
|
|
199
226
|
condition.with(**@notes, site: node.node_id)
|
|
@@ -260,10 +287,7 @@ module PathCoverage
|
|
|
260
287
|
inner = leaf_decisions(operand)
|
|
261
288
|
loc = operand.location
|
|
262
289
|
range = loc.start_offset...loc.end_offset
|
|
263
|
-
|
|
264
|
-
outcome = Manifest.fixed_outcome(operand) ||
|
|
265
|
-
given_outcome(operand)
|
|
266
|
-
@notes[:fixed][range] = outcome if outcome
|
|
290
|
+
note_leaf(operand, range, tested: true)
|
|
267
291
|
nonempty = nonempty_call?(operand, inner)
|
|
268
292
|
contained(inner, range, nonempty:)
|
|
269
293
|
end
|
|
@@ -3,48 +3,65 @@ require_relative 'condition'
|
|
|
3
3
|
module PathCoverage
|
|
4
4
|
# The part of the manifest's visitor that builds a case decision,
|
|
5
5
|
# named case: its arms are its whens, each holding a when
|
|
6
|
-
# condition of its patterns, and its else.
|
|
7
|
-
#
|
|
6
|
+
# condition of its patterns, and its else. Every case is one,
|
|
7
|
+
# with a subject or none, and with splats among its patterns or
|
|
8
|
+
# none: each pattern is a leaf, a splat one leaf for all its
|
|
9
|
+
# items, and Ruby itself matches or tests it. It relies on its
|
|
10
|
+
# includer's extent_of, statement_arm and known_outcome.
|
|
8
11
|
module ManifestCase
|
|
9
|
-
# True for a case whose paths can be seen: one with a
|
|
10
|
-
# subject, as each pattern is then matched against it, and
|
|
11
|
-
# no splat among its patterns, whose items cannot each be
|
|
12
|
-
# stood in for.
|
|
13
|
-
def case_decision?(node)
|
|
14
|
-
return false if node.predicate.nil?
|
|
15
|
-
|
|
16
|
-
patterns = node.conditions.flat_map(&:conditions)
|
|
17
|
-
!patterns.map(&:type).include?(:splat_node)
|
|
18
|
-
end
|
|
19
|
-
|
|
20
12
|
# A case as a Condition, its extent its case keyword to the
|
|
21
|
-
# end of its subject,
|
|
13
|
+
# end of its subject, or of the keyword when it has none, as
|
|
14
|
+
# its arms are statements; its whole the case to its end.
|
|
22
15
|
def case_condition(node)
|
|
23
|
-
node.predicate
|
|
24
|
-
|
|
16
|
+
subject = node.predicate
|
|
17
|
+
subject&.accept(self)
|
|
18
|
+
whens = node.conditions.map { when_arm(_1, subject) }
|
|
25
19
|
start = node.case_keyword_loc.start_offset
|
|
20
|
+
head = subject ? subject.location : node.case_keyword_loc
|
|
26
21
|
Condition.new(
|
|
27
22
|
operator: 'case',
|
|
28
23
|
operands: [*whens, *case_else(node.else_clause)],
|
|
29
|
-
extent: start...
|
|
24
|
+
extent: start...head.end_offset, whole: extent_of(node)
|
|
30
25
|
)
|
|
31
26
|
end
|
|
32
27
|
|
|
33
28
|
# One when as an arm, its body a when condition whose leaves
|
|
34
|
-
# are its patterns' byte ranges
|
|
35
|
-
#
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
29
|
+
# are its patterns' byte ranges: matched against subject, so
|
|
30
|
+
# never fixed; or, in a case with no subject, a TRUTHY_WHEN,
|
|
31
|
+
# tested for truth, so fixed as a leaf is. Its extent is its
|
|
32
|
+
# patterns as written, parentheses and splats included.
|
|
33
|
+
def when_arm(clause, subject)
|
|
34
|
+
tested = subject.nil?
|
|
35
|
+
written = clause.conditions
|
|
36
|
+
patterns = written.map { pattern_leaf(_1, tested:) }
|
|
37
|
+
first, last = written.values_at(0, -1).map { extent_of(_1) }
|
|
41
38
|
body = Condition.new(
|
|
42
|
-
operator: 'when', operands: patterns,
|
|
43
|
-
extent:
|
|
39
|
+
operator: tested ? TRUTHY_WHEN : 'when', operands: patterns,
|
|
40
|
+
extent: first.begin...last.end
|
|
44
41
|
)
|
|
45
42
|
statement_arm(:when, clause.statements).with(body:)
|
|
46
43
|
end
|
|
47
44
|
|
|
45
|
+
# The byte range of pattern as a leaf: a splat's expression,
|
|
46
|
+
# whose items are its patterns, eg xs in *xs; else the pattern
|
|
47
|
+
# inside any parentheses, so a decision that is the whole of it
|
|
48
|
+
# is the whole of the leaf. Either is an expression the case
|
|
49
|
+
# takes as written when a call that gives it back stands in
|
|
50
|
+
# its place. It is noted as a leaf is, tested for truth in a
|
|
51
|
+
# case with no subject; a splat never is, as it tests each of
|
|
52
|
+
# its items, so is never fixed. Decisions inside the pattern
|
|
53
|
+
# are decisions of their own, even in a case in another's leaf,
|
|
54
|
+
# so a decision that is the whole of a pattern gives it its
|
|
55
|
+
# outcome.
|
|
56
|
+
def pattern_leaf(pattern, tested:)
|
|
57
|
+
scoped { pattern.accept(self) }
|
|
58
|
+
splat = pattern.type == :splat_node
|
|
59
|
+
node = splat ? pattern.expression : grouped(pattern)
|
|
60
|
+
range = extent_of(node)
|
|
61
|
+
note_leaf(node, range, tested: tested && !splat)
|
|
62
|
+
range
|
|
63
|
+
end
|
|
64
|
+
|
|
48
65
|
# A case's else arm, none when it has no else.
|
|
49
66
|
def case_else(clause)
|
|
50
67
|
clause ? [statement_arm(:else, clause.statements)] : []
|
|
@@ -43,6 +43,26 @@ module PathCoverage
|
|
|
43
43
|
# The one outcome each of a decision's values gives a leaf.
|
|
44
44
|
GIVEN = { [true] => :truthy, [false] => :falsy }.freeze
|
|
45
45
|
|
|
46
|
+
# Notes the leaf node, at range, among its decision's commands
|
|
47
|
+
# when it is no bare argument; and, tested for truth, among its
|
|
48
|
+
# flip-flops when it is one, and among its fixed leaves when it
|
|
49
|
+
# can have one outcome only.
|
|
50
|
+
def note_leaf(node, range, tested:)
|
|
51
|
+
@notes[:commands] << range if Manifest.bare_less?(node)
|
|
52
|
+
return unless tested
|
|
53
|
+
|
|
54
|
+
@notes[:flip_flops] << range if Manifest.flip_flop?(node)
|
|
55
|
+
outcome = known_outcome(node)
|
|
56
|
+
@notes[:fixed][range] = outcome if outcome
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# The one outcome a leaf tested for truth can have, by its
|
|
60
|
+
# form or, for a decision of its own, its values; nil when it
|
|
61
|
+
# can have either.
|
|
62
|
+
def known_outcome(node)
|
|
63
|
+
Manifest.fixed_outcome(node) || given_outcome(node)
|
|
64
|
+
end
|
|
65
|
+
|
|
46
66
|
# The one outcome the leaf node can have, as the decision it
|
|
47
67
|
# wholly is gives it on every one of its paths, eg :truthy for
|
|
48
68
|
# a select, whose array is truthy; nil when its paths give
|