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.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +286 -199
  3. data/exe/path_coverage_report +4 -0
  4. data/lib/path_coverage/call_trace.rb +88 -0
  5. data/lib/path_coverage/calls.rb +49 -43
  6. data/lib/path_coverage/card_context.rb +104 -0
  7. data/lib/path_coverage/card_forks.rb +44 -0
  8. data/lib/path_coverage/card_gaps.rb +125 -0
  9. data/lib/path_coverage/card_scopes.rb +37 -0
  10. data/lib/path_coverage/card_tree.rb +79 -66
  11. data/lib/path_coverage/card_way_lists.rb +72 -0
  12. data/lib/path_coverage/card_ways.rb +123 -67
  13. data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +35 -33
  14. data/lib/path_coverage/condition.rb +14 -14
  15. data/lib/path_coverage/configuration.rb +7 -4
  16. data/lib/path_coverage/configuration_limits.rb +52 -0
  17. data/lib/path_coverage/configuration_paths.rb +7 -7
  18. data/lib/path_coverage/console_summary.rb +24 -22
  19. data/lib/path_coverage/{site.rb → decision.rb} +2 -2
  20. data/lib/path_coverage/decision_locations.rb +76 -0
  21. data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +13 -11
  22. data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
  23. data/lib/path_coverage/fold_branches.rb +18 -0
  24. data/lib/path_coverage/fold_cells.rb +42 -0
  25. data/lib/path_coverage/{html_tree.rb → fold_rows.rb} +42 -71
  26. data/lib/path_coverage/gaps.rb +33 -15
  27. data/lib/path_coverage/json_reader.rb +54 -19
  28. data/lib/path_coverage/json_reports.rb +102 -0
  29. data/lib/path_coverage/json_writer.rb +27 -12
  30. data/lib/path_coverage/kernel.rb +56 -58
  31. data/lib/path_coverage/kinds/base.rb +16 -16
  32. data/lib/path_coverage/kinds/block.rb +16 -15
  33. data/lib/path_coverage/kinds/case.rb +14 -12
  34. data/lib/path_coverage/kinds/chain.rb +40 -11
  35. data/lib/path_coverage/kinds/choice.rb +4 -3
  36. data/lib/path_coverage/kinds/contains.rb +40 -32
  37. data/lib/path_coverage/kinds/safe.rb +38 -26
  38. data/lib/path_coverage/kinds/write.rb +6 -6
  39. data/lib/path_coverage/kinds.rb +2 -2
  40. data/lib/path_coverage/leaving.rb +44 -0
  41. data/lib/path_coverage/loop_forks.rb +57 -0
  42. data/lib/path_coverage/loop_nodes.rb +55 -0
  43. data/lib/path_coverage/manifest.rb +92 -58
  44. data/lib/path_coverage/manifest_blocks.rb +5 -26
  45. data/lib/path_coverage/manifest_case.rb +2 -2
  46. data/lib/path_coverage/manifest_chains.rb +8 -8
  47. data/lib/path_coverage/manifest_choices.rb +23 -20
  48. data/lib/path_coverage/manifest_disabled.rb +97 -0
  49. data/lib/path_coverage/manifest_folding.rb +43 -40
  50. data/lib/path_coverage/manifest_ignoring.rb +6 -6
  51. data/lib/path_coverage/manifest_writes.rb +11 -10
  52. data/lib/path_coverage/measured_by.rb +20 -0
  53. data/lib/path_coverage/merge.rb +7 -7
  54. data/lib/path_coverage/method_loops.rb +128 -0
  55. data/lib/path_coverage/method_statements.rb +130 -127
  56. data/lib/path_coverage/method_ways.rb +142 -0
  57. data/lib/path_coverage/paths_report.rb +16 -16
  58. data/lib/path_coverage/recorder.rb +57 -36
  59. data/lib/path_coverage/recorder_blocks.rb +17 -17
  60. data/lib/path_coverage/recorder_cases.rb +12 -11
  61. data/lib/path_coverage/recorder_chains.rb +12 -12
  62. data/lib/path_coverage/recorder_writes.rb +9 -9
  63. data/lib/path_coverage/render.rb +58 -51
  64. data/lib/path_coverage/result.rb +32 -31
  65. data/lib/path_coverage/rewriter.rb +39 -27
  66. data/lib/path_coverage/rewriter_nested.rb +13 -13
  67. data/lib/path_coverage/runs_report.rb +84 -0
  68. data/lib/path_coverage/source_lines.rb +16 -16
  69. data/lib/path_coverage/source_report.rb +100 -0
  70. data/lib/path_coverage/stable_leaves.rb +162 -0
  71. data/lib/path_coverage/statement_arms.rb +31 -30
  72. data/lib/path_coverage/summary.rb +4 -62
  73. data/lib/path_coverage/totals.rb +10 -8
  74. data/lib/path_coverage/version.rb +1 -1
  75. data/lib/path_coverage/walk.rb +106 -0
  76. data/lib/path_coverage/way_algebras.rb +127 -0
  77. data/lib/path_coverage/ways_report.rb +91 -0
  78. data/lib/path_coverage/ways_totals.rb +46 -0
  79. data/lib/path_coverage/within_methods.rb +135 -80
  80. metadata +33 -28
  81. data/lib/path_coverage/card.rb +0 -14
  82. data/lib/path_coverage/html/file.html +0 -33
  83. data/lib/path_coverage/html/index.html +0 -29
  84. data/lib/path_coverage/html/settings.js +0 -50
  85. data/lib/path_coverage/html/side.js +0 -599
  86. data/lib/path_coverage/html/sort.js +0 -34
  87. data/lib/path_coverage/html/style.css +0 -484
  88. data/lib/path_coverage/html/tabs.js +0 -15
  89. data/lib/path_coverage/html/theme.js +0 -27
  90. data/lib/path_coverage/html_branch.rb +0 -30
  91. data/lib/path_coverage/html_cards.rb +0 -88
  92. data/lib/path_coverage/html_cells.rb +0 -71
  93. data/lib/path_coverage/html_file.rb +0 -123
  94. data/lib/path_coverage/html_index.rb +0 -128
  95. data/lib/path_coverage/html_index_head.rb +0 -29
  96. data/lib/path_coverage/html_labels.rb +0 -16
  97. data/lib/path_coverage/html_methods.rb +0 -66
  98. data/lib/path_coverage/html_nav.rb +0 -67
  99. data/lib/path_coverage/html_report.rb +0 -131
  100. data/lib/path_coverage/html_statements.rb +0 -186
  101. data/lib/path_coverage/html_tabs.rb +0 -26
  102. data/lib/path_coverage/html_template.rb +0 -26
@@ -1,98 +1,110 @@
1
+ require_relative 'card_forks'
1
2
  require_relative 'manifest'
2
3
  require_relative 'method_statements'
3
- require_relative 'site_paths'
4
- require_relative 'site_tree'
4
+ require_relative 'decision_paths'
5
+ require_relative 'decision_tree'
5
6
 
6
7
  module PathCoverage
7
- # How the rows of each card nest: for each row, the site it
8
- # runs inside, the arm of that site it is in, and the paths of
9
- # that site on which it runs, so a reader can tell which paths
8
+ # How the rows of each card nest: for each row, the decision it
9
+ # runs inside, the arm of that decision it is in, and the paths of
10
+ # that decision on which it runs, so a reader can tell which paths
10
11
  # reach a row, and count the paths through a card as a whole.
11
12
  module CardTree
12
- # Part of a site's code that a row can be inside: an arm of
13
- # statements, or a leaf; owner the key of the site's row,
13
+ # Part of a decision's code that a row can be inside: an arm of
14
+ # statements, or a leaf; owner the key of the decision's row,
14
15
  # range its bytes, arm its arm's name, nil for a leaf, on
15
- # the indices of the site's possible paths that run it, and
16
- # leaf a leaf's index among the site's leaves, nil for an arm.
17
- Region = Data.define(:owner, :range, :arm, :on, :leaf)
16
+ # the indices of the decision's possible paths that run it, and
17
+ # leaf a leaf's index among the decision's leaves, nil for an arm,
18
+ # and leaves its arm's Leaving word, nil for a leaf or an arm that
19
+ # does not leave the method.
20
+ Region = Data.define(:owner, :range, :arm, :on, :leaf, :leaves)
18
21
 
19
- # The place of a row inside no other row's site.
20
- TOP = {
21
- 'parent' => nil, 'in' => nil, 'on' => nil,
22
- 'leaf' => nil, 'gives' => nil
23
- }.freeze
22
+ # The place of a row inside no other row's decision.
23
+ TOP = { 'parent' => nil, 'in' => nil, 'on' => nil,
24
+ 'leaf' => nil, 'gives' => nil }.freeze
24
25
 
25
26
  # Example: def f(a); if a then 1 end; end gives the value
26
27
  # row 1 its parent, the if, in its then arm, on its path 0.
27
- # { def site => { "line:column" => { 'parent' =>, 'in' =>,
28
+ # { def decision => { "line:column" => { 'parent' =>, 'in' =>,
28
29
  # 'on' =>, 'leaf' =>, 'gives' => } } } for each def in
29
30
  # source, a row keyed by where it is named. parent is the
30
- # "line:column" of the site the row runs inside, in the name
31
- # of its arm, on the indices of that site's possible paths,
32
- # as SitePaths.possible orders them, that run the row, leaf
33
- # the index of that site's leaf holding it, and gives, for a
34
- # row whose site is the whole of that leaf, the leaf's
31
+ # "line:column" of the decision the row runs inside, in the name
32
+ # of its arm, on the indices of that decision's possible paths,
33
+ # as DecisionPaths.possible orders them, that run the row, leaf
34
+ # the index of that decision's leaf holding it, and gives, for a
35
+ # row whose decision is the whole of that leaf, the leaf's
35
36
  # outcome on each of the row's own possible paths, nil where
36
37
  # not known; each nil where it does not apply.
37
- def self.of(source, path, sites = nil)
38
- sites = Manifest.sites(source, path) if sites.nil?
39
- cards = MethodStatements.of(source, path, sites)
40
- cards.transform_values { |rows| nested(source, sites, rows) }
38
+ def self.of(source, path, decisions = nil)
39
+ decisions = Manifest.decisions(source, path) if decisions.nil?
40
+ cards = MethodStatements.of(source, path, decisions)
41
+ cards.transform_values { nested(source, decisions, _1) }
41
42
  end
42
43
 
43
44
  # { "line:column" => its place } for each of rows, a card's,
44
- # each placed in the innermost region of another row's site
45
- # holding all of its own site's code, or, for a row that is
46
- # no site, where it starts: a site starting where its child
47
- # does, eg an || whose first operand is an any?, holds that
48
- # child, and is not held by it.
49
- def self.nested(source, sites, rows)
45
+ # each placed in the innermost region of another row's
46
+ # decision holding all of its own decision's code, or, for a
47
+ # row that is no decision, where it starts: a decision
48
+ # starting where its child does, eg an || whose first operand
49
+ # is an any?, holds that child, and is not held by it. Each of
50
+ # forks, { loop's name => [its bytes, its body's] }, as
51
+ # LoopForks gives them, is placed too, as CardForks places it.
52
+ def self.nested(source, decisions, rows, forks = {})
53
+ regions = card_regions(source, decisions, rows) +
54
+ CardForks.regions(forks)
55
+ places = rows_placed(source, rows, [decisions, regions])
56
+ CardForks.placed(places, forks, regions)
57
+ end
58
+
59
+ # { "line:column" => its place } for each of rows, a card's,
60
+ # among regions, as nested places them.
61
+ def self.rows_placed(source, rows, (decisions, regions))
50
62
  starts = line_starts(source)
51
- regions = card_regions(source, sites, rows)
52
63
  rows.to_h do |row|
53
64
  at = starts[row.line - 1] + row.column
54
- site = row.site ? sites.fetch(row.site) : nil
55
- span = site ? site.extent : at...(at + 1)
65
+ decision = row.decision ? decisions.fetch(row.decision) : nil
66
+ span = decision ? decision.extent : at...(at + 1)
56
67
  key = key_of(row)
57
- [key, placed(key, span, regions, site)]
68
+ [key, placed(key, span, regions, decision)]
58
69
  end
59
70
  end
60
71
 
61
- # The regions of the sites of rows, a card's, sites being
62
- # { site => Condition }.
63
- def self.card_regions(source, sites, rows)
64
- rows.select(&:site).flat_map do |row|
65
- regions_of(source, key_of(row), sites.fetch(row.site))
72
+ # The regions of the decisions of rows, a card's, decisions being
73
+ # { decision => Condition }.
74
+ def self.card_regions(source, decisions, rows)
75
+ rows.select(&:decision).flat_map do |row|
76
+ regions_of(source, key_of(row), decisions.fetch(row.decision))
66
77
  end
67
78
  end
68
79
 
69
80
  # The place of the row keyed key, its code the bytes span,
70
- # site its Condition, nil for a row that is no site: in the
71
- # site owning the smallest region of another row holding
72
- # span, as place gives it; TOP for a row in no such region.
73
- def self.placed(key, span, regions, site)
81
+ # decision its Condition, nil for a row that is no decision:
82
+ # in the decision owning the smallest region of another row
83
+ # holding span, as place gives it; TOP for a row in no such
84
+ # region.
85
+ def self.placed(key, span, regions, decision)
74
86
  holding = holding(key, span, regions)
75
87
  inner = smallest(holding)
76
88
  return TOP if inner.nil?
77
89
 
78
90
  owner = inner.owner
79
91
  own = holding.group_by(&:owner).fetch(owner)
80
- place(owner, own).merge(leaf_place(own, site))
92
+ place(owner, own).merge(leaf_place(own, decision))
81
93
  end
82
94
 
83
95
  # The leaf and gives of a row's place, own being the regions
84
- # of its parent holding it, site its Condition: the index of
85
- # the innermost leaf holding it; and, when site is the whole
86
- # of that leaf, site's value on each of its possible paths,
96
+ # of its parent holding it, decision its Condition: the index of
97
+ # the innermost leaf holding it; and, when decision is the whole
98
+ # of that leaf, decision's value on each of its possible paths,
87
99
  # as its kind gives them, the leaf's outcome there. Neither
88
- # for a row that is no site, a return or a raise, which gives
100
+ # for a row that is no decision, a return or a raise, which gives
89
101
  # its parent nothing.
90
- def self.leaf_place(own, site)
91
- return { 'leaf' => nil, 'gives' => nil } if site.nil?
102
+ def self.leaf_place(own, decision)
103
+ return { 'leaf' => nil, 'gives' => nil } if decision.nil?
92
104
 
93
105
  leaf = smallest(own.reject(&:arm))
94
- whole = leaf && site.extent == leaf.range
95
- gives = whole ? SitePaths.values(site) : nil
106
+ whole = leaf && decision.extent == leaf.range
107
+ gives = whole ? DecisionPaths.values(decision) : nil
96
108
  { 'leaf' => leaf&.leaf, 'gives' => gives }
97
109
  end
98
110
 
@@ -105,8 +117,8 @@ module PathCoverage
105
117
  held.fetch(true, [])
106
118
  end
107
119
 
108
- # A row's place in the site of the row keyed owner, own being
109
- # the regions of that site holding the row: the innermost arm
120
+ # A row's place in the decision of the row keyed owner, own being
121
+ # the regions of that decision holding the row: the innermost arm
110
122
  # of them, nil for none, and the paths on which every one of
111
123
  # them runs, as the row runs only where all of them do.
112
124
  def self.place(owner, own)
@@ -116,15 +128,14 @@ module PathCoverage
116
128
  end
117
129
 
118
130
  # The region of regions with the fewest bytes; nil for none.
119
- def self.smallest(regions)
120
- regions.min_by { |region| region.range.size }
121
- end
131
+ def self.smallest(regions) = regions.min_by { _1.range.size }
122
132
 
123
- # The regions of condition, the site of the row keyed owner:
133
+ # The regions of condition, the decision of the row keyed owner:
124
134
  # its arms', and its leaves'.
125
135
  def self.regions_of(source, owner, condition)
126
- possible = SitePaths.possible(condition)
127
- node = SiteTree.node(source, condition, possible, (0..).each)
136
+ possible = DecisionPaths.possible(condition)
137
+ node = DecisionTree.node(source, condition, possible,
138
+ (0..).each)
128
139
  arm_regions(owner, condition, node) +
129
140
  leaf_regions(owner, condition, possible)
130
141
  end
@@ -137,7 +148,8 @@ module PathCoverage
137
148
  possible[i][index].nil?
138
149
  end
139
150
  on = skipped.fetch(false)
140
- Region.new(owner:, range:, arm: nil, on:, leaf: index)
151
+ Region.new(owner:, range:, arm: nil, on:, leaf: index,
152
+ leaves: nil)
141
153
  end
142
154
  end
143
155
 
@@ -166,14 +178,15 @@ module PathCoverage
166
178
  cells = node[:cells]
167
179
  on = cells.each_index.group_by { cells[_1] }.fetch(true, [])
168
180
  own = Region.new(owner:, range: arm.extent, arm: arm.kind.to_s,
169
- on:, leaf: nil)
181
+ on:, leaf: nil, leaves: arm.leaves)
170
182
  [own, *body]
171
183
  end
172
184
 
173
185
  # The "line:column" key of row, from its name.
174
- def self.key_of(row)
175
- row.name.split(':').last(2).join(':')
176
- end
186
+ def self.key_of(row) = key_at(row.name)
187
+
188
+ # The "line:column" key of the row or loop named name.
189
+ def self.key_at(name) = name.split(':').last(2).join(':')
177
190
 
178
191
  # The byte offset where each line of source starts, first
179
192
  # line first.
@@ -0,0 +1,72 @@
1
+ require_relative 'method_ways'
2
+ require_relative 'ways_report'
3
+
4
+ module PathCoverage
5
+ # The ways a method's card 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 CardWayLists
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_card 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
@@ -1,91 +1,152 @@
1
1
  require_relative 'card_tree'
2
2
  require_relative 'manifest'
3
3
  require_relative 'method_statements'
4
- require_relative 'site_paths'
4
+ require_relative 'decision_paths'
5
+ require_relative 'stable_leaves'
6
+ require_relative 'way_algebras'
5
7
 
6
8
  module PathCoverage
7
- # The number of ways one call can go through each card, folded
8
- # over its tree, as CardTree places its rows. Each row gives a
9
- # pair, [ways that go on, ways that leave the method]: a return
10
- # or a raise leaves, and so does each path of a site taking an
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
11
13
  # arm that returns or raises, eg return 0 if a. Rows one after
12
14
  # another multiply, but only the ways that go on reach the next
13
- # row; a site adds up its paths, each with the rows it runs on
14
- # that path one after another; and a row that is the whole of a
15
- # leaf counts only those of its paths that give the leaf the
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
16
18
  # outcome the path has, so no impossible way is counted where
17
- # gives knows it. A block's body counts once.
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.
18
28
  class CardWays
29
+ include CardForks
30
+
19
31
  # Example: { "k.rb:1:0" => 3 } for def f(a, b), return 0 if a,
20
32
  # then a ?: of b: 1 way leaves, 2 go on.
21
- # { def site => its ways } for each def in source, its sites
22
- # those of no settings.
23
- def self.of(source, path)
24
- sites = Manifest.sites(source, path)
25
- cards = MethodStatements.of(source, path, sites)
26
- cards.transform_values { |rows| card(source, sites, rows) }
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
27
41
  end
28
42
 
29
- # The ways through a card of rows in source, sites being
30
- # { site => Condition }, as Manifest.sites gives them.
31
- def self.card(source, sites, rows)
32
- places = CardTree.nested(source, sites, rows)
33
- new(source, rows, places, sites).total
34
- end
35
-
36
- # The pair of rows one after another, from their pairs: the
37
- # ways that go on through them all, and those that leave at
38
- # one of them, having gone on through every one before it.
39
- def self.sequence(pairs)
40
- pairs.reduce([1, 0]) do |(on, left), (more, leaving)|
41
- [on * more, left + (on * leaving)]
42
- end
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
43
57
  end
44
58
 
45
59
  # The fold of rows, a card's in source, placed as places has
46
- # them, each site row's Condition among sites.
47
- def initialize(source, rows, places, sites)
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))
48
65
  @source = source
49
66
  @rows = rows.to_h { |row| [CardTree.key_of(row), row] }
50
67
  @places = places
51
- @sites = sites
68
+ @decisions = decisions
69
+ @ways = WayAlgebras::Tallied.new(algebra)
52
70
  @children = places.keys.group_by { places[_1]['parent'] }
71
+ @names = names
53
72
  end
54
73
 
55
- # The ways through the card: its top rows' pairs, one after
56
- # another, those that go on and those that leave.
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.
57
77
  def total
58
78
  tops = @children.fetch(nil, []).map { |key| pair_of(key) }
59
- CardWays.sequence(tops).sum
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
60
91
  end
61
92
 
62
- # The pair of the row keyed key: a site's adds up its paths'
93
+ # The pair of the row keyed key: a decision's adds up its paths'
63
94
  # pairs; any other row is a return or a raise, as no other is
64
- # a row, so leaves.
95
+ # a row, so leaves, at its one path.
65
96
  def pair_of(key)
97
+ return fork_pair(key) unless @rows.key?(key)
98
+
66
99
  row = @rows[key]
67
- return [0, 1] unless row.site
100
+ leaves = [@ways.zero, @ways.unit([key, 0])]
101
+ return leaves if row.decision.nil?
68
102
 
69
- pairs = possible(row.site).each_index.map do |path|
103
+ pairs = possible(row.decision).each_index.map do |path|
70
104
  pair_on(key, path)
71
105
  end
72
106
  added(pairs)
73
107
  end
74
108
 
75
- # The pair of the site row keyed key on its path at index
76
- # path: the rows it runs on that path, one after another, each
77
- # given that path's outcomes; every way of it leaving when the
78
- # path takes an arm that leaves.
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.
79
113
  def pair_on(key, path)
80
- outcomes = possible(@rows[key].site)[path]
114
+ outcomes = possible(@rows[key].decision)[path]
81
115
  pairs = running(key, path).map do |child|
82
116
  child_pair(child, outcomes)
83
117
  end
84
- pair = CardWays.sequence(pairs)
85
- leaving(key).include?(path) ? [0, pair.sum] : pair
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)]
86
123
  end
87
124
 
88
- # The keys of the rows the site row keyed key runs on its path
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
89
150
  # at index path, in the order they are written.
90
151
  def running(key, path)
91
152
  runs = @children.fetch(key, []).group_by do |child|
@@ -103,38 +164,33 @@ module PathCoverage
103
164
  gives = place['gives']
104
165
  return pair_of(child) if gives.nil?
105
166
 
106
- fits = [nil, outcome_of(outcomes, place)]
167
+ fits = [nil, outcomes[place['leaf']]]
168
+ none = [@ways.zero, @ways.zero]
107
169
  pairs = gives.each_index.map do |path|
108
- fits.include?(gives[path]) ? pair_on(child, path) : [0, 0]
170
+ fits.include?(gives[path]) ? pair_on(child, path) : none
109
171
  end
110
172
  added(pairs)
111
173
  end
112
174
 
113
- # The outcome on a parent's path, outcomes, of the leaf place
114
- # names.
115
- def outcome_of(outcomes, place)
116
- outcomes[place['leaf']]
117
- end
118
-
119
- # The indices of the paths of the site row keyed key that take
175
+ # The indices of the paths of the decision row keyed key that take
120
176
  # an arm whose code returns or raises.
121
177
  def leaving(key)
122
- site = @sites.fetch(@rows[key].site)
123
- regions = CardTree.regions_of(@source, key, site)
124
- left = regions.group_by do |region|
125
- region.arm && @source.byteslice(region.range).match?(LEAVES)
126
- end
127
- left.fetch(true, []).flat_map(&:on)
178
+ decision = @decisions.fetch(@rows[key].decision)
179
+ regions = CardTree.regions_of(@source, key, decision)
180
+ regions.select(&:leaves).flat_map(&:on)
128
181
  end
129
182
 
130
- # pairs added up, each part on its own.
183
+ # pairs added up, each part on its own; no ways for no pairs.
131
184
  def added(pairs)
132
- pairs.reduce([0, 0]) { |(a, b), (c, d)| [a + c, b + d] }
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
133
189
  end
134
190
 
135
- # The possible paths of the site named site.
136
- def possible(site)
137
- SitePaths.possible(@sites.fetch(site))
191
+ # The possible paths of the decision named decision.
192
+ def possible(decision)
193
+ DecisionPaths.possible(@decisions.fetch(decision))
138
194
  end
139
195
  end
140
196
  end
@@ -1,11 +1,10 @@
1
- require 'cgi/escape'
2
1
  require 'prism'
3
2
 
4
3
  module PathCoverage
5
4
  # The colours of a file's code on its page: each token Prism
6
5
  # lexes, as Ruby itself does, gets the class of its kind, eg k
7
6
  # for a keyword, so the page's CSS colours it.
8
- module HtmlSyntax
7
+ module CodeTokens
9
8
  # The class of each coloured token type that is not a keyword
10
9
  # or a number: c comment, s string, t constant, v instance
11
10
  # variable, o an operator that decides a path.
@@ -116,52 +115,55 @@ module PathCoverage
116
115
  end
117
116
  end
118
117
 
119
- # The names of the operators that decide a path, as a site
118
+ # The names of the operators that decide a path, as a decision
120
119
  # tree's labels write them. Alone, some lex as other tokens,
121
120
  # eg ?: as a character and || as two pipes, so each is
122
121
  # coloured as an operator by name.
123
122
  DECIDING = %w[&& || &. ?: &&= ||=].freeze
124
123
 
125
- # text, a fragment of code on one line, eg a site tree's
126
- # operator or operand, escaped and coloured as the code it is
127
- # part of; one of DECIDING as the operator it names.
128
- def self.fragment(text)
129
- if DECIDING.include?(text)
130
- return %(<span class="o">#{CGI.escapeHTML(text)}</span>)
131
- end
124
+ # Example: x && y as { text: 'x && y', tokens: [[2, 4, 'o']] }.
125
+ # text, a fragment of code on one line, eg a decision tree's
126
+ # operator or operand, as page data holds it: its text and the
127
+ # [from, to, class] of each range coloured as the code it is
128
+ # part of; one of DECIDING wholly the operator it names.
129
+ def self.code(text)
130
+ return { text:, tokens: [[0, text.length, 'o']] } if
131
+ DECIDING.include?(text)
132
132
 
133
- html(text, of(text).fetch(1, []))
133
+ { text:, tokens: of(text).fetch(1, []) }
134
134
  end
135
135
 
136
- # Example: if/else as <span class="k">if</span>/<span
137
- # class="k">else</span>.
138
- # A row's or a fold's label, coloured as fragment colours it;
139
- # but a label naming a choice and its else, eg if/else, each of
140
- # its words, as code would read /else as a regexp's start.
141
- def self.label(text)
142
- return fragment(text) unless text.match?(%r{\A\w+/else\z})
136
+ # Example: if/else, each of if and else a keyword.
137
+ # The code of a row's or a fold's label, coloured as code
138
+ # colours it; but a label naming a choice and its else, eg
139
+ # if/else, each of its words, as code would read /else as a
140
+ # regexp's start.
141
+ def self.label_code(text)
142
+ return code(text) unless text.match?(%r{\A\w+/else\z})
143
143
 
144
- text.split('/').map { fragment(_1) }.join('/')
144
+ words = text.split('/').map { code(_1) }
145
+ words.reduce { |one, other| joined_code(one, '/', other) }
145
146
  end
146
147
 
147
- # A ternary's arm's label, eg ? total > 100, as HTML: its ? or
148
- # : coloured as an operator, the code after it as code.
149
- def self.ternary_arm(label)
148
+ # Example: ? total > 100, its ? coloured as an operator.
149
+ # The code of a ternary's arm's label: its ? or : coloured as
150
+ # an operator, the code after it as code.
151
+ def self.ternary_arm_code(label)
150
152
  mark, code = label.split(' ', 2)
151
- %(<span class="o">#{mark}</span> #{fragment(code.to_s)})
153
+ marked = { text: mark, tokens: [[0, mark.length, 'o']] }
154
+ joined_code(marked, ' ', code(code.to_s))
152
155
  end
153
156
 
154
- # text, escaped, each of tokens' ranges, [[from, to, class],
155
- # ...], in a span of its class.
156
- def self.html(text, tokens)
157
- at = 0
158
- parts = tokens.map do |from, to, css|
159
- plain = CGI.escapeHTML(text[at...from])
160
- at = to
161
- token = CGI.escapeHTML(text[from...to])
162
- %(#{plain}<span class="#{css}">#{token}</span>)
157
+ # Example: if, / and else give if/else, its else's ranges moved
158
+ # on by 3.
159
+ # The code of one, then between, uncoloured, then other.
160
+ def self.joined_code(one, between, other)
161
+ shift = one[:text].length + between.length
162
+ moved = other[:tokens].map do |from, to, css|
163
+ [from + shift, to + shift, css]
163
164
  end
164
- parts.join + CGI.escapeHTML(text[at..])
165
+ { text: one[:text] + between + other[:text],
166
+ tokens: one[:tokens] + moved }
165
167
  end
166
168
  end
167
169
  end