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
@@ -0,0 +1,177 @@
1
+ require 'prism'
2
+ require_relative 'card_tree'
3
+ require_relative 'card_ways'
4
+ require_relative 'html_statements'
5
+ require_relative 'method_statements'
6
+ require_relative 'source_lines'
7
+
8
+ module PathCoverage
9
+ # What is measured within each method of a covered file.
10
+ module WithinMethods
11
+ # Where each statement of the covered file at path, parsed
12
+ # as Manifest.parse gives it, that is no site is reached, as
13
+ # MethodStatements.reaches gives them.
14
+ def self.reaches(path, parsed, config)
15
+ source, sites = parsed.values_at(:source, :sites)
16
+ MethodStatements.reaches(source, config.relative(path), sites)
17
+ end
18
+
19
+ # Example: { "k.rb:0:0" => [1, 2], "k.rb:1:0" => [2, 4] }.
20
+ # Each def's card total, [taken, size], in every file in
21
+ # files, { full path => {source:, sites:} }, from paths, as
22
+ # Result#paths gives them, and reached, as Recorder#reached
23
+ # does; and each file's top level's, its sites on no card,
24
+ # named for line 0, which no def is on.
25
+ def self.cards(files, config, paths, reached)
26
+ files.map do |full_path, parsed|
27
+ path = config.relative(full_path)
28
+ source, sites = parsed.values_at(:source, :sites)
29
+ top = MethodStatements.top_level(source, path, paths, sites)
30
+ MethodStatements.totals(source, path, paths, reached, sites)
31
+ .merge("#{path}:0:0" => top)
32
+ end.reduce({}, :merge)
33
+ end
34
+
35
+ # Example: { "k.rb:1:0" => { "name" => "ship", "first" => 1,
36
+ # "last" => 3, "rows" => [{ "at" => "2:04", ... }] } }.
37
+ # Each card in every file in files, a def's and a top card's,
38
+ # as its page draws it, for the JSON report, so a reader
39
+ # finds a card's gap without the page; result a Result,
40
+ # reached as cards takes it.
41
+ def self.listed(files, config, result, reached)
42
+ files.map do |full_path, parsed|
43
+ path = config.relative(full_path)
44
+ recorded = [result.operands, result.paths]
45
+ listed_in(path, parsed, result.paths, reached)
46
+ .merge(listed_tops(path, parsed, recorded))
47
+ end.reduce({}, :merge)
48
+ end
49
+
50
+ # Example: { "k.rb" => { "1:0" => { "2:2" => { "parent" =>
51
+ # nil, "in" => nil, "on" => nil } } } }.
52
+ # How the rows of each card nest, in every file in files: by
53
+ # the file's path, then its card's line:column, a def's card
54
+ # and each top card, as listed names them, then each row's,
55
+ # as CardTree places them; result a Result.
56
+ def self.tree(files, config, result)
57
+ files.to_h do |full_path, parsed|
58
+ path = config.relative(full_path)
59
+ [path, tree_in(path, parsed, result)]
60
+ end
61
+ end
62
+
63
+ # How the rows of each card of the file at path nest, parsed
64
+ # as Manifest.parse gives it, as tree gives them.
65
+ def self.tree_in(path, parsed, result)
66
+ source, sites = parsed.values_at(:source, :sites)
67
+ cards = CardTree.of(source, path, sites).to_a +
68
+ tree_tops(path, parsed, result)
69
+ cards.to_h do |site, rows|
70
+ [site.split(':').last(2) * ':', rows]
71
+ end
72
+ end
73
+
74
+ # [[card site, its rows' places]] for each top card of the
75
+ # file at path, as listed_tops names them.
76
+ def self.tree_tops(path, parsed, result)
77
+ source, sites = parsed.values_at(:source, :sites)
78
+ lines = SourceLines.of(source, sites, result.operands)
79
+ spans(path, parsed, lines).map do |span, rows|
80
+ nested = CardTree.nested(source, sites, rows)
81
+ ["#{path}:#{span.begin}:0", nested]
82
+ end
83
+ end
84
+
85
+ # The top cards of the file at path, as its page draws them:
86
+ # each under the site of its first line's start, named for
87
+ # its scope, as listed gives them; operands and site_paths as
88
+ # Result gives them.
89
+ def self.listed_tops(path, parsed, (operands, site_paths))
90
+ source, sites = parsed.values_at(:source, :sites)
91
+ lines = SourceLines.of(source, sites, operands)
92
+ scopes = scopes(source)
93
+ spans(path, parsed, lines).to_h do |span, rows|
94
+ site = "#{path}:#{span.begin}:0"
95
+ taken = rows.zip(MethodStatements.taken(rows, site_paths))
96
+ ways = CardWays.card(source, sites, rows)
97
+ named = [scopes[span.begin], span.end, ways, nil]
98
+ [site, card(site, named, taken)]
99
+ end
100
+ end
101
+
102
+ # { span => its top-level rows } of the file at path, each
103
+ # row under the span its line is in, lines as SourceLines.of
104
+ # gives them.
105
+ def self.spans(path, parsed, lines)
106
+ source, sites = parsed.values_at(:source, :sites)
107
+ rows = MethodStatements.top_rows(source, path, sites)
108
+ rows.group_by { lines[_1.line - 1][:span] }
109
+ end
110
+
111
+ # A lambda giving the scope of a line of source, as scope
112
+ # gives it, its modules and classes found once.
113
+ def self.scopes(source)
114
+ holders = holders_in(Prism.parse(source).value)
115
+ ->(line) { scope(holders, line) }
116
+ end
117
+
118
+ # Example: line 2, inside module Shop on lines 1 to 9, gives
119
+ # Shop.
120
+ # The name of the innermost of holders, modules and classes,
121
+ # whose lines hold line, a top card's scope, as a def is a
122
+ # method card's; file for a line in none.
123
+ def self.scope(holders, line)
124
+ holding = holders.select do |node|
125
+ location = node.location
126
+ (location.start_line..location.end_line).cover?(line)
127
+ end
128
+ holding.empty? ? 'file' : holding.last.name.to_s
129
+ end
130
+
131
+ # The kinds of node that are a top card's scope.
132
+ HOLDERS = [Prism::ModuleNode, Prism::ClassNode].freeze
133
+
134
+ # Every module and class node in the tree at node, each
135
+ # before those nested in it.
136
+ def self.holders_in(node)
137
+ own = HOLDERS.include?(node.class) ? [node] : []
138
+ own + node.compact_child_nodes.flat_map { holders_in(_1) }
139
+ end
140
+
141
+ # The cards of the file at path, parsed as Manifest.parse
142
+ # gives it, as listed gives them.
143
+ def self.listed_in(path, parsed, paths, reached)
144
+ source, sites = parsed.values_at(:source, :sites)
145
+ names = MethodStatements.names(source, path, sites)
146
+ lasts = MethodStatements.lasts(source, path, sites)
147
+ MethodStatements.of(source, path, sites).to_h do |site, rows|
148
+ taken = MethodStatements.taken(rows, paths, reached)
149
+ ways = CardWays.card(source, sites, rows)
150
+ calls = reached&.fetch(site, 0)
151
+ named = [names[site], lasts[site], ways, calls]
152
+ [site, card(site, named, rows.zip(taken))]
153
+ end
154
+ end
155
+
156
+ # The card of the def at site, named name, ending on line
157
+ # last, with ways through it, as ways folds them, and calls,
158
+ # how many times it was called, nil for a top card, which is no
159
+ # method's, or when not known: its first line, from site, and
160
+ # a row for each [statement, taken] of rows.
161
+ def self.card(site, (name, last, ways, calls), rows)
162
+ first = Integer(site.split(':')[-2])
163
+ listed = rows.map { |statement, count| row(statement, count) }
164
+ { 'name' => name, 'first' => first, 'last' => last,
165
+ 'ways' => ways, 'calls' => calls, 'rows' => listed }
166
+ end
167
+
168
+ # A card's row: statement's line:column, as its page labels
169
+ # it, its top, its paths, count, how many were taken, and
170
+ # its site, nil for a statement that is no site.
171
+ def self.row(statement, count)
172
+ { 'at' => HtmlStatements.label(statement),
173
+ 'top' => statement.top, 'paths' => statement.paths,
174
+ 'taken' => count, 'site' => statement.site }
175
+ end
176
+ end
177
+ end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: path_coverage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Jagger
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2026-11-03 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: prism
@@ -26,7 +25,6 @@ dependencies:
26
25
  version: '1.0'
27
26
  description: Records which paths each condition and each method took, and reports
28
27
  the paths never taken in the terminal and as HTML.
29
- email:
30
28
  executables:
31
29
  - path_coverage_collector
32
30
  extensions: []
@@ -38,6 +36,9 @@ files:
38
36
  - lib/path_coverage.rb
39
37
  - lib/path_coverage/auto.rb
40
38
  - lib/path_coverage/calls.rb
39
+ - lib/path_coverage/card.rb
40
+ - lib/path_coverage/card_tree.rb
41
+ - lib/path_coverage/card_ways.rb
41
42
  - lib/path_coverage/collector.rb
42
43
  - lib/path_coverage/condition.rb
43
44
  - lib/path_coverage/configuration.rb
@@ -46,17 +47,24 @@ files:
46
47
  - lib/path_coverage/gaps.rb
47
48
  - lib/path_coverage/html/file.html
48
49
  - lib/path_coverage/html/index.html
50
+ - lib/path_coverage/html/settings.js
49
51
  - lib/path_coverage/html/side.js
50
52
  - lib/path_coverage/html/sort.js
51
53
  - lib/path_coverage/html/style.css
52
54
  - lib/path_coverage/html/tabs.js
53
- - lib/path_coverage/html/toggle.js
55
+ - lib/path_coverage/html/theme.js
56
+ - lib/path_coverage/html_branch.rb
57
+ - lib/path_coverage/html_cards.rb
58
+ - lib/path_coverage/html_cells.rb
54
59
  - lib/path_coverage/html_file.rb
55
60
  - lib/path_coverage/html_index.rb
56
61
  - lib/path_coverage/html_index_head.rb
62
+ - lib/path_coverage/html_labels.rb
63
+ - lib/path_coverage/html_methods.rb
57
64
  - lib/path_coverage/html_nav.rb
58
- - lib/path_coverage/html_paths.rb
59
65
  - lib/path_coverage/html_report.rb
66
+ - lib/path_coverage/html_statements.rb
67
+ - lib/path_coverage/html_syntax.rb
60
68
  - lib/path_coverage/html_tabs.rb
61
69
  - lib/path_coverage/html_template.rb
62
70
  - lib/path_coverage/html_tree.rb
@@ -81,8 +89,7 @@ files:
81
89
  - lib/path_coverage/manifest_ignoring.rb
82
90
  - lib/path_coverage/manifest_writes.rb
83
91
  - lib/path_coverage/merge.rb
84
- - lib/path_coverage/path_gaps.rb
85
- - lib/path_coverage/path_manifest.rb
92
+ - lib/path_coverage/method_statements.rb
86
93
  - lib/path_coverage/paths_report.rb
87
94
  - lib/path_coverage/post.rb
88
95
  - lib/path_coverage/recorder.rb
@@ -98,15 +105,16 @@ files:
98
105
  - lib/path_coverage/site_paths.rb
99
106
  - lib/path_coverage/site_tree.rb
100
107
  - lib/path_coverage/source_lines.rb
108
+ - lib/path_coverage/statement_arms.rb
101
109
  - lib/path_coverage/summary.rb
102
110
  - lib/path_coverage/totals.rb
103
111
  - lib/path_coverage/version.rb
104
- homepage: https://github.com/cyber-dojo/path_coverage
112
+ - lib/path_coverage/within_methods.rb
113
+ homepage: https://github.com/path-coverage/ruby
105
114
  licenses:
106
115
  - BSD-2-Clause
107
116
  metadata:
108
117
  rubygems_mfa_required: 'true'
109
- post_install_message:
110
118
  rdoc_options: []
111
119
  require_paths:
112
120
  - lib
@@ -121,8 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
129
  - !ruby/object:Gem::Version
122
130
  version: '0'
123
131
  requirements: []
124
- rubygems_version: 3.0.3.1
125
- signing_key:
132
+ rubygems_version: 4.0.23
126
133
  specification_version: 4
127
134
  summary: Path coverage for Ruby, alongside SimpleCov branch coverage
128
135
  test_files: []
@@ -1,12 +0,0 @@
1
- // Opens and closes the tree a line-number button names in its
2
- // aria-controls, keeping aria-expanded in step.
3
- (function () {
4
- document.querySelectorAll('button[aria-controls]').forEach(function (button) {
5
- button.addEventListener('click', function () {
6
- var tree = document.getElementById(button.getAttribute('aria-controls'));
7
- var open = button.getAttribute('aria-expanded') === 'true';
8
- button.setAttribute('aria-expanded', open ? 'false' : 'true');
9
- tree.hidden = open;
10
- });
11
- });
12
- })();
@@ -1,138 +0,0 @@
1
- require 'cgi/escape'
2
- require 'prism'
3
- require_relative 'path_gaps'
4
-
5
- module PathCoverage
6
- # SPIKE: the path grids of one covered file's page, hidden in
7
- # the page until side.js moves them into the rail. Each grid
8
- # has a row per decision, to sit level with that decision's
9
- # line, and a column per possible path, never-taken first.
10
- module HtmlPaths
11
- # The hidden grids of every method in methods.
12
- def self.grids(source, methods, paths)
13
- ends = end_lines(source)
14
- arms = arm_lines(source)
15
- methods.map do |site, method|
16
- grid(site, method, paths.fetch(site, {}), ends, arms)
17
- end.join("\n")
18
- end
19
-
20
- # { an if's first line => its parts' lines } for every if:
21
- # head, its if and predicate; then, its then arm; else, its
22
- # else (or elsif) keyword through its arm; end, its end line.
23
- # Each is [first, last], nil when absent; a modifier if or
24
- # ternary is all head.
25
- def self.arm_lines(source)
26
- arms = {}
27
- walk = lambda do |node|
28
- arms[node.location.start_line] = if_parts(node) if node.type == :if_node
29
- node.compact_child_nodes.each { |child| walk.call(child) }
30
- end
31
- walk.call(Prism.parse(source).value)
32
- arms
33
- end
34
-
35
- # The lines of an if's parts, as arm_lines gives them.
36
- def self.if_parts(node)
37
- start = node.location.start_line
38
- return { head: [start, start] } if node.end_keyword_loc.nil?
39
-
40
- last = node.end_keyword_loc.start_line
41
- then_arm = span(node.statements)
42
- else_part = else_lines(node.subsequent, last)
43
- body = [then_arm&.first, else_part&.first, last].compact.min
44
- {
45
- head: [start, [start, body - 1].max], then: then_arm,
46
- else: else_part, end: [last, last]
47
- }
48
- end
49
-
50
- # The lines from an else or elsif keyword to the end of its
51
- # arm, the line before the if's end for an elsif.
52
- def self.else_lines(otherwise, last)
53
- return nil if otherwise.nil?
54
- return [otherwise.location.start_line, last - 1] if otherwise.type == :if_node
55
-
56
- first = otherwise.else_keyword_loc.start_line
57
- [first, otherwise.statements ? otherwise.statements.location.end_line : first]
58
- end
59
-
60
- # The first and last lines of statements, nil for none.
61
- def self.span(statements)
62
- return nil if statements.nil?
63
-
64
- loc = statements.location
65
- [loc.start_line, loc.end_line]
66
- end
67
-
68
- # { def's first line => its last line } for every def.
69
- def self.end_lines(source)
70
- ends = {}
71
- walk = lambda do |node|
72
- if node.type == :def_node
73
- loc = node.location
74
- ends[loc.start_line] = loc.end_line
75
- end
76
- node.compact_child_nodes.each { |child| walk.call(child) }
77
- end
78
- walk.call(Prism.parse(source).value)
79
- ends
80
- end
81
-
82
- # One method's grid: its calls row, level with its def, then
83
- # a row per decision, level with that decision's line.
84
- def self.grid(site, method, taken, ends, arms)
85
- first = site.split(':')[-2].to_i
86
- every = PathGaps.possible(method.decisions)
87
- ordered = every.each_with_index.sort_by do |path, i|
88
- [taken.key?(path) ? 1 : 0, i]
89
- end.map(&:first)
90
- missing = every.count { |path| !taken.key?(path) }
91
- status = missing.zero? ? 'covered' : 'gaps'
92
- rows = [calls_row(first, ordered, taken, method, every.size - missing)]
93
- method.decisions.each_with_index do |decision, i|
94
- rows << decision_row(decision, i, ordered, taken, arms)
95
- end
96
- attrs = [
97
- %(id="paths-#{first}" data-first="#{first}"),
98
- %(data-col="#{site.split(':').last}"),
99
- %(data-last="#{ends.fetch(first, first)}" data-status="#{status}")
100
- ].join(' ')
101
- %(<div class="pathgrid" #{attrs} hidden="hidden">#{rows.join}</div>)
102
- end
103
-
104
- # The row level with the def: how many calls took each path,
105
- # never-taken ones marked.
106
- def self.calls_row(first, ordered, taken, method, seen)
107
- cells = ordered.map do |path|
108
- calls = taken.fetch(path, 0)
109
- css = calls.zero? ? 'pcell never' : 'pcell taken'
110
- %(<span class="#{css}">#{calls.zero? ? '0' : calls}</span>)
111
- end
112
- name = CGI.escapeHTML(method.name.to_s)
113
- label = %(<span class="plabel">#{seen}/#{ordered.size}</span>)
114
- title = %( title="#{name}: #{seen} of #{ordered.size} paths taken")
115
- %(<div class="prow calls" data-line="#{first}"#{title}>#{label}#{cells.join}</div>)
116
- end
117
-
118
- # The row level with a decision: T or F for each path that
119
- # makes it, those of never-taken paths marked, blank for a
120
- # path that ended before it.
121
- def self.decision_row(decision, index, ordered, taken, arms)
122
- cells = ordered.map do |path|
123
- outcome = path[index]
124
- next '<span class="pcell ended"></span>' if outcome.nil?
125
-
126
- never = taken.key?(path) ? '' : ' never'
127
- %(<span class="pcell #{outcome}#{never}">#{outcome ? 'T' : 'F'}</span>)
128
- end
129
- label = %(<span class="plabel">#{decision.line}</span>)
130
- arm = arms.fetch(decision.line, {})
131
- parts = %i[head then else end].map do |part|
132
- %(data-#{part}="#{arm[part]&.join(',')}")
133
- end
134
- data = [%(data-line="#{decision.line}"), *parts].join(' ')
135
- %(<div class="prow" #{data}>#{label}#{cells.join}</div>)
136
- end
137
- end
138
- end
@@ -1,41 +0,0 @@
1
- module PathCoverage
2
- # The terminal worklist for paths: one line per path a method
3
- # never took, and nothing for a method that took them all.
4
- module PathGaps
5
- OUTCOMES = [true, false].freeze
6
-
7
- # The gap lines of methods, as PathManifest.methods gives
8
- # them, for the paths the recorder counted, eg
9
- # "k.rb:1:0 fee path never taken: line 3 true, line 6 true".
10
- def self.lines(methods, paths)
11
- methods.flat_map do |site, method|
12
- taken = paths.fetch(site, {})
13
- every = possible(method.decisions)
14
- every.reject { |path| taken.key?(path) }.map do |path|
15
- gap = "#{site} #{method.name} path never taken"
16
- "#{gap}: #{steps(method, path)}"
17
- end
18
- end
19
- end
20
-
21
- # Every path through decisions, true before false at each:
22
- # one taking a decision's leaving outcome ends there.
23
- def self.possible(decisions)
24
- return [[]] if decisions.empty?
25
-
26
- first, *rest = decisions
27
- OUTCOMES.flat_map do |outcome|
28
- next [[outcome]] if outcome == first.exits
29
-
30
- possible(rest).map { |tail| [outcome] + tail }
31
- end
32
- end
33
-
34
- # path's outcomes, each named by its decision's line.
35
- def self.steps(method, path)
36
- path.each_with_index.map do |outcome, index|
37
- "line #{method.decisions[index].line} #{outcome}"
38
- end.join(', ')
39
- end
40
- end
41
- end
@@ -1,110 +0,0 @@
1
- require 'prism'
2
- require_relative 'manifest'
3
- require_relative 'site'
4
-
5
- module PathCoverage
6
- # One if a method's path passes: its line, the byte range of
7
- # its predicate, and whether that predicate is a command call.
8
- # exits is the outcome whose arm always leaves the method,
9
- # ending the path there, or nil when neither arm does.
10
- Decision = Data.define(:line, :range, :command, :exits) do
11
- # A decision with no arm that leaves by default.
12
- def initialize(line:, range:, command:, exits: nil)
13
- super
14
- end
15
- end
16
-
17
- # A method whose calls each take one path through its
18
- # decisions, in source order. entry is the byte offset of its
19
- # first decision's statement, where each call's path starts.
20
- PathMethod = Data.define(:name, :entry, :decisions)
21
-
22
- # Finds, without running it, the decisions of each method in
23
- # source whose paths are to be counted.
24
- module PathManifest
25
- # { def site => PathMethod } for each def in source.
26
- def self.methods(source, path)
27
- collector = PathMethods.new(path)
28
- Prism.parse(source).value.accept(collector)
29
- collector.methods
30
- end
31
-
32
- # Collects each def and the ifs at the top level of its body.
33
- class PathMethods < Prism::Visitor
34
- # Starts with no methods found.
35
- def initialize(path)
36
- @path = path
37
- @methods = {}
38
- super()
39
- end
40
-
41
- # The methods found so far.
42
- attr_reader :methods
43
-
44
- # Notes node's top-level ifs as its decisions, when it has
45
- # at least two (with fewer its paths are its branches),
46
- # then any def inside it, each a method of its own.
47
- def visit_def_node(node)
48
- ifs = statements_of(node.body).select do |statement|
49
- statement.type == :if_node
50
- end
51
- note(node, ifs) if ifs.size >= 2
52
- super
53
- end
54
-
55
- # Notes node as a method whose decisions are ifs.
56
- def note(node, ifs)
57
- @methods[Site.of(@path, node)] = PathMethod.new(
58
- name: node.name,
59
- entry: ifs.first.location.start_offset,
60
- decisions: ifs.map { |statement| decision(statement) }
61
- )
62
- end
63
-
64
- # The top-level statements of a def's body: those before
65
- # any rescue, and none for an empty or endless def.
66
- def statements_of(body)
67
- body = body.statements if body&.type == :begin_node
68
- body&.type == :statements_node ? body.body : []
69
- end
70
-
71
- # The Decision of an if statement.
72
- def decision(statement)
73
- predicate = statement.predicate
74
- loc = predicate.location
75
- Decision.new(
76
- line: loc.start_line,
77
- range: loc.start_offset...loc.end_offset,
78
- command: Manifest.command?(predicate) || false,
79
- exits: exits(statement)
80
- )
81
- end
82
-
83
- LEAVING_CALLS = %i[fail raise].freeze
84
-
85
- # The outcome of an if statement whose arm always leaves
86
- # the method: true for its then arm, false for its else
87
- # arm, nil for neither.
88
- def exits(statement)
89
- return true if leaves?(statement.statements)
90
-
91
- otherwise = statement.subsequent
92
- false if otherwise&.type == :else_node &&
93
- leaves?(otherwise.statements)
94
- end
95
-
96
- # True when the last of statements is a return, or a
97
- # fail or raise of the method's own; false for an empty
98
- # arm, which has no statements.
99
- def leaves?(statements)
100
- return false if statements.nil?
101
-
102
- last = statements.body.last
103
- return true if last.type == :return_node
104
-
105
- last.type == :call_node && last.receiver.nil? &&
106
- LEAVING_CALLS.include?(last.name)
107
- end
108
- end
109
- end
110
- end