path_coverage 0.0.2 → 0.2.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 (58) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +330 -84
  3. data/exe/path_coverage_collector +12 -1
  4. data/lib/path_coverage/calls.rb +106 -0
  5. data/lib/path_coverage/collector.rb +88 -22
  6. data/lib/path_coverage/condition.rb +77 -3
  7. data/lib/path_coverage/configuration.rb +52 -12
  8. data/lib/path_coverage/configuration_paths.rb +56 -0
  9. data/lib/path_coverage/console_summary.rb +77 -0
  10. data/lib/path_coverage/gaps.rb +25 -9
  11. data/lib/path_coverage/html/index.html +3 -14
  12. data/lib/path_coverage/html/side.js +36 -6
  13. data/lib/path_coverage/html/sort.js +27 -21
  14. data/lib/path_coverage/html/style.css +32 -1
  15. data/lib/path_coverage/html/tabs.js +15 -0
  16. data/lib/path_coverage/html_index.rb +105 -19
  17. data/lib/path_coverage/html_index_head.rb +49 -0
  18. data/lib/path_coverage/html_report.rb +44 -12
  19. data/lib/path_coverage/html_tabs.rb +26 -0
  20. data/lib/path_coverage/html_tree.rb +47 -17
  21. data/lib/path_coverage/json_reader.rb +94 -0
  22. data/lib/path_coverage/json_writer.rb +44 -8
  23. data/lib/path_coverage/kernel.rb +53 -45
  24. data/lib/path_coverage/kinds/base.rb +71 -0
  25. data/lib/path_coverage/kinds/block.rb +136 -0
  26. data/lib/path_coverage/kinds/case.rb +89 -0
  27. data/lib/path_coverage/kinds/chain.rb +45 -0
  28. data/lib/path_coverage/kinds/choice.rb +68 -0
  29. data/lib/path_coverage/kinds/contains.rb +111 -0
  30. data/lib/path_coverage/kinds/safe.rb +151 -0
  31. data/lib/path_coverage/kinds/write.rb +54 -0
  32. data/lib/path_coverage/kinds.rb +37 -0
  33. data/lib/path_coverage/manifest.rb +93 -26
  34. data/lib/path_coverage/manifest_blocks.rb +96 -0
  35. data/lib/path_coverage/manifest_case.rb +54 -0
  36. data/lib/path_coverage/manifest_chains.rb +76 -0
  37. data/lib/path_coverage/manifest_choices.rb +64 -32
  38. data/lib/path_coverage/manifest_folding.rb +91 -0
  39. data/lib/path_coverage/manifest_ignoring.rb +35 -0
  40. data/lib/path_coverage/manifest_writes.rb +67 -0
  41. data/lib/path_coverage/merge.rb +19 -3
  42. data/lib/path_coverage/post.rb +8 -8
  43. data/lib/path_coverage/recorder.rb +43 -0
  44. data/lib/path_coverage/recorder_blocks.rb +68 -0
  45. data/lib/path_coverage/recorder_cases.rb +23 -0
  46. data/lib/path_coverage/recorder_chains.rb +40 -0
  47. data/lib/path_coverage/recorder_writes.rb +26 -0
  48. data/lib/path_coverage/render.rb +58 -46
  49. data/lib/path_coverage/result.rb +19 -0
  50. data/lib/path_coverage/rewriter.rb +64 -37
  51. data/lib/path_coverage/rewriter_nested.rb +62 -0
  52. data/lib/path_coverage/site.rb +6 -1
  53. data/lib/path_coverage/site_paths.rb +9 -46
  54. data/lib/path_coverage/site_tree.rb +31 -36
  55. data/lib/path_coverage/summary.rb +64 -17
  56. data/lib/path_coverage/totals.rb +29 -0
  57. data/lib/path_coverage/version.rb +1 -1
  58. metadata +35 -7
@@ -1,66 +1,29 @@
1
1
  require_relative 'condition'
2
+ require_relative 'kinds'
2
3
 
3
4
  module PathCoverage
4
5
  # The paths an evaluation of a site can take, worked out
5
6
  # from its condition alone: the outcome of each leaf, in leaf
6
7
  # order, nil for a leaf that evaluation never reaches.
7
8
  module SitePaths
8
- # Every possible path through site's condition, true
9
- # before false.
9
+ # Every possible path through site's condition, true before
10
+ # false, as its kind gives the ways a site of it can be
11
+ # evaluated.
10
12
  def self.possible(site)
11
- walk(site, site.fixed).map do |outcomes, _value|
13
+ ways = Kinds.of(site.operator).root_ways(site, site.fixed)
14
+ ways.map do |outcomes, _value|
12
15
  site.leaves.map { |range| outcomes[range] }
13
16
  end
14
17
  end
15
18
 
16
- # The outcome of an operand on which each operator goes on
17
- # to its next operand; any other ends the chain there.
18
- CONTINUES_ON = {
19
- '&&' => true, 'and' => true, '||' => false, 'or' => false
20
- }.freeze
21
-
22
19
  # [[{ leaf range => outcome }, value], ...] for each way
23
20
  # node can be evaluated, value being the outcome of node as
24
- # a whole; fixed is the site's { leaf range => its one
25
- # outcome }.
21
+ # a whole, as its kind gives them; fixed is the site's
22
+ # { leaf range => its one outcome }.
26
23
  def self.walk(node, fixed)
27
24
  return leaf(node, fixed) unless node.is_a?(Condition)
28
- return ternary(node, fixed) if CHOICES.include?(node.operator)
29
-
30
- chain(node, fixed)
31
- end
32
25
 
33
- # The ways a chain of one operator can be evaluated: each
34
- # way of its first operand, going on to the next while each
35
- # outcome is the one its operator goes on with.
36
- def self.chain(node, fixed)
37
- first, *rest = node.operands
38
- goes_on = CONTINUES_ON.fetch(node.operator)
39
- rest.reduce(walk(first, fixed)) do |ways, operand|
40
- ways.flat_map do |outcomes, value|
41
- next [[outcomes, value]] unless value == goes_on
42
-
43
- walk(operand, fixed).map do |more, after|
44
- [outcomes.merge(more), after]
45
- end
46
- end
47
- end
48
- end
49
-
50
- # The ways a ternary or an if can be evaluated: each way of
51
- # its predicate, then each way of the condition the arm it
52
- # picks is, if that arm is one; an if with no else arm
53
- # picks none. Its value is that of no choice, so is nil.
54
- def self.ternary(node, fixed)
55
- predicate, then_arm, else_arm = node.operands
56
- walk(predicate, fixed).flat_map do |outcomes, value|
57
- body = (value ? then_arm : else_arm)&.body
58
- next [[outcomes, nil]] if body.nil?
59
-
60
- walk(body, fixed).map do |more, _|
61
- [outcomes.merge(more), nil]
62
- end
63
- end
26
+ Kinds.of(node.operator).ways(node, fixed)
64
27
  end
65
28
 
66
29
  # The ways a leaf can be evaluated: true, then false, or
@@ -1,4 +1,5 @@
1
1
  require_relative 'condition'
2
+ require_relative 'kinds'
2
3
 
3
4
  module PathCoverage
4
5
  # The tree shown under a site's line on its file's page: a
@@ -21,17 +22,25 @@ module PathCoverage
21
22
  tree.merge(counts: columns.map { |taken| taken[:count] })
22
23
  end
23
24
 
24
- # The tree of condition, each leaf taking the next index of
25
- # leaves, so leaves pair with path entries left to right.
25
+ # The tree of condition, as its kind builds it, each leaf
26
+ # taking the next index of leaves, so leaves pair with path
27
+ # entries left to right.
26
28
  def self.node(source, condition, outcomes, leaves)
27
- operands = condition.operands.map do |operand|
29
+ kind = Kinds.of(condition.operator)
30
+ kind.node(source, condition, outcomes, leaves)
31
+ end
32
+
33
+ # condition's operands' nodes, leaves and nested conditions
34
+ # built in order, its arms left for arms to build after.
35
+ def self.operands_of(source, condition, outcomes, leaves)
36
+ nodes = condition.operands.map do |operand|
28
37
  case operand
29
38
  when Condition then node(source, operand, outcomes, leaves)
30
39
  when Arm then operand
31
40
  else leaf(source, operand, outcomes, leaves.next)
32
41
  end
33
42
  end
34
- arms(source, condition, operands, outcomes, leaves)
43
+ Kinds.of(condition.operator).texted(source, condition, nodes)
35
44
  end
36
45
 
37
46
  # condition's node from its operands' nodes, its ternary
@@ -42,47 +51,33 @@ module PathCoverage
42
51
  operands = operands.map do |operand|
43
52
  next operand unless operand.is_a?(Arm)
44
53
 
45
- arm(source, operand, predicate[:cells], outcomes, leaves)
54
+ decided = [predicate[:cells], THEN_ON[condition.operator]]
55
+ arm(source, operand, decided, outcomes, leaves)
46
56
  end
47
- cells = value(condition.operator, operands)
57
+ operator = condition.operator
58
+ cells = Kinds.of(operator).value(operator, operands)
48
59
  { operator: condition.operator, cells:, operands: }
49
60
  end
50
61
 
51
- # One arm of a ternary or an if: its text, nil for an arm
52
- # with no statements, whether each column took it, from the
53
- # predicate's cells, and the tree of its condition.
54
- def self.arm(source, arm, decided, outcomes, leaves)
55
- picked = arm.kind == :then
56
- body = arm.body && node(source, arm.body, outcomes, leaves)
57
- text = arm.extent && source.byteslice(arm.extent)
62
+ # An arm's text, nil for one with no statements.
63
+ def self.text_of(source, arm)
64
+ arm.extent ? source.byteslice(arm.extent) : nil
65
+ end
66
+
67
+ # One arm of a ternary, an if or an unless: its text, nil
68
+ # for an arm with no statements, whether each column took
69
+ # it, from the predicate's cells and the outcome on which
70
+ # its choice takes its then arm, and the tree of its
71
+ # condition.
72
+ def self.arm(source, arm, (decided, then_on), outcomes, leaves)
73
+ picked = arm.kind == :then ? then_on : !then_on
74
+ body = arm.body ? node(source, arm.body, outcomes, leaves) : nil
58
75
  {
59
- arm: arm.kind, text:,
76
+ arm: arm.kind, text: text_of(source, arm),
60
77
  cells: decided.map { |outcome| outcome == picked }, body:
61
78
  }
62
79
  end
63
80
 
64
- # A condition's value in each column: a ternary's or an
65
- # if's is its taken arm's body's, nil for a plain arm; a
66
- # chain's is that of the last operand it reached.
67
- def self.value(operator, operands)
68
- if CHOICES.include?(operator)
69
- _, *arms = operands
70
- arms.map { |arm| arm[:body]&.fetch(:cells) }
71
- .then { |bodies| ternary_value(arms, bodies) }
72
- else
73
- operands.map { |operand| operand[:cells] }
74
- .transpose.map { |column| column.compact.last }
75
- end
76
- end
77
-
78
- # Each column's value from the arm it took.
79
- def self.ternary_value(arms, bodies)
80
- arms.first[:cells].each_index.map do |column|
81
- taken = arms.index { |arm| arm[:cells][column] }
82
- taken && bodies[taken]&.fetch(column)
83
- end
84
- end
85
-
86
81
  # One leaf's text and its outcome in each column.
87
82
  def self.leaf(source, range, outcomes, index)
88
83
  {
@@ -1,28 +1,75 @@
1
+ require_relative 'configuration'
2
+ require_relative 'totals'
3
+
1
4
  module PathCoverage
2
- # The per-file numbers the HTML index page shows.
5
+ # The per-file numbers the HTML index page shows, and their
6
+ # total over every file.
3
7
  module Summary
4
- # One row per file that has sites in operands, as
5
- # Result#operands gives them, in path order.
6
- def self.files(operands)
7
- by_path = operands.group_by { |site, _| path_of(site) }
8
- by_path.sort.map { |path, sites| row(path, sites) }
8
+ # One row per file that has sites in site_paths, as
9
+ # Result#paths gives them, in path order: its sites, and
10
+ # its decisions total. Its methods total is nil unless
11
+ # methods, as PathManifest.methods gives them, and paths, as
12
+ # the recorder counts them, are given, as they are when
13
+ # methods are measured.
14
+ def self.files(site_paths, methods = {}, paths = nil)
15
+ by_path = site_paths.group_by { |site, _| path_of(site) }
16
+ by_path.sort.map do |path, sites|
17
+ {
18
+ path:, sites: sites.size,
19
+ decisions: Totals.decisions(sites.to_h),
20
+ methods: paths ? methods_of(path, methods, paths) : nil
21
+ }
22
+ end
23
+ end
24
+
25
+ # The methods total of the file at path; [0, 0] for a file
26
+ # with no method of two or more decisions.
27
+ def self.methods_of(path, methods, paths)
28
+ own = methods.select { |site, _| path_of(site) == path }
29
+ Totals.methods(own, paths)
9
30
  end
10
31
 
11
- # The summary row of one file's sites.
12
- def self.row(path, sites)
13
- operands = sites.flat_map { |_, counts| counts }
14
- outcomes = operands.sum { |counts| counts[:only] ? 1 : 2 }
15
- seen = operands.sum { |counts| seen_in(counts) }
32
+ # The sum of each column over rows, a methods total being
33
+ # nil when the rows' are.
34
+ def self.total(rows)
35
+ methods = rows.map { |row| row[:methods] }
16
36
  {
17
- path:, sites: sites.size, seen:, outcomes:,
18
- percent: (100.0 * seen / outcomes).round,
19
- missing: outcomes - seen
37
+ sites: rows.sum { |row| row[:sites] },
38
+ decisions: add(rows.map { |row| row[:decisions] }),
39
+ methods: methods.first ? add(methods) : nil
20
40
  }
21
41
  end
22
42
 
23
- # How many of an operand's two outcomes were seen.
24
- def self.seen_in(counts)
25
- [counts[:truthy], counts[:falsy]].count(&:positive?)
43
+ # [name, rows, total] for each of groups, { name => [glob,
44
+ # ...] }, in order: the rows whose path matches any of its
45
+ # globs, and their total. Then Ungrouped, the rows no group
46
+ # matches, when there are any.
47
+ def self.groups(rows, groups)
48
+ grouped = groups.map do |name, globs|
49
+ own = rows.select { |row| matches?(row[:path], globs) }
50
+ [name, own, total(own)]
51
+ end
52
+ every = groups.values.flatten
53
+ rest = rows.reject { |row| matches?(row[:path], every) }
54
+ return grouped if rest.empty?
55
+
56
+ grouped << ['Ungrouped', rest, total(rest)]
57
+ end
58
+
59
+ # True when path matches any of globs.
60
+ def self.matches?(path, globs)
61
+ globs.any? do |glob|
62
+ File.fnmatch?(glob, path, Configuration::GLOB_FLAGS)
63
+ end
64
+ end
65
+
66
+ # [taken, possible] pairs added together, eg [[1, 3], [2, 4]]
67
+ # gives [3, 7]; no pairs, as for a group no file is in, gives
68
+ # [0, 0].
69
+ def self.add(pairs)
70
+ pairs.reduce([0, 0]) do |(taken, possible), (more, of)|
71
+ [taken + more, possible + of]
72
+ end
26
73
  end
27
74
 
28
75
  # The file a site is in: its name minus :line:column.
@@ -0,0 +1,29 @@
1
+ require_relative 'path_gaps'
2
+
3
+ module PathCoverage
4
+ # The two coverage totals, each [taken, possible] paths over
5
+ # every covered file. They count the very paths the gap
6
+ # lines list as never taken.
7
+ module Totals
8
+ # Paths within each decision: site_paths, as Result#paths
9
+ # gives them, lists every possible path of each site with
10
+ # its count; one with a count above zero was taken.
11
+ def self.decisions(site_paths)
12
+ every = site_paths.values.flatten
13
+ [every.count { |each| each[:count].positive? }, every.size]
14
+ end
15
+
16
+ # Paths within each method, through its decisions: methods, as
17
+ # PathManifest.methods gives them, each with every path
18
+ # through its decisions; paths, as the recorder counts
19
+ # them, holds the ones taken.
20
+ def self.methods(methods, paths)
21
+ methods.reduce([0, 0]) do |(taken, possible), (site, method)|
22
+ every = PathGaps.possible(method.decisions)
23
+ seen = paths.fetch(site, {})
24
+ [taken + every.count { |path| seen.key?(path) },
25
+ possible + every.size]
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module PathCoverage
2
- VERSION = '0.0.2'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: path_coverage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Jagger
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-11-02 00:00:00.000000000 Z
11
+ date: 2026-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prism
@@ -26,7 +26,7 @@ dependencies:
26
26
  version: '1.0'
27
27
  description: Records which paths each condition and each method took, and reports
28
28
  the paths never taken in the terminal and as HTML.
29
- email:
29
+ email:
30
30
  executables:
31
31
  - path_coverage_collector
32
32
  extensions: []
@@ -37,48 +37,76 @@ files:
37
37
  - exe/path_coverage_collector
38
38
  - lib/path_coverage.rb
39
39
  - lib/path_coverage/auto.rb
40
+ - lib/path_coverage/calls.rb
40
41
  - lib/path_coverage/collector.rb
41
42
  - lib/path_coverage/condition.rb
42
43
  - lib/path_coverage/configuration.rb
44
+ - lib/path_coverage/configuration_paths.rb
45
+ - lib/path_coverage/console_summary.rb
43
46
  - lib/path_coverage/gaps.rb
44
47
  - lib/path_coverage/html/file.html
45
48
  - lib/path_coverage/html/index.html
46
49
  - lib/path_coverage/html/side.js
47
50
  - lib/path_coverage/html/sort.js
48
51
  - lib/path_coverage/html/style.css
52
+ - lib/path_coverage/html/tabs.js
49
53
  - lib/path_coverage/html/toggle.js
50
54
  - lib/path_coverage/html_file.rb
51
55
  - lib/path_coverage/html_index.rb
56
+ - lib/path_coverage/html_index_head.rb
52
57
  - lib/path_coverage/html_nav.rb
53
58
  - lib/path_coverage/html_paths.rb
54
59
  - lib/path_coverage/html_report.rb
60
+ - lib/path_coverage/html_tabs.rb
55
61
  - lib/path_coverage/html_template.rb
56
62
  - lib/path_coverage/html_tree.rb
63
+ - lib/path_coverage/json_reader.rb
57
64
  - lib/path_coverage/json_writer.rb
58
65
  - lib/path_coverage/kernel.rb
66
+ - lib/path_coverage/kinds.rb
67
+ - lib/path_coverage/kinds/base.rb
68
+ - lib/path_coverage/kinds/block.rb
69
+ - lib/path_coverage/kinds/case.rb
70
+ - lib/path_coverage/kinds/chain.rb
71
+ - lib/path_coverage/kinds/choice.rb
72
+ - lib/path_coverage/kinds/contains.rb
73
+ - lib/path_coverage/kinds/safe.rb
74
+ - lib/path_coverage/kinds/write.rb
59
75
  - lib/path_coverage/manifest.rb
76
+ - lib/path_coverage/manifest_blocks.rb
77
+ - lib/path_coverage/manifest_case.rb
78
+ - lib/path_coverage/manifest_chains.rb
60
79
  - lib/path_coverage/manifest_choices.rb
80
+ - lib/path_coverage/manifest_folding.rb
81
+ - lib/path_coverage/manifest_ignoring.rb
82
+ - lib/path_coverage/manifest_writes.rb
61
83
  - lib/path_coverage/merge.rb
62
84
  - lib/path_coverage/path_gaps.rb
63
85
  - lib/path_coverage/path_manifest.rb
64
86
  - lib/path_coverage/paths_report.rb
65
87
  - lib/path_coverage/post.rb
66
88
  - lib/path_coverage/recorder.rb
89
+ - lib/path_coverage/recorder_blocks.rb
90
+ - lib/path_coverage/recorder_cases.rb
91
+ - lib/path_coverage/recorder_chains.rb
92
+ - lib/path_coverage/recorder_writes.rb
67
93
  - lib/path_coverage/render.rb
68
94
  - lib/path_coverage/result.rb
69
95
  - lib/path_coverage/rewriter.rb
96
+ - lib/path_coverage/rewriter_nested.rb
70
97
  - lib/path_coverage/site.rb
71
98
  - lib/path_coverage/site_paths.rb
72
99
  - lib/path_coverage/site_tree.rb
73
100
  - lib/path_coverage/source_lines.rb
74
101
  - lib/path_coverage/summary.rb
102
+ - lib/path_coverage/totals.rb
75
103
  - lib/path_coverage/version.rb
76
104
  homepage: https://github.com/cyber-dojo/path_coverage
77
105
  licenses:
78
106
  - BSD-2-Clause
79
107
  metadata:
80
108
  rubygems_mfa_required: 'true'
81
- post_install_message:
109
+ post_install_message:
82
110
  rdoc_options: []
83
111
  require_paths:
84
112
  - lib
@@ -93,8 +121,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
121
  - !ruby/object:Gem::Version
94
122
  version: '0'
95
123
  requirements: []
96
- rubygems_version: 3.5.22
97
- signing_key:
124
+ rubygems_version: 3.0.3.1
125
+ signing_key:
98
126
  specification_version: 4
99
127
  summary: Path coverage for Ruby, alongside SimpleCov branch coverage
100
128
  test_files: []