path_coverage 0.5.0 → 0.6.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 +7 -134
- data/lib/path_coverage/call_trace.rb +26 -5
- data/lib/path_coverage/calls.rb +24 -0
- data/lib/path_coverage/configuration_limits.rb +1 -1
- data/lib/path_coverage/console_summary.rb +13 -13
- data/lib/path_coverage/fold_rows.rb +2 -2
- data/lib/path_coverage/gaps.rb +1 -1
- data/lib/path_coverage/json_reader.rb +3 -3
- data/lib/path_coverage/json_reports.rb +28 -21
- data/lib/path_coverage/json_writer.rb +4 -4
- data/lib/path_coverage/kernel.rb +41 -20
- data/lib/path_coverage/leaving.rb +14 -6
- data/lib/path_coverage/loop_forks.rb +1 -1
- data/lib/path_coverage/loop_nodes.rb +41 -5
- data/lib/path_coverage/method_loops.rb +56 -12
- data/lib/path_coverage/method_statements.rb +25 -12
- data/lib/path_coverage/method_ways.rb +48 -20
- data/lib/path_coverage/{card_context.rb → paths_context.rb} +16 -16
- data/lib/path_coverage/paths_fitting.rb +31 -0
- data/lib/path_coverage/paths_forks.rb +72 -0
- data/lib/path_coverage/{card_gaps.rb → paths_gaps.rb} +38 -26
- data/lib/path_coverage/paths_leaving.rb +40 -0
- data/lib/path_coverage/paths_rescues.rb +146 -0
- data/lib/path_coverage/{card_scopes.rb → paths_scopes.rb} +6 -6
- data/lib/path_coverage/{card_tree.rb → paths_tree.rb} +19 -15
- data/lib/path_coverage/{card_way_lists.rb → paths_way_lists.rb} +3 -3
- data/lib/path_coverage/paths_ways.rb +193 -0
- data/lib/path_coverage/recorder.rb +14 -0
- data/lib/path_coverage/render.rb +6 -6
- data/lib/path_coverage/rescue_forks.rb +174 -0
- data/lib/path_coverage/rewriter.rb +83 -14
- data/lib/path_coverage/source_report.rb +4 -4
- data/lib/path_coverage/stable_leaves.rb +3 -3
- data/lib/path_coverage/statement_arms.rb +23 -9
- data/lib/path_coverage/totals.rb +5 -5
- data/lib/path_coverage/version.rb +1 -1
- data/lib/path_coverage/way_algebras.rb +2 -2
- data/lib/path_coverage/ways_report.rb +8 -8
- data/lib/path_coverage/ways_totals.rb +7 -7
- data/lib/path_coverage/within_methods.rb +61 -61
- metadata +12 -8
- data/lib/path_coverage/card_forks.rb +0 -44
- data/lib/path_coverage/card_ways.rb +0 -196
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
require_relative '
|
|
2
|
-
require_relative '
|
|
3
|
-
require_relative '
|
|
4
|
-
require_relative '
|
|
5
|
-
require_relative '
|
|
1
|
+
require_relative 'paths_context'
|
|
2
|
+
require_relative 'paths_scopes'
|
|
3
|
+
require_relative 'paths_tree'
|
|
4
|
+
require_relative 'paths_way_lists'
|
|
5
|
+
require_relative 'paths_ways'
|
|
6
6
|
require_relative 'loop_forks'
|
|
7
7
|
require_relative 'method_loops'
|
|
8
8
|
require_relative 'method_statements'
|
|
@@ -12,38 +12,29 @@ require_relative 'source_lines'
|
|
|
12
12
|
module PathCoverage
|
|
13
13
|
# What is measured within each method of a covered file.
|
|
14
14
|
module WithinMethods
|
|
15
|
-
# Where each statement of the covered file at path, parsed
|
|
16
|
-
# as Manifest.parse gives it, that is no decision is reached,
|
|
17
|
-
# as MethodStatements.reaches gives them.
|
|
18
|
-
def self.reaches(path, parsed, config)
|
|
19
|
-
source, decisions = parsed.values_at(:source, :decisions)
|
|
20
|
-
relative = config.relative(path)
|
|
21
|
-
MethodStatements.reaches(source, relative, decisions)
|
|
22
|
-
end
|
|
23
|
-
|
|
24
15
|
# Example: { "k.rb:0:0" => [1, 2], "k.rb:1:0" => [2, 4] }.
|
|
25
|
-
# Each def's
|
|
16
|
+
# Each def's flow total, [taken, size], in every file in
|
|
26
17
|
# files, { full path => {source:, decisions:} }, from paths, as
|
|
27
18
|
# Result#paths gives them, and reached, as Recorder#reached
|
|
28
|
-
# does; and each file's top level's, its decisions on no
|
|
19
|
+
# does; and each file's top level's, its decisions on no flow,
|
|
29
20
|
# named for line 0, which no def is on.
|
|
30
|
-
def self.
|
|
21
|
+
def self.flows(files, config, paths, reached)
|
|
31
22
|
files.map do |full_path, parsed|
|
|
32
23
|
path = config.relative(full_path)
|
|
33
24
|
source, decisions = parsed.values_at(:source, :decisions)
|
|
34
25
|
args = [source, path, paths]
|
|
35
26
|
top = MethodStatements.top_level(*args, decisions)
|
|
36
|
-
|
|
37
|
-
|
|
27
|
+
PathsContext.totals(*args, reached, decisions)
|
|
28
|
+
.merge("#{path}:0:0" => top)
|
|
38
29
|
end.reduce({}, :merge)
|
|
39
30
|
end
|
|
40
31
|
|
|
41
32
|
# Example: { "k.rb:1:0" => { "name" => "ship", "first" => 1,
|
|
42
33
|
# "last" => 3, "rows" => [{ "at" => "2:04", ... }] } }.
|
|
43
|
-
# Each
|
|
34
|
+
# Each flow in every file in files, a def's and a top flow's,
|
|
44
35
|
# as its page draws it, for the JSON report, so a reader
|
|
45
|
-
# finds a
|
|
46
|
-
# counted [reached, ways], reached as
|
|
36
|
+
# finds a flow's gap without the page; result a Result;
|
|
37
|
+
# counted [reached, ways], reached as flows takes it, ways as
|
|
47
38
|
# a CallTrace counts them, nil when not known.
|
|
48
39
|
def self.listed(files, config, result, counted)
|
|
49
40
|
files.map do |full_path, parsed|
|
|
@@ -57,10 +48,10 @@ module PathCoverage
|
|
|
57
48
|
|
|
58
49
|
# Example: { "k.rb" => { "1:0" => { "2:2" => { "parent" =>
|
|
59
50
|
# nil, "in" => nil, "on" => nil } } } }.
|
|
60
|
-
# How the rows of each
|
|
61
|
-
# the file's path, then its
|
|
62
|
-
# and each top
|
|
63
|
-
# as
|
|
51
|
+
# How the rows of each flow nest, in every file in files: by
|
|
52
|
+
# the file's path, then its flow's line:column, a def's flow
|
|
53
|
+
# and each top flow, as listed names them, then each row's,
|
|
54
|
+
# as PathsTree places them; result a Result.
|
|
64
55
|
def self.tree(files, config, result)
|
|
65
56
|
files.to_h do |full_path, parsed|
|
|
66
57
|
path = config.relative(full_path)
|
|
@@ -68,42 +59,42 @@ module PathCoverage
|
|
|
68
59
|
end
|
|
69
60
|
end
|
|
70
61
|
|
|
71
|
-
# How the rows of each
|
|
62
|
+
# How the rows of each flow of the file at path nest, parsed
|
|
72
63
|
# as Manifest.parse gives it, as tree gives them.
|
|
73
64
|
def self.tree_in(path, parsed, result)
|
|
74
65
|
source, decisions = parsed.values_at(:source, :decisions)
|
|
75
|
-
|
|
66
|
+
flows = PathsTree.of(source, path, decisions).to_a +
|
|
76
67
|
tree_tops(path, parsed, result)
|
|
77
|
-
|
|
68
|
+
flows.to_h do |decision, rows|
|
|
78
69
|
[decision.split(':').last(2) * ':', rows]
|
|
79
70
|
end
|
|
80
71
|
end
|
|
81
72
|
|
|
82
|
-
# [[
|
|
73
|
+
# [[flow decision, its rows' places]] for each top flow of the
|
|
83
74
|
# file at path, as listed_tops names them.
|
|
84
75
|
def self.tree_tops(path, parsed, result)
|
|
85
76
|
source, decisions = parsed.values_at(:source, :decisions)
|
|
86
77
|
lines = SourceLines.of(source, decisions, result.operands)
|
|
87
78
|
spans(path, parsed, lines).map do |span, rows|
|
|
88
|
-
nested =
|
|
79
|
+
nested = PathsTree.nested(source, decisions, rows)
|
|
89
80
|
["#{path}:#{span.begin}:0", nested]
|
|
90
81
|
end
|
|
91
82
|
end
|
|
92
83
|
|
|
93
|
-
# The top
|
|
84
|
+
# The top flows of the file at path, as its page draws them:
|
|
94
85
|
# each under the decision of its first line's start, named for
|
|
95
86
|
# its scope, as listed gives them; operands and decision_paths
|
|
96
87
|
# as Result gives them.
|
|
97
88
|
def self.listed_tops(path, parsed, (operands, decision_paths))
|
|
98
89
|
source, decisions = parsed.values_at(:source, :decisions)
|
|
99
90
|
lines = SourceLines.of(source, decisions, operands)
|
|
100
|
-
scopes =
|
|
91
|
+
scopes = PathsScopes.of(source)
|
|
101
92
|
spans(path, parsed, lines).to_h do |span, rows|
|
|
102
93
|
decision = "#{path}:#{span.begin}:0"
|
|
103
94
|
taken = rows.zip(MethodStatements.taken(rows, decision_paths))
|
|
104
|
-
ways =
|
|
95
|
+
ways = PathsWays.flow(source, decisions, rows)
|
|
105
96
|
named = [scopes[span.begin], span.end, ways, nil]
|
|
106
|
-
[decision,
|
|
97
|
+
[decision, flow(decision, named, taken)]
|
|
107
98
|
end
|
|
108
99
|
end
|
|
109
100
|
|
|
@@ -116,10 +107,10 @@ module PathCoverage
|
|
|
116
107
|
rows.group_by { lines[_1.line - 1][:span] }
|
|
117
108
|
end
|
|
118
109
|
|
|
119
|
-
# The
|
|
110
|
+
# The flows of the file at path, parsed as Manifest.parse
|
|
120
111
|
# gives it, as listed gives them, each listing its ways too
|
|
121
112
|
# when counted's ways are known, counted being [reached, ways,
|
|
122
|
-
# limit, loops], limit the most ways a
|
|
113
|
+
# limit, loops], limit the most ways a flow lists, loops
|
|
123
114
|
# whether each loop is a fork.
|
|
124
115
|
def self.listed_in(path, parsed, paths, (reached, *counted))
|
|
125
116
|
source, decisions = parsed.values_at(:source, :decisions)
|
|
@@ -129,36 +120,36 @@ module PathCoverage
|
|
|
129
120
|
MethodStatements.of(source, path, decisions).to_h do |key, rows|
|
|
130
121
|
count = lister.count(key, rows)
|
|
131
122
|
named = [*titles[key], count, reached&.fetch(key, 0)]
|
|
132
|
-
shown =
|
|
123
|
+
shown = method_flow(key, rows, named, context)
|
|
133
124
|
[key, shown.merge(lister.lists(key, rows, count))]
|
|
134
125
|
end
|
|
135
126
|
end
|
|
136
127
|
|
|
137
|
-
# The
|
|
128
|
+
# The flow of the def at key, its rows, named as flow takes
|
|
138
129
|
# named; context is [source, decisions, paths, reached], as
|
|
139
130
|
# listed_in has them.
|
|
140
|
-
def self.
|
|
131
|
+
def self.method_flow(key, rows, named, context)
|
|
141
132
|
source, decisions, paths, reached = context
|
|
142
|
-
shown =
|
|
143
|
-
|
|
144
|
-
|
|
133
|
+
shown = PathsContext.rows(source, decisions, rows, paths,
|
|
134
|
+
reached)
|
|
135
|
+
flow(key, named, shown)
|
|
145
136
|
end
|
|
146
137
|
|
|
147
138
|
# { def decision => [its name, its last line] } for each def in
|
|
148
|
-
# source, at path, as its
|
|
139
|
+
# source, at path, as its flow is titled.
|
|
149
140
|
def self.titles(source, path, decisions)
|
|
150
141
|
names = MethodStatements.names(source, path, decisions)
|
|
151
142
|
lasts = MethodStatements.lasts(source, path, decisions)
|
|
152
143
|
names.to_h { |key, name| [key, [name, lasts[key]]] }
|
|
153
144
|
end
|
|
154
145
|
|
|
155
|
-
# The way lists of each
|
|
146
|
+
# The way lists of each flow of a file, as PathsWayLists gives
|
|
156
147
|
# them.
|
|
157
148
|
class WayLister
|
|
158
149
|
# The lister of the file at path, source, its decisions as
|
|
159
150
|
# Manifest.parse gives them; counted [ways, limit]: ways as a
|
|
160
151
|
# CallTrace counts them, nil when not known, and limit the
|
|
161
|
-
# most ways a
|
|
152
|
+
# most ways a flow lists.
|
|
162
153
|
def initialize(source, path, decisions, (ways, limit, loops))
|
|
163
154
|
@source = source
|
|
164
155
|
@decisions = decisions
|
|
@@ -166,35 +157,44 @@ module PathCoverage
|
|
|
166
157
|
@limit = limit
|
|
167
158
|
@loops = MethodLoops.of(source, path, decisions)
|
|
168
159
|
@forks = LoopForks.of(source, path, decisions)
|
|
160
|
+
@rescues = RescueForks.of(source, path)
|
|
169
161
|
@forking = loops
|
|
170
162
|
end
|
|
171
163
|
|
|
172
|
-
# How many ways go through the
|
|
173
|
-
# rows
|
|
164
|
+
# How many ways go through the flow of the def at key, its
|
|
165
|
+
# rows. Each loop is a scope, and a fork when loops are. Its
|
|
166
|
+
# rescue is a fork.
|
|
174
167
|
def count(key, rows)
|
|
175
|
-
|
|
176
|
-
|
|
168
|
+
PathsWays.flow(@source, @decisions, rows,
|
|
169
|
+
WayAlgebras::Counted, [true, *scopes(key)])
|
|
177
170
|
end
|
|
178
171
|
|
|
179
|
-
# The way lists of the
|
|
172
|
+
# The way lists of the flow of the def at key, its rows, with
|
|
180
173
|
# count ways through it: none when the ways are not known;
|
|
181
174
|
# those its calls took and its loops' passes; and, when count
|
|
182
175
|
# is no more than the limit, its covered, gaps and impossible.
|
|
183
176
|
def lists(key, rows, count)
|
|
184
177
|
return {} if @ways.nil?
|
|
185
178
|
|
|
186
|
-
walked =
|
|
179
|
+
walked = PathsWayLists.walked(key, @ways, loops(key))
|
|
187
180
|
return walked if count > @limit
|
|
188
181
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
checked
|
|
193
|
-
walked.merge(CardWayLists.checked(key, @ways, checked))
|
|
182
|
+
split = MethodWays.split_flow(@source, @decisions, rows,
|
|
183
|
+
scopes(key))
|
|
184
|
+
checked = [split, forked(key).keys]
|
|
185
|
+
walked.merge(PathsWayLists.checked(key, @ways, checked))
|
|
194
186
|
end
|
|
195
187
|
|
|
196
188
|
private
|
|
197
189
|
|
|
190
|
+
# [forks, rescues, forking] of the def at key, as
|
|
191
|
+
# MethodWays.split_flow takes them. forks is each of its
|
|
192
|
+
# loops, as LoopForks gives them. rescues is its rescue
|
|
193
|
+
# forks. forking says whether its loops fork.
|
|
194
|
+
def scopes(key)
|
|
195
|
+
[@forks.fetch(key), @rescues.fetch(key), @forking]
|
|
196
|
+
end
|
|
197
|
+
|
|
198
198
|
# The names of the loops of the def at key: those holding a
|
|
199
199
|
# row, and those that fork.
|
|
200
200
|
def loops(key)
|
|
@@ -208,19 +208,19 @@ module PathCoverage
|
|
|
208
208
|
end
|
|
209
209
|
end
|
|
210
210
|
|
|
211
|
-
# The
|
|
211
|
+
# The flow of the def at decision, named name, ending on line
|
|
212
212
|
# last, with ways through it, as ways folds them, and calls,
|
|
213
|
-
# how many times it was called, nil for a top
|
|
213
|
+
# how many times it was called, nil for a top flow, which is no
|
|
214
214
|
# method's, or when not known: its first line, from decision, and
|
|
215
215
|
# a row for each [statement, taken] of rows.
|
|
216
|
-
def self.
|
|
216
|
+
def self.flow(decision, (name, last, ways, calls), rows)
|
|
217
217
|
first = Integer(decision.split(':')[-2])
|
|
218
218
|
listed = rows.map { |statement, count| row(statement, count) }
|
|
219
219
|
{ 'name' => name, 'first' => first, 'last' => last,
|
|
220
220
|
'ways' => ways, 'calls' => calls, 'rows' => listed }
|
|
221
221
|
end
|
|
222
222
|
|
|
223
|
-
# A
|
|
223
|
+
# A flow's row: statement's line:column, as its page labels
|
|
224
224
|
# it, its top, its paths, count, how many were taken, and
|
|
225
225
|
# its decision, nil for a statement that is no decision.
|
|
226
226
|
def self.row(statement, count)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: path_coverage
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.6.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jon Jagger
|
|
@@ -39,13 +39,6 @@ files:
|
|
|
39
39
|
- lib/path_coverage/auto.rb
|
|
40
40
|
- lib/path_coverage/call_trace.rb
|
|
41
41
|
- lib/path_coverage/calls.rb
|
|
42
|
-
- lib/path_coverage/card_context.rb
|
|
43
|
-
- lib/path_coverage/card_forks.rb
|
|
44
|
-
- lib/path_coverage/card_gaps.rb
|
|
45
|
-
- lib/path_coverage/card_scopes.rb
|
|
46
|
-
- lib/path_coverage/card_tree.rb
|
|
47
|
-
- lib/path_coverage/card_way_lists.rb
|
|
48
|
-
- lib/path_coverage/card_ways.rb
|
|
49
42
|
- lib/path_coverage/code_tokens.rb
|
|
50
43
|
- lib/path_coverage/collector.rb
|
|
51
44
|
- lib/path_coverage/condition.rb
|
|
@@ -91,7 +84,17 @@ files:
|
|
|
91
84
|
- lib/path_coverage/method_loops.rb
|
|
92
85
|
- lib/path_coverage/method_statements.rb
|
|
93
86
|
- lib/path_coverage/method_ways.rb
|
|
87
|
+
- lib/path_coverage/paths_context.rb
|
|
88
|
+
- lib/path_coverage/paths_fitting.rb
|
|
89
|
+
- lib/path_coverage/paths_forks.rb
|
|
90
|
+
- lib/path_coverage/paths_gaps.rb
|
|
91
|
+
- lib/path_coverage/paths_leaving.rb
|
|
94
92
|
- lib/path_coverage/paths_report.rb
|
|
93
|
+
- lib/path_coverage/paths_rescues.rb
|
|
94
|
+
- lib/path_coverage/paths_scopes.rb
|
|
95
|
+
- lib/path_coverage/paths_tree.rb
|
|
96
|
+
- lib/path_coverage/paths_way_lists.rb
|
|
97
|
+
- lib/path_coverage/paths_ways.rb
|
|
95
98
|
- lib/path_coverage/post.rb
|
|
96
99
|
- lib/path_coverage/recorder.rb
|
|
97
100
|
- lib/path_coverage/recorder_blocks.rb
|
|
@@ -99,6 +102,7 @@ files:
|
|
|
99
102
|
- lib/path_coverage/recorder_chains.rb
|
|
100
103
|
- lib/path_coverage/recorder_writes.rb
|
|
101
104
|
- lib/path_coverage/render.rb
|
|
105
|
+
- lib/path_coverage/rescue_forks.rb
|
|
102
106
|
- lib/path_coverage/result.rb
|
|
103
107
|
- lib/path_coverage/rewriter.rb
|
|
104
108
|
- lib/path_coverage/rewriter_nested.rb
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
module PathCoverage
|
|
2
|
-
# How the loops of a card fork when loops are decisions, each of
|
|
3
|
-
# forks being { loop's name => [its bytes, its body's] }, as
|
|
4
|
-
# LoopForks gives them: a loop is placed as a row that is no
|
|
5
|
-
# decision is, by its bytes; its body is a region of it run on
|
|
6
|
-
# its path 1, at least one item, so holds its rows; its path 0
|
|
7
|
-
# is no item. Its module functions place it, for CardTree; its
|
|
8
|
-
# instance method folds it, for CardWays, which includes it.
|
|
9
|
-
module CardForks
|
|
10
|
-
# The region of each loop of forks: its body, run on its path
|
|
11
|
-
# 1.
|
|
12
|
-
def self.regions(forks)
|
|
13
|
-
forks.map do |name, (_, body)|
|
|
14
|
-
CardTree::Region.new(owner: CardTree.key_at(name),
|
|
15
|
-
range: body, arm: 'body', on: [1],
|
|
16
|
-
leaf: nil, leaves: nil)
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
# places, { key => its place }, a card's rows', with the place
|
|
21
|
-
# of each loop of forks too, among regions, all in the order
|
|
22
|
-
# they start; places alone with no loop.
|
|
23
|
-
def self.placed(places, forks, regions)
|
|
24
|
-
return places if forks.empty?
|
|
25
|
-
|
|
26
|
-
loops = forks.to_h do |name, (span, _)|
|
|
27
|
-
key = CardTree.key_at(name)
|
|
28
|
-
[key, CardTree.placed(key, span, regions, nil)]
|
|
29
|
-
end
|
|
30
|
-
merged = places.merge(loops)
|
|
31
|
-
merged.sort_by { _1[0].split(':').map(&:to_i) }.to_h
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
# The pair of the loop keyed key, placed but no row: no pass,
|
|
35
|
-
# going on, or at least one, then the rows of its body one
|
|
36
|
-
# after another, walked once.
|
|
37
|
-
def fork_pair(key)
|
|
38
|
-
pairs = running(key, 1).map { pair_of(_1) }
|
|
39
|
-
own = @ways.unit([key, 1])
|
|
40
|
-
some = sequence(pairs).map { @ways.times(own, _1) }
|
|
41
|
-
added([[@ways.unit([key, 0]), @ways.zero], some])
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
end
|
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
require_relative 'card_tree'
|
|
2
|
-
require_relative 'manifest'
|
|
3
|
-
require_relative 'method_statements'
|
|
4
|
-
require_relative 'decision_paths'
|
|
5
|
-
require_relative 'stable_leaves'
|
|
6
|
-
require_relative 'way_algebras'
|
|
7
|
-
|
|
8
|
-
module PathCoverage
|
|
9
|
-
# The ways one call can go through each card, folded over its
|
|
10
|
-
# tree, as CardTree places its rows. Each row gives a pair,
|
|
11
|
-
# [ways that go on, ways that leave the method]: a return or a
|
|
12
|
-
# raise leaves, and so does each path of a decision taking an
|
|
13
|
-
# arm that returns or raises, eg return 0 if a. Rows one after
|
|
14
|
-
# another multiply, but only the ways that go on reach the next
|
|
15
|
-
# row; a decision adds up its paths, each with the rows it runs
|
|
16
|
-
# on that path one after another; and a row that is the whole of
|
|
17
|
-
# a leaf counts only those of its paths that give the leaf the
|
|
18
|
-
# outcome the path has, so no impossible way is counted where
|
|
19
|
-
# gives knows it. A block's body counts once, unless the block
|
|
20
|
-
# forks, as CardForks folds it.
|
|
21
|
-
# Each way also notes the outcome it gives each leaf that
|
|
22
|
-
# StableLeaves finds, by its local's name, and two ways joined
|
|
23
|
-
# one after the other that give one name both outcomes are no
|
|
24
|
-
# way, as no call can take them.
|
|
25
|
-
# The fold is in one of WayAlgebras: Counted counts the ways,
|
|
26
|
-
# by default; Labelled gives the paths taken on any of them, and
|
|
27
|
-
# Listed lists them.
|
|
28
|
-
class CardWays
|
|
29
|
-
include CardForks
|
|
30
|
-
|
|
31
|
-
# Example: { "k.rb:1:0" => 3 } for def f(a, b), return 0 if a,
|
|
32
|
-
# then a ?: of b: 1 way leaves, 2 go on.
|
|
33
|
-
# { def decision => its ways, in algebra } for each def in
|
|
34
|
-
# source, its decisions those of no settings.
|
|
35
|
-
def self.of(source, path, algebra = WayAlgebras::Counted)
|
|
36
|
-
decisions = Manifest.decisions(source, path)
|
|
37
|
-
cards = MethodStatements.of(source, path, decisions)
|
|
38
|
-
cards.transform_values do |rows|
|
|
39
|
-
card(source, decisions, rows, algebra)
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
# The ways through a card of rows in source, in algebra,
|
|
44
|
-
# decisions being { decision => Condition }, as
|
|
45
|
-
# Manifest.decisions gives them; folding is [stable, forks]:
|
|
46
|
-
# with stable false, those no call can take too, as no leaf is
|
|
47
|
-
# then assigned; each loop of forks, as LoopForks gives them,
|
|
48
|
-
# a fork, labelled [its key, 0] for 0 items, [its key, 1] for
|
|
49
|
-
# 1+ items, then its body's rows, walked once.
|
|
50
|
-
def self.card(source, decisions, rows,
|
|
51
|
-
algebra = WayAlgebras::Counted,
|
|
52
|
-
folding = [true, {}])
|
|
53
|
-
stable, forks = folding
|
|
54
|
-
places = CardTree.nested(source, decisions, rows, forks)
|
|
55
|
-
names = stable ? StableLeaves.of(source, decisions, rows) : {}
|
|
56
|
-
new(source, rows, places, decisions, [algebra, names]).total
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
# The fold of rows, a card's in source, placed as places has
|
|
60
|
-
# them, each decision row's Condition among decisions; folding
|
|
61
|
-
# is [algebra, names]: its ways in algebra, each step of it a
|
|
62
|
-
# tally of them, as WayAlgebras::Tallied has them, assigning
|
|
63
|
-
# the leaves names has, as StableLeaves finds them.
|
|
64
|
-
def initialize(source, rows, places, decisions, (algebra, names))
|
|
65
|
-
@source = source
|
|
66
|
-
@rows = rows.to_h { |row| [CardTree.key_of(row), row] }
|
|
67
|
-
@places = places
|
|
68
|
-
@decisions = decisions
|
|
69
|
-
@ways = WayAlgebras::Tallied.new(algebra)
|
|
70
|
-
@children = places.keys.group_by { places[_1]['parent'] }
|
|
71
|
-
@names = names
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
# The ways through the card, in its algebra: its top rows'
|
|
75
|
-
# pairs, one after another, those that go on and those that
|
|
76
|
-
# leave, whatever their assignments.
|
|
77
|
-
def total
|
|
78
|
-
tops = @children.fetch(nil, []).map { |key| pair_of(key) }
|
|
79
|
-
@ways.total(@ways.plus(*sequence(tops)))
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
# The pair of rows one after another, from their pairs: the
|
|
83
|
-
# ways that go on through them all, and those that leave at
|
|
84
|
-
# one of them, having gone on through every one before it.
|
|
85
|
-
def sequence(pairs)
|
|
86
|
-
start = [@ways.one, @ways.zero]
|
|
87
|
-
pairs.reduce(start) do |(on, left), (more, out)|
|
|
88
|
-
through = @ways.times(on, out)
|
|
89
|
-
[@ways.times(on, more), @ways.plus(left, through)]
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
# The pair of the row keyed key: a decision's adds up its paths'
|
|
94
|
-
# pairs; any other row is a return or a raise, as no other is
|
|
95
|
-
# a row, so leaves, at its one path.
|
|
96
|
-
def pair_of(key)
|
|
97
|
-
return fork_pair(key) unless @rows.key?(key)
|
|
98
|
-
|
|
99
|
-
row = @rows[key]
|
|
100
|
-
leaves = [@ways.zero, @ways.unit([key, 0])]
|
|
101
|
-
return leaves if row.decision.nil?
|
|
102
|
-
|
|
103
|
-
pairs = possible(row.decision).each_index.map do |path|
|
|
104
|
-
pair_on(key, path)
|
|
105
|
-
end
|
|
106
|
-
added(pairs)
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
# The pair of the decision row keyed key on its path at index
|
|
110
|
-
# path: that path taken, then the rows it runs on that path,
|
|
111
|
-
# one after another, each given that path's outcomes; every
|
|
112
|
-
# way of it leaving when the path takes an arm that leaves.
|
|
113
|
-
def pair_on(key, path)
|
|
114
|
-
outcomes = possible(@rows[key].decision)[path]
|
|
115
|
-
pairs = running(key, path).map do |child|
|
|
116
|
-
child_pair(child, outcomes)
|
|
117
|
-
end
|
|
118
|
-
own = own(key, path, outcomes)
|
|
119
|
-
on, left = sequence(pairs).map { @ways.times(own, _1) }
|
|
120
|
-
return [on, left] unless leaving(key).include?(path)
|
|
121
|
-
|
|
122
|
-
[@ways.zero, @ways.plus(on, left)]
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
# The tally of the one way that takes the decision row keyed
|
|
126
|
-
# key's path at index path, with outcomes, giving each name
|
|
127
|
-
# the outcome of its leaves on it; no way when it gives a name
|
|
128
|
-
# both outcomes, as eg a && b || a && c does with a true then
|
|
129
|
-
# false.
|
|
130
|
-
def own(key, path, outcomes)
|
|
131
|
-
named = named(key, outcomes)
|
|
132
|
-
assignment = named.to_h
|
|
133
|
-
return @ways.zero if assignment.size < named.size
|
|
134
|
-
|
|
135
|
-
@ways.unit([key, path], assignment)
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
# The distinct [name, outcome] of each leaf, of the decision of
|
|
139
|
-
# the row keyed key, that outcomes reaches and that has a name.
|
|
140
|
-
def named(key, outcomes)
|
|
141
|
-
leaves = @decisions.fetch(@rows[key].decision).leaves
|
|
142
|
-
leaves.zip(outcomes).filter_map do |range, outcome|
|
|
143
|
-
next if outcome.nil? || !@names.key?(range)
|
|
144
|
-
|
|
145
|
-
[@names[range], outcome]
|
|
146
|
-
end.uniq
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
# The keys of the rows the decision row keyed key runs on its path
|
|
150
|
-
# at index path, in the order they are written.
|
|
151
|
-
def running(key, path)
|
|
152
|
-
runs = @children.fetch(key, []).group_by do |child|
|
|
153
|
-
@places[child]['on'].include?(path)
|
|
154
|
-
end
|
|
155
|
-
runs.fetch(true, [])
|
|
156
|
-
end
|
|
157
|
-
|
|
158
|
-
# The pair of the row keyed child, its parent's path having
|
|
159
|
-
# outcomes: all of its own, for a row that gives nothing; else
|
|
160
|
-
# those of only its paths that give its leaf the outcome that
|
|
161
|
-
# path has, or that give what is not known.
|
|
162
|
-
def child_pair(child, outcomes)
|
|
163
|
-
place = @places[child]
|
|
164
|
-
gives = place['gives']
|
|
165
|
-
return pair_of(child) if gives.nil?
|
|
166
|
-
|
|
167
|
-
fits = [nil, outcomes[place['leaf']]]
|
|
168
|
-
none = [@ways.zero, @ways.zero]
|
|
169
|
-
pairs = gives.each_index.map do |path|
|
|
170
|
-
fits.include?(gives[path]) ? pair_on(child, path) : none
|
|
171
|
-
end
|
|
172
|
-
added(pairs)
|
|
173
|
-
end
|
|
174
|
-
|
|
175
|
-
# The indices of the paths of the decision row keyed key that take
|
|
176
|
-
# an arm whose code returns or raises.
|
|
177
|
-
def leaving(key)
|
|
178
|
-
decision = @decisions.fetch(@rows[key].decision)
|
|
179
|
-
regions = CardTree.regions_of(@source, key, decision)
|
|
180
|
-
regions.select(&:leaves).flat_map(&:on)
|
|
181
|
-
end
|
|
182
|
-
|
|
183
|
-
# pairs added up, each part on its own; no ways for no pairs.
|
|
184
|
-
def added(pairs)
|
|
185
|
-
none = [@ways.zero, @ways.zero]
|
|
186
|
-
pairs.reduce(none) do |(on, left), (more, out)|
|
|
187
|
-
[@ways.plus(on, more), @ways.plus(left, out)]
|
|
188
|
-
end
|
|
189
|
-
end
|
|
190
|
-
|
|
191
|
-
# The possible paths of the decision named decision.
|
|
192
|
-
def possible(decision)
|
|
193
|
-
DecisionPaths.possible(@decisions.fetch(decision))
|
|
194
|
-
end
|
|
195
|
-
end
|
|
196
|
-
end
|