path_coverage 0.2.0 → 0.3.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 (57) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +112 -113
  3. data/lib/path_coverage/calls.rb +4 -11
  4. data/lib/path_coverage/card.rb +14 -0
  5. data/lib/path_coverage/condition.rb +8 -1
  6. data/lib/path_coverage/configuration.rb +1 -17
  7. data/lib/path_coverage/configuration_paths.rb +4 -2
  8. data/lib/path_coverage/console_summary.rb +15 -17
  9. data/lib/path_coverage/gaps.rb +12 -9
  10. data/lib/path_coverage/html/file.html +5 -5
  11. data/lib/path_coverage/html/index.html +3 -1
  12. data/lib/path_coverage/html/settings.js +50 -0
  13. data/lib/path_coverage/html/side.js +341 -432
  14. data/lib/path_coverage/html/style.css +330 -225
  15. data/lib/path_coverage/html/theme.js +27 -0
  16. data/lib/path_coverage/html_branch.rb +30 -0
  17. data/lib/path_coverage/html_cards.rb +111 -0
  18. data/lib/path_coverage/html_cells.rb +66 -0
  19. data/lib/path_coverage/html_file.rb +82 -68
  20. data/lib/path_coverage/html_index.rb +28 -29
  21. data/lib/path_coverage/html_index_head.rb +10 -30
  22. data/lib/path_coverage/html_labels.rb +23 -0
  23. data/lib/path_coverage/html_methods.rb +66 -0
  24. data/lib/path_coverage/html_nav.rb +50 -50
  25. data/lib/path_coverage/html_report.rb +45 -49
  26. data/lib/path_coverage/html_statements.rb +166 -0
  27. data/lib/path_coverage/html_syntax.rb +156 -0
  28. data/lib/path_coverage/html_template.rb +6 -0
  29. data/lib/path_coverage/html_tree.rb +36 -72
  30. data/lib/path_coverage/json_reader.rb +21 -14
  31. data/lib/path_coverage/json_writer.rb +26 -13
  32. data/lib/path_coverage/kernel.rb +29 -49
  33. data/lib/path_coverage/kinds/block.rb +2 -1
  34. data/lib/path_coverage/kinds/case.rb +7 -4
  35. data/lib/path_coverage/kinds/contains.rb +6 -4
  36. data/lib/path_coverage/kinds/safe.rb +5 -3
  37. data/lib/path_coverage/manifest.rb +2 -1
  38. data/lib/path_coverage/manifest_blocks.rb +3 -4
  39. data/lib/path_coverage/manifest_case.rb +2 -3
  40. data/lib/path_coverage/manifest_choices.rb +4 -3
  41. data/lib/path_coverage/manifest_folding.rb +1 -1
  42. data/lib/path_coverage/merge.rb +3 -3
  43. data/lib/path_coverage/method_statements.rb +300 -0
  44. data/lib/path_coverage/recorder.rb +12 -31
  45. data/lib/path_coverage/render.rb +19 -22
  46. data/lib/path_coverage/rewriter.rb +36 -55
  47. data/lib/path_coverage/site_tree.rb +9 -0
  48. data/lib/path_coverage/statement_arms.rb +161 -0
  49. data/lib/path_coverage/summary.rb +11 -15
  50. data/lib/path_coverage/totals.rb +6 -12
  51. data/lib/path_coverage/version.rb +1 -1
  52. data/lib/path_coverage/within_methods.rb +92 -0
  53. metadata +16 -11
  54. data/lib/path_coverage/html/toggle.js +0 -12
  55. data/lib/path_coverage/html_paths.rb +0 -138
  56. data/lib/path_coverage/path_gaps.rb +0 -41
  57. data/lib/path_coverage/path_manifest.rb +0 -110
@@ -0,0 +1,161 @@
1
+ module PathCoverage
2
+ # The lists of statements a method's card has rows for: a def's
3
+ # body, the arms of each site in it whose arms are statements,
4
+ # and the body of each block in it, as the card lays them out
5
+ # flat. It relies on its includer's rows_in, site? and
6
+ # site_name.
7
+ module StatementArms
8
+ # The nodes a statement's sites are never looked for in: a
9
+ # block's, a lambda's and a begin's statements are rows of
10
+ # their own, after the statement holding them, and a def's
11
+ # body runs only when it is called, its sites rows of its own
12
+ # card.
13
+ UNSEEN = %i[block_node begin_node def_node lambda_node].freeze
14
+
15
+ # Example: return a ? 1 : 2 if x gives the ?:.
16
+ # The sites inside site that are sites of their own, in the
17
+ # order they are written: in its leaves, eg a modifier if's
18
+ # arm or a &. call's receiver; not in its arms of statements,
19
+ # nor in a body, which have rows of their own. A site is
20
+ # named where it starts, so a node starting where a site
21
+ # does, eg a ?:'s predicate, names that site: each is kept
22
+ # once, and site itself not at all. None for a top that is no
23
+ # site, eg a def inside a def, whose sites are on its own card.
24
+ def inner_sites(site)
25
+ return [] unless site?(site)
26
+
27
+ found = heads(site).flat_map { sites_within(_1) }
28
+ named = found.to_h { [site_name(_1), _1] }
29
+ named.except(site_name(site)).values
30
+ end
31
+
32
+ # node when it is a site, then each site within it, in
33
+ # order; none in a body or a def.
34
+ def sites_within(node)
35
+ return [] if UNSEEN.include?(node.type)
36
+
37
+ own = site?(node) ? [node] : []
38
+ own + heads(node).flat_map { sites_within(_1) }
39
+ end
40
+
41
+ # Example: items.map { |i| i.price } gives the price row.
42
+ # The rows of the bodies in statement: of each block's and
43
+ # lambda's statements, a block method's included,
44
+ # as its block's sites are sites of their own, and a reach
45
+ # call before its one expression sits outside the
46
+ # expression's own record call; and of each begin's parts.
47
+ def body_rows(statement)
48
+ bodies_in(statement).flat_map do |body|
49
+ rows_in(statements_of(body))
50
+ end
51
+ end
52
+
53
+ # The body of each block and lambda in node, run 0 or more
54
+ # times, and each begin: none inside an arm of statements or
55
+ # another body, whose rows are their own, nor in a def, whose
56
+ # rows are its own card's.
57
+ def bodies_in(node)
58
+ case node.type
59
+ when :def_node then []
60
+ when :block_node, :lambda_node then [node.body]
61
+ when :begin_node then [node]
62
+ else heads(node).flat_map { bodies_in(_1) }
63
+ end
64
+ end
65
+
66
+ # node's children outside its arms of statements: all of
67
+ # them for a node with none, else its predicate, and a
68
+ # case's patterns too.
69
+ def heads(node)
70
+ return node.compact_child_nodes if arms_of(node).empty?
71
+ return [node.predicate] unless node.type == :case_node
72
+
73
+ [node.predicate, *node.conditions.flat_map(&:conditions)]
74
+ end
75
+
76
+ # The statements of node's arms, then's before else's; none
77
+ # when node has no arms of statements, as a modifier's arm,
78
+ # and a ?:'s, are part of the site's own expression.
79
+ def arms_of(node)
80
+ case node.type
81
+ when :if_node then if_arms(node)
82
+ when :unless_node then unless_arms(node)
83
+ when :case_node then case_arms(node)
84
+ else []
85
+ end
86
+ end
87
+
88
+ # A case's arms' statements: each when's in turn, then its
89
+ # else's.
90
+ def case_arms(node)
91
+ whens = node.conditions.flat_map do |clause|
92
+ statements_of(clause.statements)
93
+ end
94
+ whens + else_arms(node.else_clause)
95
+ end
96
+
97
+ # An if's arms' statements, an elsif standing as a statement
98
+ # of its if's else; none for a ?: or a modifier if.
99
+ def if_arms(node)
100
+ return [] if node.if_keyword_loc.nil? || then_first?(node)
101
+
102
+ statements_of(node.statements) + else_arms(node.subsequent)
103
+ end
104
+
105
+ # An unless's arms' statements; none for a modifier unless.
106
+ def unless_arms(node)
107
+ return [] if then_first?(node)
108
+
109
+ statements_of(node.statements) + else_arms(node.else_clause)
110
+ end
111
+
112
+ # True for an if or unless whose then arm is written before
113
+ # its predicate: a modifier, eg x if y.
114
+ def then_first?(node)
115
+ statements = node.statements
116
+ return false if statements.nil?
117
+
118
+ predicate = node.predicate.location.start_offset
119
+ statements.location.start_offset < predicate
120
+ end
121
+
122
+ # The statements of an if's else, an elsif as one of them,
123
+ # none for none.
124
+ def else_arms(node)
125
+ return [node] if node&.type == :if_node
126
+
127
+ clause_statements(node)
128
+ end
129
+
130
+ # The statements of clause, an else or an ensure; none for
131
+ # none.
132
+ def clause_statements(clause)
133
+ clause ? statements_of(clause.statements) : []
134
+ end
135
+
136
+ # The top-level statements of a def's body, a block's, or an
137
+ # arm's; with a rescue, those of each part in turn.
138
+ def statements_of(body)
139
+ return begun(body) if body&.type == :begin_node
140
+
141
+ body ? body.body : []
142
+ end
143
+
144
+ # A begin's statements, then each rescue's, as each runs when
145
+ # its exception is raised, its else's, run when none is, and
146
+ # its ensure's, run either way.
147
+ def begun(node)
148
+ statements_of(node.statements) + rescued(node.rescue_clause) +
149
+ clause_statements(node.else_clause) +
150
+ clause_statements(node.ensure_clause)
151
+ end
152
+
153
+ # The statements of clause, a rescue, then of each rescue
154
+ # after it; none for none.
155
+ def rescued(clause)
156
+ return [] if clause.nil?
157
+
158
+ statements_of(clause.statements) + rescued(clause.subsequent)
159
+ end
160
+ end
161
+ end
@@ -7,36 +7,32 @@ module PathCoverage
7
7
  module Summary
8
8
  # One row per file that has sites in site_paths, as
9
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)
10
+ # its decisions total, and its methods total, its defs' cards
11
+ # in cards, { def site => [taken, size] }, added up.
12
+ def self.files(site_paths, cards)
15
13
  by_path = site_paths.group_by { |site, _| path_of(site) }
16
14
  by_path.sort.map do |path, sites|
17
15
  {
18
16
  path:, sites: sites.size,
19
17
  decisions: Totals.decisions(sites.to_h),
20
- methods: paths ? methods_of(path, methods, paths) : nil
18
+ methods: methods_of(path, cards)
21
19
  }
22
20
  end
23
21
  end
24
22
 
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)
23
+ # The methods total of the file at path, its defs' cards
24
+ # added up; [0, 0] for a file with no def.
25
+ def self.methods_of(path, cards)
26
+ Totals.methods(cards.select { |site, _| path_of(site) == path })
30
27
  end
31
28
 
32
- # The sum of each column over rows, a methods total being
33
- # nil when the rows' are.
29
+ # The sum of each column over rows; no rows, as for a group
30
+ # no file is in, gives 0 sites and [0, 0] totals.
34
31
  def self.total(rows)
35
- methods = rows.map { |row| row[:methods] }
36
32
  {
37
33
  sites: rows.sum { |row| row[:sites] },
38
34
  decisions: add(rows.map { |row| row[:decisions] }),
39
- methods: methods.first ? add(methods) : nil
35
+ methods: add(rows.map { |row| row[:methods] })
40
36
  }
41
37
  end
42
38
 
@@ -1,5 +1,3 @@
1
- require_relative 'path_gaps'
2
-
3
1
  module PathCoverage
4
2
  # The two coverage totals, each [taken, possible] paths over
5
3
  # every covered file. They count the very paths the gap
@@ -13,16 +11,12 @@ module PathCoverage
13
11
  [every.count { |each| each[:count].positive? }, every.size]
14
12
  end
15
13
 
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]
14
+ # Example: cards [4, 6] and [17, 21] give [21, 27].
15
+ # Paths within each method: cards is { def site => [taken,
16
+ # size] }, each method's card's paths taken of its size.
17
+ def self.methods(cards)
18
+ cards.values.reduce([0, 0]) do |(taken, possible), (more, of)|
19
+ [taken + more, possible + of]
26
20
  end
27
21
  end
28
22
  end
@@ -1,3 +1,3 @@
1
1
  module PathCoverage
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
@@ -0,0 +1,92 @@
1
+ require_relative 'html_cards'
2
+ require_relative 'html_statements'
3
+ require_relative 'method_statements'
4
+ require_relative 'source_lines'
5
+
6
+ module PathCoverage
7
+ # What is measured within each method of a covered file.
8
+ module WithinMethods
9
+ # Where each statement of the covered file at path, parsed
10
+ # as Manifest.parse gives it, that is no site is reached, as
11
+ # MethodStatements.reaches gives them.
12
+ def self.reaches(path, parsed, config)
13
+ source, sites = parsed.values_at(:source, :sites)
14
+ MethodStatements.reaches(source, config.relative(path), sites)
15
+ end
16
+
17
+ # Example: { "k.rb:0:0" => [1, 2], "k.rb:1:0" => [2, 4] }.
18
+ # Each def's card total, [taken, size], in every file in
19
+ # files, { full path => {source:, sites:} }, from paths, as
20
+ # Result#paths gives them, and reached, as Recorder#reached
21
+ # does; and each file's top level's, its sites on no card,
22
+ # named for line 0, which no def is on.
23
+ def self.cards(files, config, paths, reached)
24
+ files.map do |full_path, parsed|
25
+ path = config.relative(full_path)
26
+ source, sites = parsed.values_at(:source, :sites)
27
+ top = MethodStatements.top_level(source, path, paths, sites)
28
+ MethodStatements.totals(source, path, paths, reached, sites)
29
+ .merge("#{path}:0:0" => top)
30
+ end.reduce({}, :merge)
31
+ end
32
+
33
+ # Example: { "k.rb:1:0" => { "name" => "ship", "first" => 1,
34
+ # "last" => 3, "rows" => [{ "at" => "2:04", ... }] } }.
35
+ # Each card in every file in files, a def's and a top card's,
36
+ # as its page draws it, for the JSON report, so a reader
37
+ # finds a card's gap without the page; result a Result,
38
+ # reached as cards takes it.
39
+ def self.listed(files, config, result, reached)
40
+ files.map do |full_path, parsed|
41
+ path = config.relative(full_path)
42
+ listed_in(path, parsed, result.paths, reached)
43
+ .merge(listed_tops(path, parsed, result))
44
+ end.reduce({}, :merge)
45
+ end
46
+
47
+ # The top cards of the file at path, as its page draws them:
48
+ # each under the site of its first line's start, named for
49
+ # its scope, as listed gives them.
50
+ def self.listed_tops(path, parsed, result)
51
+ source, sites = parsed.values_at(:source, :sites)
52
+ lines = SourceLines.of(source, sites, result.operands)
53
+ scopes = HtmlCards.scopes(source)
54
+ HtmlCards.spans(path, parsed, lines).to_h do |span, rows|
55
+ site = "#{path}:#{span.begin}:0"
56
+ taken = rows.zip(MethodStatements.taken(rows, result.paths))
57
+ [site, card(site, scopes[span.begin], span.end, taken)]
58
+ end
59
+ end
60
+
61
+ # The cards of the file at path, parsed as Manifest.parse
62
+ # gives it, as listed gives them.
63
+ def self.listed_in(path, parsed, paths, reached)
64
+ source, sites = parsed.values_at(:source, :sites)
65
+ names = MethodStatements.names(source, path, sites)
66
+ lasts = MethodStatements.lasts(source, path, sites)
67
+ MethodStatements.of(source, path, sites).to_h do |site, rows|
68
+ taken = MethodStatements.taken(rows, paths, reached)
69
+ [site, card(site, names[site], lasts[site], rows.zip(taken))]
70
+ end
71
+ end
72
+
73
+ # The card of the def at site named name, ending on line
74
+ # last: its first line, from site, and a row for each
75
+ # [statement, taken] of rows.
76
+ def self.card(site, name, last, rows)
77
+ first = Integer(site.split(':')[-2])
78
+ listed = rows.map { |statement, count| row(statement, count) }
79
+ { 'name' => name, 'first' => first, 'last' => last,
80
+ 'rows' => listed }
81
+ end
82
+
83
+ # A card's row: statement's line:column, as its page labels
84
+ # it, its top, its paths, count, how many were taken, and
85
+ # its site, nil for a statement that is no site.
86
+ def self.row(statement, count)
87
+ { 'at' => HtmlStatements.label(statement),
88
+ 'top' => statement.top, 'paths' => statement.paths,
89
+ 'taken' => count, 'site' => statement.site }
90
+ end
91
+ end
92
+ 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.3.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,7 @@ 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
41
40
  - lib/path_coverage/collector.rb
42
41
  - lib/path_coverage/condition.rb
43
42
  - lib/path_coverage/configuration.rb
@@ -46,17 +45,24 @@ files:
46
45
  - lib/path_coverage/gaps.rb
47
46
  - lib/path_coverage/html/file.html
48
47
  - lib/path_coverage/html/index.html
48
+ - lib/path_coverage/html/settings.js
49
49
  - lib/path_coverage/html/side.js
50
50
  - lib/path_coverage/html/sort.js
51
51
  - lib/path_coverage/html/style.css
52
52
  - lib/path_coverage/html/tabs.js
53
- - lib/path_coverage/html/toggle.js
53
+ - lib/path_coverage/html/theme.js
54
+ - lib/path_coverage/html_branch.rb
55
+ - lib/path_coverage/html_cards.rb
56
+ - lib/path_coverage/html_cells.rb
54
57
  - lib/path_coverage/html_file.rb
55
58
  - lib/path_coverage/html_index.rb
56
59
  - lib/path_coverage/html_index_head.rb
60
+ - lib/path_coverage/html_labels.rb
61
+ - lib/path_coverage/html_methods.rb
57
62
  - lib/path_coverage/html_nav.rb
58
- - lib/path_coverage/html_paths.rb
59
63
  - lib/path_coverage/html_report.rb
64
+ - lib/path_coverage/html_statements.rb
65
+ - lib/path_coverage/html_syntax.rb
60
66
  - lib/path_coverage/html_tabs.rb
61
67
  - lib/path_coverage/html_template.rb
62
68
  - lib/path_coverage/html_tree.rb
@@ -81,8 +87,7 @@ files:
81
87
  - lib/path_coverage/manifest_ignoring.rb
82
88
  - lib/path_coverage/manifest_writes.rb
83
89
  - lib/path_coverage/merge.rb
84
- - lib/path_coverage/path_gaps.rb
85
- - lib/path_coverage/path_manifest.rb
90
+ - lib/path_coverage/method_statements.rb
86
91
  - lib/path_coverage/paths_report.rb
87
92
  - lib/path_coverage/post.rb
88
93
  - lib/path_coverage/recorder.rb
@@ -98,15 +103,16 @@ files:
98
103
  - lib/path_coverage/site_paths.rb
99
104
  - lib/path_coverage/site_tree.rb
100
105
  - lib/path_coverage/source_lines.rb
106
+ - lib/path_coverage/statement_arms.rb
101
107
  - lib/path_coverage/summary.rb
102
108
  - lib/path_coverage/totals.rb
103
109
  - lib/path_coverage/version.rb
110
+ - lib/path_coverage/within_methods.rb
104
111
  homepage: https://github.com/cyber-dojo/path_coverage
105
112
  licenses:
106
113
  - BSD-2-Clause
107
114
  metadata:
108
115
  rubygems_mfa_required: 'true'
109
- post_install_message:
110
116
  rdoc_options: []
111
117
  require_paths:
112
118
  - lib
@@ -121,8 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
127
  - !ruby/object:Gem::Version
122
128
  version: '0'
123
129
  requirements: []
124
- rubygems_version: 3.0.3.1
125
- signing_key:
130
+ rubygems_version: 4.0.23
126
131
  specification_version: 4
127
132
  summary: Path coverage for Ruby, alongside SimpleCov branch coverage
128
133
  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