path_coverage 0.2.0 → 0.4.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 (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +172 -121
  3. data/lib/path_coverage/calls.rb +4 -11
  4. data/lib/path_coverage/card.rb +14 -0
  5. data/lib/path_coverage/card_tree.rb +186 -0
  6. data/lib/path_coverage/card_ways.rb +140 -0
  7. data/lib/path_coverage/condition.rb +11 -4
  8. data/lib/path_coverage/configuration.rb +1 -17
  9. data/lib/path_coverage/configuration_paths.rb +4 -2
  10. data/lib/path_coverage/console_summary.rb +15 -17
  11. data/lib/path_coverage/gaps.rb +12 -9
  12. data/lib/path_coverage/html/file.html +5 -5
  13. data/lib/path_coverage/html/index.html +3 -1
  14. data/lib/path_coverage/html/settings.js +50 -0
  15. data/lib/path_coverage/html/side.js +470 -432
  16. data/lib/path_coverage/html/style.css +345 -226
  17. data/lib/path_coverage/html/theme.js +27 -0
  18. data/lib/path_coverage/html_branch.rb +30 -0
  19. data/lib/path_coverage/html_cards.rb +88 -0
  20. data/lib/path_coverage/html_cells.rb +71 -0
  21. data/lib/path_coverage/html_file.rb +82 -68
  22. data/lib/path_coverage/html_index.rb +28 -29
  23. data/lib/path_coverage/html_index_head.rb +10 -30
  24. data/lib/path_coverage/html_labels.rb +16 -0
  25. data/lib/path_coverage/html_methods.rb +66 -0
  26. data/lib/path_coverage/html_nav.rb +50 -50
  27. data/lib/path_coverage/html_report.rb +45 -49
  28. data/lib/path_coverage/html_statements.rb +186 -0
  29. data/lib/path_coverage/html_syntax.rb +167 -0
  30. data/lib/path_coverage/html_template.rb +6 -0
  31. data/lib/path_coverage/html_tree.rb +90 -88
  32. data/lib/path_coverage/json_reader.rb +21 -14
  33. data/lib/path_coverage/json_writer.rb +26 -13
  34. data/lib/path_coverage/kernel.rb +32 -51
  35. data/lib/path_coverage/kinds/block.rb +2 -1
  36. data/lib/path_coverage/kinds/case.rb +7 -9
  37. data/lib/path_coverage/kinds/choice.rb +3 -7
  38. data/lib/path_coverage/kinds/contains.rb +9 -5
  39. data/lib/path_coverage/kinds/safe.rb +6 -18
  40. data/lib/path_coverage/kinds/write.rb +0 -6
  41. data/lib/path_coverage/manifest.rb +5 -3
  42. data/lib/path_coverage/manifest_blocks.rb +3 -4
  43. data/lib/path_coverage/manifest_case.rb +2 -3
  44. data/lib/path_coverage/manifest_choices.rb +41 -20
  45. data/lib/path_coverage/manifest_folding.rb +48 -11
  46. data/lib/path_coverage/merge.rb +3 -3
  47. data/lib/path_coverage/method_statements.rb +305 -0
  48. data/lib/path_coverage/recorder.rb +12 -31
  49. data/lib/path_coverage/render.rb +19 -22
  50. data/lib/path_coverage/rewriter.rb +36 -55
  51. data/lib/path_coverage/site_paths.rb +8 -0
  52. data/lib/path_coverage/site_tree.rb +9 -0
  53. data/lib/path_coverage/statement_arms.rb +194 -0
  54. data/lib/path_coverage/summary.rb +11 -15
  55. data/lib/path_coverage/totals.rb +6 -12
  56. data/lib/path_coverage/version.rb +1 -1
  57. data/lib/path_coverage/within_methods.rb +177 -0
  58. metadata +19 -12
  59. data/lib/path_coverage/html/toggle.js +0 -12
  60. data/lib/path_coverage/html_paths.rb +0 -138
  61. data/lib/path_coverage/path_gaps.rb +0 -41
  62. data/lib/path_coverage/path_manifest.rb +0 -110
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c68ba875b18ba000eb6be70f5c7bcb294ab4d50915868b913825c3f53bc60a93
4
- data.tar.gz: 2beaba1e32cd4110ff9d04d3ab06e8c689395748566502a092175d8a339c9a5d
3
+ metadata.gz: bcbf082eb9c226b16a8fa03916aa7be19184fade6b6d762514713158b563222f
4
+ data.tar.gz: edab6c4a7cbc923e21fefc7f98292c5900d06c42ae6c0e889033ccb6f6c39728
5
5
  SHA512:
6
- metadata.gz: 6539c5ce97bd892ee616d0b1f780c79f18624381a404d3e6d2c60f6245957243b5ac7c395e130496fbbb2ba0ac6d69b4e9daf8e4fd235cb124d184aa3f8e5ad2
7
- data.tar.gz: f72f62b796ad07d0290f56a88bc920ef1088b81abc04c6667ae1ee612e97499cc2f40ca6689acb1e8df35ebff607b463e8ec5ef271de11227bcf01017e98bb42
6
+ metadata.gz: 46cc81a9adaf89ed954e57715670f6a2c456e720c35490d0e19290712c963ba9bc5478291145a766cd1717af4c24fda4e3da36b1858e9b4fc130fb4d23d70eec
7
+ data.tar.gz: 6ed68e326285dd500eca2a5cb6553dfb921d6827dcd90cb7899722bc94d509198723a814b5334b3676b115aede150f9160690add5ad2e696100125add13f2ab1
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Gem Version](https://img.shields.io/gem/v/path_coverage)](https://rubygems.org/gems/path_coverage)
4
4
  [![Downloads](https://img.shields.io/gem/dt/path_coverage)](https://rubygems.org/gems/path_coverage)
5
- [![License](https://img.shields.io/github/license/cyber-dojo/path_coverage)](LICENSE.md)
5
+ [![License](https://img.shields.io/github/license/path-coverage/ruby)](LICENSE.md)
6
6
 
7
7
  Path coverage for Ruby, to run alongside SimpleCov's line and branch
8
8
  coverage.
@@ -12,81 +12,98 @@ Status: sites cover `&&`, `||`, `and`, `or`, the ternary `?:`,
12
12
  with a subject, and the block methods `any?` `all?` `none?` `find`
13
13
  `detect` `find_index` `take_while` `select` `filter` `reject`
14
14
  `partition` `count` `filter_map`, the writes `||=` and `&&=`, and `&.`
15
- safe-navigation chains. Method paths cover `if` decisions.
15
+ safe-navigation chains. Method paths cover every site at a method's
16
+ top level.
16
17
  More decisions hidden inside calls come next (see Roadmap).
17
18
 
18
- Demo: https://cyber-dojo.github.io/path_coverage/ shows the HTML report
19
+ Demo: https://path-coverage.github.io/ruby/ shows the HTML report
19
20
  for the mock source tree in `demo/`. `make demo` builds and opens the
20
21
  same report locally.
21
22
 
22
- ## Getting started
23
+ ## Overview
24
+
25
+ A *site* is one decision in your code: an `&&`, `||`, `if`,
26
+ `case`, `&.` chain, `||=`, or a block method such as `any?`.
27
+ Each way it can go is a *path*. For example
23
28
 
24
29
  ```ruby
25
- # test coverage config, before any application code is required
26
- require 'simplecov'
27
- require 'path_coverage'
28
- SimpleCov.start do
29
- # ...
30
- end
31
- PathCoverage.start do
32
- root APP_DIR # cover globs and site names are relative to it
33
- cover 'source/**/*.rb' # each matching file is instrumented as it loads
34
- cover 'test/**/*.rb' # more than one cover glob may be given
35
- skip 'source/vendor/**/*.rb' # except these, though a cover glob matches them
36
- coverage_dir '/tmp/coverage' # where path_coverage.json is written
37
- measure :decisions, :methods # :decisions alone unless :methods is named
38
- report :gaps, :html # the reporters run at exit (default :gaps);
39
- # also :summary and :post
40
- group 'code', 'source/**/*.rb' # each group a tab of its own on index.html
41
- group 'test', 'test/**/*.rb'
42
- ignore '&&=' # operators whose sites are not measured
43
- treat 'name', as: :present # a call to name is never nil
44
- treat 'any?', as: :nonempty # any? is true when its receiver has items
45
- end
30
+ name.size > 2 && name.ascii_only?
46
31
  ```
47
32
 
33
+ has three paths: true then true, true then false, and false
34
+ alone, as `&&` then skips its right side.
35
+
36
+ Line and branch coverage can be 100% while a path was never
37
+ taken. path_coverage counts how often each path was taken:
38
+ within each site, and within each method, through its sites.
39
+
40
+ As the run exits it writes its report:
41
+ - `path_coverage.json`, for AIs and other tools.
42
+ - `index.html`, with a page per file, for people.
48
43
 
49
44
  ## Reading the HTML
50
45
 
51
- Clicking a line number on a file's HTML page opens that line's popout.
46
+ Clicking a `def` line on a file's HTML page opens that method's
47
+ *card* to its left. One card is always open.
48
+
49
+ A card has a row for each site in the method, by `line:col`,
50
+ with its counts of paths not taken and taken.
51
+
52
+ Clicking a row's `▼` opens it:
53
+ - on the left is the site's code, as a tree.
54
+ - on the right is one column for each possible path through it.
55
+ - the column's head is `≀0` for a path never taken, and `≀*`
56
+ for one taken (hover for its count). Look for zeros!
57
+ - the symbols in a column line up with the tree's rows, and
58
+ are explained below.
52
59
 
53
- Each popout has two sections:
54
- - on the left is a hierarchical representation of the code fragment.
55
- - on the right is one column for each possible path through that fragment.
56
- - at the top of the column is the count of that path. Look for zeros!
57
- - the symbols in the column align with their fragment and are explained below.
60
+ Hovering over code in the open card's method opens the tree holding
61
+ that code, its row lit, or, for a `return` or `raise`, lights its row.
62
+ A long piece of code scrolls within its row.
58
63
 
59
- ### Boolean: `T` `F`
64
+ ### Boolean: `T` `F`
60
65
 
61
- ![A file's page: line 2, name.size > 2 && name.ascii_only?, its button pressed, its popout open beside it](docs/example-outcome.png)
66
+ Many parts of a site, such as `name.size > 2`, are true or false.
67
+
68
+ ![A file's page: line 2, name.size > 2 && name.ascii_only?, its card open to its left](docs/example-outcome.png)
62
69
 
63
70
  - `T` the expression was true.
64
71
  - `F` the expression was false.
65
72
 
66
- In this example, there was no test which exercised the path where `name.size > 2` was `true`, and
73
+ In this example, there was no test which exercised the path where `name.size > 2` was `true`, and
67
74
  `name.ascii_only?` was `false`.
68
75
 
69
- ### Unreachable: blank
76
+ ### Unreachable: blank
77
+
78
+ Some parts of a site are not always evaluated.
70
79
 
71
- ![A file's page: line 2, @x = dee || dum, its button pressed, its popout open beside it](docs/example-unreachable.png)
80
+ ![A file's page: line 2, @x = dee || dum, its card open to its left](docs/example-unreachable.png)
72
81
 
73
- - "blank" the expression is never evaluated.
82
+ - "blank" the expression is never evaluated.
74
83
 
75
84
  In the second column `dee` was `T`, so because of `||`'s short-circuiting, `dum` is never evaluated.
76
85
 
77
86
  ### Taken: `✓`
78
87
 
79
- ![A file's page: line 2, if paid, its button pressed, its popout open beside it](docs/example-taken.png)
88
+ A `?:` takes one of its arms.
89
+
90
+ ![A file's page: line 2, paid ? 'paid' : 'due', its card open to its left](docs/example-taken.png)
80
91
 
81
- - `✓` marks the column whose path took that "arm"
92
+ - `✓` marks the column whose path goes to that arm
82
93
 
83
- The `paid` row is the `if`'s predicate, `T` or `F`. Below it is one row
84
- per arm, named `then` and `else`. The first column, `paid` false, took `else`; the
85
- second, `paid` true, took `then`.
94
+ The `paid` row is the `?:`'s predicate, `T` or `F`. Below it is one row
95
+ per arm, `? 'paid'` and `: 'due'`. The first column, `paid` false, never
96
+ taken, goes to `: 'due'`; the second, `paid` true, to `? 'paid'`.
97
+
98
+ An `if`, `unless`, `case` or `||=` shows no arm rows: which arm a path
99
+ takes follows from its predicate, and the code in an arm is a row of
100
+ its own on the card.
86
101
 
87
102
  ### Navigation: `∅` `●`
88
103
 
89
- ![A file's page: line 2, user&.account&.owner, its button pressed, its popout open beside it](docs/example-nil.png)
104
+ `&.` skips the rest of its chain when its receiver is nil.
105
+
106
+ ![A file's page: line 2, user&.account&.owner, its card open to its left](docs/example-nil.png)
90
107
 
91
108
  - `∅` the receiver was nil, so `&.` skipped the rest of the chain.
92
109
  - `●` the receiver was not nil, so `&.` made the call.
@@ -96,7 +113,9 @@ In this example, there was no test which exercised the path where
96
113
 
97
114
  ### Count: `0` `1` `*`
98
115
 
99
- ![A file's page: line 2, ids.any? { |id| id.odd? }, its button pressed, its popout open beside it](docs/example-count.png)
116
+ A block method can call its block on any number of items.
117
+
118
+ ![A file's page: line 2, ids.any? { |id| id.odd? }, its card open to its left](docs/example-count.png)
100
119
 
101
120
  - `0` the block was called with no items.
102
121
  - `1` the block was called with one item.
@@ -108,7 +127,9 @@ the block was called with two or more items and every answer was
108
127
 
109
128
  ### Both: `TF`
110
129
 
111
- ![A file's page: line 2, ids.select { |id| id.odd? }, its button pressed, its popout open beside it](docs/example-both.png)
130
+ A block's answers can differ from item to item.
131
+
132
+ ![A file's page: line 2, ids.select { |id| id.odd? }, its card open to its left](docs/example-both.png)
112
133
 
113
134
  - `T` every answer of the block was true.
114
135
  - `F` every answer of the block was false.
@@ -122,8 +143,33 @@ the block was called with two or more items and every answer was
122
143
 
123
144
  Ruby code can be very compact so short expressions can generate lots of paths!
124
145
 
125
- ![A file's page: line 2, if user&.admin? && ids.select { |id| id.odd? }.any?, its button pressed, its popout open beside it](docs/example-overview.png)
146
+ ![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)
147
+
148
+
149
+ ## Getting started
126
150
 
151
+ ```ruby
152
+ # test coverage config, before any application code is required
153
+ require 'simplecov'
154
+ require 'path_coverage'
155
+ SimpleCov.start do
156
+ # ...
157
+ end
158
+ PathCoverage.start do
159
+ root APP_DIR # cover globs and site names are relative to it
160
+ cover 'source/**/*.rb' # each matching file is instrumented as it loads
161
+ cover 'test/**/*.rb' # more than one cover glob may be given
162
+ skip 'source/vendor/**/*.rb' # except these, though a cover glob matches them
163
+ coverage_dir '/tmp/coverage' # where path_coverage.json is written
164
+ report :gaps, :html # the reporters run at exit (default :gaps);
165
+ # also :summary and :post
166
+ group 'code', 'source/**/*.rb' # each group a tab of its own on index.html
167
+ group 'test', 'test/**/*.rb'
168
+ ignore '&&=' # operators whose sites are not measured
169
+ treat 'name', as: :present # a call to name is never nil
170
+ treat 'any?', as: :nonempty # any? is true when its receiver has items
171
+ end
172
+ ```
127
173
 
128
174
  ## Use when you cannot edit the code!
129
175
 
@@ -171,10 +217,11 @@ As the process exits:
171
217
 
172
218
  - `path_coverage.json` is always written to `coverage_dir`: every site
173
219
  of every covered file, with each operand's true and false counts under
174
- `sites`, each site's paths under `paths`, each method's paths taken
175
- under `method_paths` when `:methods` is measured, and the paths taken
220
+ `sites`, each site's paths under `paths`, each method's card under
221
+ `cards`, how each card's rows nest under `tree`, the statements
222
+ reached under `reached`, and the paths taken
176
223
  of all possible under `totals`, as [taken, possible]: `decisions`,
177
- `methods` when `:methods` is measured, and, with groups, each group's
224
+ `methods`, and, with groups, each group's
178
225
  own under `groups`, eg `"groups": {"code": {"decisions": [481, 567]}}`.
179
226
  It also names the kinds of site measured under `kinds`, any `ignored`
180
227
  operators and `treats`, and any paths taken that a treatment rules
@@ -188,24 +235,21 @@ As the process exits:
188
235
  every run left. A report whose kinds, `ignored` or `treats` differ
189
236
  from `config`'s is refused, as its sites list their paths
190
237
  differently.
191
- - `:gaps` prints one line per path never taken, in manifest order: a
192
- site's naming the outcome of each leaf on it, a method's the outcome
193
- of each decision on it, by line:
238
+ - `:gaps` prints one line per site path never taken, in manifest
239
+ order, naming the outcome of each leaf on it:
194
240
 
195
241
  ```text
196
242
  source/kata.rb:3:23 path never taken: `ok?(id)` true, `new?(id)` false
197
243
  source/kata.rb:4:24 never reached
198
- source/fee.rb:2:2 fee path never taken: line 4 true, line 7 false
199
244
  ```
200
245
 
201
246
  ### Decisions
202
247
 
203
- The paths within each decision are always measured, whatever `measure`
204
- names.
205
-
206
248
  A site is named by the `path:line:column` where it starts, the path
207
- relative to `root`. A site in a covered file the run never loaded is
208
- still reported, as `never reached`.
249
+ relative to `root`; but an `if` or `unless` at its keyword, a `?:` at
250
+ its `?`, and a block method's call at its method, so no two sites
251
+ share a name. A site in a covered file the run never loaded is still
252
+ reported, as `never reached`.
209
253
 
210
254
  A path is the outcome of each leaf one evaluation of the site reached, in
211
255
  order; a leaf that short-circuiting skipped is not on it. Each leaf is
@@ -308,51 +352,76 @@ cannot be closed is the lesser error than hiding one that can.
308
352
 
309
353
  ### Methods
310
354
 
311
- With `measure :decisions, :methods`, each method with two or more
312
- decisions also counts each call's path through them: the outcome of each
313
- decision it reached, in order. A method is named by the `path:line:column`
314
- of its `def`, then its name. Each decision is one outcome on the path,
315
- however many leaves its predicate has; those are its site's to show. So a
316
- site's columns stay its own leaves' combinations, and a method's its own
317
- decisions', rather than multiplying together. A decision whose arm always
318
- leaves the method ends the path there.
355
+ Each method has a card, named by the `path:line:column` of its `def`,
356
+ with a row for each statement that shapes its paths: each site, and
357
+ each `return` or `raise`, which ends a path. Any other statement, eg
358
+ `x = 1`, runs exactly when what holds it does, so is no row. A site's
359
+ row counts its paths; a `return` or `raise` row is one path, taken
360
+ when it is reached. A method's total adds up its rows. Its title says
361
+ how many times it was called, so a method never called is a gap,
362
+ even one with no rows. A file's code outside any method gets a card
363
+ of its own.
319
364
 
320
- The JSON report's `method_paths` lists each method's paths taken, with
321
- their counts, so a later render, or several runs' reports merged, still
322
- shows them. A report with none, eg from a run that did not measure
323
- `:methods`, renders as no method path taken:
365
+ The JSON report's `cards` hold each card's rows, its `ways` (the ways
366
+ one call can go through it) and its `calls`:
324
367
 
325
368
  ```json
326
- "method_paths": {
327
- "source/fee.rb:2:2": [
328
- { "path": [true, false], "count": 1 }
329
- ]
369
+ "cards": {
370
+ "label.rb:1:0": {
371
+ "name": "label", "first": 1, "last": 7, "ways": 2, "calls": 1,
372
+ "rows": [
373
+ { "at": "2:02", "top": "if", "paths": 2, "taken": 1, "site": "label.rb:2:2" },
374
+ { "at": "3:04", "top": "return", "paths": 1, "taken": 1, "site": null },
375
+ { "at": "5:04", "top": "return", "paths": 1, "taken": 0, "site": null }
376
+ ]
377
+ }
330
378
  }
331
379
  ```
332
380
 
381
+ Its `tree` says how those rows nest, by file, then card, then row.
382
+ A row inside a site names that site as its `parent`, the arm it is
383
+ `in`, and `on`, the indices of the parent's paths that run it, in the
384
+ order `paths` lists them. A row at the method's top has none of these.
385
+
386
+ ```json
387
+ "tree": {
388
+ "label.rb": {
389
+ "1:0": {
390
+ "2:2": { "parent": null, "in": null, "on": null, "leaf": null, "gives": null },
391
+ "3:4": { "parent": "2:2", "in": "then", "on": [0], "leaf": null, "gives": null },
392
+ "5:4": { "parent": "2:2", "in": "else", "on": [1], "leaf": null, "gives": null }
393
+ }
394
+ }
395
+ }
396
+ ```
397
+
398
+ A row inside one of its parent's leaves names that `leaf`, by index.
399
+ A row that is the whole of that leaf, like the `any?` here, also
400
+ `gives` the leaf's outcome on each of its own paths, `null` where not
401
+ known:
402
+
403
+ ```ruby
404
+ def led?(xs)
405
+ xs.any? { |x| x.odd? } || xs.empty?
406
+ end
407
+ ```
408
+
409
+ ```json
410
+ "2:5": { "parent": "2:2", "in": null, "on": [0, 1, 2], "leaf": 0,
411
+ "gives": [false, true, false, true, false] }
412
+ ```
413
+
414
+ Its 5 paths are 0 items, then 1 item and 2+ items, each with a last
415
+ answer true or false.
416
+
333
417
  ### Reporters
334
418
 
335
- - `:html` writes `index.html` to `coverage_dir`: the covered files with
336
- their sites and their paths taken of all possible, within each decision
337
- and, when `:methods` is measured, within each method (eg
338
- `9 / 28 (32%)`, or `-` for a file with no method of two or more
339
- decisions), each column sortable, and a Total row for every file
340
- together. Each file name opens `files/<path>.html`, the file's
341
- source. On a line holding a site the line number is a button (blue
342
- and outlined when every outcome was seen, solid orange when any is
343
- missing) that opens a grid of the site's paths, on the left. Its rows
344
- are the site's tree: its operators, nested as Ruby groups them, a
345
- ternary's arms marked `?` and `:`, an `if`'s named `then` and `else`,
346
- and its leaves. Its columns are the site's paths, never taken first,
347
- each headed by its count, orange when zero; a leaf shows its outcome on
348
- each path, T or F, blank where the path never reached it, and a plain
349
- arm a dot on each path that took it. A site written over several lines
350
- has one button over all of its line numbers. Grids with gaps start
351
- open. A header that stays at the top of the page lists every site as a
352
- link to its line, covered sites first, then those with gaps, and arrows
353
- beside each site with gaps jump to the previous and next one, wrapping
354
- round at either end. Measuring paths, each method's grid of its
355
- decisions' paths opens on the right.
419
+ - `:html` writes `index.html` to `coverage_dir`: a sortable row per
420
+ covered file, with its sites and its paths taken of all possible
421
+ within each method (eg `9 / 28 32%`, or `-` for a file with no
422
+ method of two or more decisions), and a Total row. Each file name
423
+ opens `files/<path>.html`, the file's source with its cards, as
424
+ Reading the HTML shows.
356
425
  - `:post` sends the JSON report, as an HTTP POST, to the url set by
357
426
  `collector 'http://...'`. It is for a run whose own files are thrown
358
427
  away, such as a failing `docker build` step. It prints nothing, never
@@ -360,7 +429,7 @@ shows them. A report with none, eg from a run that did not measure
360
429
  that cannot be reached or does not answer within a second, so the
361
430
  run's output and exit status are unchanged.
362
431
  - `:summary` prints each total as SimpleCov prints its line and branch
363
- coverage, the methods line only when `:methods` is measured:
432
+ coverage:
364
433
 
365
434
  ```text
366
435
  Path coverage within decisions: 527 / 631 (83.52%)
@@ -396,9 +465,8 @@ PathCoverage.leave(PathCoverage.enter("k.rb:2:2", 2), PathCoverage.record("k.rb:
396
465
 
397
466
  An `if` site's leave call wraps its predicate alone, as its path is
398
467
  decided there, so an arm that returns or raises still counts it.
399
- Measuring paths, each method starts a path at its entry, and each
400
- decision's predicate is wrapped in a decide call too, outside its site's
401
- calls.
468
+ Each statement that is no site gets a reach call before it, counting
469
+ its run, for its card's row.
402
470
 
403
471
  A command call such as `ok? id` (after `and`) is not valid as a bare
404
472
  argument, so that leaf alone gets parentheses of its own, and so does a
@@ -521,8 +589,8 @@ test:
521
589
  Branch coverage: 43 / 43 (100.00%)
522
590
  Path coverage within decisions: 65 / 84 (77.38%)
523
591
  Path coverage within methods: 3 / 3 (100.00%)
524
- Coverage report: .../reports/index.html
525
- Path coverage report: .../reports/paths/index.html
592
+ SimpleCov report: .../reports/index.html
593
+ PathCov report: .../reports/paths/index.html
526
594
  coverage: ALLOWED
527
595
  ```
528
596
 
@@ -539,24 +607,7 @@ for `lib/` are the same with it as without.
539
607
  Pages workflow (`.github/workflows/pages.yml`) runs the same script on
540
608
  each push to `main` and publishes `site/`.
541
609
 
542
- `bin/capture_readme_images.sh` rebuilds the demo and saves this README's
543
- two screenshots, `docs/decision.png` and `docs/method.png`, from it with
544
- headless Chrome. Run it again whenever the report's look changes.
545
-
546
-
547
- ## Within each decision
548
-
549
- The first `if` can go three ways: member and paid, member and not
550
- paid, not a member. A member who has not paid may never have been
551
- tried.
552
-
553
- ![The first if's grid: a column per way it can go, each headed by
554
- its count, the member who has not paid at 0](docs/decision.png)
555
-
556
- ## Within each method
557
-
558
- The two `if`s can go four ways together. A paying member with a small
559
- order may never have been tried.
610
+ `bin/capture_card_images.sh` runs the examples in `demo/cards/` and
611
+ saves this README's screenshots, `docs/example-*.png`, with headless
612
+ Chrome. Run it again whenever the card's look changes.
560
613
 
561
- ![fee's grid: a column per way its two ifs can go together, each
562
- headed by its count, true then false at 0 and selected](docs/method.png)
@@ -91,16 +91,9 @@ module PathCoverage
91
91
  @recorder.chained(site, index, first, value)
92
92
  end
93
93
 
94
- # The call rewritten code makes as a call reaches its
95
- # method's first decision; returns the call's new path.
96
- def self.path(site, size)
97
- @recorder.path(site, size)
98
- end
99
-
100
- # The call rewritten code makes around each decision's
101
- # predicate, with exits for one whose arm leaves the method;
102
- # returns value unchanged.
103
- def self.decide(path, index, value, exits = nil)
104
- @recorder.decide(path, index, value, exits)
94
+ # The call rewritten code makes before each statement that is
95
+ # no site, counting its run.
96
+ def self.reach(name)
97
+ @recorder.reach(name)
105
98
  end
106
99
  end
@@ -0,0 +1,14 @@
1
+ module PathCoverage
2
+ # One card of a file's page, as the page lays out the code
3
+ # around it: its first and last lines, its def's column, its
4
+ # status, gaps or covered, whether it is a top card, of sites
5
+ # outside every def, how many sites a top card holds, its
6
+ # button showing that count, nil for a method's, and its HTML.
7
+ Card = Data.define(:first, :last, :column, :status, :top, :sites,
8
+ :html) do
9
+ # True when line is one of the card's lines.
10
+ def holds?(line)
11
+ (first..last).cover?(line)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,186 @@
1
+ require_relative 'manifest'
2
+ require_relative 'method_statements'
3
+ require_relative 'site_paths'
4
+ require_relative 'site_tree'
5
+
6
+ module PathCoverage
7
+ # How the rows of each card nest: for each row, the site it
8
+ # runs inside, the arm of that site it is in, and the paths of
9
+ # that site on which it runs, so a reader can tell which paths
10
+ # reach a row, and count the paths through a card as a whole.
11
+ module CardTree
12
+ # Part of a site's code that a row can be inside: an arm of
13
+ # statements, or a leaf; owner the key of the site's row,
14
+ # range its bytes, arm its arm's name, nil for a leaf, on
15
+ # the indices of the site's possible paths that run it, and
16
+ # leaf a leaf's index among the site's leaves, nil for an arm.
17
+ Region = Data.define(:owner, :range, :arm, :on, :leaf)
18
+
19
+ # The place of a row inside no other row's site.
20
+ TOP = {
21
+ 'parent' => nil, 'in' => nil, 'on' => nil,
22
+ 'leaf' => nil, 'gives' => nil
23
+ }.freeze
24
+
25
+ # Example: def f(a); if a then 1 end; end gives the value
26
+ # row 1 its parent, the if, in its then arm, on its path 0.
27
+ # { def site => { "line:column" => { 'parent' =>, 'in' =>,
28
+ # 'on' =>, 'leaf' =>, 'gives' => } } } for each def in
29
+ # source, a row keyed by where it is named. parent is the
30
+ # "line:column" of the site the row runs inside, in the name
31
+ # of its arm, on the indices of that site's possible paths,
32
+ # as SitePaths.possible orders them, that run the row, leaf
33
+ # the index of that site's leaf holding it, and gives, for a
34
+ # row whose site is the whole of that leaf, the leaf's
35
+ # outcome on each of the row's own possible paths, nil where
36
+ # not known; each nil where it does not apply.
37
+ def self.of(source, path, sites = nil)
38
+ sites = Manifest.sites(source, path) if sites.nil?
39
+ cards = MethodStatements.of(source, path, sites)
40
+ cards.transform_values { |rows| nested(source, sites, rows) }
41
+ end
42
+
43
+ # { "line:column" => its place } for each of rows, a card's,
44
+ # each placed in the innermost region of another row's site
45
+ # holding all of its own site's code, or, for a row that is
46
+ # no site, where it starts: a site starting where its child
47
+ # does, eg an || whose first operand is an any?, holds that
48
+ # child, and is not held by it.
49
+ def self.nested(source, sites, rows)
50
+ starts = line_starts(source)
51
+ regions = card_regions(source, sites, rows)
52
+ rows.to_h do |row|
53
+ at = starts[row.line - 1] + row.column
54
+ site = row.site ? sites.fetch(row.site) : nil
55
+ span = site ? site.extent : at...(at + 1)
56
+ key = key_of(row)
57
+ [key, placed(key, span, regions, site)]
58
+ end
59
+ end
60
+
61
+ # The regions of the sites of rows, a card's, sites being
62
+ # { site => Condition }.
63
+ def self.card_regions(source, sites, rows)
64
+ rows.select(&:site).flat_map do |row|
65
+ regions_of(source, key_of(row), sites.fetch(row.site))
66
+ end
67
+ end
68
+
69
+ # The place of the row keyed key, its code the bytes span,
70
+ # site its Condition, nil for a row that is no site: in the
71
+ # site owning the smallest region of another row holding
72
+ # span, as place gives it; TOP for a row in no such region.
73
+ def self.placed(key, span, regions, site)
74
+ holding = holding(key, span, regions)
75
+ inner = smallest(holding)
76
+ return TOP if inner.nil?
77
+
78
+ owner = inner.owner
79
+ own = holding.group_by(&:owner).fetch(owner)
80
+ place(owner, own).merge(leaf_place(own, site))
81
+ end
82
+
83
+ # The leaf and gives of a row's place, own being the regions
84
+ # of its parent holding it, site its Condition: the index of
85
+ # the innermost leaf holding it; and, when site is the whole
86
+ # of that leaf, site's value on each of its possible paths,
87
+ # as its kind gives them, the leaf's outcome there. Neither
88
+ # for a row that is no site, a return or a raise, which gives
89
+ # its parent nothing.
90
+ def self.leaf_place(own, site)
91
+ return { 'leaf' => nil, 'gives' => nil } if site.nil?
92
+
93
+ leaf = smallest(own.reject(&:arm))
94
+ whole = leaf && site.extent == leaf.range
95
+ gives = whole ? SitePaths.values(site) : nil
96
+ { 'leaf' => leaf&.leaf, 'gives' => gives }
97
+ end
98
+
99
+ # The regions of regions holding all of the bytes span, but
100
+ # for those of the row keyed key itself.
101
+ def self.holding(key, span, regions)
102
+ held = regions.group_by do |region|
103
+ region.owner != key && region.range.cover?(span)
104
+ end
105
+ held.fetch(true, [])
106
+ end
107
+
108
+ # A row's place in the site of the row keyed owner, own being
109
+ # the regions of that site holding the row: the innermost arm
110
+ # of them, nil for none, and the paths on which every one of
111
+ # them runs, as the row runs only where all of them do.
112
+ def self.place(owner, own)
113
+ arm = smallest(own.select(&:arm))
114
+ on = own.map(&:on).reduce(:&)
115
+ { 'parent' => owner, 'in' => arm&.arm, 'on' => on }
116
+ end
117
+
118
+ # The region of regions with the fewest bytes; nil for none.
119
+ def self.smallest(regions)
120
+ regions.min_by { |region| region.range.size }
121
+ end
122
+
123
+ # The regions of condition, the site of the row keyed owner:
124
+ # its arms', and its leaves'.
125
+ def self.regions_of(source, owner, condition)
126
+ possible = SitePaths.possible(condition)
127
+ node = SiteTree.node(source, condition, possible, (0..).each)
128
+ arm_regions(owner, condition, node) +
129
+ leaf_regions(owner, condition, possible)
130
+ end
131
+
132
+ # The regions of condition's leaves, in no arm, each run on
133
+ # the paths of possible with an outcome at its index.
134
+ def self.leaf_regions(owner, condition, possible)
135
+ condition.leaves.each_with_index.map do |range, index|
136
+ skipped = possible.each_index.group_by do |i|
137
+ possible[i][index].nil?
138
+ end
139
+ on = skipped.fetch(false)
140
+ Region.new(owner:, range:, arm: nil, on:, leaf: index)
141
+ end
142
+ end
143
+
144
+ # The regions of the arms in condition, node its tree, each
145
+ # operand's node at its own index: of its own arms, and of
146
+ # those of each condition nested in it, in an operand or in
147
+ # an arm's body.
148
+ def self.arm_regions(owner, condition, node)
149
+ built = condition.operands.zip(node[:operands])
150
+ built.flat_map do |operand, operand_node|
151
+ case operand
152
+ when Condition then arm_regions(owner, operand, operand_node)
153
+ when Arm then arm_region(owner, operand, operand_node)
154
+ else []
155
+ end
156
+ end
157
+ end
158
+
159
+ # The regions of arm, node its node: its own, named by its
160
+ # kind and run on the paths that take it, none for an arm
161
+ # with no statements, which holds no row; then its body's.
162
+ def self.arm_region(owner, arm, node)
163
+ body = arm.body ? arm_regions(owner, arm.body, node[:body]) : []
164
+ return body if arm.extent.nil?
165
+
166
+ cells = node[:cells]
167
+ on = cells.each_index.group_by { cells[_1] }.fetch(true, [])
168
+ own = Region.new(owner:, range: arm.extent, arm: arm.kind.to_s,
169
+ on:, leaf: nil)
170
+ [own, *body]
171
+ end
172
+
173
+ # The "line:column" key of row, from its name.
174
+ def self.key_of(row)
175
+ row.name.split(':').last(2).join(':')
176
+ end
177
+
178
+ # The byte offset where each line of source starts, first
179
+ # line first.
180
+ def self.line_starts(source)
181
+ source.each_line.reduce([0]) do |starts, line|
182
+ starts << (starts.last + line.bytesize)
183
+ end
184
+ end
185
+ end
186
+ end