path_coverage 0.7.1 → 0.8.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 (64) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +45 -19
  3. data/lib/path_coverage/call_trace.rb +1 -1
  4. data/lib/path_coverage/calls.rb +35 -8
  5. data/lib/path_coverage/condition.rb +35 -9
  6. data/lib/path_coverage/current_test.rb +28 -0
  7. data/lib/path_coverage/decision.rb +7 -0
  8. data/lib/path_coverage/decision_sites.rb +40 -0
  9. data/lib/path_coverage/decision_tree.rb +11 -21
  10. data/lib/path_coverage/evaluated.rb +23 -0
  11. data/lib/path_coverage/fold_rows.rb +7 -8
  12. data/lib/path_coverage/gaps.rb +4 -4
  13. data/lib/path_coverage/json_reader.rb +22 -17
  14. data/lib/path_coverage/json_reports.rb +5 -3
  15. data/lib/path_coverage/json_writer.rb +12 -8
  16. data/lib/path_coverage/kernel.rb +11 -3
  17. data/lib/path_coverage/kinds/base.rb +9 -0
  18. data/lib/path_coverage/kinds/block.rb +7 -1
  19. data/lib/path_coverage/kinds/case.rb +63 -20
  20. data/lib/path_coverage/kinds/chain.rb +13 -3
  21. data/lib/path_coverage/kinds/choice.rb +5 -0
  22. data/lib/path_coverage/kinds/contains.rb +6 -0
  23. data/lib/path_coverage/kinds/safe.rb +3 -3
  24. data/lib/path_coverage/kinds/write.rb +3 -3
  25. data/lib/path_coverage/kinds.rb +14 -0
  26. data/lib/path_coverage/loop_nodes.rb +15 -10
  27. data/lib/path_coverage/manifest.rb +61 -35
  28. data/lib/path_coverage/manifest_case.rb +43 -26
  29. data/lib/path_coverage/manifest_folding.rb +20 -0
  30. data/lib/path_coverage/merge.rb +8 -8
  31. data/lib/path_coverage/method_statements.rb +41 -61
  32. data/lib/path_coverage/minitest.rb +7 -0
  33. data/lib/path_coverage/minitest_tests.rb +25 -0
  34. data/lib/path_coverage/paths_context.rb +2 -2
  35. data/lib/path_coverage/paths_forks.rb +6 -3
  36. data/lib/path_coverage/paths_leaving.rb +3 -1
  37. data/lib/path_coverage/paths_regions.rb +78 -0
  38. data/lib/path_coverage/paths_rescues.rb +5 -2
  39. data/lib/path_coverage/paths_scopes.rb +2 -1
  40. data/lib/path_coverage/paths_tree.rb +10 -68
  41. data/lib/path_coverage/paths_way_lists.rb +11 -10
  42. data/lib/path_coverage/recorder.rb +28 -47
  43. data/lib/path_coverage/recorder_blocks.rb +3 -3
  44. data/lib/path_coverage/recorder_cases.rb +70 -17
  45. data/lib/path_coverage/recorder_chains.rb +2 -2
  46. data/lib/path_coverage/render.rb +7 -3
  47. data/lib/path_coverage/report_tallies.rb +79 -0
  48. data/lib/path_coverage/report_text.rb +23 -0
  49. data/lib/path_coverage/rescue_forks.rb +3 -0
  50. data/lib/path_coverage/result.rb +17 -16
  51. data/lib/path_coverage/rewriter.rb +42 -114
  52. data/lib/path_coverage/rewriter_marks.rb +108 -0
  53. data/lib/path_coverage/source_report.rb +5 -6
  54. data/lib/path_coverage/stable_leaves.rb +6 -26
  55. data/lib/path_coverage/statement_arms.rb +23 -25
  56. data/lib/path_coverage/statement_tops.rb +53 -0
  57. data/lib/path_coverage/tally.rb +129 -0
  58. data/lib/path_coverage/testing.rb +13 -0
  59. data/lib/path_coverage/totals.rb +3 -3
  60. data/lib/path_coverage/version.rb +1 -1
  61. data/lib/path_coverage/walk.rb +4 -3
  62. data/lib/path_coverage/ways_report.rb +11 -10
  63. data/lib/path_coverage/within_methods.rb +12 -3
  64. metadata +13 -1
@@ -1,5 +1,6 @@
1
1
  require_relative 'condition'
2
2
  require_relative 'kinds'
3
+ require_relative 'rewriter_marks'
3
4
  require_relative 'rewriter_nested'
4
5
 
5
6
  module PathCoverage
@@ -9,110 +10,15 @@ module PathCoverage
9
10
  module Rewriter
10
11
  # Returns source with each operand in decisions wrapped in a
11
12
  # record call, each decision as a whole in a leave call that
12
- # enters it first, then the inserts of marks, as mark_inserts
13
- # gives them. Only text is inserted, never a newline, so
14
- # backtrace line numbers still match the file on disk.
13
+ # enters it first, then the inserts of marks, as
14
+ # RewriterMarks.inserts gives them. Only text is inserted, never
15
+ # a newline, so backtrace line numbers still match the file on
16
+ # disk.
15
17
  def self.rewrite(source, decisions, marks = {})
16
18
  inserts = decisions.flat_map do |decision, condition|
17
19
  decision_inserts(decision, condition)
18
20
  end
19
- apply(source.dup, inserts + mark_inserts(marks))
20
- end
21
-
22
- # The inserts of marks, a hash. Each key is optional, and a
23
- # missing key marks nothing. reaches is { name => offset }:
24
- # each statement there gets a reach call before it, counting
25
- # its runs. passes is { name => offset }: each loop body there
26
- # gets a pass call first. entries is { name => offset }: each
27
- # method's entry is reached there, outermost, before any
28
- # statement's reach at the same place. completions is { rescue
29
- # fork's name => where }: each body completing is noted there.
30
- # rescues is { [fork's name, number] => offset }: each rescue
31
- # running is noted there. values is { fork's name => byte
32
- # range }: each rescue modifier's rescue running is noted
33
- # there.
34
- def self.mark_inserts(marks)
35
- reach_inserts(marks.fetch(:reaches, {})) +
36
- pass_inserts(marks.fetch(:passes, {})) +
37
- reach_inserts(marks.fetch(:entries, {}), :entry) +
38
- completed_inserts(marks.fetch(:completions, {})) +
39
- rescued_inserts(marks.fetch(:rescues, {})) +
40
- ran_inserts(marks.fetch(:values, {}))
41
- end
42
-
43
- # Example: { "k.rb:3:18" => 25...26 } wraps bytes 25...26.
44
- # The inserts that wrap the rescue of each rescue modifier in
45
- # values, { fork's name => its byte range }, in a ran call. The
46
- # call keeps the rescue's value, and notes that its rescue 1
47
- # ran.
48
- def self.ran_inserts(values)
49
- values.flat_map do |name, range|
50
- rank = [range.size, LAYERS.index(:completed)]
51
- call = "PathCoverage.ran(#{name.inspect}, 1, ("
52
- [[range.begin, call, rank], [range.end, '))', rank, :close]]
53
- end
54
- end
55
-
56
- # Example: { ["k.rb:4:2", 1] => 52 } puts a rescued call at 52.
57
- # The inserts that put a rescued call after the header of each
58
- # rescue in rescues, { [fork's name, its number] => offset }.
59
- # The call is a statement of its own, the first its rescue
60
- # runs.
61
- def self.rescued_inserts(rescues)
62
- rank = [Float::INFINITY, LAYERS.index(:reach)]
63
- rescues.map do |(name, number), at|
64
- call = "; PathCoverage.rescued(#{name.inspect}, #{number})"
65
- [at, call, rank]
66
- end
67
- end
68
-
69
- # Example: { "k.rb:4:2" => 30...43 } wraps bytes 30...43.
70
- # The inserts that note each body of completions completing.
71
- # completions is { rescue fork's name => where }. A byte range
72
- # is a last statement, wrapped in a completed call that keeps
73
- # its value, inside any reach before it. A byte offset is an
74
- # empty body. It gets a completed call of nil, a statement of
75
- # its own, because the body's value is nil either way.
76
- def self.completed_inserts(completions)
77
- completions.flat_map do |name, at|
78
- call = "PathCoverage.completed(#{name.inspect}, "
79
- next [[at, "#{call}nil); ", own_rank]] unless at.is_a?(Range)
80
-
81
- rank = [at.size, LAYERS.index(:completed)]
82
- [[at.begin, "#{call}(", rank], [at.end, '))', rank, :close]]
83
- end
84
- end
85
-
86
- # The rank of a completed call that is a statement of its own,
87
- # for an empty body. It is inside any reach at the same place.
88
- def self.own_rank = [Float::INFINITY, LAYERS.index(:completed)]
89
-
90
- # Example: { "k.rb:4:16" => 60 } puts a pass call at 60.
91
- # The inserts that put a pass call, a statement of its own,
92
- # first in each loop body in passes, { name => offset }. It
93
- # comes before the reach of a row at the same place, so the
94
- # row is a step of the pass.
95
- def self.pass_inserts(passes)
96
- rank = [Float::INFINITY, LAYERS.index(:pass)]
97
- passes.map do |name, at|
98
- [at, "PathCoverage.pass(#{name.inspect}); ", rank]
99
- end
100
- end
101
-
102
- # Example: { "k.rb:2:2" => 18 } puts a reach call at 18.
103
- # The inserts that put a reach call, a statement of its
104
- # own, before each statement in reaches, outside all else
105
- # there, in layer. A statement reached by
106
- # its byte range, eg an endless def's body, is wrapped in
107
- # parentheses with its call, being one expression.
108
- def self.reach_inserts(reaches, layer = :reach)
109
- rank = [Float::INFINITY, LAYERS.index(layer)]
110
- reaches.flat_map do |name, at|
111
- call = "PathCoverage.reach(#{name.inspect}); "
112
- next [[at, call, rank]] unless at.is_a?(Range)
113
-
114
- [[at.begin, "(#{call}", rank], [at.end, ')', rank, :close]]
115
- end
21
+ apply(source.dup, inserts + RewriterMarks.inserts(marks))
116
22
  end
117
23
 
118
24
  # The inserts for one decision, as its kind gives them, eg its
@@ -135,11 +41,20 @@ module PathCoverage
135
41
  call = nested.fetch(range) { record_call(decision, i) }
136
42
  next [] unless call
137
43
 
138
- wrap(call, range, condition.commands.include?(range), :leaf)
44
+ wrap(call, range, passing(condition, range), :leaf)
139
45
  end
140
46
  records + RewriterNested.inserts(decision, condition)
141
47
  end
142
48
 
49
+ # How the leaf of condition at range is passed to its call, as
50
+ # FORMS has it: tested for a flip-flop, grouped for a command
51
+ # call, else bare.
52
+ def self.passing(condition, range)
53
+ return :tested if condition.flip_flops.include?(range)
54
+
55
+ condition.commands.include?(range) ? :grouped : :bare
56
+ end
57
+
143
58
  WORD_OPERATORS = %w[and or].freeze
144
59
 
145
60
  # The inserts that wrap decision as a whole in a leave call,
@@ -154,7 +69,8 @@ module PathCoverage
154
69
  wrapped = Kinds.of(condition.operator).trailed(condition)
155
70
  nested = wrapped.is_a?(Condition)
156
71
  word = nested && WORD_OPERATORS.include?(wrapped.operator)
157
- wrap(call, nested ? wrapped.extent : wrapped, word, :decision)
72
+ form = word ? :grouped : :bare
73
+ wrap(call, nested ? wrapped.extent : wrapped, form, :decision)
158
74
  end
159
75
 
160
76
  # The opening of the record call around operand index of
@@ -167,18 +83,30 @@ module PathCoverage
167
83
  # between inserts wrapping the same range: a leaf's call, a
168
84
  # nested kind's call around its whole, eg end_items, a decision's
169
85
  # leave, a rescue body's last statement's completed, a
170
- # statement's reach, a loop's pass, and a method's entry.
171
- LAYERS = %i[leaf kind decision completed reach pass entry].freeze
172
-
173
- # The two inserts that wrap the code at range in call. Their
174
- # order, [size, rank], nests them among inserts at the same
175
- # offset: the longer range outside, the rank of layer in
176
- # LAYERS breaking a tie. A command call is parenthesised, as
177
- # it is no bare argument; nothing else is, since parentheses
178
- # make a statement, which gives its line a line-coverage
179
- # count of its own.
180
- def self.wrap(call, range, command, layer)
181
- open, close = command ? ['(', '))'] : ['', ')']
86
+ # statement's reach, a case arm's took, a loop's pass, and a
87
+ # method's entry.
88
+ LAYERS = %i[
89
+ leaf kind decision completed reach arm pass entry
90
+ ].freeze
91
+
92
+ # The ways a wrap passes the code at its range to its call:
93
+ # bare; grouped, in parentheses, as code that is no bare
94
+ # argument must be, eg a command call; or tested, as a ternary's
95
+ # predicate, as a flip-flop must be, which only a condition
96
+ # keeps one, the leaf being tested for truth either way. Only
97
+ # these two add parentheses, which make a statement, giving its
98
+ # line a line-coverage count of its own.
99
+ FORMS = {
100
+ bare: ['', ')'], grouped: ['(', '))'],
101
+ tested: ['(', ') ? true : false)']
102
+ }.freeze
103
+
104
+ # The two inserts that wrap the code at range in call, passed
105
+ # in form, one of FORMS. Their order, [size, rank], nests them
106
+ # among inserts at the same offset: the longer range outside,
107
+ # the rank of layer in LAYERS breaking a tie.
108
+ def self.wrap(call, range, form, layer)
109
+ open, close = FORMS.fetch(form)
182
110
  order = [range.size, LAYERS.index(layer)]
183
111
  [
184
112
  [range.begin, call + open, order],
@@ -0,0 +1,108 @@
1
+ module PathCoverage
2
+ # The part of the rewriter that marks statements, as against
3
+ # decisions: each statement's reach, each loop pass, each
4
+ # method's entry, and each rescue fork's body completing or rescue
5
+ # running. Each insert is ranked among those at the same offset by
6
+ # Rewriter::LAYERS.
7
+ module RewriterMarks
8
+ # The inserts of marks, a hash. Each key is optional, and a
9
+ # missing key marks nothing. reaches is { name => offset }:
10
+ # each statement there gets a reach call before it, counting
11
+ # its runs. passes is { name => offset }: each loop body there
12
+ # gets a pass call first. entries is { name => offset }: each
13
+ # method's entry is reached there, outermost, before any
14
+ # statement's reach at the same place. completions is { rescue
15
+ # fork's name => where }: each body completing is noted there.
16
+ # rescues is { [fork's name, number] => offset }: each rescue
17
+ # running is noted there. values is { fork's name => byte
18
+ # range }: each rescue modifier's rescue running is noted
19
+ # there.
20
+ def self.inserts(marks)
21
+ reach_inserts(marks.fetch(:reaches, {})) +
22
+ pass_inserts(marks.fetch(:passes, {})) +
23
+ reach_inserts(marks.fetch(:entries, {}), :entry) +
24
+ completed_inserts(marks.fetch(:completions, {})) +
25
+ rescued_inserts(marks.fetch(:rescues, {})) +
26
+ ran_inserts(marks.fetch(:values, {}))
27
+ end
28
+
29
+ # The rank of an insert of size in layer, one of
30
+ # Rewriter::LAYERS.
31
+ def self.rank(size, layer)
32
+ [size, Rewriter::LAYERS.index(layer)]
33
+ end
34
+
35
+ # Example: { "k.rb:3:18" => 25...26 } wraps bytes 25...26.
36
+ # The inserts that wrap the rescue of each rescue modifier in
37
+ # values, { fork's name => its byte range }, in a ran call. The
38
+ # call keeps the rescue's value, and notes that its rescue 1
39
+ # ran.
40
+ def self.ran_inserts(values)
41
+ values.flat_map do |name, range|
42
+ order = rank(range.size, :completed)
43
+ call = "PathCoverage.ran(#{name.inspect}, 1, ("
44
+ [[range.begin, call, order], [range.end, '))', order, :close]]
45
+ end
46
+ end
47
+
48
+ # Example: { ["k.rb:4:2", 1] => 52 } puts a rescued call at 52.
49
+ # The inserts that put a rescued call after the header of each
50
+ # rescue in rescues, { [fork's name, its number] => offset }.
51
+ # The call is a statement of its own, the first its rescue
52
+ # runs.
53
+ def self.rescued_inserts(rescues)
54
+ order = rank(Float::INFINITY, :reach)
55
+ rescues.map do |(name, number), at|
56
+ call = "; PathCoverage.rescued(#{name.inspect}, #{number})"
57
+ [at, call, order]
58
+ end
59
+ end
60
+
61
+ # Example: { "k.rb:4:2" => 30...43 } wraps bytes 30...43.
62
+ # The inserts that note each body of completions completing.
63
+ # completions is { rescue fork's name => where }. A byte range
64
+ # is a last statement, wrapped in a completed call that keeps
65
+ # its value, inside any reach before it. A byte offset is an
66
+ # empty body. It gets a completed call of nil, a statement of
67
+ # its own, inside any reach at the same place, because the
68
+ # body's value is nil either way.
69
+ def self.completed_inserts(completions)
70
+ completions.flat_map do |name, at|
71
+ call = "PathCoverage.completed(#{name.inspect}, "
72
+ own = rank(Float::INFINITY, :completed)
73
+ next [[at, "#{call}nil); ", own]] unless at.is_a?(Range)
74
+
75
+ order = rank(at.size, :completed)
76
+ [[at.begin, "#{call}(", order], [at.end, '))', order, :close]]
77
+ end
78
+ end
79
+
80
+ # Example: { "k.rb:4:16" => 60 } puts a pass call at 60.
81
+ # The inserts that put a pass call, a statement of its own,
82
+ # first in each loop body in passes, { name => offset }. It
83
+ # comes before the reach of a row at the same place, so the
84
+ # row is a step of the pass.
85
+ def self.pass_inserts(passes)
86
+ order = rank(Float::INFINITY, :pass)
87
+ passes.map do |name, at|
88
+ [at, "PathCoverage.pass(#{name.inspect}); ", order]
89
+ end
90
+ end
91
+
92
+ # Example: { "k.rb:2:2" => 18 } puts a reach call at 18.
93
+ # The inserts that put a reach call, a statement of its
94
+ # own, before each statement in reaches, outside all else
95
+ # there, in layer. A statement reached by
96
+ # its byte range, eg an endless def's body, is wrapped in
97
+ # parentheses with its call, being one expression.
98
+ def self.reach_inserts(reaches, layer = :reach)
99
+ order = rank(Float::INFINITY, layer)
100
+ reaches.flat_map do |name, at|
101
+ call = "PathCoverage.reach(#{name.inspect}); "
102
+ next [[at, call, order]] unless at.is_a?(Range)
103
+
104
+ [[at.begin, "(#{call}", order], [at.end, ')', order, :close]]
105
+ end
106
+ end
107
+ end
108
+ end
@@ -14,7 +14,7 @@ module PathCoverage
14
14
  SCHEMA = 1
15
15
 
16
16
  # The URL of the schema the source report follows.
17
- SCHEMA_URL = 'https://path-coverage.github.io/schemas/v2/' \
17
+ SCHEMA_URL = 'https://path-coverage.github.io/schemas/v3/' \
18
18
  'path_coverage.source.schema.json'.freeze
19
19
 
20
20
  # The source report of a run, headed by its schema's URL and
@@ -78,7 +78,7 @@ module PathCoverage
78
78
  # The decision named name, of decisions, { name => Condition }, in
79
79
  # source: the [first, last] lines it spans, and its fold's
80
80
  # rows, headed by top, its row's top, a column for each of
81
- # paths, [{path:, count:}], in their order, with no counts.
81
+ # paths, [{path:, tests:}], in their order.
82
82
  def self.decision(source, decisions, name, paths, top)
83
83
  condition = decisions.fetch(name)
84
84
  last = SourceLines.end_line(source, condition.leaves.last)
@@ -86,15 +86,14 @@ module PathCoverage
86
86
  { span:, fold: fold(source, condition, paths, top) }
87
87
  end
88
88
 
89
- # The rows of condition's fold, headed by top, its decision row
90
- # with no counts.
89
+ # The rows of condition's fold, headed by top, a column for each
90
+ # of paths.
91
91
  def self.fold(source, condition, paths, top)
92
92
  outcomes = paths.map { _1[:path] }
93
93
  tree = DecisionTree.built(source, condition, outcomes)
94
94
  located = DecisionLocations.located(source, condition, tree)
95
95
  else_at = DecisionLocations.else_at(source, condition)
96
- own, *rest = FoldRows.rows(located.merge(else_at:, label: top))
97
- [own.except(:counts), *rest]
96
+ FoldRows.rows(located.merge(else_at:, label: top))
98
97
  end
99
98
  end
100
99
  end
@@ -1,5 +1,7 @@
1
1
  require 'prism'
2
2
  require_relative 'condition'
3
+ require_relative 'evaluated'
4
+ require_relative 'kinds'
3
5
 
4
6
  module PathCoverage
5
7
  # The leaves of a flow whose outcomes cannot differ within one
@@ -8,12 +10,6 @@ module PathCoverage
8
10
  # variable's truthiness. Every such read in a call has the same
9
11
  # outcome, so a way needing two outcomes of it is no way.
10
12
  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
13
  # The calls through which code can reach a method's locals
18
14
  # and rewrite them out of its sight: a binding, a string eval,
19
15
  # or any call by name, which could be either.
@@ -32,7 +28,7 @@ module PathCoverage
32
28
  # calls any of REWRITERS.
33
29
  def self.of(source, decisions, rows)
34
30
  conditions = decisions.values_at(*rows.map(&:decision).compact)
35
- ranges = conditions.flat_map { truthy_leaves(_1) }
31
+ ranges = conditions.flat_map { Kinds.truthy_leaves(_1) }
36
32
  return {} if ranges.empty?
37
33
 
38
34
  method = def_of(source, ranges.first)
@@ -41,24 +37,6 @@ module PathCoverage
41
37
  shared(read_in(method).slice(*ranges))
42
38
  end
43
39
 
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
40
  # The innermost def in source holding the bytes range, nil
63
41
  # when none does.
64
42
  def self.def_of(source, range)
@@ -73,7 +51,7 @@ module PathCoverage
73
51
  # Every def node in node, node itself included.
74
52
  def self.defs_in(node)
75
53
  own = node.type == :def_node ? [node] : []
76
- own + node.compact_child_nodes.flat_map { defs_in(_1) }
54
+ own + Evaluated.children(node).flat_map { defs_in(_1) }
77
55
  end
78
56
 
79
57
  # { read byte range => local name } of each read in the def
@@ -104,6 +82,8 @@ module PathCoverage
104
82
  # another variable; each local written anywhere; and whether
105
83
  # any of REWRITERS is called.
106
84
  class Reads < Prism::Visitor
85
+ include Evaluated::Visitor
86
+
107
87
  # Starts outside any block, having noted nothing.
108
88
  def initialize
109
89
  @reads = {}
@@ -1,9 +1,11 @@
1
+ require_relative 'evaluated'
2
+
1
3
  module PathCoverage
2
4
  # The lists of statements a method's flow has rows for: a def's
3
5
  # body, the arms of each decision in it whose arms are statements,
4
6
  # and the body of each block in it, as the flow lays them out
5
7
  # flat. It relies on its includer's rows_in, decision? and
6
- # decision_name.
8
+ # top_of.
7
9
  module StatementArms
8
10
  # The nodes a statement's decisions are never looked for in: a
9
11
  # block's, a lambda's and a begin's statements are rows of
@@ -12,17 +14,6 @@ module PathCoverage
12
14
  # flow.
13
15
  UNSEEN = %i[block_node begin_node def_node lambda_node].freeze
14
16
 
15
- # Example: if/else for if a then 1 else 2 end.
16
- # operator, a decision node's, with /else after it when node is an
17
- # if or an unless with an else, so its row names both arms;
18
- # operator alone for any other, eg a ?:, whose arms are ? and :.
19
- def with_else(operator, node)
20
- return operator unless %w[if unless].include?(operator)
21
-
22
- clause = operator == 'if' ? node.subsequent : node.else_clause
23
- clause&.type == :else_node ? "#{operator}/else" : operator
24
- end
25
-
26
17
  # The tops of statements that end a path. A return or a raise
27
18
  # leaves the method. A next, a break or a retry leaves its pass
28
19
  # or its loop.
@@ -64,17 +55,13 @@ module PathCoverage
64
55
  # own, in the order they are written: in its leaves, eg a
65
56
  # modifier if's arm or a &. call's receiver; not in its arms
66
57
  # 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.
58
+ # Each is its own site node, found once. None for a top that
59
+ # is no decision, eg a def inside a def, whose decisions are
60
+ # on its own flow.
72
61
  def inner_decisions(decision)
73
62
  return [] unless decision?(decision)
74
63
 
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
64
+ heads(decision).flat_map { decisions_within(_1) }
78
65
  end
79
66
 
80
67
  # node when it is a decision, then each decision within it, in
@@ -111,14 +98,25 @@ module PathCoverage
111
98
  end
112
99
  end
113
100
 
114
- # node's children outside its arms of statements: all of
115
- # them for a node with none, else its predicate, and a
116
- # case's patterns too.
101
+ # node's children outside its arms of statements: all those
102
+ # that can run for a node with none, so none of a defined?'s,
103
+ # else its predicate; for a case, its subject, when it has one,
104
+ # and its patterns.
117
105
  def heads(node)
118
- return node.compact_child_nodes if arms_of(node).empty?
106
+ return Evaluated.children(node) if arms_of(node).empty?
119
107
  return [node.predicate] unless node.type == :case_node
120
108
 
121
- [node.predicate, *node.conditions.flat_map(&:conditions)]
109
+ node.compact_child_nodes.flat_map { case_heads(_1) }
110
+ end
111
+
112
+ # The heads among child, a child of a case: a when's patterns,
113
+ # none of its else, or else child itself, its subject.
114
+ def case_heads(child)
115
+ case child.type
116
+ when :when_node then child.conditions
117
+ when :else_node then []
118
+ else [child]
119
+ end
122
120
  end
123
121
 
124
122
  # The statements of node's arms, then's before else's; none
@@ -0,0 +1,53 @@
1
+ require_relative 'leaving'
2
+
3
+ module PathCoverage
4
+ # What each statement of a method's flow is as written, the top
5
+ # its row is named by, eg if/else or =. It relies on its
6
+ # includer's decision_at.
7
+ module StatementTops
8
+ TOPS = { call_node: '()', def_node: 'def' }.freeze
9
+
10
+ # What statement is as written: a decision's operator, eg if,
11
+ # when statement is the site of a decision, an elsif
12
+ # being elsif, as written, an if or unless with an else
13
+ # if/else or unless/else; else = for a write, Leaving's word,
14
+ # return or raise, for a statement that leaves the method,
15
+ # () for a call, def for a def inside it, value for anything
16
+ # else.
17
+ def top_of(statement)
18
+ decision = decision_at(statement)
19
+ return 'elsif' if decision && elsif?(statement)
20
+ return with_else(decision.operator, statement) if decision
21
+ return '=' if write?(statement)
22
+
23
+ word = Leaving.word(statement)
24
+ return word if word
25
+
26
+ TOPS.fetch(statement.type, 'value')
27
+ end
28
+
29
+ # Example: if/else for if a then 1 else 2 end.
30
+ # operator, a decision node's, with /else after it when node is an
31
+ # if or an unless with an else, so its row names both arms;
32
+ # operator alone for any other, eg a ?:, whose arms are ? and :.
33
+ def with_else(operator, node)
34
+ return operator unless %w[if unless].include?(operator)
35
+
36
+ clause = operator == 'if' ? node.subsequent : node.else_clause
37
+ clause&.type == :else_node ? "#{operator}/else" : operator
38
+ end
39
+
40
+ # True for an if written as an elsif.
41
+ def elsif?(statement)
42
+ statement.type == :if_node && statement.if_keyword == 'elsif'
43
+ end
44
+
45
+ # True for a write of any kind: of a variable, eg @x = 1
46
+ # or x += 1, or a call Ruby makes of an index or attribute
47
+ # write, eg a[i] = x or a.b = x.
48
+ def write?(statement)
49
+ statement.type.end_with?('_write_node') ||
50
+ (statement.type == :call_node && statement.attribute_write?)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,129 @@
1
+ require 'digest'
2
+
3
+ module PathCoverage
4
+ # How often something happened, by test: every count of what a
5
+ # run did is a Tally, { test id => n }, so a report can tell which
6
+ # tests take which paths. Its total is the sum over its tests,
7
+ # never stored beside it. A Tally is no Integer, so a count left
8
+ # unconverted anywhere fails loudly rather than miscounting. The
9
+ # JSON report lists a tally by its tests' keys, each the first 12
10
+ # hex digits of the sha256 of the test's id, and names each key's
11
+ # test once, in its tests.
12
+ class Tally
13
+ # The id of what runs outside every test, eg a file's load.
14
+ NO_TEST = '(no test)'.freeze
15
+
16
+ # How many hex digits of a test id's sha256 its key keeps.
17
+ KEY_DIGITS = 12
18
+
19
+ # The tally of by_test, { test id => n }; ArgumentError naming
20
+ # a test whose n is below 1.
21
+ def initialize(by_test = {})
22
+ @by_test = by_test.dup
23
+ @by_test.each do |test, count|
24
+ raise ArgumentError, "#{test} counted #{count}" if count < 1
25
+ end
26
+ end
27
+
28
+ # { test id => n } of the tests that made it happen.
29
+ attr_reader :by_test
30
+
31
+ # Counts one more happening, made by test.
32
+ def add(test)
33
+ @by_test[test] = @by_test.fetch(test, 0) + 1
34
+ end
35
+
36
+ # How often it happened, over every test.
37
+ def total
38
+ @by_test.values.sum
39
+ end
40
+
41
+ # True when no test made it happen.
42
+ def empty?
43
+ @by_test.empty?
44
+ end
45
+
46
+ # The tally of self's happenings and other's, test by test.
47
+ def +(other)
48
+ Tally.new(@by_test.merge(other.by_test) { |_, a, b| a + b })
49
+ end
50
+
51
+ # True when other, a Tally, counts the same tests, each the same
52
+ # number of times. A bare count is no Tally, so comparing one
53
+ # fails loudly rather than coming out quietly unequal.
54
+ def ==(other)
55
+ @by_test == other.by_test
56
+ end
57
+
58
+ # Tally({ test id => n }), as it is built.
59
+ def inspect
60
+ "Tally(#{@by_test.inspect})"
61
+ end
62
+
63
+ # { key => Tally } that starts each key's Tally empty the first
64
+ # time the key is read, as a run counts on every event.
65
+ def self.per_key
66
+ Hash.new { |tallies, key| tallies[key] = Tally.new }
67
+ end
68
+
69
+ # The key the JSON report lists test's counts under.
70
+ def self.key(test)
71
+ Digest::SHA256.hexdigest(test)[0, KEY_DIGITS]
72
+ end
73
+
74
+ # { key => test id } of tests, as the JSON report's tests lists
75
+ # them; ArgumentError when two share a key, as joined refuses.
76
+ def self.named(tests)
77
+ joined(tests.map { { key(_1) => _1 } })
78
+ end
79
+
80
+ # { key => test id } of maps, each a report's tests, joined;
81
+ # ArgumentError naming a key and both ids when maps give it
82
+ # two, as their counts would be added as one test's.
83
+ def self.joined(maps)
84
+ maps.each_with_object({}) do |each, found|
85
+ each.each do |at, test|
86
+ known = found.fetch(at) { found[at] = test }
87
+ next if known == test
88
+
89
+ raise ArgumentError,
90
+ "key #{at} names both #{known} and #{test}"
91
+ end
92
+ end
93
+ end
94
+
95
+ # report, of Hashes, Arrays and values, with every Tally in it
96
+ # listed, and 'tests' naming the key of each test those tallies
97
+ # count, in id order: the one place a report's tests come from,
98
+ # so its tests and its tallies always agree.
99
+ def self.jsonable(report)
100
+ ids = []
101
+ body = listed_in(report, ids)
102
+ body.merge('tests' => named(ids.uniq.sort))
103
+ end
104
+
105
+ # value with every Tally in it listed, each Tally's tests added
106
+ # to ids.
107
+ def self.listed_in(value, ids)
108
+ case value
109
+ when Tally
110
+ ids.concat(value.by_test.keys)
111
+ listed(value)
112
+ when Hash then value.transform_values { listed_in(_1, ids) }
113
+ when Array then value.map { listed_in(_1, ids) }
114
+ else value
115
+ end
116
+ end
117
+
118
+ # { key => n } of counted, a Tally, as the JSON report lists it.
119
+ def self.listed(counted)
120
+ counted.by_test.transform_keys { key(_1) }
121
+ end
122
+
123
+ # The Tally of listed, { key => n } as listed gives it, tests
124
+ # being the report's { key => test id }.
125
+ def self.read(listed, tests)
126
+ Tally.new(listed.transform_keys { tests.fetch(_1) })
127
+ end
128
+ end
129
+ end