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,162 @@
1
+ require 'prism'
2
+ require_relative 'condition'
3
+
4
+ module PathCoverage
5
+ # The leaves of a flow whose outcomes cannot differ within one
6
+ # call: each a read of a local variable that its method never
7
+ # writes, eg a parameter, where the leaf's outcome is the
8
+ # variable's truthiness. Every such read in a call has the same
9
+ # outcome, so a way needing two outcomes of it is no way.
10
+ module StableLeaves
11
+ # The operators whose leaf operands' outcomes are their
12
+ # truthiness. A when's leaf is whether its pattern matched, a
13
+ # &. receiver's whether it was nil, false included in not nil,
14
+ # and a block method's leaves count items or answers.
15
+ TRUTHY = (%w[&& and || or] + CHOICES).freeze
16
+
17
+ # The calls through which code can reach a method's locals
18
+ # and rewrite them out of its sight: a binding, a string eval,
19
+ # or any call by name, which could be either.
20
+ REWRITERS = %i[
21
+ binding eval instance_eval class_eval module_eval
22
+ send __send__ public_send method
23
+ ].freeze
24
+
25
+ # Example: { 32...33 => :a, 54...55 => :a } for def f(a),
26
+ # return 0 if a, then a ? 1 : 2.
27
+ # { leaf byte range => local name } for each truthy leaf of
28
+ # the decisions of rows, a flow's in source, decisions being
29
+ # { decision => Condition }, that reads, outside any block, a
30
+ # local its def never writes, for each name two or more such
31
+ # leaves read; none for a flow in no def, or one whose def
32
+ # calls any of REWRITERS.
33
+ def self.of(source, decisions, rows)
34
+ conditions = decisions.values_at(*rows.map(&:decision).compact)
35
+ ranges = conditions.flat_map { truthy_leaves(_1) }
36
+ return {} if ranges.empty?
37
+
38
+ method = def_of(source, ranges.first)
39
+ return {} if method.nil?
40
+
41
+ shared(read_in(method).slice(*ranges))
42
+ end
43
+
44
+ # The leaves of condition, a leaf a byte range, whose outcome
45
+ # is their truthiness, its nested conditions' included.
46
+ def self.truthy_leaves(condition)
47
+ operands = condition.operands
48
+ own = TRUTHY.include?(condition.operator) ? operands : []
49
+ own.grep(Range) + operands.flat_map { inner_leaves(_1) }
50
+ end
51
+
52
+ # The truthy leaves of operand, a leaf, a Condition or an Arm:
53
+ # none for a leaf, which its condition counts, or a plain arm.
54
+ def self.inner_leaves(operand)
55
+ case operand
56
+ when Condition then truthy_leaves(operand)
57
+ when Arm then operand.body ? truthy_leaves(operand.body) : []
58
+ else []
59
+ end
60
+ end
61
+
62
+ # The innermost def in source holding the bytes range, nil
63
+ # when none does.
64
+ def self.def_of(source, range)
65
+ defs = defs_in(Prism.parse(source).value)
66
+ holding = defs.select do |node|
67
+ loc = node.location
68
+ (loc.start_offset...loc.end_offset).cover?(range)
69
+ end
70
+ holding.min_by { _1.location.length }
71
+ end
72
+
73
+ # Every def node in node, node itself included.
74
+ def self.defs_in(node)
75
+ own = node.type == :def_node ? [node] : []
76
+ own + node.compact_child_nodes.flat_map { defs_in(_1) }
77
+ end
78
+
79
+ # { read byte range => local name } of each read in the def
80
+ # node method, outside any block, of a local it never writes;
81
+ # none when it calls any of REWRITERS.
82
+ def self.read_in(method)
83
+ reads = Reads.new
84
+ reads.visit_child_nodes(method)
85
+ return {} if reads.rewrites
86
+
87
+ written = reads.written
88
+ reads.reads.each_with_object({}) do |(range, name), kept|
89
+ kept[range] = name unless written.include?(name)
90
+ end
91
+ end
92
+
93
+ # The entries of named, { range => name }, whose name two or
94
+ # more of them share.
95
+ def self.shared(named)
96
+ counts = named.values.tally
97
+ named.each_with_object({}) do |(range, name), shared|
98
+ shared[range] = name if counts[name] > 1
99
+ end
100
+ end
101
+
102
+ # Notes, in a def's body, each read of a local outside any
103
+ # block, def or lambda, where a local of the same name may be
104
+ # another variable; each local written anywhere; and whether
105
+ # any of REWRITERS is called.
106
+ class Reads < Prism::Visitor
107
+ # Starts outside any block, having noted nothing.
108
+ def initialize
109
+ @reads = {}
110
+ @written = []
111
+ @rewrites = false
112
+ @depth = 0
113
+ super
114
+ end
115
+
116
+ # { byte range => name } of each read noted; the names of
117
+ # the locals written; and true when a rewriter is called.
118
+ attr_reader :reads, :written, :rewrites
119
+
120
+ # Notes a read, outside any block.
121
+ def visit_local_variable_read_node(node)
122
+ loc = node.location
123
+ range = loc.start_offset...loc.end_offset
124
+ @reads[range] = node.name if @depth.zero?
125
+ super
126
+ end
127
+
128
+ # Notes the name of a write of a local, eg a = 1, a += 1, a
129
+ # ||= 1, or a target, eg in a, b = 1, 2 or rescue => a.
130
+ def visit_written(node)
131
+ @written << node.name
132
+ visit_child_nodes(node)
133
+ end
134
+
135
+ %i[
136
+ local_variable_write_node local_variable_operator_write_node
137
+ local_variable_or_write_node local_variable_and_write_node
138
+ local_variable_target_node
139
+ ].each do |type|
140
+ alias_method :"visit_#{type}", :visit_written
141
+ end
142
+
143
+ # Notes a call of a rewriter.
144
+ def visit_call_node(node)
145
+ @rewrites = true if REWRITERS.include?(node.name)
146
+ super
147
+ end
148
+
149
+ # Visits a block, a lambda or a def a level deeper, so its
150
+ # reads are not noted.
151
+ def visit_inner(node)
152
+ @depth += 1
153
+ visit_child_nodes(node)
154
+ @depth -= 1
155
+ end
156
+
157
+ %i[block_node lambda_node def_node].each do |type|
158
+ alias_method :"visit_#{type}", :visit_inner
159
+ end
160
+ end
161
+ end
162
+ end
@@ -1,19 +1,19 @@
1
1
  module PathCoverage
2
- # The lists of statements a method's card has rows for: a def's
3
- # body, the arms of each site in it whose arms are statements,
4
- # and the body of each block in it, as the card lays them out
5
- # flat. It relies on its includer's rows_in, site? and
6
- # site_name.
2
+ # The lists of statements a method's flow has rows for: a def's
3
+ # body, the arms of each decision in it whose arms are statements,
4
+ # and the body of each block in it, as the flow lays them out
5
+ # flat. It relies on its includer's rows_in, decision? and
6
+ # decision_name.
7
7
  module StatementArms
8
- # The nodes a statement's sites are never looked for in: a
8
+ # The nodes a statement's decisions are never looked for in: a
9
9
  # block's, a lambda's and a begin's statements are rows of
10
10
  # their own, after the statement holding them, and a def's
11
- # body runs only when it is called, its sites rows of its own
12
- # card.
11
+ # body runs only when it is called, its decisions rows of its own
12
+ # flow.
13
13
  UNSEEN = %i[block_node begin_node def_node lambda_node].freeze
14
14
 
15
15
  # Example: if/else for if a then 1 else 2 end.
16
- # operator, a site node's, with /else after it when node is an
16
+ # operator, a decision node's, with /else after it when node is an
17
17
  # if or an unless with an else, so its row names both arms;
18
18
  # operator alone for any other, eg a ?:, whose arms are ? and :.
19
19
  def with_else(operator, node)
@@ -23,14 +23,16 @@ module PathCoverage
23
23
  clause&.type == :else_node ? "#{operator}/else" : operator
24
24
  end
25
25
 
26
- # The tops of statements that end a path, leaving the method.
27
- LEAVING = %w[return raise].freeze
26
+ # The tops of statements that end a path. A return or a raise
27
+ # leaves the method. A next, a break or a retry leaves its pass
28
+ # or its loop.
29
+ LEAVING = %w[return raise next break retry].freeze
28
30
 
29
- # True for a row that shapes its method's paths: a site, or a
30
- # return or a raise, which ends a path. Any other, eg x = 1,
31
+ # True for a row that shapes its method's paths: a decision, or a
32
+ # statement in LEAVING, which ends a path. Any other, eg x = 1,
31
33
  # runs exactly where what holds it does, so is no row.
32
34
  def shapes?(node)
33
- site?(node) || LEAVING.include?(top_of(node))
35
+ decision?(node) || LEAVING.include?(top_of(node))
34
36
  end
35
37
 
36
38
  # Where node, a def, is entered: its body's start, or, for an
@@ -40,41 +42,54 @@ module PathCoverage
40
42
  body = node.body
41
43
  return node.end_keyword_loc.start_offset if body.nil?
42
44
 
43
- loc = body.location
45
+ loc = entered(body).location
44
46
  start = loc.start_offset
45
47
  node.equal_loc ? start...loc.end_offset : start
46
48
  end
47
49
 
50
+ # The node where body, a def's, is entered on every call.
51
+ # Usually that is body itself. A body with a rescue or an
52
+ # ensure has a begin that starts at the def. Its statements
53
+ # are entered instead. With no statements, its first rescue
54
+ # is, or else its ensure.
55
+ def entered(body)
56
+ return body unless body.type == :begin_node
57
+
58
+ [body.statements, body.rescue_clause, body.ensure_clause]
59
+ .compact.first
60
+ end
61
+
48
62
  # Example: return a ? 1 : 2 if x gives the ?:.
49
- # The sites inside site that are sites of their own, in the
50
- # order they are written: in its leaves, eg a modifier if's
51
- # arm or a &. call's receiver; not in its arms of statements,
52
- # nor in a body, which have rows of their own. A site is
53
- # named where it starts, so a node starting where a site
54
- # does, eg a ?:'s predicate, names that site: each is kept
55
- # once, and site itself not at all. None for a top that is no
56
- # site, eg a def inside a def, whose sites are on its own card.
57
- def inner_sites(site)
58
- return [] unless site?(site)
59
-
60
- found = heads(site).flat_map { sites_within(_1) }
61
- named = found.to_h { [site_name(_1), _1] }
62
- named.except(site_name(site)).values
63
- end
64
-
65
- # node when it is a site, then each site within it, in
63
+ # The decisions inside decision that are decisions of their
64
+ # own, in the order they are written: in its leaves, eg a
65
+ # modifier if's arm or a &. call's receiver; not in its arms
66
+ # of statements, nor in a body, which have rows of their own.
67
+ # A decision is named where it starts, so a node starting
68
+ # where a decision does, eg a ?:'s predicate, names that
69
+ # decision: each is kept once, and decision itself not at
70
+ # all. None for a top that is no decision, eg a def inside a
71
+ # def, whose decisions are on its own flow.
72
+ def inner_decisions(decision)
73
+ return [] unless decision?(decision)
74
+
75
+ found = heads(decision).flat_map { decisions_within(_1) }
76
+ named = found.to_h { [decision_name(_1), _1] }
77
+ named.except(decision_name(decision)).values
78
+ end
79
+
80
+ # node when it is a decision, then each decision within it, in
66
81
  # order; none in a body or a def.
67
- def sites_within(node)
82
+ def decisions_within(node)
68
83
  return [] if UNSEEN.include?(node.type)
69
84
 
70
- own = site?(node) ? [node] : []
71
- own + heads(node).flat_map { sites_within(_1) }
85
+ own = decision?(node) ? [node] : []
86
+ own + heads(node).flat_map { decisions_within(_1) }
72
87
  end
73
88
 
74
89
  # Example: items.map { |i| i.price } gives the price row.
75
90
  # The rows of the bodies in statement: of each block's and
76
91
  # lambda's statements, a block method's included,
77
- # as its block's sites are sites of their own, and a reach
92
+ # as its block's decisions are decisions of their own, and a reach
78
93
  # call before its one expression sits outside the
79
94
  # expression's own record call; and of each begin's parts.
80
95
  def body_rows(statement)
@@ -86,7 +101,7 @@ module PathCoverage
86
101
  # The body of each block and lambda in node, run 0 or more
87
102
  # times, and each begin: none inside an arm of statements or
88
103
  # another body, whose rows are their own, nor in a def, whose
89
- # rows are its own card's.
104
+ # rows are its own flow's.
90
105
  def bodies_in(node)
91
106
  case node.type
92
107
  when :def_node then []
@@ -108,7 +123,7 @@ module PathCoverage
108
123
 
109
124
  # The statements of node's arms, then's before else's; none
110
125
  # when node has no arms of statements, as a modifier's arm,
111
- # and a ?:'s, are part of the site's own expression.
126
+ # and a ?:'s, are part of the decision's own expression.
112
127
  def arms_of(node)
113
128
  case node.type
114
129
  when :if_node then if_arms(node)
@@ -1,57 +1,8 @@
1
1
  require_relative 'configuration'
2
- require_relative 'totals'
3
2
 
4
3
  module PathCoverage
5
- # The per-file numbers the HTML index page shows, and their
6
- # total over every file.
4
+ # Which file a decision is in, and whether a file is in a group.
7
5
  module Summary
8
- # One row per file that has sites in site_paths, as
9
- # Result#paths gives them, in path order: its sites, and
10
- # its decisions total, and its methods total, its defs' cards
11
- # in cards, { def site => [taken, size] }, added up.
12
- def self.files(site_paths, cards)
13
- by_path = site_paths.group_by { |site, _| path_of(site) }
14
- by_path.sort.map do |path, sites|
15
- {
16
- path:, sites: sites.size,
17
- decisions: Totals.decisions(sites.to_h),
18
- methods: methods_of(path, cards)
19
- }
20
- end
21
- end
22
-
23
- # The methods total of the file at path, its defs' cards
24
- # added up; [0, 0] for a file with no def.
25
- def self.methods_of(path, cards)
26
- Totals.methods(cards.select { |site, _| path_of(site) == path })
27
- end
28
-
29
- # The sum of each column over rows; no rows, as for a group
30
- # no file is in, gives 0 sites and [0, 0] totals.
31
- def self.total(rows)
32
- {
33
- sites: rows.sum { |row| row[:sites] },
34
- decisions: add(rows.map { |row| row[:decisions] }),
35
- methods: add(rows.map { |row| row[:methods] })
36
- }
37
- end
38
-
39
- # [name, rows, total] for each of groups, { name => [glob,
40
- # ...] }, in order: the rows whose path matches any of its
41
- # globs, and their total. Then Ungrouped, the rows no group
42
- # matches, when there are any.
43
- def self.groups(rows, groups)
44
- grouped = groups.map do |name, globs|
45
- own = rows.select { |row| matches?(row[:path], globs) }
46
- [name, own, total(own)]
47
- end
48
- every = groups.values.flatten
49
- rest = rows.reject { |row| matches?(row[:path], every) }
50
- return grouped if rest.empty?
51
-
52
- grouped << ['Ungrouped', rest, total(rest)]
53
- end
54
-
55
6
  # True when path matches any of globs.
56
7
  def self.matches?(path, globs)
57
8
  globs.any? do |glob|
@@ -59,18 +10,9 @@ module PathCoverage
59
10
  end
60
11
  end
61
12
 
62
- # [taken, possible] pairs added together, eg [[1, 3], [2, 4]]
63
- # gives [3, 7]; no pairs, as for a group no file is in, gives
64
- # [0, 0].
65
- def self.add(pairs)
66
- pairs.reduce([0, 0]) do |(taken, possible), (more, of)|
67
- [taken + more, possible + of]
68
- end
69
- end
70
-
71
- # The file a site is in: its name minus :line:column.
72
- def self.path_of(site)
73
- site.sub(/:\d+:\d+\z/, '')
13
+ # The file a decision is in: its name minus :line:column.
14
+ def self.path_of(decision)
15
+ decision.sub(/:\d+:\d+\z/, '')
74
16
  end
75
17
  end
76
18
  end
@@ -3,19 +3,21 @@ module PathCoverage
3
3
  # every covered file. They count the very paths the gap
4
4
  # lines list as never taken.
5
5
  module Totals
6
- # Paths within each decision: site_paths, as Result#paths
7
- # gives them, lists every possible path of each site with
8
- # its count; one with a count above zero was taken.
9
- def self.decisions(site_paths)
10
- every = site_paths.values.flatten
6
+ # Paths alone, each decision's counted on its own:
7
+ # decision_paths, as Result#paths gives them, lists every
8
+ # possible path of each decision with its count; one with a
9
+ # count above zero was taken.
10
+ def self.alone(decision_paths)
11
+ every = decision_paths.values.flatten
11
12
  [every.count { |each| each[:count].positive? }, every.size]
12
13
  end
13
14
 
14
- # Example: cards [4, 6] and [17, 21] give [21, 27].
15
- # Paths within each method: cards is { def site => [taken,
16
- # size] }, each method's card's paths taken of its size.
17
- def self.methods(cards)
18
- cards.values.reduce([0, 0]) do |(taken, possible), (more, of)|
15
+ # Example: flows [4, 6] and [17, 21] give [21, 27].
16
+ # Paths combined through each method: flows is { def decision =>
17
+ # [taken, size] }, each method's flow's paths taken of its
18
+ # size, its rows' paths added up.
19
+ def self.combined(flows)
20
+ flows.values.reduce([0, 0]) do |(taken, possible), (more, of)|
19
21
  [taken + more, possible + of]
20
22
  end
21
23
  end
@@ -1,3 +1,3 @@
1
1
  module PathCoverage
2
- VERSION = '0.4.0'.freeze
2
+ VERSION = '0.6.0'.freeze
3
3
  end
@@ -0,0 +1,106 @@
1
+ require_relative 'condition'
2
+
3
+ module PathCoverage
4
+ # The way one call is taking through its method, kept as a stack
5
+ # of units: the call's own, then each loop under way, innermost
6
+ # last. A step goes to the innermost loop holding its row,
7
+ # closing first any loop under way that does not hold it. Each
8
+ # pass of a loop is a way of its own, counted under the loop;
9
+ # the unit holding the loop takes it as one step: how many
10
+ # passes it made, 0, 1 or 2+, and the set of ways they took. So
11
+ # a way holds each row at most once, however many passes a loop
12
+ # makes.
13
+ class Walk
14
+ # A call, or a loop under way, named by its entry or its loop,
15
+ # with the steps of its way so far, or of its latest pass, how
16
+ # many passes it has made, and the distinct ways of those ended.
17
+ Unit = Struct.new(:name, :steps, :passes, :seen)
18
+
19
+ # A call of the method whose entry is named entry, just
20
+ # entered, counting each way it ends on ways, { unit's name =>
21
+ # { way => count } }; loops is { row's name => the names of
22
+ # the loops holding it, outermost first }, and repeats { row's
23
+ # name => the while or until whose test holds it }.
24
+ def initialize(entry, ways, loops, repeats = {})
25
+ @ways = ways
26
+ @loops = loops
27
+ @repeats = repeats
28
+ @units = [unit(entry)]
29
+ end
30
+
31
+ # The name of the entry of the method being called.
32
+ def entry = @units.first.name
33
+
34
+ # Adds step, a row's [name, ...], to the way of the innermost
35
+ # loop under way holding its row, or the call's own; but no
36
+ # step of a row in the test of a while under way, as each test
37
+ # after its first repeats its first.
38
+ def step(step)
39
+ name = step[0]
40
+ return if @units.map(&:name).include?(@repeats[name])
41
+
42
+ close_to(@loops[name])
43
+ @units.last.steps << step
44
+ end
45
+
46
+ # Starts a pass of the loop named loop, counting its pass
47
+ # before when there is one.
48
+ def passed(loop)
49
+ opened = opened(loop)
50
+ if opened.passes.positive?
51
+ count(opened)
52
+ opened.steps = []
53
+ end
54
+ opened.passes += 1
55
+ end
56
+
57
+ # Ends the loop named loop, its items all given.
58
+ def exhausted(loop)
59
+ opened(loop)
60
+ close
61
+ end
62
+
63
+ # Ends the call, closing every loop under way, and counts its
64
+ # way.
65
+ def finish
66
+ close_to([])
67
+ count(@units.first)
68
+ end
69
+
70
+ private
71
+
72
+ # A unit named name, with no step or pass yet.
73
+ def unit(name) = Unit.new(name, [], 0, Set.new)
74
+
75
+ # The unit of the loop named loop, innermost, any loop under
76
+ # way inside it closed; a new one when it is not under way.
77
+ def opened(loop)
78
+ close_to([*@loops[loop], loop])
79
+ @units << unit(loop) unless @units.last.name == loop
80
+ @units.last
81
+ end
82
+
83
+ # Closes each loop under way, innermost first, until one of
84
+ # holding, the loops holding a row, is innermost.
85
+ def close_to(holding)
86
+ close while @units.size > 1 && holding.none?(@units.last.name)
87
+ end
88
+
89
+ # Ends the innermost loop under way: counts its last pass, if
90
+ # it made one, and adds it to the way holding it as one step.
91
+ def close
92
+ ended = @units.pop
93
+ count(ended) if ended.passes.positive?
94
+ items = ITEMS[[ended.passes, 2].min]
95
+ @units.last.steps << [ended.name, [items], ended.seen]
96
+ end
97
+
98
+ # Counts unit's steps as one more taking of its way, noting it
99
+ # among the ways seen.
100
+ def count(unit)
101
+ way = unit.steps.dup.freeze
102
+ (@ways[unit.name] ||= Hash.new(0))[way] += 1
103
+ unit.seen << way
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,127 @@
1
+ module PathCoverage
2
+ # The algebras PathsWays can fold a flow's ways in, each giving
3
+ # no ways, the one way through no rows, the one way taking a
4
+ # labelled path, and ways added and joined one after another.
5
+ # A way's label at a row is [row key, path index].
6
+ module WayAlgebras
7
+ # Ways as how many there are.
8
+ module Counted
9
+ # No ways.
10
+ def self.zero = 0
11
+
12
+ # The one way through no rows.
13
+ def self.one = 1
14
+
15
+ # The one way that takes the path label names.
16
+ def self.unit(_label) = 1
17
+
18
+ # The ways of ways, then those of more.
19
+ def self.plus(ways, more) = ways + more
20
+
21
+ # Each way of ways going on to each way of more.
22
+ def self.times(ways, more) = ways * more
23
+ end
24
+
25
+ # Ways as the labels taken on any of them, so a row's paths
26
+ # that no way takes are those whose labels are missing.
27
+ module Labelled
28
+ # No labels.
29
+ def self.zero = []
30
+
31
+ # The one way through no rows, taking no label.
32
+ def self.one = []
33
+
34
+ # The one way that takes the path label names.
35
+ def self.unit(label) = [label]
36
+
37
+ # The labels of ways or of more.
38
+ def self.plus(ways, more) = ways | more
39
+
40
+ # The labels of each way of ways going on to each of more:
41
+ # every one of either, as PathsWays never joins no ways.
42
+ def self.times(ways, more) = ways | more
43
+ end
44
+
45
+ # Ways as a list of them, each the labels it takes.
46
+ module Listed
47
+ # No ways.
48
+ def self.zero = []
49
+
50
+ # The one way through no rows, taking no label.
51
+ def self.one = [[]]
52
+
53
+ # The one way that takes the path label names.
54
+ def self.unit(label) = [[label]]
55
+
56
+ # The ways of ways, then those of more.
57
+ def self.plus(ways, more) = ways + more
58
+
59
+ # Each way of ways going on to each way of more, its labels
60
+ # then theirs.
61
+ def self.times(ways, more)
62
+ ways.product(more).map { |before, after| before + after }
63
+ end
64
+ end
65
+
66
+ # Ways in an algebra, each noting an assignment, { local name =>
67
+ # outcome }, as a tally, { assignment => ways in the algebra }.
68
+ # Ways whose assignments disagree, giving a name both outcomes,
69
+ # join into no way, as no call can take them.
70
+ class Tallied
71
+ # Ways in algebra, one of the modules above.
72
+ def initialize(algebra)
73
+ @algebra = algebra
74
+ end
75
+
76
+ # No ways.
77
+ def zero = {}
78
+
79
+ # The one way through no rows, assigning nothing.
80
+ def one = { {} => @algebra.one }
81
+
82
+ # The one way that takes the path label names, with
83
+ # assignment.
84
+ def unit(label, assignment = {})
85
+ { assignment => @algebra.unit(label) }
86
+ end
87
+
88
+ # The ways of tally, then those of more, those of one
89
+ # assignment added up.
90
+ def plus(tally, more)
91
+ tally.merge(more) { |_, ways, others| add(ways, others) }
92
+ end
93
+
94
+ # Each way of tally going on to each way of more, but for
95
+ # those whose assignments disagree.
96
+ def times(tally, more)
97
+ pairs = tally.to_a.product(more.to_a)
98
+ pairs.reduce(zero) do |all, ((was, ways), (now, others))|
99
+ next all unless agree?(was, now)
100
+
101
+ joint = @algebra.times(ways, others)
102
+ plus(all, { was.merge(now) => joint })
103
+ end
104
+ end
105
+
106
+ # The ways of tally, whatever their assignments, in the
107
+ # algebra.
108
+ def total(tally)
109
+ each = tally.values
110
+ each.reduce(@algebra.zero) { |all, ways| add(all, ways) }
111
+ end
112
+
113
+ private
114
+
115
+ # The ways of ways, then those of others, in the algebra.
116
+ def add(ways, others)
117
+ @algebra.plus(ways, others)
118
+ end
119
+
120
+ # True when the assignments was and now give no name both
121
+ # outcomes.
122
+ def agree?(was, now)
123
+ was.all? { |name, value| now.fetch(name, value) == value }
124
+ end
125
+ end
126
+ end
127
+ end