path_coverage 0.7.2 → 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 (61) 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 +24 -6
  6. data/lib/path_coverage/current_test.rb +28 -0
  7. data/lib/path_coverage/decision_tree.rb +11 -21
  8. data/lib/path_coverage/evaluated.rb +23 -0
  9. data/lib/path_coverage/fold_rows.rb +7 -8
  10. data/lib/path_coverage/gaps.rb +4 -4
  11. data/lib/path_coverage/json_reader.rb +22 -17
  12. data/lib/path_coverage/json_reports.rb +5 -3
  13. data/lib/path_coverage/json_writer.rb +12 -8
  14. data/lib/path_coverage/kernel.rb +11 -3
  15. data/lib/path_coverage/kinds/base.rb +9 -0
  16. data/lib/path_coverage/kinds/block.rb +7 -1
  17. data/lib/path_coverage/kinds/case.rb +63 -20
  18. data/lib/path_coverage/kinds/chain.rb +13 -3
  19. data/lib/path_coverage/kinds/choice.rb +5 -0
  20. data/lib/path_coverage/kinds/contains.rb +6 -0
  21. data/lib/path_coverage/kinds/safe.rb +3 -3
  22. data/lib/path_coverage/kinds/write.rb +3 -3
  23. data/lib/path_coverage/kinds.rb +14 -0
  24. data/lib/path_coverage/loop_nodes.rb +5 -3
  25. data/lib/path_coverage/manifest.rb +57 -33
  26. data/lib/path_coverage/manifest_case.rb +43 -26
  27. data/lib/path_coverage/manifest_folding.rb +20 -0
  28. data/lib/path_coverage/merge.rb +8 -8
  29. data/lib/path_coverage/method_statements.rb +5 -4
  30. data/lib/path_coverage/minitest.rb +7 -0
  31. data/lib/path_coverage/minitest_tests.rb +25 -0
  32. data/lib/path_coverage/paths_context.rb +2 -2
  33. data/lib/path_coverage/paths_forks.rb +6 -3
  34. data/lib/path_coverage/paths_leaving.rb +3 -1
  35. data/lib/path_coverage/paths_regions.rb +78 -0
  36. data/lib/path_coverage/paths_rescues.rb +5 -2
  37. data/lib/path_coverage/paths_scopes.rb +2 -1
  38. data/lib/path_coverage/paths_tree.rb +10 -68
  39. data/lib/path_coverage/paths_way_lists.rb +11 -10
  40. data/lib/path_coverage/recorder.rb +28 -47
  41. data/lib/path_coverage/recorder_blocks.rb +3 -3
  42. data/lib/path_coverage/recorder_cases.rb +70 -17
  43. data/lib/path_coverage/recorder_chains.rb +2 -2
  44. data/lib/path_coverage/render.rb +7 -3
  45. data/lib/path_coverage/report_tallies.rb +79 -0
  46. data/lib/path_coverage/report_text.rb +23 -0
  47. data/lib/path_coverage/rescue_forks.rb +3 -0
  48. data/lib/path_coverage/result.rb +17 -16
  49. data/lib/path_coverage/rewriter.rb +42 -114
  50. data/lib/path_coverage/rewriter_marks.rb +108 -0
  51. data/lib/path_coverage/source_report.rb +5 -6
  52. data/lib/path_coverage/stable_leaves.rb +6 -26
  53. data/lib/path_coverage/statement_arms.rb +18 -5
  54. data/lib/path_coverage/tally.rb +129 -0
  55. data/lib/path_coverage/testing.rb +13 -0
  56. data/lib/path_coverage/totals.rb +3 -3
  57. data/lib/path_coverage/version.rb +1 -1
  58. data/lib/path_coverage/walk.rb +4 -3
  59. data/lib/path_coverage/ways_report.rb +11 -10
  60. data/lib/path_coverage/within_methods.rb +12 -3
  61. metadata +11 -1
@@ -1,24 +1,77 @@
1
1
  module PathCoverage
2
- # The part of the recorder that notes a case nested in another
3
- # decision, eg (case a when 1 then b end) && c: the decision
4
- # around it enters and leaves, so its patterns only note
5
- # whether they matched. It relies on its includer's record.
2
+ # The part of the recorder that notes a case's patterns. Ruby
3
+ # itself matches each pattern against the subject, or tests it
4
+ # for truth in a case with no subject, in the code's own frame;
5
+ # so no pattern's outcome is seen when it is matched. Instead
6
+ # tried notes each pattern reached, which tells the pattern
7
+ # reached before it did not match; took, first in a when arm,
8
+ # tells the pattern last reached matched; missed, after the last
9
+ # pattern, tells it did not; and settled, around the whole case,
10
+ # tells a pattern still pending matched, its when arm having no
11
+ # statements. A case that is a decision of its own is decided in
12
+ # took and missed, before an arm that may leave runs; one nested
13
+ # in another decision's leaf only notes, for that decision to
14
+ # decide. It relies on its includer's trails, record and leave.
6
15
  module RecorderCases
7
- # A stand-in for the pattern of a case nested in decision, its
8
- # match noted as the decision's index-th leaf.
9
- NestedMatch = Struct.new(:recorder, :decision, :index,
10
- :pattern) do
11
- # Whether pattern matches subject, exactly as pattern says,
12
- # noted on the decision's path.
13
- def ===(subject)
14
- recorder.record(decision, index, pattern === subject)
15
- end
16
+ # Notes the index-th leaf of decision reached, a pattern whose
17
+ # outcome is pending, the pattern pending before it not having
18
+ # matched; returns pattern unchanged. decision is always under
19
+ # way: a case is entered before its subject, and a case in a
20
+ # leaf runs inside its decision's evaluation.
21
+ def tried(decision, index, pattern)
22
+ trail = trails(decision).last
23
+ pending(trail, false)
24
+ trail.pending = index
25
+ pattern
16
26
  end
17
27
 
18
- # The stand-in for pattern, the index-th leaf of decision, in a
19
- # case nested in it.
20
- def pattern(decision, index, pattern)
21
- NestedMatch.new(self, decision, index, pattern)
28
+ # Notes that the pattern pending in decision, a case of its own,
29
+ # matched, and decides it, as its arm is about to run.
30
+ def took(decision)
31
+ trail = trails(decision).last
32
+ pending(trail, true)
33
+ leave(trail, nil)
34
+ end
35
+
36
+ # Notes that the pattern pending in decision, a case of its own,
37
+ # did not match, and decides it; returns no patterns.
38
+ def missed(decision)
39
+ trail = trails(decision).last
40
+ pending(trail, false)
41
+ leave(trail, nil)
42
+ []
43
+ end
44
+
45
+ # Notes that the pattern pending in decision, around a case in
46
+ # its leaf, matched.
47
+ def took_within(decision)
48
+ pending(trails(decision).last, true)
49
+ nil
50
+ end
51
+
52
+ # Notes that the pattern pending in decision, around a case in
53
+ # its leaf, did not match; returns no patterns.
54
+ def missed_within(decision)
55
+ pending(trails(decision).last, false)
56
+ []
57
+ end
58
+
59
+ # Notes that a pattern still pending in decision when its case
60
+ # ends matched, as only a when arm with no statements leaves it
61
+ # so; returns value, the case's, unchanged.
62
+ def settled(decision, value)
63
+ pending(trails(decision).last, true)
64
+ value
65
+ end
66
+
67
+ # Notes outcome as that of the pattern pending on trail, if
68
+ # any, which is then pending no more.
69
+ def pending(trail, outcome)
70
+ index = trail&.pending
71
+ return if index.nil?
72
+
73
+ record(trail.decision, index, outcome)
74
+ trail.pending = nil
22
75
  end
23
76
  end
24
77
  end
@@ -2,7 +2,7 @@ require_relative 'condition'
2
2
 
3
3
  module PathCoverage
4
4
  # The part of the recorder that notes &. chains. It relies on
5
- # its includer's counts, record and trails.
5
+ # its includer's count, record and trails.
6
6
  module RecorderChains
7
7
  # Notes value, the index-th leaf of decision, a &. receiver, as
8
8
  # not nil or nil, and returns it unchanged; it counts truthy
@@ -12,7 +12,7 @@ module PathCoverage
12
12
  def present(decision, index, first, value)
13
13
  return value unless reached?(decision, first, index)
14
14
 
15
- @counts[[decision, index]][value.nil? ? :falsy : :truthy] += 1
15
+ count([decision, index], value.nil? ? :falsy : :truthy)
16
16
  trail = trails(decision).last
17
17
  trail.outcomes[index] = PRESENCE[value.nil? ? 1 : 0] if trail
18
18
  value
@@ -6,6 +6,8 @@ require_relative 'json_reports'
6
6
  require_relative 'manifest'
7
7
  require_relative 'paths_report'
8
8
  require_relative 'post'
9
+ require_relative 'report_text'
10
+ require_relative 'tally'
9
11
  require_relative 'decision_paths'
10
12
  require_relative 'within_methods'
11
13
 
@@ -65,13 +67,15 @@ module PathCoverage
65
67
 
66
68
  # decision_paths with every decision in files that it lacks, eg
67
69
  # from a JSON report with no paths, given each possible
68
- # path at a count of zero: unable to say which paths such
70
+ # path with an empty Tally: unable to say which paths such
69
71
  # a decision took, the report claims none.
70
72
  def self.every_decision(decision_paths, files)
71
73
  none = files.values.each_with_object({}) do |parsed, all|
72
74
  parsed[:decisions].each do |decision, condition|
73
75
  possible = DecisionPaths.possible(condition)
74
- all[decision] = possible.map { |path| { path:, count: 0 } }
76
+ all[decision] = possible.map do |path|
77
+ { path:, tests: Tally.new }
78
+ end
75
79
  end
76
80
  end
77
81
  none.merge(decision_paths)
@@ -90,7 +94,7 @@ module PathCoverage
90
94
  lines = Gaps.lines(operands, paths:, texts:, outside:)
91
95
  ruled = PathsContext.ruled_out(files, config, paths, reached)
92
96
  lines += Gaps.ruled_by_method(ruled, texts)
93
- lines.each { |line| puts line }
97
+ ReportText.utf8(lines).each { |line| puts line }
94
98
  end
95
99
 
96
100
  # Runs the reporters chosen in config from the JSON reports
@@ -0,0 +1,79 @@
1
+ require_relative 'tally'
2
+
3
+ module PathCoverage
4
+ # Reads back the tallies of a JSON report, the inverse of
5
+ # Tally.jsonable: each place the schema puts a tally, { key => n },
6
+ # is read as a Tally by the report's tests, { key => test id }.
7
+ # Each place is named, as an empty tally and an empty list both
8
+ # read {}; a place a report leaves out stays out.
9
+ module ReportTallies
10
+ # The top-level keys of a report that hold tallies.
11
+ PLACES = %w[decisions paths reached outside flows].freeze
12
+
13
+ # The keys of a flow whose lists' items each have tests.
14
+ TAKEN_LISTS = %w[taken covered].freeze
15
+
16
+ # json, a JSON report as parsed, with every tally in it a Tally.
17
+ def self.restored(json)
18
+ tests = json.fetch('tests')
19
+ read = ->(listed) { Tally.read(listed, tests) }
20
+ PLACES.each_with_object(json.dup) do |place, restored|
21
+ next unless json.key?(place)
22
+
23
+ restored[place] = send(:"#{place}_of", json[place], read)
24
+ end
25
+ end
26
+
27
+ # A report's decisions, each operand's truthy and falsy read.
28
+ def self.decisions_of(decisions, read)
29
+ decisions.transform_values do |leaves|
30
+ leaves.map do |leaf|
31
+ leaf.merge('truthy' => read.call(leaf['truthy']),
32
+ 'falsy' => read.call(leaf['falsy']))
33
+ end
34
+ end
35
+ end
36
+
37
+ # A report's paths, each decision's taken paths' tests read.
38
+ def self.paths_of(paths, read)
39
+ paths.transform_values do |decision|
40
+ decision.merge('taken' => taken(decision['taken'], read))
41
+ end
42
+ end
43
+
44
+ # A report's reached, each statement's reaches read.
45
+ def self.reached_of(reached, read)
46
+ reached.transform_values { read.call(_1) }
47
+ end
48
+
49
+ # A report's outside, each decision's paths' tests read.
50
+ def self.outside_of(outside, read)
51
+ outside.transform_values { taken(_1, read) }
52
+ end
53
+
54
+ # A report's flows, each read as flow reads it.
55
+ def self.flows_of(flows, read)
56
+ flows.transform_values { flow(_1, read) }
57
+ end
58
+
59
+ # A flow with its calls read, null for a top flow; each item's
60
+ # tests read in its taken and covered ways; and in each loop's
61
+ # passes.
62
+ def self.flow(flow, read)
63
+ restored = flow.merge('calls' => flow['calls']&.then(&read))
64
+ TAKEN_LISTS.each do |key|
65
+ restored[key] = taken(flow[key], read) if flow.key?(key)
66
+ end
67
+ return restored unless flow.key?('passes')
68
+
69
+ passes = flow['passes'].transform_values { taken(_1, read) }
70
+ restored.merge('passes' => passes)
71
+ end
72
+
73
+ # list, each item with tests, a path or a way taken, its tests
74
+ # read.
75
+ def self.taken(list, read)
76
+ list.map { _1.merge('tests' => read.call(_1['tests'])) }
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,23 @@
1
+ module PathCoverage
2
+ # The text the reports write, in UTF-8 as JSON and the console
3
+ # take it. A covered file's text is read in its own source
4
+ # encoding, so every string cut from it is transcoded here, where
5
+ # it leaves the gem, and nowhere else.
6
+ module ReportText
7
+ # value with every String in it, at any depth of Hashes and
8
+ # Arrays, in UTF-8: each character as written, and each byte
9
+ # its encoding cannot read, or UTF-8 cannot hold, U+FFFD, so
10
+ # characters still count as they did.
11
+ def self.utf8(value)
12
+ case value
13
+ when String
14
+ value.encode(Encoding::UTF_8, invalid: :replace,
15
+ undef: :replace).scrub
16
+ when Array then value.map { utf8(_1) }
17
+ when Hash
18
+ value.to_h { |key, item| [utf8(key), utf8(item)] }
19
+ else value
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,5 +1,6 @@
1
1
  require 'prism'
2
2
  require_relative 'decision'
3
+ require_relative 'evaluated'
3
4
  require_relative 'leaving'
4
5
 
5
6
  module PathCoverage
@@ -27,6 +28,8 @@ module PathCoverage
27
28
 
28
29
  # Finds the rescue fork of each def it visits.
29
30
  class Found < Prism::Visitor
31
+ include Evaluated::Visitor
32
+
30
33
  # Starts with no def visited. Forks are named as at path.
31
34
  def initialize(path)
32
35
  @path = path
@@ -1,37 +1,35 @@
1
1
  require_relative 'decision_paths'
2
+ require_relative 'tally'
2
3
 
3
4
  module PathCoverage
4
5
  # What a run found: every decision the manifest knows of, with
5
- # the counts its operands were recorded with. Every
6
+ # the Tallies its operands were recorded with. Every
6
7
  # reporter builds on this.
7
8
  class Result
8
- NEVER_RAN = { truthy: 0, falsy: 0 }.freeze
9
-
10
9
  # decisions is { decision => Condition }, from the manifest;
11
- # counts is { [decision, index] => counts } and decision_paths is
12
- # { decision => { path => evaluations } }, both from the
13
- # recorder.
10
+ # counts is { [decision, index] => { truthy:, falsy: } } of
11
+ # Tallies and decision_paths is { decision => { path => Tally } },
12
+ # both from the recorder.
14
13
  def initialize(decisions, counts, decision_paths = {})
15
14
  @decisions = decisions
16
15
  @counts = counts
17
16
  @decision_paths = decision_paths
18
17
  end
19
18
 
20
- # { decision => [{ path:, count: }, ...] }: every possible path
21
- # of each decision, as DecisionPaths gives them, with the
22
- # number of evaluations that took it, zero for one never
23
- # taken.
19
+ # { decision => [{ path:, tests: }, ...] }: every possible path
20
+ # of each decision, as DecisionPaths gives them, with the Tally
21
+ # of the evaluations that took it, empty for one never taken.
24
22
  def paths
25
23
  @decisions.to_h do |decision, condition|
26
24
  taken = @decision_paths.fetch(decision, {})
27
25
  all = DecisionPaths.possible(condition).map do |path|
28
- { path:, count: taken.fetch(path, 0) }
26
+ { path:, tests: taken.fetch(path) { Tally.new } }
29
27
  end
30
28
  [decision, all]
31
29
  end
32
30
  end
33
31
 
34
- # { decision => { path => evaluations } } of each path taken
32
+ # { decision => { path => Tally } } of each path taken
35
33
  # that is none of its decision's possible paths, eg a &. receiver
36
34
  # nil though treated as present; only decisions with one. Such a
37
35
  # path shows a treatment to be wrong, so it is reported, never
@@ -51,8 +49,9 @@ module PathCoverage
51
49
  end
52
50
 
53
51
  # { decision => [counts of operand 0, counts of operand 1, ...] },
54
- # with zero counts for an operand that never ran, and
55
- # only: naming the one outcome a fixed leaf can have.
52
+ # each { truthy:, falsy: } of Tallies, empty for an operand that
53
+ # never ran, and only: naming the one outcome a fixed leaf can
54
+ # have.
56
55
  def operands
57
56
  @decisions.to_h do |decision, condition|
58
57
  leaves = condition.leaves.each_with_index.map do |range, i|
@@ -71,9 +70,11 @@ module PathCoverage
71
70
  only ? counts.merge(only:) : counts
72
71
  end
73
72
 
74
- # The recorded counts of one operand, or zeros.
73
+ # The recorded Tallies of one operand, or empty ones.
75
74
  def counts_of(decision, index)
76
- @counts.fetch([decision, index], NEVER_RAN)
75
+ @counts.fetch([decision, index]) do
76
+ { truthy: Tally.new, falsy: Tally.new }
77
+ end
77
78
  end
78
79
  end
79
80
  end
@@ -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