path_coverage 0.4.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.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +260 -300
  3. data/exe/path_coverage_report +4 -0
  4. data/lib/path_coverage/call_trace.rb +109 -0
  5. data/lib/path_coverage/calls.rb +73 -43
  6. data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +35 -33
  7. data/lib/path_coverage/condition.rb +14 -14
  8. data/lib/path_coverage/configuration.rb +7 -4
  9. data/lib/path_coverage/configuration_limits.rb +52 -0
  10. data/lib/path_coverage/configuration_paths.rb +7 -7
  11. data/lib/path_coverage/console_summary.rb +28 -26
  12. data/lib/path_coverage/{site.rb → decision.rb} +2 -2
  13. data/lib/path_coverage/decision_locations.rb +76 -0
  14. data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +13 -11
  15. data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
  16. data/lib/path_coverage/fold_branches.rb +18 -0
  17. data/lib/path_coverage/fold_cells.rb +42 -0
  18. data/lib/path_coverage/{html_tree.rb → fold_rows.rb} +43 -72
  19. data/lib/path_coverage/gaps.rb +33 -15
  20. data/lib/path_coverage/json_reader.rb +54 -19
  21. data/lib/path_coverage/json_reports.rb +109 -0
  22. data/lib/path_coverage/json_writer.rb +29 -14
  23. data/lib/path_coverage/kernel.rb +77 -58
  24. data/lib/path_coverage/kinds/base.rb +16 -16
  25. data/lib/path_coverage/kinds/block.rb +16 -15
  26. data/lib/path_coverage/kinds/case.rb +14 -12
  27. data/lib/path_coverage/kinds/chain.rb +40 -11
  28. data/lib/path_coverage/kinds/choice.rb +4 -3
  29. data/lib/path_coverage/kinds/contains.rb +40 -32
  30. data/lib/path_coverage/kinds/safe.rb +38 -26
  31. data/lib/path_coverage/kinds/write.rb +6 -6
  32. data/lib/path_coverage/kinds.rb +2 -2
  33. data/lib/path_coverage/leaving.rb +52 -0
  34. data/lib/path_coverage/loop_forks.rb +57 -0
  35. data/lib/path_coverage/loop_nodes.rb +91 -0
  36. data/lib/path_coverage/manifest.rb +92 -58
  37. data/lib/path_coverage/manifest_blocks.rb +5 -26
  38. data/lib/path_coverage/manifest_case.rb +2 -2
  39. data/lib/path_coverage/manifest_chains.rb +8 -8
  40. data/lib/path_coverage/manifest_choices.rb +23 -20
  41. data/lib/path_coverage/manifest_disabled.rb +97 -0
  42. data/lib/path_coverage/manifest_folding.rb +43 -40
  43. data/lib/path_coverage/manifest_ignoring.rb +6 -6
  44. data/lib/path_coverage/manifest_writes.rb +11 -10
  45. data/lib/path_coverage/measured_by.rb +20 -0
  46. data/lib/path_coverage/merge.rb +7 -7
  47. data/lib/path_coverage/method_loops.rb +172 -0
  48. data/lib/path_coverage/method_statements.rb +150 -134
  49. data/lib/path_coverage/method_ways.rb +170 -0
  50. data/lib/path_coverage/paths_context.rb +104 -0
  51. data/lib/path_coverage/paths_fitting.rb +31 -0
  52. data/lib/path_coverage/paths_forks.rb +72 -0
  53. data/lib/path_coverage/paths_gaps.rb +137 -0
  54. data/lib/path_coverage/paths_leaving.rb +40 -0
  55. data/lib/path_coverage/paths_report.rb +16 -16
  56. data/lib/path_coverage/paths_rescues.rb +146 -0
  57. data/lib/path_coverage/paths_scopes.rb +37 -0
  58. data/lib/path_coverage/paths_tree.rb +203 -0
  59. data/lib/path_coverage/paths_way_lists.rb +72 -0
  60. data/lib/path_coverage/paths_ways.rb +193 -0
  61. data/lib/path_coverage/recorder.rb +71 -36
  62. data/lib/path_coverage/recorder_blocks.rb +17 -17
  63. data/lib/path_coverage/recorder_cases.rb +12 -11
  64. data/lib/path_coverage/recorder_chains.rb +12 -12
  65. data/lib/path_coverage/recorder_writes.rb +9 -9
  66. data/lib/path_coverage/render.rb +58 -51
  67. data/lib/path_coverage/rescue_forks.rb +174 -0
  68. data/lib/path_coverage/result.rb +32 -31
  69. data/lib/path_coverage/rewriter.rb +116 -35
  70. data/lib/path_coverage/rewriter_nested.rb +13 -13
  71. data/lib/path_coverage/runs_report.rb +84 -0
  72. data/lib/path_coverage/source_lines.rb +16 -16
  73. data/lib/path_coverage/source_report.rb +100 -0
  74. data/lib/path_coverage/stable_leaves.rb +162 -0
  75. data/lib/path_coverage/statement_arms.rb +53 -38
  76. data/lib/path_coverage/summary.rb +4 -62
  77. data/lib/path_coverage/totals.rb +12 -10
  78. data/lib/path_coverage/version.rb +1 -1
  79. data/lib/path_coverage/walk.rb +106 -0
  80. data/lib/path_coverage/way_algebras.rb +127 -0
  81. data/lib/path_coverage/ways_report.rb +91 -0
  82. data/lib/path_coverage/ways_totals.rb +46 -0
  83. data/lib/path_coverage/within_methods.rb +152 -97
  84. metadata +39 -30
  85. data/lib/path_coverage/card.rb +0 -14
  86. data/lib/path_coverage/card_tree.rb +0 -186
  87. data/lib/path_coverage/card_ways.rb +0 -140
  88. data/lib/path_coverage/html/file.html +0 -33
  89. data/lib/path_coverage/html/index.html +0 -29
  90. data/lib/path_coverage/html/settings.js +0 -50
  91. data/lib/path_coverage/html/side.js +0 -599
  92. data/lib/path_coverage/html/sort.js +0 -34
  93. data/lib/path_coverage/html/style.css +0 -484
  94. data/lib/path_coverage/html/tabs.js +0 -15
  95. data/lib/path_coverage/html/theme.js +0 -27
  96. data/lib/path_coverage/html_branch.rb +0 -30
  97. data/lib/path_coverage/html_cards.rb +0 -88
  98. data/lib/path_coverage/html_cells.rb +0 -71
  99. data/lib/path_coverage/html_file.rb +0 -123
  100. data/lib/path_coverage/html_index.rb +0 -128
  101. data/lib/path_coverage/html_index_head.rb +0 -29
  102. data/lib/path_coverage/html_labels.rb +0 -16
  103. data/lib/path_coverage/html_methods.rb +0 -66
  104. data/lib/path_coverage/html_nav.rb +0 -67
  105. data/lib/path_coverage/html_report.rb +0 -131
  106. data/lib/path_coverage/html_statements.rb +0 -186
  107. data/lib/path_coverage/html_tabs.rb +0 -26
  108. data/lib/path_coverage/html_template.rb +0 -26
@@ -0,0 +1,203 @@
1
+ require_relative 'paths_forks'
2
+ require_relative 'paths_rescues'
3
+ require_relative 'manifest'
4
+ require_relative 'method_statements'
5
+ require_relative 'decision_paths'
6
+ require_relative 'decision_tree'
7
+
8
+ module PathCoverage
9
+ # How the rows of each flow nest: for each row, the decision it
10
+ # runs inside, the arm of that decision it is in, and the paths of
11
+ # that decision on which it runs, so a reader can tell which paths
12
+ # reach a row, and count the paths through a flow as a whole.
13
+ module PathsTree
14
+ # Part of a decision's code that a row can be inside: an arm of
15
+ # statements, or a leaf; owner the key of the decision's row,
16
+ # range its bytes, arm its arm's name, nil for a leaf, on
17
+ # the indices of the decision's possible paths that run it, and
18
+ # leaf a leaf's index among the decision's leaves, nil for an arm,
19
+ # and leaves its arm's Leaving word, nil for a leaf or an arm that
20
+ # does not leave the method.
21
+ Region = Data.define(:owner, :range, :arm, :on, :leaf, :leaves)
22
+
23
+ # The place of a row inside no other row's decision.
24
+ TOP = { 'parent' => nil, 'in' => nil, 'on' => nil,
25
+ 'leaf' => nil, 'gives' => nil }.freeze
26
+
27
+ # Example: def f(a); if a then 1 end; end gives the value
28
+ # row 1 its parent, the if, in its then arm, on its path 0.
29
+ # { def decision => { "line:column" => { 'parent' =>, 'in' =>,
30
+ # 'on' =>, 'leaf' =>, 'gives' => } } } for each def in
31
+ # source, a row keyed by where it is named. parent is the
32
+ # "line:column" of the decision the row runs inside, in the name
33
+ # of its arm, on the indices of that decision's possible paths,
34
+ # as DecisionPaths.possible orders them, that run the row, leaf
35
+ # the index of that decision's leaf holding it, and gives, for a
36
+ # row whose decision is the whole of that leaf, the leaf's
37
+ # outcome on each of the row's own possible paths, nil where
38
+ # not known; each nil where it does not apply.
39
+ def self.of(source, path, decisions = nil)
40
+ decisions = Manifest.decisions(source, path) if decisions.nil?
41
+ flows = MethodStatements.of(source, path, decisions)
42
+ flows.transform_values { nested(source, decisions, _1) }
43
+ end
44
+
45
+ # { "line:column" => its place } for each of rows, a flow's,
46
+ # each placed in the innermost region of another row's
47
+ # decision holding all of its own decision's code, or, for a
48
+ # row that is no decision, where it starts: a decision
49
+ # starting where its child does, eg an || whose first operand
50
+ # is an any?, holds that child, and is not held by it. Each of
51
+ # forks, { loop's name => [its bytes, its body's] }, as
52
+ # LoopForks gives them, is placed too. So is each of rescues,
53
+ # { fork's name => [its rescues, its bytes, its body's, [each
54
+ # rescue's body's]] }, as RescueForks gives them. scopes is
55
+ # [forks, rescues].
56
+ def self.nested(source, decisions, rows, scopes = [{}, {}])
57
+ regions = row_regions(source, decisions, rows) +
58
+ PathsForks.regions(*scopes)
59
+ places = rows_placed(source, rows, [decisions, regions])
60
+ PathsForks.placed(places, scopes, regions)
61
+ end
62
+
63
+ # { "line:column" => its place } for each of rows, a flow's,
64
+ # among regions, as nested places them.
65
+ def self.rows_placed(source, rows, (decisions, regions))
66
+ starts = line_starts(source)
67
+ rows.to_h do |row|
68
+ at = starts[row.line - 1] + row.column
69
+ decision = row.decision ? decisions.fetch(row.decision) : nil
70
+ span = decision ? decision.extent : at...(at + 1)
71
+ key = key_of(row)
72
+ [key, placed(key, span, regions, decision)]
73
+ end
74
+ end
75
+
76
+ # The regions of the decisions of rows, a flow's, decisions being
77
+ # { decision => Condition }.
78
+ def self.row_regions(source, decisions, rows)
79
+ rows.select(&:decision).flat_map do |row|
80
+ regions_of(source, key_of(row), decisions.fetch(row.decision))
81
+ end
82
+ end
83
+
84
+ # The place of the row keyed key, its code the bytes span,
85
+ # decision its Condition, nil for a row that is no decision:
86
+ # in the decision owning the smallest region of another row
87
+ # holding span, as place gives it; TOP for a row in no such
88
+ # region.
89
+ def self.placed(key, span, regions, decision)
90
+ holding = holding(key, span, regions)
91
+ inner = smallest(holding)
92
+ return TOP if inner.nil?
93
+
94
+ owner = inner.owner
95
+ own = holding.group_by(&:owner).fetch(owner)
96
+ place(owner, own).merge(leaf_place(own, decision))
97
+ end
98
+
99
+ # The leaf and gives of a row's place, own being the regions
100
+ # of its parent holding it, decision its Condition: the index of
101
+ # the innermost leaf holding it; and, when decision is the whole
102
+ # of that leaf, decision's value on each of its possible paths,
103
+ # as its kind gives them, the leaf's outcome there. Neither
104
+ # for a row that is no decision, a return or a raise, which gives
105
+ # its parent nothing.
106
+ def self.leaf_place(own, decision)
107
+ return { 'leaf' => nil, 'gives' => nil } if decision.nil?
108
+
109
+ leaf = smallest(own.reject(&:arm))
110
+ whole = leaf && decision.extent == leaf.range
111
+ gives = whole ? DecisionPaths.values(decision) : nil
112
+ { 'leaf' => leaf&.leaf, 'gives' => gives }
113
+ end
114
+
115
+ # The regions of regions holding all of the bytes span, but
116
+ # for those of the row keyed key itself.
117
+ def self.holding(key, span, regions)
118
+ held = regions.group_by do |region|
119
+ region.owner != key && region.range.cover?(span)
120
+ end
121
+ held.fetch(true, [])
122
+ end
123
+
124
+ # A row's place in the decision of the row keyed owner, own being
125
+ # the regions of that decision holding the row: the innermost arm
126
+ # of them, nil for none, and the paths on which every one of
127
+ # them runs, as the row runs only where all of them do.
128
+ def self.place(owner, own)
129
+ arm = smallest(own.select(&:arm))
130
+ on = own.map(&:on).reduce(:&)
131
+ { 'parent' => owner, 'in' => arm&.arm, 'on' => on }
132
+ end
133
+
134
+ # The region of regions with the fewest bytes; nil for none.
135
+ def self.smallest(regions) = regions.min_by { _1.range.size }
136
+
137
+ # The regions of condition, the decision of the row keyed owner:
138
+ # its arms', and its leaves'.
139
+ def self.regions_of(source, owner, condition)
140
+ possible = DecisionPaths.possible(condition)
141
+ node = DecisionTree.node(source, condition, possible,
142
+ (0..).each)
143
+ arm_regions(owner, condition, node) +
144
+ leaf_regions(owner, condition, possible)
145
+ end
146
+
147
+ # The regions of condition's leaves, in no arm, each run on
148
+ # the paths of possible with an outcome at its index.
149
+ def self.leaf_regions(owner, condition, possible)
150
+ condition.leaves.each_with_index.map do |range, index|
151
+ skipped = possible.each_index.group_by do |i|
152
+ possible[i][index].nil?
153
+ end
154
+ on = skipped.fetch(false)
155
+ Region.new(owner:, range:, arm: nil, on:, leaf: index,
156
+ leaves: nil)
157
+ end
158
+ end
159
+
160
+ # The regions of the arms in condition, node its tree, each
161
+ # operand's node at its own index: of its own arms, and of
162
+ # those of each condition nested in it, in an operand or in
163
+ # an arm's body.
164
+ def self.arm_regions(owner, condition, node)
165
+ built = condition.operands.zip(node[:operands])
166
+ built.flat_map do |operand, operand_node|
167
+ case operand
168
+ when Condition then arm_regions(owner, operand, operand_node)
169
+ when Arm then arm_region(owner, operand, operand_node)
170
+ else []
171
+ end
172
+ end
173
+ end
174
+
175
+ # The regions of arm, node its node: its own, named by its
176
+ # kind and run on the paths that take it, none for an arm
177
+ # with no statements, which holds no row; then its body's.
178
+ def self.arm_region(owner, arm, node)
179
+ body = arm.body ? arm_regions(owner, arm.body, node[:body]) : []
180
+ return body if arm.extent.nil?
181
+
182
+ cells = node[:cells]
183
+ on = cells.each_index.group_by { cells[_1] }.fetch(true, [])
184
+ own = Region.new(owner:, range: arm.extent, arm: arm.kind.to_s,
185
+ on:, leaf: nil, leaves: arm.leaves)
186
+ [own, *body]
187
+ end
188
+
189
+ # The "line:column" key of row, from its name.
190
+ def self.key_of(row) = key_at(row.name)
191
+
192
+ # The "line:column" key of the row or loop named name.
193
+ def self.key_at(name) = name.split(':').last(2).join(':')
194
+
195
+ # The byte offset where each line of source starts, first
196
+ # line first.
197
+ def self.line_starts(source)
198
+ source.each_line.reduce([0]) do |starts, line|
199
+ starts << (starts.last + line.bytesize)
200
+ end
201
+ end
202
+ end
203
+ end
@@ -0,0 +1,72 @@
1
+ require_relative 'method_ways'
2
+ require_relative 'ways_report'
3
+
4
+ module PathCoverage
5
+ # The ways a method's flow lists for the JSON report: those its
6
+ # calls took, its loops' passes, and of the ways one call can take,
7
+ # those some call took, covered, and those none did, gaps.
8
+ module PathsWayLists
9
+ # { "taken", "passes" } of the method whose entry is named
10
+ # entry: the ways its calls took, ways being { unit's name =>
11
+ # { way => count } }, as a CallTrace counts them; and the passes
12
+ # of each of loops, the names of the method's loops.
13
+ def self.walked(entry, ways, loops)
14
+ { 'taken' => WaysReport.taken(ways.fetch(entry, {})),
15
+ 'passes' => passes(ways, loops) }
16
+ end
17
+
18
+ # { "covered", "gaps", "impossible", "impossible_taken" } of
19
+ # the method whose entry is named entry, ways as walked has
20
+ # them; checked [split, forked]: split [possible, impossible],
21
+ # the ways one call can take, and those none can, as
22
+ # MethodWays.split_flow lists them, forked the names of the
23
+ # loops that fork there.
24
+ def self.checked(entry, ways, ((possible, impossible), forked))
25
+ seen = witnessed(ways.fetch(entry, {}), forked)
26
+ { 'covered' => covered(possible, seen),
27
+ 'gaps' => listed(split(possible, seen).last),
28
+ 'impossible' => listed(impossible),
29
+ 'impossible_taken' => listed(split(impossible, seen).first) }
30
+ end
31
+
32
+ # [taken, untaken] of ways: those some call witnessed, as seen
33
+ # counts them, and those none did.
34
+ def self.split(ways, seen)
35
+ by = ways.group_by { seen.key?(MethodWays.matched(_1)) }
36
+ [by.fetch(true, []), by.fetch(false, [])]
37
+ end
38
+
39
+ # Each of ways as the JSON report lists a way.
40
+ def self.listed(ways)
41
+ ways.map { WaysReport.way(_1) }
42
+ end
43
+
44
+ # { loop's at => its passes' ways, as WaysReport.taken lists
45
+ # them } of each of loops, ways as of has them.
46
+ def self.passes(ways, loops)
47
+ loops.to_h do |loop|
48
+ [WaysReport.at(loop), WaysReport.taken(ways.fetch(loop, {}))]
49
+ end
50
+ end
51
+
52
+ # { way => how many calls witnessed it } of walked, { way =>
53
+ # count }, each way's steps ordered, as MethodWays.witnessed
54
+ # gives them, each loop of forked a fork.
55
+ def self.witnessed(walked, forked)
56
+ walked.each_with_object(Hash.new(0)) do |(way, count), seen|
57
+ MethodWays.witnessed(way, forked).each { seen[_1] += count }
58
+ end
59
+ end
60
+
61
+ # [{ "way", "count" }] of each of possible some call took, as
62
+ # seen counts them.
63
+ def self.covered(possible, seen)
64
+ possible.filter_map do |way|
65
+ count = seen.fetch(MethodWays.matched(way), 0)
66
+ next unless count.positive?
67
+
68
+ { 'way' => WaysReport.way(way), 'count' => count }
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,193 @@
1
+ require_relative 'paths_fitting'
2
+ require_relative 'paths_leaving'
3
+ require_relative 'paths_rescues'
4
+ require_relative 'paths_tree'
5
+ require_relative 'manifest'
6
+ require_relative 'method_statements'
7
+ require_relative 'decision_paths'
8
+ require_relative 'stable_leaves'
9
+ require_relative 'way_algebras'
10
+
11
+ module PathCoverage
12
+ # The ways one call can go through each flow, folded over its
13
+ # tree, as PathsTree places its rows. Each row gives a pair,
14
+ # [ways that go on, ways that leave the method]: a return or a
15
+ # raise leaves, and so does each path of a decision taking an
16
+ # arm that returns or raises, eg return 0 if a. Rows one after
17
+ # another multiply, but only the ways that go on reach the next
18
+ # row; a decision adds up its paths, each with the rows it runs
19
+ # on that path one after another; and a row that is the whole of
20
+ # a leaf counts only those of its paths that give the leaf the
21
+ # outcome the path has, so no impossible way is counted where
22
+ # gives knows it. A block's body counts once, unless the block
23
+ # forks, as PathsForks folds it.
24
+ # Each way also notes the outcome it gives each leaf that
25
+ # StableLeaves finds, by its local's name, and two ways joined
26
+ # one after the other that give one name both outcomes are no
27
+ # way, as no call can take them.
28
+ # The fold is in one of WayAlgebras: Counted counts the ways,
29
+ # by default; Labelled gives the paths taken on any of them, and
30
+ # Listed lists them.
31
+ class PathsWays
32
+ include PathsFitting
33
+ include PathsForks
34
+ include PathsLeaving
35
+ include PathsRescues
36
+
37
+ # Example: { "k.rb:1:0" => 3 } for def f(a, b), return 0 if a,
38
+ # then a ?: of b: 1 way leaves, 2 go on.
39
+ # { def decision => its ways, in algebra } for each def in
40
+ # source, its decisions those of no settings.
41
+ def self.of(source, path, algebra = WayAlgebras::Counted)
42
+ decisions = Manifest.decisions(source, path)
43
+ flows = MethodStatements.of(source, path, decisions)
44
+ flows.transform_values do |rows|
45
+ flow(source, decisions, rows, algebra)
46
+ end
47
+ end
48
+
49
+ # The ways through a flow of rows in source, in algebra,
50
+ # decisions being { decision => Condition }, as
51
+ # Manifest.decisions gives them. folding is [stable, forks,
52
+ # rescues, forking]. With stable false, the ways include those
53
+ # no call can take, as no leaf is then assigned. Each loop of
54
+ # forks, as LoopForks gives them, is a scope that a next or a
55
+ # break ends a pass of. When forking, each loop is a fork too,
56
+ # labelled [its key, 0] for 0 items and [its key, 1] for 1+
57
+ # items, then its body's rows, walked once. Each rescue fork of
58
+ # rescues, a def's, forks the flow.
59
+ def self.flow(source, decisions, rows,
60
+ algebra = WayAlgebras::Counted,
61
+ folding = [true, {}, {}, false])
62
+ stable, forks, rescues, forking = folding
63
+ places = PathsTree.nested(source, decisions, rows,
64
+ [forks, rescues])
65
+ names = stable ? StableLeaves.of(source, decisions, rows) : {}
66
+ new(source, rows, places, decisions,
67
+ [algebra, names, rescues, forking]).total
68
+ end
69
+
70
+ # The fold of rows, a flow's in source, placed as places has
71
+ # them. Each decision row's Condition is in decisions. folding
72
+ # is [algebra, names, rescues, forking]. Its ways are in
73
+ # algebra, and each step of it tallies them, as
74
+ # WayAlgebras::Tallied does. names assigns leaves, as
75
+ # StableLeaves finds them. rescues are the def's rescue forks,
76
+ # as RescueForks gives them. forking says whether loops fork.
77
+ def initialize(source, rows, places, decisions,
78
+ (algebra, names, rescues, forking))
79
+ @source = source
80
+ @rows = rows.to_h { |row| [PathsTree.key_of(row), row] }
81
+ @places = places
82
+ @decisions = decisions
83
+ @ways = WayAlgebras::Tallied.new(algebra)
84
+ @children = places.keys.group_by { places[_1]['parent'] }
85
+ @names = names
86
+ @rescues = PathsRescues.counts(rescues)
87
+ @forking = forking
88
+ end
89
+
90
+ # The ways through the flow, in its algebra. Its top rows run
91
+ # one after another. Every way counts, whether it goes on or
92
+ # leaves, whatever its assignments.
93
+ def total
94
+ tops = @children.fetch(nil, []).map { |key| pair_of(key) }
95
+ all = sequence(tops)
96
+ @ways.total(all.reduce { |sum, ways| @ways.plus(sum, ways) })
97
+ end
98
+
99
+ # The parts of rows run one after another, from each row's
100
+ # parts. The ways that go on pass through every row. A way
101
+ # that leaves at one row has gone on through each row before
102
+ # it.
103
+ def sequence(many)
104
+ start = [@ways.one, *nothing.drop(1)]
105
+ many.reduce(start) do |(on, *out), (more, *leave)|
106
+ through = leave.map { @ways.times(on, _1) }
107
+ [@ways.times(on, more),
108
+ *out.zip(through).map { @ways.plus(*_1) }]
109
+ end
110
+ end
111
+
112
+ # The parts of the row keyed key. A decision adds up its
113
+ # paths' parts. Any other row is a statement that leaves, eg
114
+ # a return or a next, so it leaves by its word on its one path.
115
+ def pair_of(key)
116
+ return rescue_pair(key) if @rescues.key?(key)
117
+ return fork_pair(key) unless @rows.key?(key)
118
+
119
+ row = @rows[key]
120
+ return left(row.top, @ways.unit([key, 0])) if row.decision.nil?
121
+
122
+ many = possible(row.decision).each_index.map do |path|
123
+ pair_on(key, path)
124
+ end
125
+ added(many)
126
+ end
127
+
128
+ # The parts of the decision row keyed key on its path at index
129
+ # path. The path is taken, then the rows it runs one after
130
+ # another, each given that path's outcomes. When the path's arm
131
+ # leaves, every way of it leaves, by the arm's word.
132
+ def pair_on(key, path)
133
+ outcomes = possible(@rows[key].decision)[path]
134
+ many = running(key, path).map do |child|
135
+ child_pair(child, outcomes)
136
+ end
137
+ own = own(key, path, outcomes)
138
+ ways = sequence(many).map { @ways.times(own, _1) }
139
+ leaving_by(ways, leaving(key)[path])
140
+ end
141
+
142
+ # The tally of the one way that takes the decision row keyed
143
+ # key's path at index path, with outcomes, giving each name
144
+ # the outcome of its leaves on it; no way when it gives a name
145
+ # both outcomes, as eg a && b || a && c does with a true then
146
+ # false.
147
+ def own(key, path, outcomes)
148
+ named = named(key, outcomes)
149
+ assignment = named.to_h
150
+ return @ways.zero if assignment.size < named.size
151
+
152
+ @ways.unit([key, path], assignment)
153
+ end
154
+
155
+ # The distinct [name, outcome] of each leaf, of the decision of
156
+ # the row keyed key, that outcomes reaches and that has a name.
157
+ def named(key, outcomes)
158
+ leaves = @decisions.fetch(@rows[key].decision).leaves
159
+ leaves.zip(outcomes).filter_map do |range, outcome|
160
+ next if outcome.nil? || !@names.key?(range)
161
+
162
+ [@names[range], outcome]
163
+ end.uniq
164
+ end
165
+
166
+ # The keys of the rows the decision row keyed key runs on its path
167
+ # at index path, in the order they are written.
168
+ def running(key, path)
169
+ runs = @children.fetch(key, []).group_by do |child|
170
+ @places[child]['on'].include?(path)
171
+ end
172
+ runs.fetch(true, [])
173
+ end
174
+
175
+ # The parts of the row keyed child. outcomes is its parent's
176
+ # path's outcomes. A row that gives nothing keeps all its
177
+ # paths. Otherwise only the paths that fit outcomes count, as
178
+ # PathsFitting decides.
179
+ def child_pair(child, outcomes)
180
+ return pair_of(child) if @places[child]['gives'].nil?
181
+
182
+ many = each_fitting(child, outcomes, nothing) do |path|
183
+ pair_on(child, path)
184
+ end
185
+ added(many)
186
+ end
187
+
188
+ # The possible paths of the decision named decision.
189
+ def possible(decision)
190
+ DecisionPaths.possible(@decisions.fetch(decision))
191
+ end
192
+ end
193
+ end
@@ -1,3 +1,4 @@
1
+ require_relative 'call_trace'
1
2
  require_relative 'condition'
2
3
  require_relative 'recorder_blocks'
3
4
  require_relative 'recorder_cases'
@@ -12,78 +13,112 @@ module PathCoverage
12
13
  include RecorderChains
13
14
  include RecorderWrites
14
15
 
15
- # One evaluation's way through its site: the outcome of
16
+ # One evaluation's way through its decision: the outcome of
16
17
  # each leaf, nil for one not yet, or never, reached.
17
- Trail = Struct.new(:site, :outcomes)
18
+ Trail = Struct.new(:decision, :outcomes)
18
19
 
19
- # Starts with no outcomes, site paths or reaches counted.
20
+ # Starts with no outcomes, decision paths or reaches counted.
20
21
  def initialize
21
22
  @counts = Hash.new do |counts, key|
22
23
  counts[key] = { truthy: 0, falsy: 0 }
23
24
  end
24
- @site_paths = Hash.new do |site_paths, site|
25
- site_paths[site] = Hash.new(0)
25
+ @decision_paths = Hash.new do |decision_paths, decision|
26
+ decision_paths[decision] = Hash.new(0)
26
27
  end
27
28
  @reached = Hash.new(0)
28
29
  @trails_key = :"path_coverage_trails_#{object_id}"
30
+ @trace = CallTrace.new
29
31
  end
30
32
 
31
33
  # { name => runs } of each statement reached so far, by its
32
34
  # name, eg { "k.rb:2:2" => 2 }; none never reached.
33
35
  attr_reader :reached
34
36
 
35
- # Counts a run of the statement named name.
37
+ # The ways each call of a covered method has taken so far.
38
+ attr_reader :trace
39
+
40
+ # Counts a run of the statement named name, a step of its
41
+ # call's way.
36
42
  def reach(name)
37
43
  @reached[name] += 1
44
+ @trace.reach(name)
45
+ nil
46
+ end
47
+
48
+ # Notes a pass of the loop named name, a block called with
49
+ # one more item.
50
+ def pass(name)
51
+ @trace.pass(name)
52
+ nil
53
+ end
54
+
55
+ # Notes that the rescue fork named name completed, as a step
56
+ # of the call under way. Returns value unchanged.
57
+ def completed(name, value)
58
+ @trace.decided(name, ['completed'])
59
+ value
60
+ end
61
+
62
+ # Notes that the rescue numbered number of the rescue fork
63
+ # named name ran, as a step of the call under way.
64
+ def rescued(name, number)
65
+ @trace.decided(name, ["rescue #{number}"])
38
66
  nil
39
67
  end
40
68
 
41
- # The stack of site's evaluations under way in the current
69
+ # The stack of decision's evaluations under way in the current
42
70
  # thread (fiber), innermost last, so another thread's never
43
71
  # mix with its.
44
- def trails(site)
72
+ def trails(decision)
45
73
  stacks = Thread.current[@trails_key] ||=
46
74
  Hash.new { |all, key| all[key] = [] }
47
- stacks[site]
75
+ stacks[decision]
48
76
  end
49
77
 
50
- # { site => { [outcome, ...] => evaluations } } for every
51
- # evaluation of a site entered and left so far.
52
- attr_reader :site_paths
78
+ # { decision => { [outcome, ...] => evaluations } } for every
79
+ # evaluation of a decision entered and left so far.
80
+ attr_reader :decision_paths
53
81
 
54
- # Starts an evaluation of site, which has size leaves, as
82
+ # Starts an evaluation of decision, which has size leaves, as
55
83
  # the trail its leaves' records fill in.
56
- def enter(site, size)
57
- trail = Trail.new(site, Array.new(size))
58
- trails(site).push(trail)
84
+ def enter(decision, size)
85
+ trail = Trail.new(decision, Array.new(size))
86
+ trails(decision).push(trail)
59
87
  trail
60
88
  end
61
89
 
62
90
  # Ends trail's evaluation, counting its path, and returns
63
- # value, the site's, unchanged. An evaluation of the same
64
- # site still entered above trail, as one that raised
91
+ # value, the decision's, unchanged. An evaluation of the same
92
+ # decision still entered above trail, as one that raised
65
93
  # leaves, took trail's later outcomes, so trail's path is
66
94
  # not counted; the stack is cleared down to trail either
67
- # way.
95
+ # way. A counted path is a step of its call's way.
68
96
  def leave(trail, value)
69
- stack = trails(trail.site)
97
+ stack = trails(trail.decision)
70
98
  clean = stack.last.equal?(trail)
71
99
  nil until stack.empty? || stack.pop.equal?(trail)
72
- @site_paths[trail.site][trail.outcomes] += 1 if clean
100
+ decided(trail) if clean
73
101
  value
74
102
  end
75
103
 
104
+ # Counts trail's path, its decision's, and steps its call's
105
+ # way with it.
106
+ def decided(trail)
107
+ @decision_paths[trail.decision][trail.outcomes] += 1
108
+ @trace.decided(trail.decision, trail.outcomes)
109
+ end
110
+
76
111
  # The truthy and falsy counts of every operand recorded so
77
- # far, keyed by [site, index]; an operand that never ran
112
+ # far, keyed by [decision, index]; an operand that never ran
78
113
  # has no entry.
79
114
  attr_reader :counts
80
115
 
81
116
  # Counts value's outcome and returns it unchanged, so the
82
117
  # condition around it behaves exactly as uninstrumented.
83
- def record(site, index, value)
118
+ def record(decision, index, value)
84
119
  outcome = value ? :truthy : :falsy
85
- @counts[[site, index]][outcome] += 1
86
- trail = trails(site).last
120
+ @counts[[decision, index]][outcome] += 1
121
+ trail = trails(decision).last
87
122
  trail.outcomes[index] = outcome == :truthy if trail
88
123
  value
89
124
  end
@@ -93,30 +128,30 @@ module PathCoverage
93
128
  # matches it against the pattern, once, as the case would
94
129
  # have.
95
130
  WhenMatch = Struct.new(
96
- :recorder, :site, :index, :total, :pattern
131
+ :recorder, :decision, :index, :total, :pattern
97
132
  ) do
98
133
  # Whether pattern matches subject, exactly as pattern says,
99
- # noted on the site's path.
134
+ # noted on the decision's path.
100
135
  def ===(subject)
101
- recorder.matched(site, index, total, pattern === subject)
136
+ recorder.matched(decision, index, total, pattern === subject)
102
137
  end
103
138
  end
104
139
 
105
140
  # The stand-in for pattern, the index-th of size in the case
106
- # at site.
107
- def when_match(site, index, size, pattern)
108
- WhenMatch.new(self, site, index, size, pattern)
141
+ # at decision.
142
+ def when_match(decision, index, size, pattern)
143
+ WhenMatch.new(self, decision, index, size, pattern)
109
144
  end
110
145
 
111
146
  # Notes whether the index-th of size patterns matched, the
112
- # first entering the case's site, and a match or the last
147
+ # first entering the case's decision, and a match or the last
113
148
  # leaving it, as no more patterns are then tried; returns
114
149
  # matched unchanged.
115
- def matched(site, index, size, matched)
116
- enter(site, size) if index.zero?
117
- record(site, index, matched)
150
+ def matched(decision, index, size, matched)
151
+ enter(decision, size) if index.zero?
152
+ record(decision, index, matched)
118
153
  last = matched || index == size - 1
119
- leave(trails(site).last, matched) if last
154
+ leave(trails(decision).last, matched) if last
120
155
  matched
121
156
  end
122
157
  end