path_coverage 0.7.1 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +45 -19
  3. data/lib/path_coverage/call_trace.rb +1 -1
  4. data/lib/path_coverage/calls.rb +35 -8
  5. data/lib/path_coverage/condition.rb +35 -9
  6. data/lib/path_coverage/current_test.rb +28 -0
  7. data/lib/path_coverage/decision.rb +7 -0
  8. data/lib/path_coverage/decision_sites.rb +40 -0
  9. data/lib/path_coverage/decision_tree.rb +11 -21
  10. data/lib/path_coverage/evaluated.rb +23 -0
  11. data/lib/path_coverage/fold_rows.rb +7 -8
  12. data/lib/path_coverage/gaps.rb +4 -4
  13. data/lib/path_coverage/json_reader.rb +22 -17
  14. data/lib/path_coverage/json_reports.rb +5 -3
  15. data/lib/path_coverage/json_writer.rb +12 -8
  16. data/lib/path_coverage/kernel.rb +11 -3
  17. data/lib/path_coverage/kinds/base.rb +9 -0
  18. data/lib/path_coverage/kinds/block.rb +7 -1
  19. data/lib/path_coverage/kinds/case.rb +63 -20
  20. data/lib/path_coverage/kinds/chain.rb +13 -3
  21. data/lib/path_coverage/kinds/choice.rb +5 -0
  22. data/lib/path_coverage/kinds/contains.rb +6 -0
  23. data/lib/path_coverage/kinds/safe.rb +3 -3
  24. data/lib/path_coverage/kinds/write.rb +3 -3
  25. data/lib/path_coverage/kinds.rb +14 -0
  26. data/lib/path_coverage/loop_nodes.rb +15 -10
  27. data/lib/path_coverage/manifest.rb +61 -35
  28. data/lib/path_coverage/manifest_case.rb +43 -26
  29. data/lib/path_coverage/manifest_folding.rb +20 -0
  30. data/lib/path_coverage/merge.rb +8 -8
  31. data/lib/path_coverage/method_statements.rb +41 -61
  32. data/lib/path_coverage/minitest.rb +7 -0
  33. data/lib/path_coverage/minitest_tests.rb +25 -0
  34. data/lib/path_coverage/paths_context.rb +2 -2
  35. data/lib/path_coverage/paths_forks.rb +6 -3
  36. data/lib/path_coverage/paths_leaving.rb +3 -1
  37. data/lib/path_coverage/paths_regions.rb +78 -0
  38. data/lib/path_coverage/paths_rescues.rb +5 -2
  39. data/lib/path_coverage/paths_scopes.rb +2 -1
  40. data/lib/path_coverage/paths_tree.rb +10 -68
  41. data/lib/path_coverage/paths_way_lists.rb +11 -10
  42. data/lib/path_coverage/recorder.rb +28 -47
  43. data/lib/path_coverage/recorder_blocks.rb +3 -3
  44. data/lib/path_coverage/recorder_cases.rb +70 -17
  45. data/lib/path_coverage/recorder_chains.rb +2 -2
  46. data/lib/path_coverage/render.rb +7 -3
  47. data/lib/path_coverage/report_tallies.rb +79 -0
  48. data/lib/path_coverage/report_text.rb +23 -0
  49. data/lib/path_coverage/rescue_forks.rb +3 -0
  50. data/lib/path_coverage/result.rb +17 -16
  51. data/lib/path_coverage/rewriter.rb +42 -114
  52. data/lib/path_coverage/rewriter_marks.rb +108 -0
  53. data/lib/path_coverage/source_report.rb +5 -6
  54. data/lib/path_coverage/stable_leaves.rb +6 -26
  55. data/lib/path_coverage/statement_arms.rb +23 -25
  56. data/lib/path_coverage/statement_tops.rb +53 -0
  57. data/lib/path_coverage/tally.rb +129 -0
  58. data/lib/path_coverage/testing.rb +13 -0
  59. data/lib/path_coverage/totals.rb +3 -3
  60. data/lib/path_coverage/version.rb +1 -1
  61. data/lib/path_coverage/walk.rb +4 -3
  62. data/lib/path_coverage/ways_report.rb +11 -10
  63. data/lib/path_coverage/within_methods.rb +12 -3
  64. metadata +13 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3aba82007768b3ee01a089934083c833dcc35d9dffbde4998c04559e4d93e257
4
- data.tar.gz: 13e4c34a101f2ef4143c1c39a2be3dfd3da871d483a7b675980d944fccac002d
3
+ metadata.gz: 397df01f01a7932912565d351f5418a454c80788bc566e0749208c49e86894ef
4
+ data.tar.gz: f2104387941d43d1b7b8bb2a55357cc35875bd3c5169738c42eb74231fcd579f
5
5
  SHA512:
6
- metadata.gz: c2098204eb8f2b5a75bead720fcc93a2518db3621c1a0223fedda9dbf8a57cc09cc4ceac226ec83d242901fe7b77e4104077a14584e0d9de07c1637d6863b77d
7
- data.tar.gz: 407f0209d11862e23b65744ece44d197b17484a27c3293afac0eb617a4c81b3c39f5076e6a5df23d23c01c3502b111589b8d64908d943bc169dcf76c30364f30
6
+ metadata.gz: 700ce1c5d9e49d12fbc7030c0212ab76fbe65163ffd2fde0cdf63847da5287338e4a819aeb71d1d38b211d161d60bf601eda20a57f3342fc78b5fea43245917a
7
+ data.tar.gz: 8e8ae6a4235efef6de0920a2928cf55cdf8abf3320f0c2f5d152cd07239731bb76f34f3d07bc39fe63fa66a5249086b2f6ba7ef9f0f0b709f3c585a0ad5ec8c9
data/README.md CHANGED
@@ -67,8 +67,21 @@ PathCoverage.start do
67
67
  loops # count each loop as a decision: 0 items
68
68
  # or 1+ items (off by default)
69
69
  end
70
+ require 'path_coverage/minitest' # tally each count by the Minitest test
71
+ # that made it
70
72
  ```
71
73
 
74
+ Every count in `path_coverage.json` is a tally by test, so it tells which
75
+ tests take which paths. `require 'path_coverage/minitest'` marks each
76
+ Minitest test under way, named by its class and name, eg
77
+ `KataTest#test_ok`. Another framework names its test with
78
+ `PathCoverage.testing(id)` as it starts, eg in an RSpec `around` hook, and
79
+ `PathCoverage.testing(PathCoverage::Tally::NO_TEST)` as it ends. Code run
80
+ under no test, eg a file's load, counts under `(no test)`. The test under
81
+ way is kept per thread, so tests run in parallel threads, eg with
82
+ `parallelize_me!`, count apart, and a thread a test starts counts under
83
+ that test.
84
+
72
85
  ## Ignoring and treating
73
86
 
74
87
  `ignore '&&=', '||='` stops `&&=` and `||=` writes being measured.
@@ -247,7 +260,8 @@ As the process exits:
247
260
  it alone: `PathCoverage::Render.from_json(config)`, where `config` is a
248
261
  `PathCoverage::Configuration` built from the same settings block.
249
262
  Given several runs' reports, `Render.from_json(config, paths)` adds
250
- their counts together first, so the report shows only the gaps that
263
+ their tallies together first, test by test, so the report shows only
264
+ the gaps that
251
265
  every run left. A report whose kinds, `ignored` or `treats` differ
252
266
  from `config`'s is refused, as its decisions list their paths
253
267
  differently.
@@ -272,7 +286,7 @@ order; a leaf that short-circuiting skipped is not on it. Each leaf is
272
286
  quoted as a Markdown code span, on one line, its fence one backtick longer
273
287
  than any run of backticks inside it. The gap lines are for people; a tool
274
288
  should read the JSON, whose `paths` give each decision's leaves as
275
- `line:column` spans, and each possible path with its count.
289
+ `line:column` spans, and each possible path with the tests that took it.
276
290
 
277
291
  A decision is an outermost condition. Its leaves are numbered left to right
278
292
  across the whole of it, nested conditions included. A condition inside a
@@ -293,11 +307,13 @@ is false. It is named at its keyword, and its extent is its keyword to
293
307
  the end of its predicate, so a decision inside an arm keeps its own button.
294
308
  An `elsif` is a decision of its own.
295
309
 
296
- A `case` with a subject is a decision whose leaves are its `when` patterns,
297
- tried in order until one matches. Each pattern is wrapped in a stand-in
298
- whose `===` calls the pattern's `===` once, exactly as the `case` would,
299
- and notes whether it matched. A `case` with no subject, or with a splat
300
- among its patterns, is no decision.
310
+ A `case` is a decision whose leaves are its `when` patterns, tried in
311
+ order until one matches: matched against its subject, or, with no
312
+ subject, tested for truth. A splat, eg `when *sizes`, is one leaf, which
313
+ matches when any of its items does. Ruby itself matches every pattern, in
314
+ the method's own frame, so a Regexp pattern sets `$~` and a private `===`
315
+ is called, as without coverage. The instrumentation notes only which
316
+ pattern each try reached and which arm the `case` took.
301
317
 
302
318
  A call to `any?` `all?` `none?` `find` `detect` `find_index`
303
319
  `take_while` `select` `filter` `reject` `partition` `count` or
@@ -366,7 +382,7 @@ with a row for each statement that shapes its paths: each decision, each
366
382
  `x = 1`, runs exactly when what holds it does, so is no row. A decision's
367
383
  row counts its paths; a `return` or `raise` row is one path, taken
368
384
  when it is reached. A method's total adds up its rows. Its `calls`
369
- count how many times it was called, so a method never called is a gap,
385
+ tally the tests that called it, so a method never called is a gap,
370
386
  even one with no rows. A file's code outside any method gets a flow
371
387
  of its own.
372
388
 
@@ -467,13 +483,23 @@ decided there, so an arm that returns or raises still counts it.
467
483
  Each statement that is no decision gets a reach call before it, counting
468
484
  its run, for its flow's row.
469
485
 
470
- A command call such as `ok? id` (after `and`) is not valid as a bare
471
- argument, so that leaf alone gets parentheses of its own, and so does a
472
- whole `and` or `or` inside its leave call. Nothing else does:
473
- parentheses make a statement, and a statement on a continuation line gives
474
- that line a line-coverage count of its own, which changes SimpleCov's line
475
- totals. So a command call, or an `and` or `or`, written over several lines
476
- can add to SimpleCov's relevant lines; nothing else does.
486
+ A leaf that is not valid as a bare argument, eg the command call `ok? id`,
487
+ `not ok`, or `x in [1, 2]`, gets parentheses of its own, Prism deciding
488
+ which leaves need them, and so does a whole `and` or `or` inside its leave
489
+ call. A flip-flop, eg `(i == 3)..(i == 5)`, is one only in a condition,
490
+ so its leaf is passed as a ternary's predicate, `((leaf) ? true : false)`,
491
+ and keeps its state. Nothing else gets parentheses: they make a statement,
492
+ and a statement on a continuation line gives that line a line-coverage
493
+ count of its own, which changes SimpleCov's line totals. So such a leaf
494
+ written over several lines can add to SimpleCov's relevant lines; nothing
495
+ else does.
496
+
497
+ Code that never runs is never instrumented: the operand of `defined?` is
498
+ no decision and gets no call, so `defined?(x ||= 1)` still answers
499
+ `"assignment"`. A covered file is read in its own source encoding, its
500
+ magic comment's or UTF-8, and its text reaches the reports as UTF-8. A
501
+ covered file that does not parse is left for Ruby to load, raising its own
502
+ `SyntaxError`.
477
503
 
478
504
  `record` counts whether its value was truthy or falsy, and notes it on the
479
505
  decision's current path; `enter` starts that path, and `leave` counts it.
@@ -490,8 +516,8 @@ the run never loaded are read and parsed then.
490
516
  ## Roadmap
491
517
 
492
518
  Decisions covered now: `&&`, `||`, `and`, `or`, the ternary `?:`,
493
- `if`/`elsif`/`else` and `unless` (modifiers included), `case`/`when`
494
- with a subject, and the block methods `any?` `all?` `none?` `find`
519
+ `if`/`elsif`/`else` and `unless` (modifiers included), `case`/`when`,
520
+ and the block methods `any?` `all?` `none?` `find`
495
521
  `detect` `find_index` `take_while` `select` `filter` `reject`
496
522
  `partition` `count` `filter_map`, the writes `||=` and `&&=`, and `&.`
497
523
  safe-navigation chains. Method paths cover every decision in a method,
@@ -504,7 +530,7 @@ Next:
504
530
  with blocks of more than one expression
505
531
  - `fetch(k, default)`, `dig`, `Hash#[]` with a default: present vs default
506
532
  - `x || default` in value context: nil vs not nil
507
- - `case` with no subject, and `case`/`in` patterns
533
+ - `case`/`in` patterns
508
534
  - regex matches: nil vs MatchData
509
535
  - `treat 'methods_of', as: :truthy`, so each call to it counts as a
510
536
  leaf that is only truthy, as `Foo.new` is, and a run where it returns
@@ -566,7 +592,7 @@ test:
566
592
  Paths alone: 65 / 84 (77.38%)
567
593
  Paths combined: 3 / 3 (100.00%)
568
594
  SimpleCov report: .../reports/index.html
569
- PathCov report: .../reports/paths/index.html
595
+ PathCov report: .../reports/paths/path_coverage.json
570
596
  coverage: ALLOWED
571
597
  ```
572
598
 
@@ -16,7 +16,7 @@ module PathCoverage
16
16
  @depths_key = :"path_coverage_depths_#{object_id}"
17
17
  end
18
18
 
19
- # { unit's name => { way => count } } of every way ended so
19
+ # { unit's name => { way => Tally } } of every way ended so
20
20
  # far, as Walk counts them.
21
21
  attr_reader :ways
22
22
 
@@ -21,16 +21,43 @@ module PathCoverage
21
21
  end
22
22
 
23
23
  # The call rewritten code makes around a case's pattern, the
24
- # index-th of size at decision; returns the pattern's stand-in.
25
- def self.when_match(decision, index, size, pattern)
26
- @recorder.when_match(decision, index, size, pattern)
24
+ # index-th leaf of decision, noting it reached; returns pattern
25
+ # unchanged, for the case to match or test as written.
26
+ def self.tried(decision, index, pattern)
27
+ @recorder.tried(decision, index, pattern)
27
28
  end
28
29
 
29
- # The call rewritten code makes around the pattern of a case
30
- # nested in decision, the decision's index-th leaf; returns the
31
- # pattern's stand-in.
32
- def self.pattern(decision, index, pattern)
33
- @recorder.pattern(decision, index, pattern)
30
+ # The call rewritten code makes first in a when arm of a case
31
+ # that is decision, noting its pattern matched and deciding it.
32
+ def self.took(decision)
33
+ @recorder.took(decision)
34
+ end
35
+
36
+ # The call rewritten code splats after the last pattern of a case
37
+ # that is decision, noting no pattern matched and deciding it;
38
+ # returns no patterns.
39
+ def self.missed(decision)
40
+ @recorder.missed(decision)
41
+ end
42
+
43
+ # The call rewritten code makes first in a when arm of a case in
44
+ # decision's leaf, noting its pattern matched.
45
+ def self.took_within(decision)
46
+ @recorder.took_within(decision)
47
+ end
48
+
49
+ # The call rewritten code splats after the last pattern of a case
50
+ # in decision's leaf, noting no pattern matched; returns no
51
+ # patterns.
52
+ def self.missed_within(decision)
53
+ @recorder.missed_within(decision)
54
+ end
55
+
56
+ # The call rewritten code makes around a case in decision, noting
57
+ # a when arm with no statements matched; returns value, the
58
+ # case's, unchanged.
59
+ def self.settled(decision, value)
60
+ @recorder.settled(decision, value)
34
61
  end
35
62
 
36
63
  # The call rewritten code makes around the right side of a
@@ -2,6 +2,12 @@ module PathCoverage
2
2
  # The operators whose predicate picks an arm.
3
3
  CHOICES = %w[?: if unless].freeze
4
4
 
5
+ # The operator of a when in a case with no subject: its patterns
6
+ # are tested for truth, as an ||'s operands are, where a when's
7
+ # in a case with a subject are matched against it. Its label is
8
+ # when, as it is written.
9
+ TRUTHY_WHEN = 'when truthy'.freeze
10
+
5
11
  # The operator of a safe-navigation chain, eg user&.account.
6
12
  SAFE = '&.'.freeze
7
13
 
@@ -74,24 +80,44 @@ module PathCoverage
74
80
  # raise, nil for an arm that does not leave the method.
75
81
  Arm = Data.define(:kind, :extent, :body, :leaves)
76
82
 
83
+ # The members a Condition need not be given: no command-call
84
+ # or fixed leaves, no extent and no site.
85
+ CONDITION_OPTIONAL = {
86
+ commands: [].freeze, flip_flops: [].freeze, fixed: {}.freeze,
87
+ extent: nil, site: nil
88
+ }.freeze
89
+
77
90
  # One decision's condition as the manifest found it: its operator
78
91
  # and its operands, each the byte range of a leaf or a nested
79
92
  # Condition. A decision's commands are the byte ranges of its
80
- # leaves that are command calls, eg `ok? id`; its fixed are
93
+ # leaves that are command calls, eg `ok? id`; its flip_flops are
94
+ # the byte ranges of its leaves that are flip-flops, eg
95
+ # (i == 3)..(i == 5), which only a condition makes one, each
96
+ # through any parentheses, ! or not around it; its fixed are
81
97
  # { byte range => :truthy } for leaves that can have that one
82
98
  # outcome only, eg `Clock.new`. Its extent is the byte range
83
99
  # of the whole condition as written, eg a ternary's from its
84
100
  # predicate to its else arm, which its leaves alone cannot
85
- # give.
101
+ # give. Its site is the node_id Prism gives the node it is the
102
+ # decision of, the same in every parse of the same source, so
103
+ # that node, and no other starting where it does, is known as
104
+ # the decision; nil for a Condition that is no decision of its
105
+ # own, eg one nested in another's leaf, which no node is. Its
106
+ # whole is the byte range of its node from start to end: its
107
+ # extent, but for a case, whose extent ends at its subject while
108
+ # its arms run on to its end, so that what wraps the case as a
109
+ # whole can.
86
110
  Condition = Data.define(
87
- :operator, :operands, :commands, :fixed, :extent
111
+ :operator, :operands, :commands, :flip_flops, :fixed, :extent,
112
+ :whole, :site
88
113
  ) do
89
- # A condition with no command-call or fixed leaves, and no
90
- # extent, by default.
91
- def initialize(
92
- operator:, operands:, commands: [], fixed: {}, extent: nil
93
- )
94
- super
114
+ # A condition of operator and operands, each member of
115
+ # CONDITION_OPTIONAL given, or else its default; whole, when
116
+ # not given, its extent.
117
+ def initialize(operator:, operands:, **given)
118
+ members = CONDITION_OPTIONAL.merge(given)
119
+ whole = members.fetch(:whole) { members[:extent] }
120
+ super(operator:, operands:, **members.except(:whole), whole:)
95
121
  end
96
122
 
97
123
  # The byte range of every leaf operand, left to right, a
@@ -0,0 +1,28 @@
1
+ require_relative 'tally'
2
+
3
+ module PathCoverage
4
+ # The test whose run is under way, under which every count is
5
+ # tallied. It is kept in Fiber storage, which each thread has its
6
+ # own of and which every thread or fiber it starts inherits: so
7
+ # tests run in parallel threads count apart, and work a test
8
+ # starts counts under that test. Code run under no test, eg a
9
+ # file's load, counts under Tally::NO_TEST.
10
+ module CurrentTest
11
+ # The Fiber storage key, named for this module, so a copy of
12
+ # the gem under another name keeps a test of its own.
13
+ KEY = :"#{name}"
14
+
15
+ # The id of the test under way in this thread, or
16
+ # Tally::NO_TEST when there is none.
17
+ def self.id
18
+ marked = Fiber[KEY]
19
+ marked.nil? ? Tally::NO_TEST : marked
20
+ end
21
+
22
+ # Makes test, a test's id, or Tally::NO_TEST as a test ends, the
23
+ # test under way in this thread and those it starts from now on.
24
+ def self.testing(test)
25
+ Fiber[KEY] = test
26
+ end
27
+ end
28
+ end
@@ -12,5 +12,12 @@ module PathCoverage
12
12
  def self.at(path, loc)
13
13
  "#{path}:#{loc.start_line}:#{loc.start_column}"
14
14
  end
15
+
16
+ # Example: "src/a:b.rb" for "src/a:b.rb:2:2".
17
+ # The path name names a decision in, all of it before its
18
+ # line and column, a colon in the path kept.
19
+ def self.path_of(name)
20
+ name.sub(/:\d+:\d+\z/, '')
21
+ end
15
22
  end
16
23
  end
@@ -0,0 +1,40 @@
1
+ require_relative 'decision'
2
+
3
+ module PathCoverage
4
+ # Decision mode's decisions in one file, each known by its site:
5
+ # the one node it is the decision of, by the node_id Prism gives
6
+ # it in every parse of the file. A node is a decision exactly
7
+ # when it is a site, never because it starts where one does, as
8
+ # the statements holding an if first do.
9
+ class DecisionSites
10
+ # The sites of decisions, { decision => Condition }, as
11
+ # Manifest.decisions gives them for the file at path. A
12
+ # decision named in another file is refused: a row would be
13
+ # named by one path and its decision by another.
14
+ def initialize(path, decisions)
15
+ @decisions = decisions
16
+ @names = decisions.to_h do |name, condition|
17
+ unless Decision.path_of(name) == path
18
+ raise ArgumentError, "decision #{name} is not in #{path}"
19
+ end
20
+
21
+ [condition.site, name]
22
+ end
23
+ end
24
+
25
+ # The name of the decision node is the site of, eg "k.rb:2:5";
26
+ # nil when node is the site of none.
27
+ def name_of(node)
28
+ @names[node.node_id]
29
+ end
30
+
31
+ # The Condition of the decision node is the site of; nil when
32
+ # node is the site of none.
33
+ def condition_of(node)
34
+ name = name_of(node)
35
+ return nil if name.nil?
36
+
37
+ @decisions.fetch(name)
38
+ end
39
+ end
40
+ end
@@ -2,28 +2,18 @@ require_relative 'condition'
2
2
  require_relative 'kinds'
3
3
 
4
4
  module PathCoverage
5
- # The tree shown under a decision's line on its file's page: a
6
- # grid whose columns are the decision's paths, never taken first.
5
+ # A decision's tree: a grid whose columns are paths, as its fold
6
+ # in the source report shows it.
7
7
  module DecisionTree
8
- # { operator:, counts:, cells:, operands: } for one decision,
9
- # from its paths, [{path:, count:}, ...]. counts are the
10
- # columns' counts. Every node has cells, one per column: a
11
- # leaf { text:, cells: } its outcome, nil where unreached;
12
- # a condition { operator:, cells:, operands: } its value;
13
- # an arm { arm:, text:, cells:, body: } whether the column
14
- # took it, body being its condition's tree, nil for a plain
15
- # arm. Texts are cut from source at the manifest's ranges.
16
- # An elsif's tree is labelled elsif, as it is written.
17
- def self.of(source, condition, paths)
18
- columns = paths.sort_by.with_index do |taken, i|
19
- [taken[:count].zero? ? 0 : 1, i]
20
- end
21
- tree = built(source, condition, columns.map { _1[:path] })
22
- tree.merge(counts: columns.map { |taken| taken[:count] })
23
- end
24
-
25
- # The tree of condition with no counts, a column for each of
26
- # outcomes, each path's outcomes, in the order given.
8
+ # { operator:, cells:, operands: } for condition, a column for
9
+ # each of outcomes, each path's outcomes, in the order given.
10
+ # Every node has cells, one per column: a leaf { text:, cells: }
11
+ # its outcome, nil where unreached; a condition { operator:,
12
+ # cells:, operands: } its value; an arm { arm:, text:, cells:,
13
+ # body: } whether the column took it, body being its
14
+ # condition's tree, nil for a plain arm. Texts are cut from
15
+ # source at the manifest's ranges. An elsif's tree is labelled
16
+ # elsif, as it is written.
27
17
  def self.built(source, condition, outcomes)
28
18
  tree = node(source, condition, outcomes, (0..).each)
29
19
  return tree unless elsif?(source, condition)
@@ -0,0 +1,23 @@
1
+ module PathCoverage
2
+ # The code Ruby can run, as every walk of a covered file sees it.
3
+ # The operand of a defined? never runs: defined? only asks what
4
+ # it would be. So nothing in it is a decision, a row, a loop, a
5
+ # rescue or a method, and nothing is ever inserted in it, which
6
+ # would change what defined? answers.
7
+ module Evaluated
8
+ # The nodes whose children never run.
9
+ ASKING = %i[defined_node].freeze
10
+
11
+ # node's children that can run: none of a defined?'s.
12
+ def self.children(node)
13
+ ASKING.include?(node.type) ? [] : node.compact_child_nodes
14
+ end
15
+
16
+ # The part of a Prism visitor that visits only code that can
17
+ # run, so it never descends into a defined?'s operand.
18
+ module Visitor
19
+ # Visits nothing in node, a defined?, whose operand never runs.
20
+ def visit_defined_node(_node) = nil
21
+ end
22
+ end
23
+ end
@@ -5,20 +5,19 @@ require_relative 'fold_cells'
5
5
  require_relative 'kinds'
6
6
 
7
7
  module PathCoverage
8
- # The tree of a decision, from DecisionTree.of, as its row's
9
- # fold in a flow shows it: a grid whose columns are the
10
- # decision's paths, as page data holds its rows, each a kind,
11
- # a branch, a label, where its code is and a cell per path.
8
+ # The tree of a decision, from DecisionTree.built, as its row's
9
+ # fold in the source report holds it: a grid whose columns are
10
+ # the decision's paths, each row a kind, a branch, a label, where
11
+ # its code is and a cell per path.
12
12
  module FoldRows
13
13
  # The rows of decision's fold, as page data holds them:
14
- # decision's own row, naming its operator and each path's
15
- # count, and where its else keyword is, if it has one; then
16
- # the rows of its operands.
14
+ # decision's own row, naming its operator, and where its else
15
+ # keyword is, if it has one; then the rows of its operands.
17
16
  def self.rows(decision)
18
17
  text = decision.fetch(:label) { decision[:operator] }
19
18
  label = CodeTokens.label_code(text)
20
19
  own = { kind: 'decision', branch: '', label:, at: decision[:at],
21
- else: decision[:else_at], counts: decision[:counts] }
20
+ else: decision[:else_at] }
22
21
  [own, *operand_rows(decision, '')]
23
22
  end
24
23
 
@@ -59,7 +59,7 @@ module PathCoverage
59
59
  # texts being each leaf's source text.
60
60
  def self.path_gaps(decision, paths, texts)
61
61
  paths.each_with_object([]) do |each, lines|
62
- next unless each[:count].zero?
62
+ next unless each[:tests].empty?
63
63
 
64
64
  named = steps(each[:path], texts)
65
65
  lines << "#{decision} path never taken: #{named}"
@@ -83,15 +83,15 @@ module PathCoverage
83
83
  def self.operand_gap(counts)
84
84
  return ['never evaluated'] if never_ran?(counts)
85
85
  return [] if counts[:only]
86
- return ['never true'] if counts[:truthy].zero?
87
- return ['never false'] if counts[:falsy].zero?
86
+ return ['never true'] if counts[:truthy].empty?
87
+ return ['never false'] if counts[:falsy].empty?
88
88
 
89
89
  []
90
90
  end
91
91
 
92
92
  # True when the operand was never recorded either way.
93
93
  def self.never_ran?(counts)
94
- counts[:truthy].zero? && counts[:falsy].zero?
94
+ counts[:truthy].empty? && counts[:falsy].empty?
95
95
  end
96
96
  end
97
97
  end
@@ -2,16 +2,22 @@ require 'json'
2
2
  require_relative 'kinds'
3
3
  require_relative 'measured_by'
4
4
  require_relative 'merge'
5
+ require_relative 'report_tallies'
5
6
  require_relative 'ways_report'
6
7
 
7
8
  module PathCoverage
8
9
  # Reads back the JSON reports JsonWriter writes, merging the
9
- # counts of several runs, and refusing any report whose
10
+ # Tallies of several runs, and refusing any report whose
10
11
  # decisions' paths are listed differently from a render's.
11
12
  module JsonReader
12
13
  # The keys of a JSON report's fingerprint.
13
14
  FINGERPRINT = %w[ignored treats].freeze
14
15
 
16
+ # The JSON report at path, each tally in it a Tally.
17
+ def self.load(path)
18
+ ReportTallies.restored(JSON.parse(File.read(path)))
19
+ end
20
+
15
21
  # The operands of the JSON reports at paths, merged; raises
16
22
  # unless each measured the kinds a render does, with
17
23
  # fingerprint, as Configuration#fingerprint gives it.
@@ -34,12 +40,12 @@ module PathCoverage
34
40
  end
35
41
 
36
42
  # The ways of the JSON reports at paths, as WaysReport.ways
37
- # reads them, each unit's way's counts summed; nil when none
43
+ # reads them, each unit's way's Tallies summed; nil when none
38
44
  # lists any, eg reports the :post reporter sent, so a render
39
45
  # cannot say which ways were taken.
40
46
  def self.ways(paths)
41
47
  runs = paths.map do |path|
42
- report = JSON.parse(File.read(path))
48
+ report = load(path)
43
49
  listed_ways?(report) ? WaysReport.ways(report) : nil
44
50
  end.compact
45
51
  return nil if runs.empty?
@@ -58,13 +64,13 @@ module PathCoverage
58
64
  report.key?('max_paths')
59
65
  end
60
66
 
61
- # Example: { "k.rb:2:2" => 2 } and { "k.rb:2:2" => 1 } give
62
- # { "k.rb:2:2" => 3 }.
67
+ # Example: { "k.rb:2:2" => Tally(a test: 2) } and { "k.rb:2:2"
68
+ # => Tally(a test: 1) } give { "k.rb:2:2" => Tally(a test: 3) }.
63
69
  # The reaches of the JSON reports at paths, each statement's
64
70
  # summed; nil when none has any, eg reports the :post reporter
65
71
  # sent, so a render cannot say what was reached.
66
72
  def self.reached(paths)
67
- reports = paths.map { |path| JSON.parse(File.read(path)) }
73
+ reports = paths.map { |path| load(path) }
68
74
  runs = reports.map { |report| report['reached'] }.compact
69
75
  return nil if runs.empty?
70
76
 
@@ -88,28 +94,26 @@ module PathCoverage
88
94
  end
89
95
 
90
96
  # The key lists of the JSON reports at paths, as listed_of
91
- # gives them, each path's count the sum of every report's.
97
+ # gives them, each path's Tally the sum of every report's.
92
98
  def self.merged(paths, key)
93
99
  Merge.listed(paths.map { |path| listed_of(path, key) })
94
100
  end
95
101
 
96
102
  # The JSON report at path's list under key, eg its paths a
97
- # treatment rules out, { decision => { path => count } }; none
103
+ # treatment rules out, { decision => { path => Tally } }; none
98
104
  # for a report that has none, so the render claims none.
99
105
  def self.listed_of(path, key)
100
- json = JSON.parse(File.read(path))
101
- json.fetch(key, {}).transform_values do |taken|
102
- taken.to_h { |each| [each['path'], each['count']] }
106
+ load(path).fetch(key, {}).transform_values do |taken|
107
+ taken.to_h { |each| [each['path'], each['tests']] }
103
108
  end
104
109
  end
105
110
 
106
111
  # The decision paths of the JSON report at path, as
107
112
  # Result#paths gives them; none for a report that has none.
108
113
  def self.paths_of(path)
109
- json = JSON.parse(File.read(path))
110
- json.fetch('paths', {}).transform_values do |decision|
114
+ load(path).fetch('paths', {}).transform_values do |decision|
111
115
  decision['taken'].map do |each|
112
- { path: each['path'], count: each['count'] }
116
+ { path: each['path'], tests: each['tests'] }
113
117
  end
114
118
  end
115
119
  end
@@ -118,15 +122,16 @@ module PathCoverage
118
122
  # unless it measured the kinds a render does, with
119
123
  # fingerprint.
120
124
  def self.read(path, fingerprint)
121
- json = JSON.parse(File.read(path))
125
+ json = load(path)
122
126
  same_measuring(path, json, fingerprint)
123
127
  json['decisions'].transform_values do |leaves|
124
128
  leaves.map { |leaf| counts(leaf) }
125
129
  end
126
130
  end
127
131
 
128
- # One operand's counts as JSON holds them, with the keys,
129
- # and any only: value, as the symbols Result#operands uses.
132
+ # One operand's Tallies as the report holds them, with the
133
+ # keys, and any only: value, as the symbols Result#operands
134
+ # uses.
130
135
  def self.counts(leaf)
131
136
  counts = { truthy: leaf['truthy'], falsy: leaf['falsy'] }
132
137
  only = leaf['only']
@@ -4,6 +4,7 @@ require_relative 'paths_gaps'
4
4
  require_relative 'console_summary'
5
5
  require_relative 'json_writer'
6
6
  require_relative 'paths_report'
7
+ require_relative 'report_text'
7
8
  require_relative 'source_report'
8
9
  require_relative 'within_methods'
9
10
 
@@ -11,7 +12,7 @@ module PathCoverage
11
12
  # The two JSON reports every run writes, and every render of
12
13
  # runs merged, into coverage_dir: path_coverage.json, the
13
14
  # counts, and path_coverage.source.json, what drawing each
14
- # file's code needs, for html-reporter to draw the pages from.
15
+ # file's code needs.
15
16
  module JsonReports
16
17
  # What WithinMethods reads of a run: operands, decision paths,
17
18
  # and paths a treatment rules out, as Result gives them.
@@ -61,7 +62,8 @@ module PathCoverage
61
62
  def self.write_ways(flows, files, config, tree)
62
63
  texts = PathsReport.texts(files)
63
64
  lines = PathsGaps.lines(flows, texts, parents_of(tree))
64
- File.write(ways_path(config), lines.map { "#{_1}\n" }.join)
65
+ text = ReportText.utf8(lines).map { "#{_1}\n" }.join
66
+ File.write(ways_path(config), text)
65
67
  end
66
68
 
67
69
  # { row's decision => its parent's decision } of each row in
@@ -89,7 +91,7 @@ module PathCoverage
89
91
  def self.write_source(files, config, decision_paths, flows)
90
92
  source = SourceReport.of(files, config, decision_paths, flows)
91
93
  path = config.json_path.sub(/\.json\z/, '.source.json')
92
- File.write(path, JSON.generate(source))
94
+ File.write(path, JSON.generate(ReportText.utf8(source)))
93
95
  end
94
96
 
95
97
  # The JSON report's totals, each [taken, possible] paths: