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
@@ -0,0 +1,100 @@
1
+ require_relative 'code_tokens'
2
+ require_relative 'fold_rows'
3
+ require_relative 'decision_locations'
4
+ require_relative 'decision_tree'
5
+ require_relative 'source_lines'
6
+ require_relative 'summary'
7
+
8
+ module PathCoverage
9
+ # What a renderer needs of each covered file beyond the JSON
10
+ # report's counts, worked out from its source, so it can draw
11
+ # the file's page without parsing the source itself.
12
+ module SourceReport
13
+ # The version of the source report's shape.
14
+ SCHEMA = 1
15
+
16
+ # The URL of the schema the source report follows.
17
+ SCHEMA_URL = 'https://path-coverage.github.io/schemas/v1/' \
18
+ 'path_coverage.source.schema.json'.freeze
19
+
20
+ # The source report of a run, headed by its schema's URL and
21
+ # version: config's ignored operators, the
22
+ # covered files of each of config's groups, each top of the
23
+ # rows of cards, { def decision => card }, coloured, and each of
24
+ # files', { full path => {source:, decisions:} }, lines and
25
+ # decisions, each decision's paths as decision_paths has
26
+ # them, its fold headed by its row's top.
27
+ def self.of(files, config, decision_paths, cards)
28
+ rows = cards.values.flat_map { _1['rows'] }
29
+ tops = rows.to_h { [_1['decision'], _1['top']] }
30
+ shown = files.to_h do |full_path, parsed|
31
+ [config.relative(full_path),
32
+ file(parsed, decision_paths, tops)]
33
+ end
34
+ { '$schema': SCHEMA_URL, schema: SCHEMA,
35
+ ignored: config.ignored, groups: groups(config, shown.keys),
36
+ labels: labels(rows), files: shown }
37
+ end
38
+
39
+ # { top => it coloured } of each distinct top of rows, a
40
+ # decision's or a statement's, eg return.
41
+ def self.labels(rows)
42
+ rows.map { _1['top'] }.uniq.to_h do |top|
43
+ [top, CodeTokens.label_code(top)]
44
+ end
45
+ end
46
+
47
+ # { name => the paths its globs match } of each of config's
48
+ # groups.
49
+ def self.groups(config, paths)
50
+ config.groups.transform_values do |globs|
51
+ paths.select { Summary.matches?(_1, globs) }
52
+ end
53
+ end
54
+
55
+ # One file's lines and decisions, parsed being its {source:,
56
+ # decisions:}.
57
+ def self.file(parsed, decision_paths, tops)
58
+ source, decisions = parsed.values_at(:source, :decisions)
59
+ shown = decisions.keys.to_h do |name|
60
+ paths = decision_paths.fetch(name)
61
+ [name,
62
+ decision(source, decisions, name, paths, tops.fetch(name))]
63
+ end
64
+ { lines: lines(source), decisions: shown }
65
+ end
66
+
67
+ # Example: "ok?(1)\n" gives [{ text: 'ok?(1)', tokens:
68
+ # [[4, 5, 'n']] }].
69
+ # Each line of source: its text and its coloured tokens.
70
+ def self.lines(source)
71
+ tokens = CodeTokens.of(source)
72
+ source.lines.each_with_index.map do |text, index|
73
+ { text: text.chomp, tokens: tokens.fetch(index + 1, []) }
74
+ end
75
+ end
76
+
77
+ # Example: ok?(id) && new?(id) on line 2 gives span [2, 2].
78
+ # The decision named name, of decisions, { name => Condition }, in
79
+ # source: the [first, last] lines it spans, and its fold's
80
+ # rows, headed by top, its row's top, a column for each of
81
+ # paths, [{path:, count:}], in their order, with no counts.
82
+ def self.decision(source, decisions, name, paths, top)
83
+ condition = decisions.fetch(name)
84
+ last = SourceLines.end_line(source, condition.leaves.last)
85
+ span = [SourceLines.line_of(name), last]
86
+ { span:, fold: fold(source, condition, paths, top) }
87
+ end
88
+
89
+ # The rows of condition's fold, headed by top, its decision row
90
+ # with no counts.
91
+ def self.fold(source, condition, paths, top)
92
+ outcomes = paths.map { _1[:path] }
93
+ tree = DecisionTree.built(source, condition, outcomes)
94
+ located = DecisionLocations.located(source, condition, tree)
95
+ else_at = DecisionLocations.else_at(source, condition)
96
+ own, *rest = FoldRows.rows(located.merge(else_at:, label: top))
97
+ [own.except(:counts), *rest]
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,162 @@
1
+ require 'prism'
2
+ require_relative 'condition'
3
+
4
+ module PathCoverage
5
+ # The leaves of a card 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 card'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 card 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
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,
3
+ # body, the arms of each decision in it whose arms are statements,
4
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.
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
11
+ # body runs only when it is called, its decisions rows of its own
12
12
  # card.
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)
@@ -26,11 +26,11 @@ module PathCoverage
26
26
  # The tops of statements that end a path, leaving the method.
27
27
  LEAVING = %w[return raise].freeze
28
28
 
29
- # True for a row that shapes its method's paths: a site, or a
29
+ # True for a row that shapes its method's paths: a decision, or a
30
30
  # return or a raise, which ends a path. Any other, eg x = 1,
31
31
  # runs exactly where what holds it does, so is no row.
32
32
  def shapes?(node)
33
- site?(node) || LEAVING.include?(top_of(node))
33
+ decision?(node) || LEAVING.include?(top_of(node))
34
34
  end
35
35
 
36
36
  # Where node, a def, is entered: its body's start, or, for an
@@ -46,35 +46,36 @@ module PathCoverage
46
46
  end
47
47
 
48
48
  # 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
49
+ # The decisions inside decision that are decisions of their
50
+ # own, in the order they are written: in its leaves, eg a
51
+ # modifier if's arm or a &. call's receiver; not in its arms
52
+ # of statements, nor in a body, which have rows of their own.
53
+ # A decision is named where it starts, so a node starting
54
+ # where a decision does, eg a ?:'s predicate, names that
55
+ # decision: each is kept once, and decision itself not at
56
+ # all. None for a top that is no decision, eg a def inside a
57
+ # def, whose decisions are on its own card.
58
+ def inner_decisions(decision)
59
+ return [] unless decision?(decision)
60
+
61
+ found = heads(decision).flat_map { decisions_within(_1) }
62
+ named = found.to_h { [decision_name(_1), _1] }
63
+ named.except(decision_name(decision)).values
64
+ end
65
+
66
+ # node when it is a decision, then each decision within it, in
66
67
  # order; none in a body or a def.
67
- def sites_within(node)
68
+ def decisions_within(node)
68
69
  return [] if UNSEEN.include?(node.type)
69
70
 
70
- own = site?(node) ? [node] : []
71
- own + heads(node).flat_map { sites_within(_1) }
71
+ own = decision?(node) ? [node] : []
72
+ own + heads(node).flat_map { decisions_within(_1) }
72
73
  end
73
74
 
74
75
  # Example: items.map { |i| i.price } gives the price row.
75
76
  # The rows of the bodies in statement: of each block's and
76
77
  # lambda's statements, a block method's included,
77
- # as its block's sites are sites of their own, and a reach
78
+ # as its block's decisions are decisions of their own, and a reach
78
79
  # call before its one expression sits outside the
79
80
  # expression's own record call; and of each begin's parts.
80
81
  def body_rows(statement)
@@ -108,7 +109,7 @@ module PathCoverage
108
109
 
109
110
  # The statements of node's arms, then's before else's; none
110
111
  # when node has no arms of statements, as a modifier's arm,
111
- # and a ?:'s, are part of the site's own expression.
112
+ # and a ?:'s, are part of the decision's own expression.
112
113
  def arms_of(node)
113
114
  case node.type
114
115
  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,18 +3,20 @@ 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
15
  # 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)
16
+ # Paths combined through each method: cards is { def decision =>
17
+ # [taken, size] }, each method's card's paths taken of its
18
+ # size, its rows' paths added up.
19
+ def self.combined(cards)
18
20
  cards.values.reduce([0, 0]) do |(taken, possible), (more, of)|
19
21
  [taken + more, possible + of]
20
22
  end
@@ -1,3 +1,3 @@
1
1
  module PathCoverage
2
- VERSION = '0.4.0'.freeze
2
+ VERSION = '0.5.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