path_coverage 0.4.0 → 0.5.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 +286 -199
- data/exe/path_coverage_report +4 -0
- data/lib/path_coverage/call_trace.rb +88 -0
- data/lib/path_coverage/calls.rb +49 -43
- data/lib/path_coverage/card_context.rb +104 -0
- data/lib/path_coverage/card_forks.rb +44 -0
- data/lib/path_coverage/card_gaps.rb +125 -0
- data/lib/path_coverage/card_scopes.rb +37 -0
- data/lib/path_coverage/card_tree.rb +79 -66
- data/lib/path_coverage/card_way_lists.rb +72 -0
- data/lib/path_coverage/card_ways.rb +123 -67
- data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +35 -33
- data/lib/path_coverage/condition.rb +14 -14
- data/lib/path_coverage/configuration.rb +7 -4
- data/lib/path_coverage/configuration_limits.rb +52 -0
- data/lib/path_coverage/configuration_paths.rb +7 -7
- data/lib/path_coverage/console_summary.rb +24 -22
- data/lib/path_coverage/{site.rb → decision.rb} +2 -2
- data/lib/path_coverage/decision_locations.rb +76 -0
- data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +13 -11
- data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
- data/lib/path_coverage/fold_branches.rb +18 -0
- data/lib/path_coverage/fold_cells.rb +42 -0
- data/lib/path_coverage/{html_tree.rb → fold_rows.rb} +42 -71
- data/lib/path_coverage/gaps.rb +33 -15
- data/lib/path_coverage/json_reader.rb +54 -19
- data/lib/path_coverage/json_reports.rb +102 -0
- data/lib/path_coverage/json_writer.rb +27 -12
- data/lib/path_coverage/kernel.rb +56 -58
- data/lib/path_coverage/kinds/base.rb +16 -16
- data/lib/path_coverage/kinds/block.rb +16 -15
- data/lib/path_coverage/kinds/case.rb +14 -12
- data/lib/path_coverage/kinds/chain.rb +40 -11
- data/lib/path_coverage/kinds/choice.rb +4 -3
- data/lib/path_coverage/kinds/contains.rb +40 -32
- data/lib/path_coverage/kinds/safe.rb +38 -26
- data/lib/path_coverage/kinds/write.rb +6 -6
- data/lib/path_coverage/kinds.rb +2 -2
- data/lib/path_coverage/leaving.rb +44 -0
- data/lib/path_coverage/loop_forks.rb +57 -0
- data/lib/path_coverage/loop_nodes.rb +55 -0
- data/lib/path_coverage/manifest.rb +92 -58
- data/lib/path_coverage/manifest_blocks.rb +5 -26
- data/lib/path_coverage/manifest_case.rb +2 -2
- data/lib/path_coverage/manifest_chains.rb +8 -8
- data/lib/path_coverage/manifest_choices.rb +23 -20
- data/lib/path_coverage/manifest_disabled.rb +97 -0
- data/lib/path_coverage/manifest_folding.rb +43 -40
- data/lib/path_coverage/manifest_ignoring.rb +6 -6
- data/lib/path_coverage/manifest_writes.rb +11 -10
- data/lib/path_coverage/measured_by.rb +20 -0
- data/lib/path_coverage/merge.rb +7 -7
- data/lib/path_coverage/method_loops.rb +128 -0
- data/lib/path_coverage/method_statements.rb +130 -127
- data/lib/path_coverage/method_ways.rb +142 -0
- data/lib/path_coverage/paths_report.rb +16 -16
- data/lib/path_coverage/recorder.rb +57 -36
- data/lib/path_coverage/recorder_blocks.rb +17 -17
- data/lib/path_coverage/recorder_cases.rb +12 -11
- data/lib/path_coverage/recorder_chains.rb +12 -12
- data/lib/path_coverage/recorder_writes.rb +9 -9
- data/lib/path_coverage/render.rb +58 -51
- data/lib/path_coverage/result.rb +32 -31
- data/lib/path_coverage/rewriter.rb +39 -27
- data/lib/path_coverage/rewriter_nested.rb +13 -13
- data/lib/path_coverage/runs_report.rb +84 -0
- data/lib/path_coverage/source_lines.rb +16 -16
- data/lib/path_coverage/source_report.rb +100 -0
- data/lib/path_coverage/stable_leaves.rb +162 -0
- data/lib/path_coverage/statement_arms.rb +31 -30
- data/lib/path_coverage/summary.rb +4 -62
- data/lib/path_coverage/totals.rb +10 -8
- data/lib/path_coverage/version.rb +1 -1
- data/lib/path_coverage/walk.rb +106 -0
- data/lib/path_coverage/way_algebras.rb +127 -0
- data/lib/path_coverage/ways_report.rb +91 -0
- data/lib/path_coverage/ways_totals.rb +46 -0
- data/lib/path_coverage/within_methods.rb +135 -80
- metadata +33 -28
- data/lib/path_coverage/card.rb +0 -14
- data/lib/path_coverage/html/file.html +0 -33
- data/lib/path_coverage/html/index.html +0 -29
- data/lib/path_coverage/html/settings.js +0 -50
- data/lib/path_coverage/html/side.js +0 -599
- data/lib/path_coverage/html/sort.js +0 -34
- data/lib/path_coverage/html/style.css +0 -484
- data/lib/path_coverage/html/tabs.js +0 -15
- data/lib/path_coverage/html/theme.js +0 -27
- data/lib/path_coverage/html_branch.rb +0 -30
- data/lib/path_coverage/html_cards.rb +0 -88
- data/lib/path_coverage/html_cells.rb +0 -71
- data/lib/path_coverage/html_file.rb +0 -123
- data/lib/path_coverage/html_index.rb +0 -128
- data/lib/path_coverage/html_index_head.rb +0 -29
- data/lib/path_coverage/html_labels.rb +0 -16
- data/lib/path_coverage/html_methods.rb +0 -66
- data/lib/path_coverage/html_nav.rb +0 -67
- data/lib/path_coverage/html_report.rb +0 -131
- data/lib/path_coverage/html_statements.rb +0 -186
- data/lib/path_coverage/html_tabs.rb +0 -26
- data/lib/path_coverage/html_template.rb +0 -26
|
@@ -3,7 +3,7 @@ require_relative 'base'
|
|
|
3
3
|
module PathCoverage
|
|
4
4
|
module Kinds
|
|
5
5
|
# A &. chain, eg user&.account&.owner, each receiver nil or
|
|
6
|
-
# not, a nil one skipping the rest of the chain. As a
|
|
6
|
+
# not, a nil one skipping the rest of the chain. As a decision of
|
|
7
7
|
# its own its operands are its receivers; nested in another,
|
|
8
8
|
# they are its receivers then its value, its last leaf. Each
|
|
9
9
|
# receiver is wrapped in a present call, which passes it on
|
|
@@ -19,7 +19,7 @@ module PathCoverage
|
|
|
19
19
|
chain(node.operands[0...-1], node.operands.last, fixed)
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
-
# The ways a &. chain
|
|
22
|
+
# The ways a &. chain decision can be evaluated, every operand
|
|
23
23
|
# a receiver.
|
|
24
24
|
def self.root_ways(node, fixed)
|
|
25
25
|
chain(node.operands, nil, fixed)
|
|
@@ -32,7 +32,7 @@ module PathCoverage
|
|
|
32
32
|
def self.chain(receivers, last, fixed)
|
|
33
33
|
first, *rest = receivers
|
|
34
34
|
on = if !rest.empty? then chain(rest, last, fixed)
|
|
35
|
-
elsif last then
|
|
35
|
+
elsif last then DecisionPaths.walk(last, fixed)
|
|
36
36
|
else [[{}, true]]
|
|
37
37
|
end
|
|
38
38
|
present = on.map do |outcomes, value|
|
|
@@ -52,20 +52,29 @@ module PathCoverage
|
|
|
52
52
|
end
|
|
53
53
|
end
|
|
54
54
|
|
|
55
|
-
# The inserts for a &. chain
|
|
56
|
-
# call, and the whole chain in a leave call that
|
|
57
|
-
# first. Eg user&.account becomes, in effect,
|
|
58
|
-
# leave(enter(
|
|
59
|
-
|
|
55
|
+
# The inserts for a &. chain decision: each receiver in a
|
|
56
|
+
# present call, and the whole chain in a leave call that
|
|
57
|
+
# enters it first. Eg user&.account becomes, in effect,
|
|
58
|
+
# leave(enter(decision, 1),
|
|
59
|
+
# present(decision, 0, 0, user)&.account).
|
|
60
|
+
def self.decision_inserts(decision, condition)
|
|
60
61
|
leaves = condition.leaves
|
|
61
|
-
|
|
62
|
+
name = decision.inspect
|
|
63
|
+
enter = "PathCoverage.enter(#{name}, #{leaves.size})"
|
|
62
64
|
call = "PathCoverage.leave(#{enter}, "
|
|
63
|
-
command = condition.commands.include?(condition.extent)
|
|
64
|
-
presents = leaves.each_with_index.flat_map do |range, index|
|
|
65
|
-
Rewriter.wrap(present(site, index, 0), range, false, :leaf)
|
|
66
|
-
end
|
|
67
65
|
extent = condition.extent
|
|
68
|
-
|
|
66
|
+
command = condition.commands.include?(extent)
|
|
67
|
+
presents(decision, leaves) +
|
|
68
|
+
Rewriter.wrap(call, extent, command, :decision)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# The inserts wrapping each of leaves, a &. chain decision's,
|
|
72
|
+
# in its present call.
|
|
73
|
+
def self.presents(decision, leaves)
|
|
74
|
+
leaves.each_with_index.flat_map do |range, index|
|
|
75
|
+
opening = present(decision, index, 0)
|
|
76
|
+
Rewriter.wrap(opening, range, false, :leaf)
|
|
77
|
+
end
|
|
69
78
|
end
|
|
70
79
|
|
|
71
80
|
# Its leaves need present and chained calls.
|
|
@@ -74,30 +83,32 @@ module PathCoverage
|
|
|
74
83
|
end
|
|
75
84
|
|
|
76
85
|
# { leaf range => the opening of its call } for the leaves of
|
|
77
|
-
# chain nested in a
|
|
86
|
+
# chain nested in a decision whose leaves are leaves: a
|
|
78
87
|
# receiver's present call, and the chain's value's chained
|
|
79
88
|
# call, each told where its chain's leaves start.
|
|
80
|
-
def self.calls(
|
|
89
|
+
def self.calls(decision, chain, leaves)
|
|
81
90
|
first = leaves.index(chain.operands.first)
|
|
82
91
|
*receivers, value = chain.operands
|
|
83
92
|
calls = receivers.to_h do |range|
|
|
84
|
-
[range, present(
|
|
93
|
+
[range, present(decision, leaves.index(range), first)]
|
|
85
94
|
end
|
|
86
95
|
at = leaves.index(value)
|
|
87
|
-
calls.merge(value => chained(
|
|
96
|
+
calls.merge(value => chained(decision, at, first))
|
|
88
97
|
end
|
|
89
98
|
|
|
90
99
|
# The opening of the chained call around the index-th leaf
|
|
91
|
-
# of
|
|
100
|
+
# of decision, a chain's value, its chain's leaves starting at
|
|
92
101
|
# first.
|
|
93
|
-
def self.chained(
|
|
94
|
-
"PathCoverage.chained(#{
|
|
102
|
+
def self.chained(decision, index, first)
|
|
103
|
+
"PathCoverage.chained(#{decision.inspect}, #{index}, " \
|
|
104
|
+
"#{first}, "
|
|
95
105
|
end
|
|
96
106
|
|
|
97
107
|
# The opening of the present call around the index-th leaf
|
|
98
|
-
# of
|
|
99
|
-
def self.present(
|
|
100
|
-
"PathCoverage.present(#{
|
|
108
|
+
# of decision, its chain's leaves starting at first.
|
|
109
|
+
def self.present(decision, index, first)
|
|
110
|
+
"PathCoverage.present(#{decision.inspect}, #{index}, " \
|
|
111
|
+
"#{first}, "
|
|
101
112
|
end
|
|
102
113
|
|
|
103
114
|
# Its leaves' nodes, each after the first showing only what
|
|
@@ -130,8 +141,9 @@ module PathCoverage
|
|
|
130
141
|
# is reached only when the one before is not nil.
|
|
131
142
|
def self.rows(chain, indent)
|
|
132
143
|
chain[:operands].each_with_index.map do |operand, depth|
|
|
133
|
-
|
|
134
|
-
|
|
144
|
+
gaps = FoldBranches::GAP * depth
|
|
145
|
+
branch = "#{indent}#{gaps}#{FoldBranches::ELL}"
|
|
146
|
+
FoldRows.operand_row(operand, branch)
|
|
135
147
|
end
|
|
136
148
|
end
|
|
137
149
|
end
|
|
@@ -22,21 +22,21 @@ module PathCoverage
|
|
|
22
22
|
|
|
23
23
|
# { target range => nil }: its target is never read, so
|
|
24
24
|
# never wrapped.
|
|
25
|
-
def self.calls(
|
|
25
|
+
def self.calls(_decision, inner, _leaves)
|
|
26
26
|
{ inner.operands.first => nil }
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
# A write, its target the
|
|
29
|
+
# A write, its target the decision's at-th leaf: its right side
|
|
30
30
|
# in a written_at call, which notes the target's outcome when
|
|
31
31
|
# it runs, and the whole of it in a settle call, which notes
|
|
32
32
|
# the other when it does not. A right side among commands,
|
|
33
33
|
# eg a command call, is parenthesised. Eg x ||= v becomes,
|
|
34
|
-
# in effect, settle(
|
|
35
|
-
# at, false, v)).
|
|
36
|
-
def self.inserts(
|
|
34
|
+
# in effect, settle(decision, at, true,
|
|
35
|
+
# x ||= written_at(decision, at, false, v)).
|
|
36
|
+
def self.inserts(decision, inner, at, commands)
|
|
37
37
|
_target, arm = inner.operands
|
|
38
38
|
ran = THEN_ON[inner.operator]
|
|
39
|
-
name =
|
|
39
|
+
name = decision.inspect
|
|
40
40
|
written = "PathCoverage.written_at(#{name}, #{at}, #{ran}, "
|
|
41
41
|
settle = "PathCoverage.settle(#{name}, #{at}, #{!ran}, "
|
|
42
42
|
command = commands.include?(arm.extent)
|
data/lib/path_coverage/kinds.rb
CHANGED
|
@@ -10,7 +10,7 @@ require_relative 'kinds/write'
|
|
|
10
10
|
module PathCoverage
|
|
11
11
|
# The kinds of condition, each a module that knows, for a
|
|
12
12
|
# condition named by one of its OPERATORS, the ways it can be
|
|
13
|
-
# evaluated, its value in each column of its
|
|
13
|
+
# evaluated, its value in each column of its decision's tree, and
|
|
14
14
|
# the inserts that instrument it. Each extends Kinds::Base,
|
|
15
15
|
# whose hooks give what most kinds need.
|
|
16
16
|
module Kinds
|
|
@@ -29,7 +29,7 @@ module PathCoverage
|
|
|
29
29
|
|
|
30
30
|
# The names of the kinds measured, eg chain for Chain: a
|
|
31
31
|
# report's fingerprint, as runs measuring different kinds
|
|
32
|
-
# list a
|
|
32
|
+
# list a decision's paths differently.
|
|
33
33
|
def self.names
|
|
34
34
|
ALL.map { |kind| kind.name.split('::').last.downcase }
|
|
35
35
|
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require_relative 'condition'
|
|
2
|
+
|
|
3
|
+
module PathCoverage
|
|
4
|
+
# Whether code leaves the method where it stands, decided once,
|
|
5
|
+
# from Prism's nodes, for every part of the gem that asks.
|
|
6
|
+
module Leaving
|
|
7
|
+
# The methods whose call, with no receiver, leaves the method:
|
|
8
|
+
# raise, fail, its other name, and throw, for its catch.
|
|
9
|
+
CALLS = %i[raise fail throw].freeze
|
|
10
|
+
|
|
11
|
+
# 'return' for a return, the method's name for a call of one of
|
|
12
|
+
# CALLS with no receiver, eg 'fail'; nil for a statement that
|
|
13
|
+
# does not leave the method.
|
|
14
|
+
def self.word(statement)
|
|
15
|
+
return 'return' if statement.type == :return_node
|
|
16
|
+
|
|
17
|
+
statement.name.to_s if leaving_call?(statement)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# The arm a statement that leaves the method is as an operand,
|
|
21
|
+
# eg return t in x or return t: run when the operand before it
|
|
22
|
+
# gives the outcome its operator goes on with, never a value,
|
|
23
|
+
# so never a leaf.
|
|
24
|
+
def self.arm(statement)
|
|
25
|
+
loc = statement.location
|
|
26
|
+
Arm.new(kind: :then, extent: loc.start_offset...loc.end_offset,
|
|
27
|
+
body: nil, leaves: word(statement))
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# The word of the first of an arm's statements that leaves the
|
|
31
|
+
# method, statements its code; nil for an arm with no
|
|
32
|
+
# statements or none that leaves.
|
|
33
|
+
def self.of(statements)
|
|
34
|
+
statements && statements.body.map { word(_1) }.compact.first
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# True when statement is a call of one of CALLS with no
|
|
38
|
+
# receiver.
|
|
39
|
+
def self.leaving_call?(statement)
|
|
40
|
+
statement.type == :call_node && statement.receiver.nil? &&
|
|
41
|
+
CALLS.include?(statement.name)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
require 'prism'
|
|
2
|
+
require_relative 'condition'
|
|
3
|
+
require_relative 'decision'
|
|
4
|
+
require_relative 'loop_nodes'
|
|
5
|
+
|
|
6
|
+
module PathCoverage
|
|
7
|
+
# The loops of each method that fork when loops are decisions:
|
|
8
|
+
# 0 items, or 1+ items, its body walked once. Each is one whose
|
|
9
|
+
# passes a pass call put first in its body can show, so a
|
|
10
|
+
# call's trace tells the two apart; but no block of a block
|
|
11
|
+
# method's decision, eg any?'s, whose paths count its items
|
|
12
|
+
# already.
|
|
13
|
+
module LoopForks
|
|
14
|
+
# { def decision => { loop's name => [its bytes, its body's] } }
|
|
15
|
+
# of each def in source, at path, decisions as
|
|
16
|
+
# Manifest.decisions gives them.
|
|
17
|
+
def self.of(source, path, decisions)
|
|
18
|
+
blocks = decision_blocks(decisions)
|
|
19
|
+
LoopNodes.defs(Prism.parse(source).value).to_h do |node|
|
|
20
|
+
[Decision.of(path, node), in_def(node, path, blocks)]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# { loop's name => [its bytes, its body's] } of each loop of
|
|
25
|
+
# node, a def, at path, that forks, blocks being where the
|
|
26
|
+
# block of each block method's decision ends.
|
|
27
|
+
def self.in_def(node, path, blocks)
|
|
28
|
+
loops = LoopNodes.loops_in(node.body)
|
|
29
|
+
loops.filter_map { fork(_1, path, blocks) }.to_h
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Where the block of each block method's decision among
|
|
33
|
+
# decisions ends, a byte offset, as its call does.
|
|
34
|
+
def self.decision_blocks(decisions)
|
|
35
|
+
by = decisions.values.group_by { BLOCK_PATHS.key?(_1.operator) }
|
|
36
|
+
by.fetch(true, []).to_set { _1.extent.end }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# [name, [its bytes, its body's]] of loop, at path, when it is
|
|
40
|
+
# no block method's, its call not ending at one of blocks, and
|
|
41
|
+
# a pass call can go first in its body; else nil.
|
|
42
|
+
def self.fork(loop, path, blocks)
|
|
43
|
+
body = LoopNodes.body_of(loop)
|
|
44
|
+
return if blocks.include?(loop.location.end_offset)
|
|
45
|
+
return if body.nil? || LoopNodes.modifier?(loop)
|
|
46
|
+
return unless LoopNodes.first_statement(body)
|
|
47
|
+
|
|
48
|
+
[Decision.at(path, loop.location),
|
|
49
|
+
[range(loop.location), range(body.location)]]
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# The bytes of location, as a range.
|
|
53
|
+
def self.range(location)
|
|
54
|
+
location.start_offset...location.end_offset
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
module PathCoverage
|
|
2
|
+
# The loops in a method's Prism tree: its blocks, lambdas,
|
|
3
|
+
# whiles, untils and fors, each run 0 or more times, and their
|
|
4
|
+
# bodies, as MethodLoops and LoopForks take them.
|
|
5
|
+
module LoopNodes
|
|
6
|
+
# The types of node that are loops written with a keyword,
|
|
7
|
+
# their body their statements.
|
|
8
|
+
KEYWORDS = %i[while_node until_node for_node].freeze
|
|
9
|
+
|
|
10
|
+
# The types of node that are loops.
|
|
11
|
+
LOOPS = [:block_node, :lambda_node, *KEYWORDS].freeze
|
|
12
|
+
|
|
13
|
+
# Every def under node, outermost first.
|
|
14
|
+
def self.defs(node)
|
|
15
|
+
found = node.type == :def_node ? [node] : []
|
|
16
|
+
found + node.compact_child_nodes.flat_map { defs(_1) }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Every loop under node, in the order they start, but none
|
|
20
|
+
# inside a def under it, whose loops are its own.
|
|
21
|
+
def self.loops_in(node)
|
|
22
|
+
return [] if node.nil? || node.type == :def_node
|
|
23
|
+
|
|
24
|
+
own = LOOPS.include?(node.type) ? [node] : []
|
|
25
|
+
own + node.compact_child_nodes.flat_map { loops_in(_1) }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# The body of loop: a block's or lambda's body, a keyword
|
|
29
|
+
# loop's statements; nil for an empty one.
|
|
30
|
+
def self.body_of(loop)
|
|
31
|
+
KEYWORDS.include?(loop.type) ? loop.statements : loop.body
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# True for a keyword loop written after its body, eg x += 1
|
|
35
|
+
# while x < 9, where a call put first in its body would run
|
|
36
|
+
# once, not each pass, so none goes there.
|
|
37
|
+
def self.modifier?(loop)
|
|
38
|
+
return false unless KEYWORDS.include?(loop.type)
|
|
39
|
+
|
|
40
|
+
body = loop.statements.location
|
|
41
|
+
loop.keyword_loc.start_offset > body.start_offset
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# The first statement of a loop's body, a do ... rescue ... end
|
|
45
|
+
# block's being its first before the rescue; nil when it has
|
|
46
|
+
# none there, its statements all in its rescue, which run only
|
|
47
|
+
# once a statement raises, so there is no pass to note.
|
|
48
|
+
def self.first_statement(body)
|
|
49
|
+
return body.body.first unless body.type == :begin_node
|
|
50
|
+
return if body.statements.nil?
|
|
51
|
+
|
|
52
|
+
body.statements.body.first
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -1,50 +1,78 @@
|
|
|
1
1
|
require 'prism'
|
|
2
2
|
require_relative 'condition'
|
|
3
|
+
require_relative 'leaving'
|
|
3
4
|
require_relative 'manifest_blocks'
|
|
4
5
|
require_relative 'manifest_case'
|
|
5
6
|
require_relative 'manifest_chains'
|
|
6
7
|
require_relative 'manifest_choices'
|
|
8
|
+
require_relative 'manifest_disabled'
|
|
7
9
|
require_relative 'manifest_folding'
|
|
8
10
|
require_relative 'manifest_ignoring'
|
|
9
11
|
require_relative 'manifest_writes'
|
|
10
|
-
require_relative '
|
|
12
|
+
require_relative 'decision'
|
|
11
13
|
|
|
12
14
|
module PathCoverage
|
|
13
|
-
# Finds every condition
|
|
15
|
+
# Finds every condition decision in source without running it: each
|
|
14
16
|
# outermost &&, ||, and or or, each ternary, if, unless and case
|
|
15
17
|
# with a subject, with the conditions nested in it.
|
|
16
18
|
module Manifest
|
|
17
|
-
# {
|
|
19
|
+
# { decision => Condition } for each decision in source, none of
|
|
18
20
|
# them named by an operator in ignored; a call to a method
|
|
19
21
|
# named in present, as a &.'s receiver, is never nil, and a
|
|
20
22
|
# call to one named in nonempty, eg any?, is true exactly
|
|
21
|
-
# when its receiver has items.
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
# when its receiver has items. None is on a line not
|
|
24
|
+
# analysed, as ManifestDisabled.lines gives them.
|
|
25
|
+
def self.decisions(source, path, ignored = [], present = [],
|
|
26
|
+
nonempty = [])
|
|
24
27
|
collector = ConditionSites.new(path, ignored,
|
|
25
28
|
[present, nonempty])
|
|
26
|
-
Prism.parse(source)
|
|
27
|
-
collector
|
|
29
|
+
parsed = Prism.parse(source)
|
|
30
|
+
parsed.value.accept(collector)
|
|
31
|
+
disabled = ManifestDisabled.lines(source, parsed, path)
|
|
32
|
+
collector.decisions.reject do |name, _|
|
|
33
|
+
disabled.include?(name.split(':')[-2].to_i)
|
|
34
|
+
end
|
|
28
35
|
end
|
|
29
36
|
|
|
30
|
-
# The source and
|
|
37
|
+
# The source and decisions of the covered file at full_path.
|
|
31
38
|
def self.parse(full_path, config)
|
|
32
39
|
source = File.read(full_path)
|
|
33
40
|
path = config.relative(full_path)
|
|
34
41
|
treated = %w[present nonempty].map { config.treated(_1, path) }
|
|
35
42
|
ignored = config.ignored
|
|
36
|
-
{ source:,
|
|
43
|
+
{ source:,
|
|
44
|
+
decisions: decisions(source, path, ignored, *treated) }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# True for an expression that is no bare argument, so a
|
|
48
|
+
# rewrite wrapping it in another call must parenthesise it: a
|
|
49
|
+
# command call, an and or an or, or a modifier, eg x unless y.
|
|
50
|
+
def self.bare_less?(node)
|
|
51
|
+
return true if command?(node) || modifier?(node)
|
|
52
|
+
|
|
53
|
+
%i[and_node or_node].include?(node.type) &&
|
|
54
|
+
%w[and or].include?(node.operator)
|
|
37
55
|
end
|
|
38
56
|
|
|
39
57
|
# True for a call to a named method with arguments and no
|
|
40
|
-
# parentheses, eg `id.include? 'N'`, but not `id.size == 6
|
|
41
|
-
# such a call is no bare argument, so a rewrite wrapping it
|
|
42
|
-
# in another call must parenthesise it.
|
|
58
|
+
# parentheses, eg `id.include? 'N'`, but not `id.size == 6`.
|
|
43
59
|
def self.command?(node)
|
|
44
60
|
node.type == :call_node && node.arguments &&
|
|
45
61
|
node.opening_loc.nil? && node.name.match?(/\A[a-z_]/i)
|
|
46
62
|
end
|
|
47
63
|
|
|
64
|
+
# True for a modifier if, unless or rescue, eg x if y: one
|
|
65
|
+
# with no end keyword, a ternary being no modifier.
|
|
66
|
+
def self.modifier?(node)
|
|
67
|
+
case node.type
|
|
68
|
+
when :rescue_modifier_node then true
|
|
69
|
+
when :unless_node then node.end_keyword_loc.nil?
|
|
70
|
+
when :if_node
|
|
71
|
+
!node.if_keyword_loc.nil? && node.end_keyword_loc.nil?
|
|
72
|
+
else false
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
48
76
|
# Literals and lambdas, whose value is truthy by their form;
|
|
49
77
|
# a constant is not among them, as it can hold nil or false.
|
|
50
78
|
TRUTHY_TYPES = %i[
|
|
@@ -82,7 +110,7 @@ module PathCoverage
|
|
|
82
110
|
TRUTHY_GLOBALS.include?(node.name)
|
|
83
111
|
end
|
|
84
112
|
|
|
85
|
-
# Collects each
|
|
113
|
+
# Collects each decision and its Condition.
|
|
86
114
|
class ConditionSites < Prism::Visitor
|
|
87
115
|
include ManifestBlocks
|
|
88
116
|
include ManifestCase
|
|
@@ -92,7 +120,7 @@ module PathCoverage
|
|
|
92
120
|
include ManifestIgnoring
|
|
93
121
|
include ManifestWrites
|
|
94
122
|
|
|
95
|
-
# Starts with no
|
|
123
|
+
# Starts with no decisions found, to find none named by an
|
|
96
124
|
# operator in ignored, and to take a call to a method named
|
|
97
125
|
# in present as never nil, and to one in nonempty as true
|
|
98
126
|
# exactly when its receiver has items.
|
|
@@ -101,72 +129,72 @@ module PathCoverage
|
|
|
101
129
|
@ignored = ignored
|
|
102
130
|
@present = present
|
|
103
131
|
@nonempty = nonempty
|
|
104
|
-
@
|
|
132
|
+
@decisions = {}
|
|
105
133
|
@notes = nil
|
|
106
134
|
@inner = nil
|
|
107
135
|
super()
|
|
108
136
|
end
|
|
109
137
|
|
|
110
|
-
# The
|
|
111
|
-
attr_reader :
|
|
138
|
+
# The decisions found so far.
|
|
139
|
+
attr_reader :decisions
|
|
112
140
|
|
|
113
141
|
OPERATOR_TYPES = %i[and_node or_node].freeze
|
|
114
142
|
|
|
115
|
-
# Notes an && or and
|
|
143
|
+
# Notes an && or and decision.
|
|
116
144
|
def visit_and_node(node)
|
|
117
|
-
|
|
145
|
+
add_decision(node)
|
|
118
146
|
end
|
|
119
147
|
|
|
120
|
-
# Notes an || or or
|
|
148
|
+
# Notes an || or or decision.
|
|
121
149
|
def visit_or_node(node)
|
|
122
|
-
|
|
150
|
+
add_decision(node)
|
|
123
151
|
end
|
|
124
152
|
|
|
125
|
-
# Notes a ternary or an if
|
|
153
|
+
# Notes a ternary or an if decision, a modifier if included,
|
|
126
154
|
# its arms included.
|
|
127
155
|
def visit_if_node(node)
|
|
128
|
-
|
|
156
|
+
add_decision(node)
|
|
129
157
|
end
|
|
130
158
|
|
|
131
|
-
# Notes an unless
|
|
159
|
+
# Notes an unless decision, a modifier unless included, its
|
|
132
160
|
# arms included.
|
|
133
161
|
def visit_unless_node(node)
|
|
134
|
-
|
|
162
|
+
add_decision(node)
|
|
135
163
|
end
|
|
136
164
|
|
|
137
|
-
# Notes a case
|
|
138
|
-
# case is no
|
|
165
|
+
# Notes a case decision, when its paths can be seen; any other
|
|
166
|
+
# case is no decision, though a condition inside it may be.
|
|
139
167
|
def visit_case_node(node)
|
|
140
|
-
return super unless
|
|
168
|
+
return super unless case_decision?(node)
|
|
141
169
|
|
|
142
|
-
|
|
170
|
+
add_decision(node)
|
|
143
171
|
end
|
|
144
172
|
|
|
145
|
-
# Notes a &. chain as a
|
|
173
|
+
# Notes a &. chain as a decision, or a block method's call, when
|
|
146
174
|
# its block's every call can be seen; any other call is no
|
|
147
|
-
#
|
|
175
|
+
# decision, though a condition inside it may be.
|
|
148
176
|
def visit_call_node(node)
|
|
149
|
-
return
|
|
150
|
-
return super unless
|
|
177
|
+
return add_decision(node) if safe_chain?(node)
|
|
178
|
+
return super unless block_decision?(node)
|
|
151
179
|
|
|
152
|
-
|
|
180
|
+
add_decision(node)
|
|
153
181
|
end
|
|
154
182
|
|
|
155
|
-
# Notes node as a
|
|
183
|
+
# Notes node as a decision, named where it starts: an if or an
|
|
156
184
|
# unless at its keyword. It claims its place first, so a
|
|
157
|
-
#
|
|
158
|
-
# While a leaf is being visited, any
|
|
185
|
+
# decision found inside one of its leaves or arms follows it.
|
|
186
|
+
# While a leaf is being visited, any decision is collected as
|
|
159
187
|
# part of that leaf instead, as it is part of one expression.
|
|
160
|
-
def
|
|
188
|
+
def add_decision(node)
|
|
161
189
|
return visit_child_nodes(node) if ignored?(node)
|
|
162
190
|
return @inner << nested_condition_of(node) if @inner
|
|
163
191
|
|
|
164
|
-
|
|
165
|
-
@
|
|
192
|
+
decision = Decision.at(@path, decision_loc(node))
|
|
193
|
+
@decisions[decision] = nil
|
|
166
194
|
outer = @notes
|
|
167
195
|
@notes = { commands: [], fixed: {} }
|
|
168
196
|
condition = scoped { condition_of(node) }
|
|
169
|
-
@
|
|
197
|
+
@decisions[decision] = condition.with(**@notes)
|
|
170
198
|
@notes = outer
|
|
171
199
|
end
|
|
172
200
|
|
|
@@ -190,20 +218,26 @@ module PathCoverage
|
|
|
190
218
|
|
|
191
219
|
# node's operands: a chain of the same operator flattened
|
|
192
220
|
# into one list, the other operator nested as a Condition,
|
|
193
|
-
# as is a &. chain, its value its last leaf,
|
|
194
|
-
#
|
|
221
|
+
# as is a &. chain, its value its last leaf, an operand that
|
|
222
|
+
# leaves the method an Arm, eg return t in x or return t,
|
|
223
|
+
# anything else a leaf's byte range.
|
|
195
224
|
def operands_of(node)
|
|
196
|
-
[node.left, node.right].flat_map
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
225
|
+
[node.left, node.right].flat_map { operands_in(_1, node) }
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
# The operands written, one side of node, stands for, as
|
|
229
|
+
# operands_of gives them.
|
|
230
|
+
def operands_in(written, node)
|
|
231
|
+
operand = grouped(written)
|
|
232
|
+
return [leaf(operand)] if ignored?(operand)
|
|
233
|
+
return [Leaving.arm(operand)] if Leaving.word(operand)
|
|
234
|
+
return operands_of(operand) if operand.type == node.type
|
|
235
|
+
return [condition_of(operand)] if operator?(operand)
|
|
236
|
+
if safe_chain?(operand)
|
|
237
|
+
return [chain_condition(operand, value: true)]
|
|
206
238
|
end
|
|
239
|
+
|
|
240
|
+
[leaf(operand)]
|
|
207
241
|
end
|
|
208
242
|
|
|
209
243
|
# The byte range of node as written.
|
|
@@ -217,14 +251,14 @@ module PathCoverage
|
|
|
217
251
|
OPERATOR_TYPES.include?(node.type)
|
|
218
252
|
end
|
|
219
253
|
|
|
220
|
-
# The byte range of a leaf operand, noted among its
|
|
221
|
-
# commands and fixed leaves where it is one; or, when
|
|
254
|
+
# The byte range of a leaf operand, noted among its decision's
|
|
255
|
+
# commands and fixed leaves where it is one; or, when decisions
|
|
222
256
|
# that fold are found inside it, the leaf with them nested.
|
|
223
257
|
def leaf(operand)
|
|
224
|
-
inner =
|
|
258
|
+
inner = leaf_decisions(operand)
|
|
225
259
|
loc = operand.location
|
|
226
260
|
range = loc.start_offset...loc.end_offset
|
|
227
|
-
@notes[:commands] << range if Manifest.
|
|
261
|
+
@notes[:commands] << range if Manifest.bare_less?(operand)
|
|
228
262
|
outcome = Manifest.fixed_outcome(operand) ||
|
|
229
263
|
given_outcome(operand)
|
|
230
264
|
@notes[:fixed][range] = outcome if outcome
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
require_relative 'condition'
|
|
2
2
|
|
|
3
3
|
module PathCoverage
|
|
4
|
-
# The part of the manifest's visitor that builds the
|
|
4
|
+
# The part of the manifest's visitor that builds the decision of a
|
|
5
5
|
# call to a block method, one that calls its block an item at
|
|
6
6
|
# a time, until it decides, eg any?, or for every item, eg
|
|
7
7
|
# select: its leaves are its receiver, whose items the block
|
|
@@ -14,14 +14,14 @@ module PathCoverage
|
|
|
14
14
|
# make it ignore its block, not by &., which skips it for
|
|
15
15
|
# nil, and with a block of one expression that is a value,
|
|
16
16
|
# so can be wrapped: not break, next or return.
|
|
17
|
-
def
|
|
17
|
+
def block_decision?(node)
|
|
18
18
|
return false unless BLOCK_PATHS.key?(node.name.to_s)
|
|
19
19
|
|
|
20
20
|
plain_call?(node) && value?(block_expression(node.block))
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
# True for a call on a receiver, with no arguments. A call by
|
|
24
|
-
# &. never gets here, as it is a
|
|
24
|
+
# &. never gets here, as it is a decision of its own first.
|
|
25
25
|
def plain_call?(node)
|
|
26
26
|
!node.receiver.nil? && node.arguments.nil?
|
|
27
27
|
end
|
|
@@ -60,36 +60,15 @@ module PathCoverage
|
|
|
60
60
|
)
|
|
61
61
|
end
|
|
62
62
|
|
|
63
|
-
# The byte range of block's one expression, any
|
|
63
|
+
# The byte range of block's one expression, any decision in it
|
|
64
64
|
# visited as one of its own, as the block runs once per item;
|
|
65
65
|
# noted among the commands when it is no bare argument.
|
|
66
66
|
def block_body(block)
|
|
67
67
|
body = block_expression(block)
|
|
68
68
|
scoped { body.accept(self) }
|
|
69
69
|
range = extent_of(body)
|
|
70
|
-
@notes[:commands] << range if bare_less?(body)
|
|
70
|
+
@notes[:commands] << range if Manifest.bare_less?(body)
|
|
71
71
|
range
|
|
72
72
|
end
|
|
73
|
-
|
|
74
|
-
# True for an expression that is no bare argument: a command
|
|
75
|
-
# call, an and or an or, or a modifier, eg x unless y.
|
|
76
|
-
def bare_less?(node)
|
|
77
|
-
return true if Manifest.command?(node) || modifier?(node)
|
|
78
|
-
|
|
79
|
-
%i[and_node or_node].include?(node.type) &&
|
|
80
|
-
%w[and or].include?(node.operator)
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
# True for a modifier if, unless or rescue, eg x if y: one
|
|
84
|
-
# with no end keyword, a ternary being no modifier.
|
|
85
|
-
def modifier?(node)
|
|
86
|
-
case node.type
|
|
87
|
-
when :rescue_modifier_node then true
|
|
88
|
-
when :unless_node then node.end_keyword_loc.nil?
|
|
89
|
-
when :if_node
|
|
90
|
-
!node.if_keyword_loc.nil? && node.end_keyword_loc.nil?
|
|
91
|
-
else false
|
|
92
|
-
end
|
|
93
|
-
end
|
|
94
73
|
end
|
|
95
74
|
end
|