path_coverage 0.5.0 → 0.6.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 (44) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +7 -134
  3. data/lib/path_coverage/call_trace.rb +26 -5
  4. data/lib/path_coverage/calls.rb +24 -0
  5. data/lib/path_coverage/configuration_limits.rb +1 -1
  6. data/lib/path_coverage/console_summary.rb +13 -13
  7. data/lib/path_coverage/fold_rows.rb +2 -2
  8. data/lib/path_coverage/gaps.rb +1 -1
  9. data/lib/path_coverage/json_reader.rb +3 -3
  10. data/lib/path_coverage/json_reports.rb +28 -21
  11. data/lib/path_coverage/json_writer.rb +4 -4
  12. data/lib/path_coverage/kernel.rb +41 -20
  13. data/lib/path_coverage/leaving.rb +14 -6
  14. data/lib/path_coverage/loop_forks.rb +1 -1
  15. data/lib/path_coverage/loop_nodes.rb +41 -5
  16. data/lib/path_coverage/method_loops.rb +56 -12
  17. data/lib/path_coverage/method_statements.rb +25 -12
  18. data/lib/path_coverage/method_ways.rb +48 -20
  19. data/lib/path_coverage/{card_context.rb → paths_context.rb} +16 -16
  20. data/lib/path_coverage/paths_fitting.rb +31 -0
  21. data/lib/path_coverage/paths_forks.rb +72 -0
  22. data/lib/path_coverage/{card_gaps.rb → paths_gaps.rb} +38 -26
  23. data/lib/path_coverage/paths_leaving.rb +40 -0
  24. data/lib/path_coverage/paths_rescues.rb +146 -0
  25. data/lib/path_coverage/{card_scopes.rb → paths_scopes.rb} +6 -6
  26. data/lib/path_coverage/{card_tree.rb → paths_tree.rb} +19 -15
  27. data/lib/path_coverage/{card_way_lists.rb → paths_way_lists.rb} +3 -3
  28. data/lib/path_coverage/paths_ways.rb +193 -0
  29. data/lib/path_coverage/recorder.rb +14 -0
  30. data/lib/path_coverage/render.rb +6 -6
  31. data/lib/path_coverage/rescue_forks.rb +174 -0
  32. data/lib/path_coverage/rewriter.rb +83 -14
  33. data/lib/path_coverage/source_report.rb +4 -4
  34. data/lib/path_coverage/stable_leaves.rb +3 -3
  35. data/lib/path_coverage/statement_arms.rb +23 -9
  36. data/lib/path_coverage/totals.rb +5 -5
  37. data/lib/path_coverage/version.rb +1 -1
  38. data/lib/path_coverage/way_algebras.rb +2 -2
  39. data/lib/path_coverage/ways_report.rb +8 -8
  40. data/lib/path_coverage/ways_totals.rb +7 -7
  41. data/lib/path_coverage/within_methods.rb +61 -61
  42. metadata +12 -8
  43. data/lib/path_coverage/card_forks.rb +0 -44
  44. data/lib/path_coverage/card_ways.rb +0 -196
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dfcf376aea729af49e8d6f48c4c2d73b14c4aeea64cea91ce202849645ad7b38
4
- data.tar.gz: 923eca731e697aacc281f150256279b60c9f1ef9e708fc5a860f2509424f8831
3
+ metadata.gz: 93c35dd6ca0272467a6777cf955e0684a99d4051cb3a6cfa400a15abfb78a5ef
4
+ data.tar.gz: d88286a05560d84ca89a3af86f63e3d6f7ea30eb9fee600b586052a200c5e4d9
5
5
  SHA512:
6
- metadata.gz: ddd032573b9bd84312589e4e34126fee5b5e1aab8041e11c2e15b8ce5ee681e466fc9abc1de2b4ecd6579f9945b61c77cbfce9c551722f9618119854bbc7ead5
7
- data.tar.gz: 312bf6551c5bbec5eb30a35002bb23ef4a87fe45c65833348747ffa75a17f5cb9a1ea2363fe5b77e011e4240fe0aa1c0b389d97b231690be18ac9e8886089437
6
+ metadata.gz: 36dfcb7dcc8a7982e7e196706d36a19a6d6858be4b89780af4dd57f180f4bf5cb6233af90d6f8f87a384af40a5439eb127f5c5f40a6cfd41b9c87554df48dc73
7
+ data.tar.gz: f9e7bf59cce47914b3c63c7565ba8f23663b732164ab067fb1aadb193807d59678ab11b2f7b511817428212a728fd18ba09d9e8df0557d78bd56e439d7a8cf12
data/README.md CHANGED
@@ -9,7 +9,6 @@ coverage.
9
9
 
10
10
  - [Overview](#overview)
11
11
  - [Getting started](#getting-started)
12
- - [Reading the HTML](#reading-the-html)
13
12
  - [Ignoring and treating](#ignoring-and-treating)
14
13
  - [Use when you cannot edit the code!](#use-when-you-cannot-edit-the-code)
15
14
  - [What a run reports](#what-a-run-reports)
@@ -70,123 +69,6 @@ PathCoverage.start do
70
69
  end
71
70
  ```
72
71
 
73
- ## Reading the HTML
74
-
75
- https://path-coverage.github.io/ruby/ shows a demo HTML report
76
- for the mock source tree in `demo/`. `make demo` builds and opens the
77
- same report locally.
78
-
79
- The HTML report, for people, is drawn from the two JSON files, by
80
- [html-reporter](https://github.com/path-coverage/html-reporter).
81
-
82
- ```sh
83
- docker run --rm -v "$PWD/coverage:/paths" ghcr.io/path-coverage/html-reporter /paths
84
- ```
85
-
86
- Clicking a `def` line on a file's HTML page opens that method's
87
- *card* to its left.
88
-
89
- A card has a row for each decision in the method, by `line:col`,
90
- with its counts of paths not taken and taken.
91
-
92
- Clicking a row's `▼` opens it:
93
- - on the left are the decision's parts, one per row: its
94
- operator, eg `&&`, then each operand it tests, nested
95
- operators indented under it.
96
- - on the right is one column for each possible path through it.
97
- - the column's head is `≀0` for a path never taken, and `≀*`
98
- for one taken (hover for its count). Look for zeros!
99
- - the symbols in a column line up with those rows, and
100
- are explained below.
101
-
102
- Hovering over code in the open card's method opens the tree holding
103
- that code, its row lit, or, for a `return` or `raise`, lights its row.
104
- A long piece of code scrolls within its row.
105
-
106
- ### Boolean: `T` `F`
107
-
108
- Many parts of a decision, such as `name.size > 2`, are true or false.
109
-
110
- ![A file's page: line 2, name.size > 2 && name.ascii_only?, its card open to its left](docs/example-outcome.png)
111
-
112
- - `T` the expression was true.
113
- - `F` the expression was false.
114
-
115
- In this example, there was no test which exercised the path where `name.size > 2` was `true`, and
116
- `name.ascii_only?` was `false`.
117
-
118
- ### Unreachable: blank
119
-
120
- Some parts of a decision are not always evaluated.
121
-
122
- ![A file's page: line 2, @x = dee || dum, its card open to its left](docs/example-unreachable.png)
123
-
124
- - "blank" the expression is never evaluated.
125
-
126
- In the second column `dee` was `T`, so because of `||`'s short-circuiting, `dum` is never evaluated.
127
-
128
- ### Taken: `✓`
129
-
130
- A `?:` takes one of its arms.
131
-
132
- ![A file's page: line 2, paid ? 'paid' : 'due', its card open to its left](docs/example-taken.png)
133
-
134
- - `✓` marks the column whose path goes to that arm
135
-
136
- The `paid` row is the `?:`'s predicate, `T` or `F`. Below it is one row
137
- per arm, `? 'paid'` and `: 'due'`. The first column, `paid` false, never
138
- taken, goes to `: 'due'`; the second, `paid` true, to `? 'paid'`.
139
-
140
- An `if`, `unless`, `case` or `||=` shows no arm rows: which arm a path
141
- takes follows from its predicate, and the code in an arm is a row of
142
- its own on the card.
143
-
144
- ### Navigation: `∅` `●`
145
-
146
- `&.` skips the rest of its chain when its receiver is nil.
147
-
148
- ![A file's page: line 2, user&.account&.owner, its card open to its left](docs/example-nil.png)
149
-
150
- - `∅` the receiver was nil, so `&.` skipped the rest of the chain.
151
- - `●` the receiver was not nil, so `&.` made the call.
152
-
153
- In this example, there was no test which exercised the path where
154
- `user` was nil.
155
-
156
- ### Count: `0` `1` `*`
157
-
158
- A block method can call its block on any number of items.
159
-
160
- ![A file's page: line 2, ids.any? { |id| id.odd? }, its card open to its left](docs/example-count.png)
161
-
162
- - `0` the block was called with no items.
163
- - `1` the block was called with one item.
164
- - `*` the block was called with two or more items.
165
-
166
- In this example, there was no test which exercised the path where
167
- the block was called with two or more items and every answer was
168
- `false`.
169
-
170
- ### Both: `TF`
171
-
172
- A block's answers can differ from item to item.
173
-
174
- ![A file's page: line 2, ids.select { |id| id.odd? }, its card open to its left](docs/example-both.png)
175
-
176
- - `T` every answer of the block was true.
177
- - `F` every answer of the block was false.
178
- - `TF` the block's answers were both true and false.
179
-
180
- In this example, there was no test which exercised the path where
181
- the block was called with two or more items and every answer was
182
- `false`.
183
-
184
- ### Lots of paths!
185
-
186
- Ruby code can be very compact so short expressions can generate lots of paths!
187
-
188
- ![A file's page: line 2, if user&.admin? && ids.select { |id| id.odd? }.any?, its card open to its left](docs/example-overview.png)
189
-
190
72
  ## Ignoring and treating
191
73
 
192
74
  `ignore '&&=', '||='` stops `&&=` and `||=` writes being measured.
@@ -473,20 +355,20 @@ cannot be closed is the lesser error than hiding one that can.
473
355
 
474
356
  ### Methods
475
357
 
476
- Each method has a card, named by the `path:line:column` of its `def`,
358
+ Each method has a flow, named by the `path:line:column` of its `def`,
477
359
  with a row for each statement that shapes its paths: each decision, and
478
360
  each `return` or `raise`, which ends a path. Any other statement, eg
479
361
  `x = 1`, runs exactly when what holds it does, so is no row. A decision's
480
362
  row counts its paths; a `return` or `raise` row is one path, taken
481
- when it is reached. A method's total adds up its rows. Its title says
482
- how many times it was called, so a method never called is a gap,
483
- even one with no rows. A file's code outside any method gets a card
363
+ when it is reached. A method's total adds up its rows. Its `calls`
364
+ count how many times it was called, so a method never called is a gap,
365
+ even one with no rows. A file's code outside any method gets a flow
484
366
  of its own.
485
367
 
486
- The JSON report's `cards` hold each card's rows, its `ways` and its
368
+ The JSON report's `flows` hold each flow's rows, its `ways` and its
487
369
  `calls`, and its `tree` says how those rows nest.
488
370
 
489
- A method's card counts only the paths its method can take. A
371
+ A method's flow counts only the paths its method can take. A
490
372
  local variable the method never writes, eg a parameter, read as a
491
373
  leaf outside any block, has one outcome all through a call, so
492
374
 
@@ -569,7 +451,7 @@ PathCoverage.leave(PathCoverage.enter("k.rb:2:2", 2), PathCoverage.record("k.rb:
569
451
  An `if` decision's leave call wraps its predicate alone, as its path is
570
452
  decided there, so an arm that returns or raises still counts it.
571
453
  Each statement that is no decision gets a reach call before it, counting
572
- its run, for its card's row.
454
+ its run, for its flow's row.
573
455
 
574
456
  A command call such as `ok? id` (after `and`) is not valid as a bare
575
457
  argument, so that leaf alone gets parentheses of its own, and so does a
@@ -689,12 +571,3 @@ a copy of `lib/` renamed `PathCoverageSelf` (`test/self_coverage.rb`), so
689
571
  the tests start and restart `PathCoverage` as always. SimpleCov's figures
690
572
  for `lib/` are the same with it as without.
691
573
 
692
- `make demo` (not part of `make all`) runs `demo/run.rb` through
693
- `bin/build_demo.sh`, writing the report to `site/` and opening it. The
694
- Pages workflow (`.github/workflows/pages.yml`) runs the same script on
695
- each push to `main` and publishes `site/`.
696
-
697
- `bin/capture_card_images.sh` runs the examples in `demo/cards/` and
698
- saves this README's screenshots, `docs/example-*.png`, with headless
699
- Chrome. Run it again whenever the card's look changes.
700
-
@@ -13,6 +13,7 @@ module PathCoverage
13
13
  @owners = {}
14
14
  @ways = {}
15
15
  @stack_key = :"path_coverage_calls_#{object_id}"
16
+ @depths_key = :"path_coverage_depths_#{object_id}"
16
17
  end
17
18
 
18
19
  # { unit's name => { way => count } } of every way ended so
@@ -33,15 +34,28 @@ module PathCoverage
33
34
  # reaches its entry.
34
35
  def call
35
36
  stack.push(nil)
37
+ depths.push(nil)
36
38
  end
37
39
 
38
- # Notes the statement named name reached: a method's entry
39
- # starts its call's way, unless no call is under way to take
40
- # it, as when the call began before this trace did; any other
41
- # row is a step of the innermost call of the method holding it.
40
+ # Starts a call of a covered method a retry can restart, its
41
+ # frame at depth. A call at the depth of the call under way is
42
+ # that call restarting, so it starts nothing.
43
+ def restartable_call(depth)
44
+ return if depths.last == depth
45
+
46
+ stack.push(nil)
47
+ depths.push(depth)
48
+ end
49
+
50
+ # Notes the statement named name reached. A method's entry
51
+ # starts its call's way. It does not when no call is under way
52
+ # to take it, as when the call began before this trace did. Nor
53
+ # does it when the call's way has started already, as when a
54
+ # retry runs the body again. Any other row is a step of the
55
+ # innermost call of the method holding it.
42
56
  def reach(name)
43
57
  if @entries.key?(name)
44
- enter(name) unless stack.empty?
58
+ enter(name) unless stack.empty? || stack.last
45
59
  else
46
60
  innermost(@owners[name])&.step([name])
47
61
  end
@@ -61,6 +75,7 @@ module PathCoverage
61
75
 
62
76
  # Ends the innermost call under way, counting its way.
63
77
  def return
78
+ depths.pop
64
79
  stack.pop&.finish
65
80
  end
66
81
 
@@ -79,6 +94,12 @@ module PathCoverage
79
94
  Thread.current[@stack_key] ||= []
80
95
  end
81
96
 
97
+ # The frame depth of each call under way in the current thread,
98
+ # as stack holds them. It is nil for a call no retry restarts.
99
+ def depths
100
+ Thread.current[@depths_key] ||= []
101
+ end
102
+
82
103
  # The Walk of the innermost call under way of the method
83
104
  # whose entry is named entry, else nil.
84
105
  def innermost(entry)
@@ -102,4 +102,28 @@ module PathCoverage
102
102
  def self.pass(name)
103
103
  @recorder.pass(name)
104
104
  end
105
+
106
+ # The call rewritten code makes around the last statement of a
107
+ # body with a rescue. It notes that the fork named name
108
+ # completed, its body running to its end. It returns value
109
+ # unchanged.
110
+ def self.completed(name, value)
111
+ @recorder.completed(name, value)
112
+ end
113
+
114
+ # The call rewritten code makes first in the body of the rescue
115
+ # numbered number of the rescue fork named name. It notes that
116
+ # this rescue ran.
117
+ def self.rescued(name, number)
118
+ @recorder.rescued(name, number)
119
+ end
120
+
121
+ # The call rewritten code makes around the rescue of a rescue
122
+ # modifier, its rescue numbered number of the rescue fork named
123
+ # name. It notes that this rescue ran, and returns value
124
+ # unchanged.
125
+ def self.ran(name, number, value)
126
+ @recorder.rescued(name, number)
127
+ value
128
+ end
105
129
  end
@@ -1,5 +1,5 @@
1
1
  module PathCoverage
2
- # The settings of a PathCoverage.start block that limit a card's
2
+ # The settings of a PathCoverage.start block that limit a flow's
3
3
  # ways: the most paths a method may have and still be checked,
4
4
  # and whether a loop counts as a decision. It relies on its
5
5
  # includer's ConfigurationError, and sets @max_paths and @loops
@@ -8,37 +8,37 @@ module PathCoverage
8
8
  # groups configured each group is a heading, its own totals
9
9
  # indented under it, and there is no total over every group.
10
10
  module ConsoleSummary
11
- # The summary lines of decision_paths and, when cards, { def
11
+ # The summary lines of decision_paths and, when flows, { def
12
12
  # decision => [taken, size] }, are given, as they are when
13
- # methods are measured, of cards; groups is { name =>
13
+ # methods are measured, of flows; groups is { name =>
14
14
  # [glob, ...] }.
15
- def self.lines(decision_paths, cards, groups = {})
15
+ def self.lines(decision_paths, flows, groups = {})
16
16
  if groups.empty?
17
17
  return totals_lines(totals(decision_paths,
18
- cards))
18
+ flows))
19
19
  end
20
20
 
21
- all = group_totals(decision_paths, cards, groups)
21
+ all = group_totals(decision_paths, flows, groups)
22
22
  all.flat_map do |name, totals|
23
23
  ["#{name}:", *totals_lines(totals).map { |line| " #{line}" }]
24
24
  end
25
25
  end
26
26
 
27
27
  # { 'alone' => [taken, possible] } of decision_paths, with
28
- # 'combined' => [taken, possible] of cards when given.
29
- def self.totals(decision_paths, cards)
28
+ # 'combined' => [taken, possible] of flows when given.
29
+ def self.totals(decision_paths, flows)
30
30
  totals = { 'alone' => Totals.alone(decision_paths) }
31
- return totals if cards.nil?
31
+ return totals if flows.nil?
32
32
 
33
- totals.merge('combined' => Totals.combined(cards))
33
+ totals.merge('combined' => Totals.combined(flows))
34
34
  end
35
35
 
36
36
  # { group name => totals } of each group's own decisions and
37
- # cards alone.
38
- def self.group_totals(decision_paths, cards, groups)
37
+ # flows alone.
38
+ def self.group_totals(decision_paths, flows, groups)
39
39
  groups.to_h do |name, globs|
40
- own_cards = cards ? own(cards, globs) : nil
41
- [name, totals(own(decision_paths, globs), own_cards)]
40
+ own_flows = flows ? own(flows, globs) : nil
41
+ [name, totals(own(decision_paths, globs), own_flows)]
42
42
  end
43
43
  end
44
44
 
@@ -6,7 +6,7 @@ require_relative 'kinds'
6
6
 
7
7
  module PathCoverage
8
8
  # The tree of a decision, from DecisionTree.of, as its row's
9
- # fold in a card shows it: a grid whose columns are the
9
+ # fold in a flow shows it: a grid whose columns are the
10
10
  # decision's paths, as page data holds its rows, each a kind,
11
11
  # a branch, a label, where its code is and a cell per path.
12
12
  module FoldRows
@@ -81,7 +81,7 @@ module PathCoverage
81
81
  FoldCells.values(arm[:cells], FoldCells::TAKEN)
82
82
  end
83
83
 
84
- # The operands of condition a card's fold shows: a ?:'s arms,
84
+ # The operands of condition a flow's fold shows: a ?:'s arms,
85
85
  # as its arms are part of its one expression; for any other,
86
86
  # eg an if, a ||= or a case, no arm, as which arm a path takes
87
87
  # follows from its leaves, but each arm's body, eg a when's
@@ -32,7 +32,7 @@ module PathCoverage
32
32
  # Example: ["k.rb:5:11 path taken that its method rules out:
33
33
  # `a` true"].
34
34
  # One line per path taken, or return or raise reached, that
35
- # its method rules out, ruled as CardContext.ruled_out gives
35
+ # its method rules out, ruled as PathsContext.ruled_out gives
36
36
  # them, texts being { decision => each leaf's source text }.
37
37
  # Such a line shows a fact derived from the code to be wrong.
38
38
  def self.ruled_by_method(ruled, texts)
@@ -51,9 +51,9 @@ module PathCoverage
51
51
  end
52
52
  end
53
53
 
54
- # True when report, a JSON report, lists its cards' ways: one a
55
- # run wrote, which names the max_paths its cards were listed
56
- # with; not one the :post reporter sent, which has no cards.
54
+ # True when report, a JSON report, lists its flows' ways: one a
55
+ # run wrote, which names the max_paths its flows were listed
56
+ # with; not one the :post reporter sent, which has no flows.
57
57
  def self.listed_ways?(report)
58
58
  report.key?('max_paths')
59
59
  end
@@ -1,6 +1,6 @@
1
1
  require 'fileutils'
2
2
  require 'json'
3
- require_relative 'card_gaps'
3
+ require_relative 'paths_gaps'
4
4
  require_relative 'console_summary'
5
5
  require_relative 'json_writer'
6
6
  require_relative 'paths_report'
@@ -27,26 +27,26 @@ module PathCoverage
27
27
  paths = taken.values_at(:decision_paths, :outside)
28
28
  recorded = Recorded.new(operands, *paths)
29
29
  reached = taken[:reached]
30
- cards = WithinMethods.listed(files, config, recorded,
30
+ flows = WithinMethods.listed(files, config, recorded,
31
31
  [reached, taken[:ways]])
32
32
  tree = WithinMethods.tree(files, config, recorded)
33
- write_json(recorded, files, config, [cards, reached, tree])
34
- write_ways(cards, files, config, tree)
35
- write_source(files, config, recorded.paths, cards)
33
+ write_json(recorded, files, config, [flows, reached, tree])
34
+ write_ways(flows, files, config, tree)
35
+ write_source(files, config, recorded.paths, flows)
36
36
  end
37
37
 
38
38
  # Writes recorded as the JSON report at config's json_path:
39
39
  # its operands, decision paths, paths a treatment rules out,
40
- # totals, cards, each def's card, as WithinMethods.listed
41
- # gives them, tree, how each card's rows nest, the statements
40
+ # totals, flows, each def's flow, as WithinMethods.listed
41
+ # gives them, tree, how each flow's rows nest, the statements
42
42
  # reached, when known, and config's fingerprint and limits.
43
43
  def self.write_json(recorded, files, config, listed)
44
- cards, reached, tree = listed
44
+ flows, reached, tree = listed
45
45
  report = PathsReport.of(recorded.paths, files)
46
46
  totals = totals(recorded.paths, files, config, reached)
47
47
  limits = { 'loops' => config.loops?,
48
48
  'max_paths' => config.path_limit }
49
- parts = { totals:, outside: recorded.outside, cards:, tree:,
49
+ parts = { totals:, outside: recorded.outside, flows:, tree:,
50
50
  fingerprint: config.fingerprint, limits: }
51
51
  parts[:reached] = reached if reached
52
52
  path = config.json_path
@@ -54,12 +54,21 @@ module PathCoverage
54
54
  end
55
55
 
56
56
  # Writes the ways lines, each method's untested paths, as
57
- # CardGaps.lines gives them for cards, beside the JSON report,
57
+ # PathsGaps.lines gives them for flows, beside the JSON report,
58
58
  # at ways_path, so a later check, or the :ways reporter, can
59
- # show them; tree is how each card's rows nest, as
59
+ # show them; tree is how each flow's rows nest, as
60
60
  # WithinMethods.tree gives it.
61
- def self.write_ways(cards, files, config, tree)
62
- parents = tree.flat_map do |path, nested|
61
+ def self.write_ways(flows, files, config, tree)
62
+ texts = PathsReport.texts(files)
63
+ lines = PathsGaps.lines(flows, texts, parents_of(tree))
64
+ File.write(ways_path(config), lines.map { "#{_1}\n" }.join)
65
+ end
66
+
67
+ # { row's decision => its parent's decision } of each row in
68
+ # tree that runs inside another row, as WithinMethods.tree
69
+ # gives it. Each decision is named path:line:column.
70
+ def self.parents_of(tree)
71
+ tree.flat_map do |path, nested|
63
72
  nested.values.flat_map do |rows|
64
73
  rows.filter_map do |at, place|
65
74
  next unless place['parent']
@@ -68,8 +77,6 @@ module PathCoverage
68
77
  end
69
78
  end
70
79
  end.to_h
71
- lines = CardGaps.lines(cards, PathsReport.texts(files), parents)
72
- File.write(ways_path(config), lines.map { "#{_1}\n" }.join)
73
80
  end
74
81
 
75
82
  # Where the ways lines go, beside the JSON report.
@@ -78,9 +85,9 @@ module PathCoverage
78
85
  end
79
86
 
80
87
  # Writes the source report beside the JSON report, from
81
- # files, decision_paths and cards, as write has them.
82
- def self.write_source(files, config, decision_paths, cards)
83
- source = SourceReport.of(files, config, decision_paths, cards)
88
+ # files, decision_paths and flows, as write has them.
89
+ def self.write_source(files, config, decision_paths, flows)
90
+ source = SourceReport.of(files, config, decision_paths, flows)
84
91
  path = config.json_path.sub(/\.json\z/, '.source.json')
85
92
  File.write(path, JSON.generate(source))
86
93
  end
@@ -89,13 +96,13 @@ module PathCoverage
89
96
  # within each decision, and within each method; with groups
90
97
  # configured, each group's own too, under groups.
91
98
  def self.totals(decision_paths, files, config, reached)
92
- cards = WithinMethods.cards(files, config, decision_paths,
99
+ flows = WithinMethods.flows(files, config, decision_paths,
93
100
  reached)
94
- totals = ConsoleSummary.totals(decision_paths, cards)
101
+ totals = ConsoleSummary.totals(decision_paths, flows)
95
102
  groups = config.groups
96
103
  return totals if groups.empty?
97
104
 
98
- own = ConsoleSummary.group_totals(decision_paths, cards, groups)
105
+ own = ConsoleSummary.group_totals(decision_paths, flows, groups)
99
106
  totals.merge('groups' => own)
100
107
  end
101
108
  end
@@ -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
@@ -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,