path_coverage 0.5.0 → 0.7.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 (48) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +33 -153
  3. data/lib/path_coverage/call_trace.rb +26 -5
  4. data/lib/path_coverage/calls.rb +24 -0
  5. data/lib/path_coverage/condition.rb +10 -19
  6. data/lib/path_coverage/configuration_limits.rb +1 -1
  7. data/lib/path_coverage/console_summary.rb +13 -13
  8. data/lib/path_coverage/fold_cells.rb +0 -1
  9. data/lib/path_coverage/fold_rows.rb +2 -2
  10. data/lib/path_coverage/gaps.rb +1 -1
  11. data/lib/path_coverage/json_reader.rb +3 -3
  12. data/lib/path_coverage/json_reports.rb +28 -21
  13. data/lib/path_coverage/json_writer.rb +4 -4
  14. data/lib/path_coverage/kernel.rb +41 -20
  15. data/lib/path_coverage/kinds/block.rb +37 -45
  16. data/lib/path_coverage/leaving.rb +14 -6
  17. data/lib/path_coverage/loop_forks.rb +12 -26
  18. data/lib/path_coverage/loop_nodes.rb +41 -5
  19. data/lib/path_coverage/method_loops.rb +57 -13
  20. data/lib/path_coverage/method_statements.rb +25 -12
  21. data/lib/path_coverage/method_ways.rb +49 -21
  22. data/lib/path_coverage/{card_context.rb → paths_context.rb} +16 -16
  23. data/lib/path_coverage/paths_fitting.rb +31 -0
  24. data/lib/path_coverage/paths_forks.rb +72 -0
  25. data/lib/path_coverage/{card_gaps.rb → paths_gaps.rb} +38 -26
  26. data/lib/path_coverage/paths_leaving.rb +40 -0
  27. data/lib/path_coverage/paths_rescues.rb +146 -0
  28. data/lib/path_coverage/{card_scopes.rb → paths_scopes.rb} +6 -6
  29. data/lib/path_coverage/{card_tree.rb → paths_tree.rb} +20 -16
  30. data/lib/path_coverage/{card_way_lists.rb → paths_way_lists.rb} +3 -3
  31. data/lib/path_coverage/paths_ways.rb +193 -0
  32. data/lib/path_coverage/recorder.rb +21 -4
  33. data/lib/path_coverage/recorder_blocks.rb +21 -30
  34. data/lib/path_coverage/render.rb +6 -6
  35. data/lib/path_coverage/rescue_forks.rb +174 -0
  36. data/lib/path_coverage/rewriter.rb +83 -14
  37. data/lib/path_coverage/source_report.rb +4 -4
  38. data/lib/path_coverage/stable_leaves.rb +3 -3
  39. data/lib/path_coverage/statement_arms.rb +23 -9
  40. data/lib/path_coverage/totals.rb +5 -5
  41. data/lib/path_coverage/version.rb +1 -1
  42. data/lib/path_coverage/way_algebras.rb +2 -2
  43. data/lib/path_coverage/ways_report.rb +8 -8
  44. data/lib/path_coverage/ways_totals.rb +7 -7
  45. data/lib/path_coverage/within_methods.rb +62 -62
  46. metadata +12 -8
  47. data/lib/path_coverage/card_forks.rb +0 -44
  48. data/lib/path_coverage/card_ways.rb +0 -196
@@ -7,7 +7,7 @@ module PathCoverage
7
7
  # other reporter can read back.
8
8
  module JsonWriter
9
9
  # The URL of the schema the JSON report follows.
10
- SCHEMA = 'https://path-coverage.github.io/schemas/v1/' \
10
+ SCHEMA = 'https://path-coverage.github.io/schemas/v2/' \
11
11
  'path_coverage.schema.json'.freeze
12
12
 
13
13
  # Writes operands, as Result gives them, paths, as
@@ -26,14 +26,14 @@ module PathCoverage
26
26
  LISTED = %i[outside].freeze
27
27
 
28
28
  # operands, and any paths, and parts' outside:, totals:,
29
- # cards:, as WithinMethods.listed gives them, and tree:, as
29
+ # flows:, as WithinMethods.listed gives them, and tree:, as
30
30
  # WithinMethods.tree gives it, as the JSON report's text, with
31
31
  # parts' fingerprint:, as Configuration#fingerprint gives
32
32
  # it, the names of the kinds measured, the tools that measured
33
33
  # it and their versions, and parts' reached:,
34
34
  # as Recorder#reached gives it, even empty, as a run that
35
35
  # reached no statement says so; none when not given; parts'
36
- # limits:, { "loops" =>, "max_paths" => } the cards were
36
+ # limits:, { "loops" =>, "max_paths" => } the flows were
37
37
  # listed with; headed by its schema's URL.
38
38
  def self.json(operands, paths = {}, **parts)
39
39
  fingerprint = parts.fetch(:fingerprint, {})
@@ -54,7 +54,7 @@ module PathCoverage
54
54
  end
55
55
 
56
56
  # The keys of parts written as they are, when not empty.
57
- KEPT = %i[totals cards tree].freeze
57
+ KEPT = %i[totals flows tree].freeze
58
58
 
59
59
  # { key => its value } of each of parts' KEPT keys that has
60
60
  # a value not empty.
@@ -4,6 +4,7 @@ require_relative 'manifest'
4
4
  require_relative 'method_loops'
5
5
  require_relative 'recorder'
6
6
  require_relative 'render'
7
+ require_relative 'rescue_forks'
7
8
  require_relative 'result'
8
9
  require_relative 'rewriter'
9
10
  require_relative 'within_methods'
@@ -94,44 +95,64 @@ module PathCoverage
94
95
  args = [parsed[:source], config.relative(path),
95
96
  parsed[:decisions]]
96
97
  loops = MethodLoops.of(*args)
97
- rewritten = rewritten(path, parsed, config, loops)
98
+ rescues = RescueForks.found(*args.first(2))
99
+ rewritten = rewritten(args, [loops, rescues])
98
100
  iseq = RubyVM::InstructionSequence
99
- traced(iseq.compile(rewritten, path, path),
100
- [loops, MethodLoops.repeats(*args)])
101
+ traced(iseq.compile(rewritten, path, path), known(args, loops))
101
102
  end
102
103
 
103
- # The source of parsed, the file at path, rewritten: each of its
104
- # decisions recorded, each statement reach counted, and each
105
- # pass of each of loops, { entry => loops }, noted.
106
- def self.rewritten(path, parsed, config, loops)
107
- source = parsed[:source]
108
- reaches = WithinMethods.reaches(path, parsed, config)
109
- passes = MethodLoops.passes(source, config.relative(path), loops)
110
- Rewriter.rewrite(source, parsed[:decisions], reaches, passes)
104
+ # [loops, repeats, restarts] of the file of args, [source, path,
105
+ # decisions], as traced takes them, loops as MethodLoops gives
106
+ # them.
107
+ def self.known(args, loops)
108
+ [loops, MethodLoops.repeats(*args), MethodLoops.restarts(args[0])]
109
+ end
110
+
111
+ # source rewritten, the file at relative path, with decisions
112
+ # as Manifest gives them. Each decision is recorded. Each
113
+ # statement's reach is counted. Each pass of each of loops,
114
+ # { entry => loops }, is noted. Each def's entry is counted,
115
+ # before any statement's reach there. Each rescue running, and
116
+ # each body completing, is noted where rescues, as
117
+ # RescueForks.found gives them, finds them.
118
+ def self.rewritten((source, relative, decisions), (loops, rescues))
119
+ found = MethodStatements.collect(source, relative, decisions)
120
+ passes = MethodLoops.passes(source, relative, loops)
121
+ marks = { reaches: found.reaches, passes:,
122
+ entries: found.entries,
123
+ completions: rescues.completions,
124
+ rescues: rescues.starts, values: rescues.values }
125
+ Rewriter.rewrite(source, decisions, marks)
111
126
  end
112
127
 
113
128
  # compiled, after noting each of its methods' loops and repeats,
114
129
  # [{ entry => loops }, { entry => repeats }], as MethodLoops
115
- # gives them, on the recorder's trace, and tracing each call of
116
- # them, and only of them, on the trace of whichever start is
117
- # current as it runs. Code with no method has no call to
118
- # trace, and TracePoint refuses to trace it.
119
- def self.traced(compiled, (loops, repeats))
130
+ # gives them, on the recorder's trace. Each call of them, and
131
+ # only of them, is traced on the trace of whichever start is
132
+ # current as it runs. restarts is the lines of the defs a retry
133
+ # restarts, as MethodLoops gives them. Code with no method has
134
+ # no call to trace, and TracePoint refuses to trace it.
135
+ def self.traced(compiled, (loops, repeats, restarts))
120
136
  return compiled if loops.empty?
121
137
 
122
138
  loops.each do |entry, held|
123
139
  @recorder.trace.know(entry, held, repeats[entry])
124
140
  end
125
- hook = method(:on_call_event)
141
+ hook = ->(point) { on_call_event(point, restarts) }
126
142
  TracePoint.new(:call, :return, &hook).enable(target: compiled)
127
143
  compiled
128
144
  end
129
145
 
130
146
  # Notes point, a TracePoint's :call or :return of a covered
131
- # method, on the current start's trace.
132
- def self.on_call_event(point)
147
+ # method, on the current start's trace. A call of a def a retry
148
+ # restarts, its line in restarts, is noted with its frame's
149
+ # depth, so a restart is not taken for a new call.
150
+ def self.on_call_event(point, restarts)
133
151
  trace = @recorder.trace
134
- point.event == :call ? trace.call : trace.return
152
+ return trace.return if point.event == :return
153
+ return trace.call unless restarts.include?(point.lineno)
154
+
155
+ trace.restartable_call(caller_locations.size)
135
156
  end
136
157
 
137
158
  # Writes the JSON reports, then runs the chosen reporters, for
@@ -3,9 +3,10 @@ require_relative 'base'
3
3
  module PathCoverage
4
4
  module Kinds
5
5
  # A block method's call, eg ids.any? { |id| id.odd? }: its
6
- # receiver's count of items, and its block's answers, its
7
- # value following from them. Nothing else is evaluated, eg
8
- # ids.size.
6
+ # block's answers, its value following from them. Its
7
+ # receiver's leaf has no outcome, as how many items the block
8
+ # was called with is a loop's question. Nothing else is
9
+ # evaluated, eg ids.size.
9
10
  module Block
10
11
  extend Base
11
12
 
@@ -14,57 +15,48 @@ module PathCoverage
14
15
  # A value that holds whatever the path, eg select's array.
15
16
  ALWAYS = ->(*) { true }
16
17
 
17
- # nil, nothing found, when its block was called with no
18
- # items or its last answer was false; else the item found,
19
- # which may itself be nil, so nil, as no path can tell.
20
- FOUND = ->(none, answer) { none || !answer ? false : nil }
18
+ # nil, nothing found, when its last answer was false; else
19
+ # the item found, which may itself be nil, so nil, as no
20
+ # path can tell.
21
+ FOUND = ->(answer) { answer ? nil : false }
21
22
 
22
- # A block method's value from whether its block was called
23
- # with no items, and its block's last answer; nil when the
24
- # path cannot tell.
23
+ # Its last answer, as the value of any?, all? and
24
+ # find_index.
25
+ LAST = ->(answer) { answer }
26
+
27
+ # A block method's value from its block's last answer; nil
28
+ # when the path cannot tell.
25
29
  VALUES = {
26
- 'any?' => ->(none, answer) { !none && answer },
27
- 'all?' => ->(none, answer) { none || answer },
28
- 'none?' => ->(none, answer) { none || !answer },
30
+ 'any?' => LAST, 'all?' => LAST, 'find_index' => LAST,
31
+ 'none?' => lambda(&:!),
29
32
  'detect' => FOUND,
30
33
  'find' => FOUND,
31
- 'find_index' => ->(none, answer) { !none && answer },
32
34
  'take_while' => ALWAYS,
33
35
  **EVERY_ITEM.to_h { |name| [name, ALWAYS] }
34
36
  }.freeze
35
37
 
36
38
  # Whether select's result has items: whether any answer
37
39
  # was true.
38
- KEPT = lambda do |items, answers|
39
- items != ITEMS[0] && answers != ANSWERS[1]
40
- end
40
+ KEPT = ->(answers) { answers != ANSWERS[1] }
41
41
 
42
- # Whether a block method's result has items, from how many
43
- # items its block was called with and its answers, eg
44
- # select keeps an item when any answer was true; nil when
45
- # its path cannot tell, eg take_while's last answer false
46
- # after 2+ items, which may or may not be its first.
42
+ # Whether a block method's result has items, from its
43
+ # block's answers, eg select keeps an item when any answer
44
+ # was true; nil when its path cannot tell, eg take_while's
45
+ # last answer false, which may or may not be its first.
47
46
  HAS_ITEMS = {
48
47
  'select' => KEPT, 'filter' => KEPT, 'filter_map' => KEPT,
49
- 'reject' => lambda do |items, answers|
50
- items != ITEMS[0] && answers != ANSWERS[0]
51
- end,
48
+ 'reject' => ->(answers) { answers != ANSWERS[0] },
52
49
  'partition' => ALWAYS,
53
- 'take_while' => lambda do |items, answer|
54
- next false if items == ITEMS[0]
55
- next answer if answer
56
-
57
- items == ITEMS[1] ? false : nil
58
- end
50
+ 'take_while' => ->(answer) { answer || nil }
59
51
  }.freeze
60
52
 
61
53
  # Whether decision's result has items on the path that took
62
54
  # outcomes; nil when its kind cannot tell, eg for a decision
63
55
  # that is no block method's call.
64
56
  def self.items?(decision, outcomes)
65
- receiver, body = decision.operands
57
+ _receiver, body = decision.operands
66
58
  rule = HAS_ITEMS[decision.operator]
67
- rule&.call(outcomes[receiver], outcomes[body])
59
+ rule&.call(outcomes[body])
68
60
  end
69
61
 
70
62
  # The ways node can be evaluated, fixed by its shape, its
@@ -72,25 +64,25 @@ module PathCoverage
72
64
  def self.ways(node, _fixed)
73
65
  receiver, body = node.operands
74
66
  BLOCK_PATHS[node.operator].map do |items, answer|
75
- value = path_value(node.operator, items, answer)
67
+ value = path_value(node.operator, answer)
76
68
  [{ receiver => items, body => answer }, value]
77
69
  end
78
70
  end
79
71
 
80
- # Its value on a path: its count of items and its block's
81
- # answer, eg false for any? called with 0 items, and always
82
- # truthy for select, which gives an array; nil for find
83
- # with a last answer true, whose value no path can tell.
84
- def self.path_value(operator, items, answer)
85
- VALUES[operator].call(items == ITEMS[0], answer)
72
+ # Its value on a path with its block's last answer, eg
73
+ # false for any? with false, and always truthy for select,
74
+ # which gives an array; nil for find with a last answer
75
+ # true, whose value no path can tell.
76
+ def self.path_value(operator, answer)
77
+ VALUES[operator].call(answer)
86
78
  end
87
79
 
88
- # Its value in each column, from its count of items and its
89
- # block's answer there; nil where it was not reached.
80
+ # Its value in each column, from its block's answer there;
81
+ # nil where it was not reached.
90
82
  def self.value(operator, operands)
91
- receiver, body = operands
92
- receiver[:cells].zip(body[:cells]).map do |items, answer|
93
- items && path_value(operator, items, answer)
83
+ _receiver, body = operands
84
+ body[:cells].map do |answer|
85
+ answer.nil? ? nil : path_value(operator, answer)
94
86
  end
95
87
  end
96
88
 
@@ -1,22 +1,30 @@
1
1
  require_relative 'condition'
2
2
 
3
3
  module PathCoverage
4
- # Whether code leaves the method where it stands, decided once,
5
- # from Prism's nodes, for every part of the gem that asks.
4
+ # Whether code leaves where it stands, the method, or, for a next
5
+ # or a break, its pass or its loop, decided once, from Prism's
6
+ # nodes, for every part of the gem that asks.
6
7
  module Leaving
7
8
  # The methods whose call, with no receiver, leaves the method:
8
9
  # raise, fail, its other name, and throw, for its catch.
9
10
  CALLS = %i[raise fail throw].freeze
10
11
 
11
- # 'return' for a return, the method's name for a call of one of
12
- # CALLS with no receiver, eg 'fail'; nil for a statement that
13
- # does not leave the method.
12
+ # The word a statement leaves by, or nil when it leaves
13
+ # nothing. A return, a next, a break and a retry give their
14
+ # keyword. A next, a break or a retry leaves only its pass or
15
+ # its loop. A call of one of CALLS with no receiver gives the
16
+ # method's name, eg 'fail'.
14
17
  def self.word(statement)
15
- return 'return' if statement.type == :return_node
18
+ return WORDS[statement.type] if WORDS.key?(statement.type)
16
19
 
17
20
  statement.name.to_s if leaving_call?(statement)
18
21
  end
19
22
 
23
+ # The words of the statements that leave by their keyword, by
24
+ # their node's type.
25
+ WORDS = { return_node: 'return', next_node: 'next',
26
+ break_node: 'break', retry_node: 'retry' }.freeze
27
+
20
28
  # The arm a statement that leaves the method is as an operand,
21
29
  # eg return t in x or return t: run when the operand before it
22
30
  # gives the outcome its operator goes on with, never a value,
@@ -1,5 +1,4 @@
1
1
  require 'prism'
2
- require_relative 'condition'
3
2
  require_relative 'decision'
4
3
  require_relative 'loop_nodes'
5
4
 
@@ -7,45 +6,32 @@ module PathCoverage
7
6
  # The loops of each method that fork when loops are decisions:
8
7
  # 0 items, or 1+ items, its body walked once. Each is one whose
9
8
  # passes a pass call put first in its body can show, so a
10
- # call's trace tells the two apart; but no block of a block
11
- # method's decision, eg any?'s, whose paths count its items
12
- # already.
9
+ # call's trace tells the two apart, a block method's block,
10
+ # eg any?'s, among them.
13
11
  module LoopForks
14
12
  # { def decision => { loop's name => [its bytes, its body's] } }
15
- # of each def in source, at path, decisions as
16
- # Manifest.decisions gives them.
17
- def self.of(source, path, decisions)
18
- blocks = decision_blocks(decisions)
13
+ # of each def in source, at path.
14
+ def self.of(source, path)
19
15
  LoopNodes.defs(Prism.parse(source).value).to_h do |node|
20
- [Decision.of(path, node), in_def(node, path, blocks)]
16
+ [Decision.of(path, node), in_def(node, path)]
21
17
  end
22
18
  end
23
19
 
24
20
  # { loop's name => [its bytes, its body's] } of each loop of
25
- # node, a def, at path, that forks, blocks being where the
26
- # block of each block method's decision ends.
27
- def self.in_def(node, path, blocks)
21
+ # node, a def, at path, that forks.
22
+ def self.in_def(node, path)
28
23
  loops = LoopNodes.loops_in(node.body)
29
- loops.filter_map { fork(_1, path, blocks) }.to_h
24
+ loops.filter_map { fork(_1, path) }.to_h
30
25
  end
31
26
 
32
- # Where the block of each block method's decision among
33
- # decisions ends, a byte offset, as its call does.
34
- def self.decision_blocks(decisions)
35
- by = decisions.values.group_by { BLOCK_PATHS.key?(_1.operator) }
36
- by.fetch(true, []).to_set { _1.extent.end }
37
- end
38
-
39
- # [name, [its bytes, its body's]] of loop, at path, when it is
40
- # no block method's, its call not ending at one of blocks, and
41
- # a pass call can go first in its body; else nil.
42
- def self.fork(loop, path, blocks)
27
+ # [name, [its bytes, its body's]] of loop, at path, when a
28
+ # pass call can go first in its body; else nil.
29
+ def self.fork(loop, path)
43
30
  body = LoopNodes.body_of(loop)
44
- return if blocks.include?(loop.location.end_offset)
45
31
  return if body.nil? || LoopNodes.modifier?(loop)
46
32
  return unless LoopNodes.first_statement(body)
47
33
 
48
- [Decision.at(path, loop.location),
34
+ [Decision.at(path, LoopNodes.at(loop)),
49
35
  [range(loop.location), range(body.location)]]
50
36
  end
51
37
 
@@ -1,7 +1,8 @@
1
1
  module PathCoverage
2
- # The loops in a method's Prism tree: its blocks, lambdas,
3
- # whiles, untils and fors, each run 0 or more times, and their
4
- # bodies, as MethodLoops and LoopForks take them.
2
+ # The loops in a method's Prism tree, as MethodLoops and
3
+ # LoopForks take them. Its blocks, lambdas, whiles, untils and
4
+ # fors each run 0 or more times. So does each begin whose rescue
5
+ # retries, each attempt a pass.
5
6
  module LoopNodes
6
7
  # The types of node that are loops written with a keyword,
7
8
  # their body their statements.
@@ -21,13 +22,48 @@ module PathCoverage
21
22
  def self.loops_in(node)
22
23
  return [] if node.nil? || node.type == :def_node
23
24
 
24
- own = LOOPS.include?(node.type) ? [node] : []
25
+ own = loop?(node) ? [node] : []
25
26
  own + node.compact_child_nodes.flat_map { loops_in(_1) }
26
27
  end
27
28
 
29
+ # True for node when it is a loop: one of LOOPS, or a begin
30
+ # whose rescue retries.
31
+ def self.loop?(node)
32
+ return true if LOOPS.include?(node.type)
33
+
34
+ node.type == :begin_node && retries?(node.rescue_clause)
35
+ end
36
+
37
+ # True when node, a rescue, or what it holds, retries. A begin
38
+ # inside it with a rescue of its own takes its own retries.
39
+ def self.retries?(node)
40
+ return false if node.nil?
41
+ return true if node.type == :retry_node
42
+ return false if node.type == :begin_node && node.rescue_clause
43
+
44
+ node.compact_child_nodes.any? { retries?(_1) }
45
+ end
46
+
47
+ # The location a loop is named by. A begin that starts at its
48
+ # def, as a def's body with a rescue does, is named by its end,
49
+ # the def's end. Its start is the def's own name, and no row
50
+ # starts at an end. Any other loop is named by its whole.
51
+ def self.at(loop)
52
+ implicit?(loop) ? loop.end_keyword_loc : loop.location
53
+ end
54
+
55
+ # True for a begin with no begin keyword, which starts at its
56
+ # def.
57
+ def self.implicit?(loop)
58
+ loop.type == :begin_node && loop.begin_keyword_loc.nil?
59
+ end
60
+
28
61
  # The body of loop: a block's or lambda's body, a keyword
29
- # loop's statements; nil for an empty one.
62
+ # loop's statements; nil for an empty one. A begin is its own
63
+ # body, its rescues included, as a retry runs it again.
30
64
  def self.body_of(loop)
65
+ return loop if loop.type == :begin_node
66
+
31
67
  KEYWORDS.include?(loop.type) ? loop.statements : loop.body
32
68
  end
33
69
 
@@ -1,10 +1,11 @@
1
1
  require 'prism'
2
- require_relative 'card_tree'
2
+ require_relative 'paths_tree'
3
3
  require_relative 'decision'
4
4
  require_relative 'loop_forks'
5
5
  require_relative 'loop_nodes'
6
6
  require_relative 'manifest'
7
7
  require_relative 'method_statements'
8
+ require_relative 'rescue_forks'
8
9
 
9
10
  module PathCoverage
10
11
  # The loops of each method: the blocks, lambdas, whiles, untils
@@ -14,19 +15,61 @@ module PathCoverage
14
15
  # first run's, as Walk's repeats have them.
15
16
  module MethodLoops
16
17
  # { def decision => { name => the loops holding it, outermost
17
- # first } } for each def in source, at path: each of its
18
- # rows, as MethodStatements gives them, and each loop holding
19
- # one or forking, as LoopForks finds them, by name, as Decision
20
- # names where it starts.
18
+ # first } } for each def in source, at path. It holds each of
19
+ # its rows, as MethodStatements gives them, and each of its
20
+ # rescue forks, as RescueForks gives them. It also holds each
21
+ # loop holding one of those, or forking, as LoopForks finds
22
+ # them. Each is by name, as Decision names where it starts.
21
23
  def self.of(source, path, decisions = nil)
22
24
  decisions ||= Manifest.decisions(source, path)
23
- rows = MethodStatements.of(source, path, decisions)
24
- forks = LoopForks.of(source, path, decisions)
25
- starts = CardTree.line_starts(source)
25
+ spots = spots_in(source, path, decisions)
26
+ forks = LoopForks.of(source, path)
27
+ starts = PathsTree.line_starts(source)
26
28
  LoopNodes.defs(Prism.parse(source).value).to_h do |node|
27
29
  decision = Decision.of(path, node)
28
30
  loops = [named(node, path), forks[decision].keys]
29
- [decision, holding(rows[decision], loops, starts)]
31
+ [decision, holding(spots.fetch(decision), loops, starts)]
32
+ end
33
+ end
34
+
35
+ # A spot a loop can hold, named where it is, like a row.
36
+ Spot = Data.define(:name, :line, :column)
37
+
38
+ # The lines of the defs in source whose body's rescue retries.
39
+ # A retry there restarts the method's frame, so Ruby reports a
40
+ # call of it again, with no return of the first.
41
+ def self.restarts(source)
42
+ defs = LoopNodes.defs(Prism.parse(source).value)
43
+ defs.filter_map do |node|
44
+ node.location.start_line if restarting?(node.body)
45
+ end.to_set
46
+ end
47
+
48
+ # True for body, a def's, when a retry restarts it: a begin of
49
+ # the def's own whose rescue retries. An empty body is nil.
50
+ def self.restarting?(body)
51
+ return false if body.nil?
52
+
53
+ LoopNodes.implicit?(body) && LoopNodes.loop?(body)
54
+ end
55
+
56
+ # { def decision => its spots } for each def in source, at
57
+ # path. Its spots are its rows, as MethodStatements gives them,
58
+ # and the Spot of each of its rescue forks.
59
+ def self.spots_in(source, path, decisions)
60
+ rows = MethodStatements.of(source, path, decisions)
61
+ rescues = RescueForks.of(source, path)
62
+ rows.to_h do |decision, held|
63
+ [decision, held + spots_of(rescues.fetch(decision))]
64
+ end
65
+ end
66
+
67
+ # The Spot of each rescue fork of forks, a def's, as
68
+ # RescueForks gives them.
69
+ def self.spots_of(forks)
70
+ forks.keys.map do |name|
71
+ line, column = name.split(':').last(2).map(&:to_i)
72
+ Spot.new(name:, line:, column:)
30
73
  end
31
74
  end
32
75
 
@@ -44,7 +87,7 @@ module PathCoverage
44
87
  # [name, offset] of node, a loop, where its pass call goes,
45
88
  # when it is among held and its body has a statement; else nil.
46
89
  def self.pass_at(node, path, held)
47
- name = Decision.at(path, node.location)
90
+ name = Decision.at(path, LoopNodes.at(node))
48
91
  return unless held.include?(name) && !LoopNodes.modifier?(node)
49
92
 
50
93
  first = LoopNodes.first_statement(LoopNodes.body_of(node))
@@ -59,7 +102,7 @@ module PathCoverage
59
102
  LoopNodes.loops_in(node.body).filter_map do |loop|
60
103
  keyword = LoopNodes::KEYWORDS.include?(loop.type)
61
104
  held = keyword ? loop.statements : loop
62
- [Decision.at(path, loop.location), held.location] if held
105
+ [Decision.at(path, LoopNodes.at(loop)), held.location] if held
63
106
  end
64
107
  end
65
108
 
@@ -69,7 +112,7 @@ module PathCoverage
69
112
  # no step, as Walk's repeats have them.
70
113
  def self.repeats(source, path, decisions = nil)
71
114
  rows = MethodStatements.of(source, path, decisions)
72
- starts = CardTree.line_starts(source)
115
+ starts = PathsTree.line_starts(source)
73
116
  LoopNodes.defs(Prism.parse(source).value).to_h do |node|
74
117
  decision = Decision.of(path, node)
75
118
  tests = tests_in(node.body, path)
@@ -92,7 +135,8 @@ module PathCoverage
92
135
  LoopNodes.loops_in(node).filter_map do |loop|
93
136
  next unless %i[while_node until_node].include?(loop.type)
94
137
 
95
- [Decision.at(path, loop.location), loop.predicate.location]
138
+ name = Decision.at(path, LoopNodes.at(loop))
139
+ [name, loop.predicate.location]
96
140
  end
97
141
  end
98
142
 
@@ -10,7 +10,7 @@ require_relative 'statement_arms'
10
10
  require_relative 'totals'
11
11
 
12
12
  module PathCoverage
13
- # One row of a method's card, a top-level statement or a
13
+ # One row of a method's flow, a top-level statement or a
14
14
  # decision at its top: where it starts; its top, what it is as
15
15
  # written, eg = for a write; how many paths it has; the name
16
16
  # of its decision, nil when it is no decision; and its own name, by
@@ -19,7 +19,7 @@ module PathCoverage
19
19
  :name)
20
20
 
21
21
  # Finds, without running it, the top-level statements of each
22
- # method in source, one row each in its method's card.
22
+ # method in source, one row each in its method's flow.
23
23
  module MethodStatements
24
24
  # Each function below takes decisions, { decision =>
25
25
  # Condition }, the decisions the run has in source, as
@@ -33,13 +33,13 @@ module PathCoverage
33
33
  end
34
34
 
35
35
  # { def decision => its name } for each def in source, eg
36
- # { "k.rb:4:2" => :ship }, each its card's title.
36
+ # { "k.rb:4:2" => :ship }, each its flow's title.
37
37
  def self.names(source, path, decisions = nil)
38
38
  collect(source, path, decisions).names
39
39
  end
40
40
 
41
41
  # { def decision => its last line } for each def in source, eg
42
- # { "k.rb:4:2" => 6 }, where its card ends.
42
+ # { "k.rb:4:2" => 6 }, where its flow ends.
43
43
  def self.lasts(source, path, decisions = nil)
44
44
  collect(source, path, decisions).lasts
45
45
  end
@@ -53,6 +53,16 @@ module PathCoverage
53
53
  collect(source, path, decisions).reaches
54
54
  end
55
55
 
56
+ # Example: { "k.rb:1:0" => 14 } for a def whose body starts at
57
+ # byte 14.
58
+ # Where each def in source is entered on every call, by the
59
+ # def's own name, so its calls can be counted there. Each is a
60
+ # byte offset. For an endless def it is its body's byte range,
61
+ # to be wrapped.
62
+ def self.entries(source, path, decisions = nil)
63
+ collect(source, path, decisions).entries
64
+ end
65
+
56
66
  # A Collector that has visited each def in source, with
57
67
  # decisions, or, for nil, those of no settings.
58
68
  def self.collect(source, path, decisions)
@@ -65,7 +75,7 @@ module PathCoverage
65
75
  # Example: RATE = x ? 2 : 1 at the top level, its then
66
76
  # taken, gives [1, 2].
67
77
  # [taken, possible] of the paths of the decisions in source on
68
- # no method's card, outside every def, as top_rows gives
78
+ # no method's flow, outside every def, as top_rows gives
69
79
  # them; paths as Result#paths gives them.
70
80
  def self.top_level(source, path, paths, decisions = nil)
71
81
  names = top_rows(source, path, decisions).map(&:decision)
@@ -74,13 +84,13 @@ module PathCoverage
74
84
 
75
85
  # Example: RATE = x ? 2 : 1 gives a ?: row at 1:9.
76
86
  # A Statement row for each decision in source on no method's
77
- # card, outside every def, for the card of its own lines, in
87
+ # flow, outside every def, for the flow of its own lines, in
78
88
  # the order written: where decision mode names it, its
79
89
  # operator, an elsif's elsif, and its paths.
80
90
  def self.top_rows(source, path, decisions = nil)
81
91
  collector = collect(source, path, decisions)
82
- carded = collector.methods.values.flatten.map(&:decision)
83
- loose = collector.decisions.except(*carded)
92
+ in_methods = collector.methods.values.flatten.map(&:decision)
93
+ loose = collector.decisions.except(*in_methods)
84
94
  rows = loose.map do |name, decision|
85
95
  top_row(source, name, decision)
86
96
  end
@@ -152,6 +162,7 @@ module PathCoverage
152
162
  @names = {}
153
163
  @lasts = {}
154
164
  @reaches = {}
165
+ @entries = {}
155
166
  super()
156
167
  end
157
168
 
@@ -159,8 +170,10 @@ module PathCoverage
159
170
  # names and last lines, { def decision => name } and
160
171
  # { def decision => line }; where each row that is no
161
172
  # decision is reached, as MethodStatements.reaches has it;
162
- # and the decisions it was given, { decision => Condition }.
163
- attr_reader :methods, :names, :lasts, :reaches, :decisions
173
+ # where each def is entered, as MethodStatements.entries has
174
+ # it; and the decisions it was given, { decision => Condition }.
175
+ attr_reader :methods, :names, :lasts, :reaches, :entries,
176
+ :decisions
164
177
 
165
178
  # Notes node's name, its last line, its body statements as
166
179
  # its rows, those in its decisions' arms too, but only those
@@ -178,11 +191,11 @@ module PathCoverage
178
191
 
179
192
  # Notes the name and last line of node, the def at decision, and
180
193
  # where it is entered, reached under decision's own name, so its
181
- # card can say how many times it was called.
194
+ # flow can say how many times it was called.
182
195
  def note_def(decision, node)
183
196
  @names[decision] = node.name
184
197
  @lasts[decision] = node.location.end_line
185
- @reaches[decision] = entry_at(node)
198
+ @entries[decision] = entry_at(node)
186
199
  end
187
200
 
188
201
  # Notes where each of tops that is no decision, a return or a