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,13 +1,15 @@
1
1
  require 'json'
2
2
  require_relative 'kinds'
3
3
  require_relative 'measured_by'
4
+ require_relative 'report_text'
5
+ require_relative 'tally'
4
6
 
5
7
  module PathCoverage
6
8
  # The base reporter: the result as raw JSON, which every
7
9
  # other reporter can read back.
8
10
  module JsonWriter
9
11
  # The URL of the schema the JSON report follows.
10
- SCHEMA = 'https://path-coverage.github.io/schemas/v2/' \
12
+ SCHEMA = 'https://path-coverage.github.io/schemas/v3/' \
11
13
  'path_coverage.schema.json'.freeze
12
14
 
13
15
  # Writes operands, as Result gives them, paths, as
@@ -20,7 +22,7 @@ module PathCoverage
20
22
  File.write(path, json(operands, paths, **parts))
21
23
  end
22
24
 
23
- # The keys of parts listed as { decision => [{path:, count:}] }:
25
+ # The keys of parts listed as { decision => [{path:, tests:}] }:
24
26
  # each path taken that a treatment rules out, as
25
27
  # Result#outside gives them.
26
28
  LISTED = %i[outside].freeze
@@ -34,7 +36,9 @@ module PathCoverage
34
36
  # as Recorder#reached gives it, even empty, as a run that
35
37
  # reached no statement says so; none when not given; parts'
36
38
  # limits:, { "loops" =>, "max_paths" => } the flows were
37
- # listed with; headed by its schema's URL.
39
+ # listed with; headed by its schema's URL. Every Tally in it is
40
+ # listed by its tests' keys, and its tests name each key's test,
41
+ # as Tally.jsonable lists them.
38
42
  def self.json(operands, paths = {}, **parts)
39
43
  fingerprint = parts.fetch(:fingerprint, {})
40
44
  report = { '$schema' => SCHEMA, 'decisions' => operands,
@@ -43,7 +47,7 @@ module PathCoverage
43
47
  report.merge!(lists(parts), counted(parts), kept(parts))
44
48
  report['kinds'] = Kinds.names
45
49
  report['measured_by'] = MeasuredBy.versions
46
- JSON.pretty_generate(report)
50
+ JSON.pretty_generate(ReportText.utf8(Tally.jsonable(report)))
47
51
  end
48
52
 
49
53
  # { "reached" =>, "loops" =>, "max_paths" => } of parts'
@@ -74,11 +78,11 @@ module PathCoverage
74
78
  end.to_h
75
79
  end
76
80
 
77
- # taken, { decision => { path => count } }, as JSON lists it,
78
- # eg { 'k.rb:2:2' => [{ path: [true, false], count: 1 }] }.
81
+ # taken, { decision => { path => Tally } }, as JSON lists it,
82
+ # eg { 'k.rb:2:2' => [{ path: [true, false], tests: Tally }] }.
79
83
  def self.listed(taken)
80
- taken.transform_values do |counts|
81
- counts.map { |path, count| { path:, count: } }
84
+ taken.transform_values do |tallies|
85
+ tallies.map { |path, tests| { path:, tests: } }
82
86
  end
83
87
  end
84
88
  end
@@ -7,6 +7,7 @@ require_relative 'render'
7
7
  require_relative 'rescue_forks'
8
8
  require_relative 'result'
9
9
  require_relative 'rewriter'
10
+ require_relative 'testing'
10
11
  require_relative 'within_methods'
11
12
 
12
13
  # Path coverage for Ruby. This file is the kernel: it loads
@@ -85,11 +86,11 @@ module PathCoverage
85
86
  end
86
87
  end
87
88
 
88
- # The compiled, rewritten code of path, or nil for a path
89
- # config does not cover, which Ruby then loads as normal. Each
89
+ # The compiled, rewritten code of path, or nil for a path that
90
+ # is not instrumentable, which Ruby then loads as normal. Each
90
91
  # call of a method in it is traced, as its way is recorded.
91
92
  def self.instrumented(path, config)
92
- return unless config.covers?(path)
93
+ return unless instrumentable?(path, config)
93
94
 
94
95
  parsed = @parsed[path] = Manifest.parse(path, config)
95
96
  args = [parsed[:source], config.relative(path),
@@ -101,6 +102,13 @@ module PathCoverage
101
102
  traced(iseq.compile(rewritten, path, path), known(args, loops))
102
103
  end
103
104
 
105
+ # True for a path config covers whose source parses. A source
106
+ # that does not parse is left to Ruby, to raise its own
107
+ # SyntaxError, since a rewrite of broken code could make it valid.
108
+ def self.instrumentable?(path, config)
109
+ config.covers?(path) && Prism.parse_file(path).errors.empty?
110
+ end
111
+
104
112
  # [loops, repeats, restarts] of the file of args, [source, path,
105
113
  # decisions], as traced takes them, loops as MethodLoops gives
106
114
  # them.
@@ -9,6 +9,15 @@ module PathCoverage
9
9
  ways(node, fixed)
10
10
  end
11
11
 
12
+ # The leaves among condition's own operands whose outcome is
13
+ # their truthiness, so a decision that is the whole of one
14
+ # gives it its value, and two reads of one unwritten local
15
+ # give one outcome: none for most, eg a &. receiver's
16
+ # presence, a block's receiver's items, or a write's target.
17
+ def truthy_operands(_condition)
18
+ []
19
+ end
20
+
12
21
  # Whether its leaves, nested in a decision, need calls of their
13
22
  # own in place of record calls; false for most.
14
23
  def own_calls?
@@ -12,6 +12,12 @@ module PathCoverage
12
12
 
13
13
  OPERATORS = BLOCK_PATHS.keys.freeze
14
14
 
15
+ # Its block's body, whose truthiness is each answer; not its
16
+ # receiver, whose leaf counts items.
17
+ def self.truthy_operands(condition)
18
+ [condition.operands.last]
19
+ end
20
+
15
21
  # A value that holds whatever the path, eg select's array.
16
22
  ALWAYS = ->(*) { true }
17
23
 
@@ -123,7 +129,7 @@ module PathCoverage
123
129
  name = decision.inspect
124
130
  begun = "PathCoverage.begin_items(#{name}, #{at})"
125
131
  call = "PathCoverage.end_items(#{name}, #{at}, #{begun}, "
126
- Rewriter.wrap(call, inner.extent, false, :kind)
132
+ Rewriter.wrap(call, inner.extent, :bare, :kind)
127
133
  end
128
134
  end
129
135
  end
@@ -4,9 +4,11 @@ require_relative 'chain'
4
4
  module PathCoverage
5
5
  module Kinds
6
6
  # A case, its operands its whens' and else's arms, each when
7
- # trying its patterns in turn until one matches. Each pattern
8
- # is wrapped in a stand-in, which matches the case's subject
9
- # against it once, as the case would have.
7
+ # trying its patterns in turn until one matches. Ruby itself
8
+ # matches each pattern, or tests it for truth in a case with no
9
+ # subject, in the code's own frame, so $~ and a private ===
10
+ # behave as written: the instrumentation only notes which
11
+ # pattern each try reached and which arm the case took.
10
12
  module Case
11
13
  extend Base
12
14
 
@@ -19,17 +21,10 @@ module PathCoverage
19
21
  Chain.ways(node.with(operands: whens), fixed)
20
22
  end
21
23
 
22
- # The inserts that wrap each pattern of a case decision in a
23
- # when_match call, its stand-in, which enters and leaves the
24
- # decision itself, as the case tries its patterns one by one;
25
- # its arms may return or raise, so no leave wraps it.
26
- def self.decision_inserts(decision, condition)
27
- size = condition.leaves.size
28
- condition.leaves.each_with_index.flat_map do |range, index|
29
- call = "PathCoverage.when_match(#{decision.inspect}, " \
30
- "#{index}, #{size}, "
31
- Rewriter.wrap(call, range, false, :leaf)
32
- end
24
+ # What a case decision's leave call wraps: the whole case, to
25
+ # its end, as its arms run inside it.
26
+ def self.trailed(condition)
27
+ condition.whole
33
28
  end
34
29
 
35
30
  # Its node in a decision's tree from its arms, built in order so
@@ -52,8 +47,8 @@ module PathCoverage
52
47
  def self.arm_node(source, arm, matched, (outcomes, leaves))
53
48
  condition = arm.body
54
49
  body = if condition
55
- DecisionTree.node(source, condition, outcomes,
56
- leaves)
50
+ labelled(DecisionTree.node(source, condition,
51
+ outcomes, leaves))
57
52
  end
58
53
  matched << body[:cells].map { |cell| cell == true } if body
59
54
  cells = body ? matched.last : unmatched(matched, outcomes)
@@ -61,6 +56,14 @@ module PathCoverage
61
56
  { arm: arm.kind, text:, leaves: arm.leaves, cells:, body: }
62
57
  end
63
58
 
59
+ # body, a when's node, labelled when, as it is written, when
60
+ # its operator is TRUTHY_WHEN.
61
+ def self.labelled(body)
62
+ return body unless body[:operator] == TRUTHY_WHEN
63
+
64
+ body.merge(label: 'when')
65
+ end
66
+
64
67
  # Whether each column matched none of its whens' patterns:
65
68
  # a when's cell is true where it matched.
66
69
  def self.unmatched(matched, outcomes)
@@ -74,16 +77,56 @@ module PathCoverage
74
77
  true
75
78
  end
76
79
 
77
- # { pattern range => the opening of its pattern call } for
78
- # the patterns of inner, a case nested in a decision whose
79
- # leaves are leaves.
80
+ # { pattern range => the opening of its tried call } for the
81
+ # patterns of inner, a case whose patterns are among leaves,
82
+ # decision's. The call notes its pattern reached and gives
83
+ # back what it is passed, so the case takes the pattern, or a
84
+ # splat's items, as written.
80
85
  def self.calls(decision, inner, leaves)
81
86
  inner.leaves.to_h do |range|
82
87
  index = leaves.index(range)
83
88
  [range,
84
- "PathCoverage.pattern(#{decision.inspect}, #{index}, "]
89
+ "PathCoverage.tried(#{decision.inspect}, #{index}, "]
85
90
  end
86
91
  end
92
+
93
+ # The inserts besides its patterns' calls for inner, a case
94
+ # in decision: a took first in each when arm with statements,
95
+ # noting the pattern last reached matched before the arm
96
+ # runs; a missed after the last pattern, which gives no
97
+ # patterns, noting the pattern last reached did not match;
98
+ # and a settled around the whole, noting the match of a when
99
+ # arm with no statements, which neither notes. A case that
100
+ # is decision itself decides it in took and missed, as its
101
+ # arms may leave; one nested in a decision's leaf only notes
102
+ # its patterns' outcomes, for that decision to decide.
103
+ def self.inserts(decision, inner, _at, _commands)
104
+ called = [decision.inspect, inner.site ? '' : '_within']
105
+ whens = inner.operands.select(&:body)
106
+ settled = "PathCoverage.settled(#{decision.inspect}, "
107
+ [*took_inserts(whens, called),
108
+ missed_insert(whens.last, called),
109
+ *Rewriter.wrap(settled, inner.whole, :bare, :kind)]
110
+ end
111
+
112
+ # A took call, a statement of its own, first in each of whens
113
+ # with statements, called being [the decision's name, the
114
+ # calls' suffix]. It is outside the reach of the arm's first
115
+ # statement, as the case is decided before its arm runs.
116
+ def self.took_inserts(whens, (name, suffix))
117
+ rank = [Float::INFINITY, Rewriter::LAYERS.index(:arm)]
118
+ whens.select(&:extent).map do |arm|
119
+ call = "PathCoverage.took#{suffix}(#{name}); "
120
+ [arm.extent.begin, call, rank]
121
+ end
122
+ end
123
+
124
+ # A missed call after the last pattern of last, a case's last
125
+ # when, called being [the decision's name, the calls' suffix].
126
+ def self.missed_insert(last, (name, suffix))
127
+ call = ", *PathCoverage.missed#{suffix}(#{name})"
128
+ [last.body.extent.end, call, [0, Rewriter::LAYERS.index(:leaf)]]
129
+ end
87
130
  end
88
131
  end
89
132
  end
@@ -8,16 +8,26 @@ module PathCoverage
8
8
  module Chain
9
9
  extend Base
10
10
 
11
- OPERATORS = %w[&& and || or when].freeze
11
+ OPERATORS = ['&&', 'and', '||', 'or', 'when',
12
+ TRUTHY_WHEN].freeze
12
13
 
13
14
  # The outcome of an operand on which each operator goes on
14
15
  # to its next operand; any other ends the chain there. A
15
- # when's patterns, and a case's whens, stop at a match.
16
+ # when's patterns, and a case's whens, stop at a match, or,
17
+ # tested for truth, at a truthy one.
16
18
  CONTINUES_ON = {
17
19
  '&&' => true, 'and' => true, '||' => false, 'or' => false,
18
- 'when' => false, 'case' => false
20
+ 'when' => false, TRUTHY_WHEN => false, 'case' => false
19
21
  }.freeze
20
22
 
23
+ # Its leaf operands, each tested for truth; none for a when
24
+ # in a case with a subject, whose patterns are matched.
25
+ def self.truthy_operands(condition)
26
+ return [] if condition.operator == 'when'
27
+
28
+ condition.operands.grep(Range)
29
+ end
30
+
21
31
  # The ways node can be evaluated: each way of its first
22
32
  # operand, going on to the next while each outcome is the
23
33
  # one its operator goes on with; a way that reaches an
@@ -43,6 +43,11 @@ module PathCoverage
43
43
 
44
44
  OPERATORS = CHOICES
45
45
 
46
+ # Its predicate, when a leaf, tested for truth.
47
+ def self.truthy_operands(condition)
48
+ condition.operands.take(1).grep(Range)
49
+ end
50
+
46
51
  # What its decision's leave call wraps: an if's or an unless's
47
52
  # predicate, a leaf's byte range or a nested Condition, as
48
53
  # its path is decided there and its arms are statements that
@@ -13,6 +13,12 @@ module PathCoverage
13
13
 
14
14
  OPERATORS = [CONTAINS].freeze
15
15
 
16
+ # Its leaf, the last operand, whose outcome is its value's
17
+ # truthiness.
18
+ def self.truthy_operands(condition)
19
+ [condition.operands.last]
20
+ end
21
+
16
22
  # The ways node can be evaluated: each way of every one of
17
23
  # the decisions inside it, then each way of the leaf itself, its
18
24
  # value the whole's.
@@ -63,9 +63,9 @@ module PathCoverage
63
63
  enter = "PathCoverage.enter(#{name}, #{leaves.size})"
64
64
  call = "PathCoverage.leave(#{enter}, "
65
65
  extent = condition.extent
66
- command = condition.commands.include?(extent)
66
+ form = condition.commands.include?(extent) ? :grouped : :bare
67
67
  presents(decision, leaves) +
68
- Rewriter.wrap(call, extent, command, :decision)
68
+ Rewriter.wrap(call, extent, form, :decision)
69
69
  end
70
70
 
71
71
  # The inserts wrapping each of leaves, a &. chain decision's,
@@ -73,7 +73,7 @@ module PathCoverage
73
73
  def self.presents(decision, leaves)
74
74
  leaves.each_with_index.flat_map do |range, index|
75
75
  opening = present(decision, index, 0)
76
- Rewriter.wrap(opening, range, false, :leaf)
76
+ Rewriter.wrap(opening, range, :bare, :leaf)
77
77
  end
78
78
  end
79
79
 
@@ -39,9 +39,9 @@ module PathCoverage
39
39
  name = decision.inspect
40
40
  written = "PathCoverage.written_at(#{name}, #{at}, #{ran}, "
41
41
  settle = "PathCoverage.settle(#{name}, #{at}, #{!ran}, "
42
- command = commands.include?(arm.extent)
43
- Rewriter.wrap(settle, inner.extent, false, :kind) +
44
- Rewriter.wrap(written, arm.extent, command, :leaf)
42
+ form = commands.include?(arm.extent) ? :grouped : :bare
43
+ Rewriter.wrap(settle, inner.extent, :bare, :kind) +
44
+ Rewriter.wrap(written, arm.extent, form, :leaf)
45
45
  end
46
46
  end
47
47
  end
@@ -27,6 +27,20 @@ module PathCoverage
27
27
  BY_OPERATOR.fetch(operator)
28
28
  end
29
29
 
30
+ # The byte range of every leaf of condition whose outcome is its
31
+ # truthiness, as each kind tells of its own operands, those of
32
+ # its nested conditions and of its arms' bodies included.
33
+ def self.truthy_leaves(condition)
34
+ own = of(condition.operator).truthy_operands(condition)
35
+ own + condition.operands.flat_map do |operand|
36
+ case operand
37
+ when Condition then truthy_leaves(operand)
38
+ when Arm then operand.body ? truthy_leaves(operand.body) : []
39
+ else []
40
+ end
41
+ end
42
+ end
43
+
30
44
  # The names of the kinds measured, eg chain for Chain: a
31
45
  # report's fingerprint, as runs measuring different kinds
32
46
  # list a decision's paths differently.
@@ -1,3 +1,5 @@
1
+ require_relative 'evaluated'
2
+
1
3
  module PathCoverage
2
4
  # The loops in a method's Prism tree, as MethodLoops and
3
5
  # LoopForks take them. Its blocks, lambdas, whiles, untils and
@@ -14,7 +16,7 @@ module PathCoverage
14
16
  # Every def under node, outermost first.
15
17
  def self.defs(node)
16
18
  found = node.type == :def_node ? [node] : []
17
- found + node.compact_child_nodes.flat_map { defs(_1) }
19
+ found + Evaluated.children(node).flat_map { defs(_1) }
18
20
  end
19
21
 
20
22
  # Every loop under node, in the order they start, but none
@@ -23,7 +25,7 @@ module PathCoverage
23
25
  return [] if node.nil? || node.type == :def_node
24
26
 
25
27
  own = loop?(node) ? [node] : []
26
- own + node.compact_child_nodes.flat_map { loops_in(_1) }
28
+ own + Evaluated.children(node).flat_map { loops_in(_1) }
27
29
  end
28
30
 
29
31
  # True for node when it is a loop: one of LOOPS, or a begin
@@ -41,7 +43,7 @@ module PathCoverage
41
43
  return true if node.type == :retry_node
42
44
  return false if node.type == :begin_node && node.rescue_clause
43
45
 
44
- node.compact_child_nodes.any? { retries?(_1) }
46
+ Evaluated.children(node).any? { retries?(_1) }
45
47
  end
46
48
 
47
49
  # The location a loop is named by. A begin that starts at its
@@ -67,14 +69,17 @@ module PathCoverage
67
69
  KEYWORDS.include?(loop.type) ? loop.statements : loop.body
68
70
  end
69
71
 
70
- # True for a keyword loop written after its body, eg x += 1
71
- # while x < 9, where a call put first in its body would run
72
- # once, not each pass, so none goes there.
73
- def self.modifier?(loop)
74
- return false unless KEYWORDS.include?(loop.type)
72
+ # The types of node that are loops written after their body
73
+ # when they have no end of their own.
74
+ MODIFIABLE = %i[while_node until_node].freeze
75
75
 
76
- body = loop.statements.location
77
- loop.keyword_loc.start_offset > body.start_offset
76
+ # True for a loop written after its body, eg x += 1 while
77
+ # x < 9, or begin ... end while x < 9: a while or until with no
78
+ # end of its own, where a call put first in its body would run
79
+ # once, not each pass, so none goes there. A for always has
80
+ # its end, and an empty loop is no modifier.
81
+ def self.modifier?(loop)
82
+ MODIFIABLE.include?(loop.type) && loop.closing_loc.nil?
78
83
  end
79
84
 
80
85
  # The first statement of a loop's body, a do ... rescue ... end
@@ -1,5 +1,6 @@
1
1
  require 'prism'
2
2
  require_relative 'condition'
3
+ require_relative 'evaluated'
3
4
  require_relative 'leaving'
4
5
  require_relative 'manifest_blocks'
5
6
  require_relative 'manifest_case'
@@ -13,8 +14,8 @@ require_relative 'decision'
13
14
 
14
15
  module PathCoverage
15
16
  # Finds every condition decision in source without running it: each
16
- # outermost &&, ||, and or or, each ternary, if, unless and case
17
- # with a subject, with the conditions nested in it.
17
+ # outermost &&, ||, and or or, each ternary, if, unless and case,
18
+ # with the conditions nested in it.
18
19
  module Manifest
19
20
  # { decision => Condition } for each decision in source, none of
20
21
  # them named by an operator in ignored; a call to a method
@@ -36,7 +37,7 @@ module PathCoverage
36
37
 
37
38
  # The source and decisions of the covered file at full_path.
38
39
  def self.parse(full_path, config)
39
- source = File.read(full_path)
40
+ source = source_of(full_path)
40
41
  path = config.relative(full_path)
41
42
  treated = %w[present nonempty].map { config.treated(_1, path) }
42
43
  ignored = config.ignored
@@ -44,35 +45,63 @@ module PathCoverage
44
45
  decisions: decisions(source, path, ignored, *treated) }
45
46
  end
46
47
 
47
- # True for an expression that is no bare argument, so a
48
- # rewrite wrapping it in another call must parenthesise it: a
49
- # command call, an and or an or, or a modifier, eg x unless y.
50
- def self.bare_less?(node)
51
- return true if command?(node) || modifier?(node)
52
-
53
- %i[and_node or_node].include?(node.type) &&
54
- %w[and or].include?(node.operator)
48
+ # The text of the file at full_path, its bytes as written, in
49
+ # the encoding Ruby reads it in, its magic comment's, else
50
+ # UTF-8, as Prism finds it: so byte offsets index it, and its
51
+ # characters count as Ruby counts them. Bytes that encoding
52
+ # cannot read, as in a file Ruby could not load, are binary
53
+ # text, each byte a character of its own.
54
+ def self.source_of(full_path)
55
+ bytes = File.binread(full_path)
56
+ encoding = Prism.parse(bytes).source.encoding
57
+ text = bytes.dup.force_encoding(encoding)
58
+ text.valid_encoding? ? text : bytes
55
59
  end
56
60
 
57
- # True for a call to a named method with arguments and no
58
- # parentheses, eg `id.include? 'N'`, but not `id.size == 6`.
59
- def self.command?(node)
60
- node.type == :call_node && node.arguments &&
61
- node.opening_loc.nil? && node.name.match?(/\A[a-z_]/i)
61
+ # True for a leaf tested for truth that is a flip-flop, eg
62
+ # (i == 3)..(i == 5), or one through parentheses, a ! or not,
63
+ # an && or an ||, each of which a condition passes through: so
64
+ # its condition alone makes it a flip-flop, which, passed to a
65
+ # call, would build a Range. A flip-flop under an if or ?:
66
+ # inside the leaf, or in a block, has a condition of its own.
67
+ def self.flip_flop?(node)
68
+ node.type == :flip_flop_node ||
69
+ conditioned(node).any? { flip_flop?(_1) }
62
70
  end
63
71
 
64
- # True for a modifier if, unless or rescue, eg x if y: one
65
- # with no end keyword, a ternary being no modifier.
66
- def self.modifier?(node)
72
+ # The expressions a condition on node passes into: the
73
+ # statements in parentheses, both sides of an && or ||, and the
74
+ # receiver of a ! or not; none for any other node.
75
+ def self.conditioned(node)
67
76
  case node.type
68
- when :rescue_modifier_node then true
69
- when :unless_node then node.end_keyword_loc.nil?
70
- when :if_node
71
- !node.if_keyword_loc.nil? && node.end_keyword_loc.nil?
72
- else false
77
+ when *PASSING then node.compact_child_nodes
78
+ when :call_node then negated(node)
79
+ else []
73
80
  end
74
81
  end
75
82
 
83
+ # The nodes a condition passes into each child of.
84
+ PASSING = %i[
85
+ parentheses_node statements_node and_node or_node
86
+ ].freeze
87
+
88
+ # [the receiver of node, a call, when it is a ! or not], none
89
+ # for any other call.
90
+ def self.negated(node)
91
+ node.name == :! ? [node.receiver].compact : []
92
+ end
93
+
94
+ # True for an expression that is no bare argument, so a
95
+ # rewrite wrapping it in another call must parenthesise it.
96
+ # Prism decides: the expression is bare only when, as a call's
97
+ # argument after another, it parses whole, eg `id.size == 6`,
98
+ # but not `id.include? 'N'`, `not x`, `x in y` or `x if y`.
99
+ # After another, since a rewrite's call passes it last, and a
100
+ # lone argument may be a command call where a later one may not.
101
+ def self.bare_less?(node)
102
+ Prism.parse("f(0, #{node.slice})").errors.any?
103
+ end
104
+
76
105
  # Literals and lambdas, whose value is truthy by their form;
77
106
  # a constant is not among them, as it can hold nil or false.
78
107
  TRUTHY_TYPES = %i[
@@ -112,6 +141,7 @@ module PathCoverage
112
141
 
113
142
  # Collects each decision and its Condition.
114
143
  class ConditionSites < Prism::Visitor
144
+ include Evaluated::Visitor
115
145
  include ManifestBlocks
116
146
  include ManifestCase
117
147
  include ManifestChains
@@ -162,11 +192,8 @@ module PathCoverage
162
192
  add_decision(node)
163
193
  end
164
194
 
165
- # Notes a case decision, when its paths can be seen; any other
166
- # case is no decision, though a condition inside it may be.
195
+ # Notes a case decision: every case is one.
167
196
  def visit_case_node(node)
168
- return super unless case_decision?(node)
169
-
170
197
  add_decision(node)
171
198
  end
172
199
 
@@ -181,7 +208,8 @@ module PathCoverage
181
208
  end
182
209
 
183
210
  # Notes node as a decision, named where it starts: an if or an
184
- # unless at its keyword. It claims its place first, so a
211
+ # unless at its keyword; its site node's node_id, so node is
212
+ # the one node known as it. It claims its place first, so a
185
213
  # decision found inside one of its leaves or arms follows it.
186
214
  # While a leaf is being visited, any decision is collected as
187
215
  # part of that leaf instead, as it is part of one expression.
@@ -192,9 +220,10 @@ module PathCoverage
192
220
  decision = Decision.at(@path, decision_loc(node))
193
221
  @decisions[decision] = nil
194
222
  outer = @notes
195
- @notes = { commands: [], fixed: {} }
223
+ @notes = { commands: [], flip_flops: [], fixed: {} }
196
224
  condition = scoped { condition_of(node) }
197
- @decisions[decision] = condition.with(**@notes)
225
+ @decisions[decision] =
226
+ condition.with(**@notes, site: node.node_id)
198
227
  @notes = outer
199
228
  end
200
229
 
@@ -258,10 +287,7 @@ module PathCoverage
258
287
  inner = leaf_decisions(operand)
259
288
  loc = operand.location
260
289
  range = loc.start_offset...loc.end_offset
261
- @notes[:commands] << range if Manifest.bare_less?(operand)
262
- outcome = Manifest.fixed_outcome(operand) ||
263
- given_outcome(operand)
264
- @notes[:fixed][range] = outcome if outcome
290
+ note_leaf(operand, range, tested: true)
265
291
  nonempty = nonempty_call?(operand, inner)
266
292
  contained(inner, range, nonempty:)
267
293
  end