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
@@ -1,58 +1,28 @@
1
1
  require 'cgi/escape'
2
2
  require_relative 'condition'
3
+ require_relative 'html_branch'
4
+ require_relative 'html_cells'
5
+ require_relative 'html_labels'
6
+ require_relative 'html_syntax'
3
7
  require_relative 'kinds'
4
8
 
5
9
  module PathCoverage
6
- # The tree of a span's sites on a file's page, from SiteTree.of
7
- # for each site: a grid whose columns are a site's paths.
10
+ # The tree of a site, from SiteTree.of, as its row's fold in a
11
+ # card shows it: a grid whose columns are the site's paths.
8
12
  module HtmlTree
9
- OUTCOMES = {
10
- true => 'T', false => 'F', nil => '',
11
- ITEMS[0] => '0', ITEMS[1] => '1', ITEMS[2] => '*',
12
- ANSWERS[0] => 'T', ANSWERS[1] => 'F', ANSWERS[2] => 'TF',
13
- PRESENCE[0] => [0x25CF].pack('U'),
14
- PRESENCE[1] => [0x2205].pack('U')
15
- }.freeze
16
-
17
- # A tick marks a path taking an arm.
18
- TAKEN = { true => [0x2713].pack('U'), false => '' }.freeze
19
-
20
- # The tree of a span, open when its sites have gaps, else
21
- # hidden: each site's rows in a group of their own.
22
- def self.of(number, site_trees, open)
23
- groups = site_trees.map do |site|
24
- rows = site_rows(site).join
25
- %(<div class="site" data-site="">#{rows}</div>)
26
- end
27
- hidden = open ? '' : ' hidden="hidden"'
28
- body = groups.join
29
- %(<div class="tree" id="tree-#{number}"#{hidden}>#{body}</div>)
30
- end
31
-
32
- # A site's row, naming its operator and each path's count,
33
- # then the rows of its operands.
13
+ # A site's row, naming its operator and each path's count, and
14
+ # where its else keyword is, if it has one, then the rows of
15
+ # its operands.
34
16
  def self.site_rows(site)
35
- counts = count_cells(site[:counts])
36
- [row(at(site), operator(site), counts), *operand_rows(site, '')]
37
- end
38
-
39
- # One cell per path's count, a path never taken marked as a
40
- # gap.
41
- def self.count_cells(counts)
42
- counts.map do |count|
43
- css = count.zero? ? 'count never' : 'count'
44
- %(<span class="#{css}" data-count="">#{count}</span>)
45
- end.join
17
+ counts = HtmlCells.counts(site[:counts])
18
+ attrs = at(site) + else_attr(site[:else_at])
19
+ [row(attrs, operator(site), counts), *operand_rows(site, '')]
46
20
  end
47
21
 
48
- # One cell per path, naming each with names; a &. receiver's,
49
- # shown as a glyph, titled not nil or nil for its hover text.
50
- def self.cells(values, names = OUTCOMES)
51
- values.map do |value|
52
- title = PRESENCE.include?(value) ? %( title="#{value}") : ''
53
- attrs = %(class="cell" data-cell=""#{title})
54
- %(<span #{attrs}>#{names[value]}</span>)
55
- end.join
22
+ # The data-else attribute of a site whose else keyword is at
23
+ # else_at, [line, column, line, column]; '' for none.
24
+ def self.else_attr(else_at)
25
+ else_at ? %( data-else="#{else_at.join(',')}") : ''
56
26
  end
57
27
 
58
28
  # SPIKE: the data-at attribute of a node located in its
@@ -71,47 +41,83 @@ module PathCoverage
71
41
  own = Kinds.of(condition[:operator]).rows(condition, indent)
72
42
  return own if own
73
43
 
74
- operands = unfolded(condition[:operands])
44
+ operands = unfolded(shown(condition))
75
45
  last = operands.size - 1
76
46
  operands.each_with_index.flat_map do |operand, i|
77
47
  tip = i == last
78
- branch = indent + (tip ? '└─ ' : '├─ ')
79
- under = indent + (tip ? ' ' : '│ ')
80
- node_rows(operand, branch, under, condition[:operator])
48
+ branch = indent + (tip ? HtmlBranch::ELL : HtmlBranch::TEE)
49
+ under = indent + (tip ? HtmlBranch::GAP : HtmlBranch::BAR)
50
+ node_rows(operand, branch, under)
81
51
  end
82
52
  end
83
53
 
84
- # The rows of one operand of a condition whose operator is
85
- # choice, branching after branch, any operands of its own
86
- # indented to under.
87
- def self.node_rows(operand, branch, under, choice)
88
- if operand.key?(:arm)
89
- return arm_rows(operand, branch, under, choice)
90
- end
54
+ # The rows of one operand of a condition, branching after
55
+ # branch, any operands of its own indented to under.
56
+ def self.node_rows(operand, branch, under)
57
+ return arm_rows(operand, branch, under) if operand.key?(:arm)
58
+ return [leaves_row(operand, branch)] if operand.key?(:leaves)
91
59
  return [operand_row(operand, branch)] unless operand[:operator]
92
60
 
93
61
  [subtree_row(operand, branch), *operand_rows(operand, under)]
94
62
  end
95
63
 
96
- # An arm's row: a ternary's, ? for then and : for else, an
97
- # arm holding a condition showing the condition's operator
98
- # and value, its operands below, a plain arm its text; an
99
- # if's named then or else, as its text is statements. A
100
- # plain arm marks the paths taking it.
101
- def self.arm_rows(arm, branch, under, choice)
64
+ # The row of an arm that leaves the method, as leaving gives
65
+ # it: return or raise, coloured as code, marking each path
66
+ # that leaves there, placed at its code.
67
+ def self.leaves_row(node, branch)
68
+ word = HtmlSyntax.fragment(node[:leaves])
69
+ lead = HtmlBranch.cell(branch) +
70
+ %(<span class="arm" data-label="">#{word}</span>)
71
+ cells = HtmlCells.of(node[:cells], HtmlCells::LEAVES)
72
+ row(%( data-leaves=""#{at(node)}), lead, cells)
73
+ end
74
+
75
+ # A ternary's arm's row, ? for then and : for else: an arm
76
+ # holding a condition showing the condition's operator and
77
+ # value, its operands below; a plain arm its text, marking
78
+ # the paths taking it.
79
+ def self.arm_rows(arm, branch, under)
102
80
  body = arm[:body]
103
- values = body ? cells(body[:cells]) : cells(arm[:cells], TAKEN)
104
- text = CGI.escapeHTML(arm_label(arm, choice))
105
- lead = branch_cell(branch) +
106
- %(<span class="arm" data-label="">#{text}</span>)
81
+ values = arm_cells(arm, body)
82
+ label = HtmlLabels.arm(arm)
83
+ lead = HtmlBranch.cell(branch) +
84
+ %(<span class="arm" data-label="">#{label}</span>)
107
85
  row = row(%( data-arm=""#{at(arm)}), lead, values)
108
86
  body ? [row, *operand_rows(body, under)] : [row]
109
87
  end
110
88
 
111
- # The name of an arm of a condition whose operator is choice,
112
- # as its kind names it, eg then for an if's.
113
- def self.arm_label(arm, choice)
114
- Kinds.of(choice).arm_label(arm, choice)
89
+ # The cells of arm's row: its body's values, when its body is
90
+ # a condition; else a tick on each path that took it.
91
+ def self.arm_cells(arm, body)
92
+ return HtmlCells.of(body[:cells]) if body
93
+
94
+ HtmlCells.of(arm[:cells], HtmlCells::TAKEN)
95
+ end
96
+
97
+ # The operands of condition a card's fold shows: a ?:'s arms,
98
+ # as its arms are part of its one expression; for any other,
99
+ # eg an if, a ||= or a case, no arm, as which arm a path takes
100
+ # follows from its leaves, but each arm's body, eg a when's
101
+ # patterns, which are leaves of its own, and, for an arm that
102
+ # returns or raises, where paths leave the method.
103
+ def self.shown(condition)
104
+ operands = condition[:operands]
105
+ return operands if condition[:operator] == '?:'
106
+
107
+ operands.flat_map do |operand|
108
+ next [operand] unless operand.key?(:arm)
109
+
110
+ [operand[:body], leaving(operand)].compact
111
+ end
112
+ end
113
+
114
+ # { leaves: return or raise, cells:, at: } for arm when its
115
+ # code returns or raises, cells marking the paths that took
116
+ # it, so left the method, at where its code is, so hovering it
117
+ # lights the row; nil for an arm that does neither.
118
+ def self.leaving(arm)
119
+ word = arm[:text].to_s[LEAVES, 1]
120
+ word && { leaves: word, cells: arm[:cells], at: arm[:at] }
115
121
  end
116
122
 
117
123
  # operands with each node standing for the nodes its kind
@@ -127,9 +133,9 @@ module PathCoverage
127
133
  # A nested condition's row: its branch, its operator, and
128
134
  # its value on each path.
129
135
  def self.subtree_row(condition, branch)
130
- lead = branch_cell(branch) + operator(condition)
136
+ lead = HtmlBranch.cell(branch) + operator(condition)
131
137
  attrs = %( data-subtree=""#{at(condition)})
132
- row(attrs, lead, cells(condition[:cells]))
138
+ row(attrs, lead, HtmlCells.of(condition[:cells]))
133
139
  end
134
140
 
135
141
  # A row of the grid, with attrs, lead's cells naming it,
@@ -139,29 +145,25 @@ module PathCoverage
139
145
  end
140
146
 
141
147
  # The cell naming a condition's operator, or its label when
142
- # it has one, eg select { for a site inside a leaf.
148
+ # it has one, eg select { for a site inside a leaf, coloured
149
+ # as the code it is part of.
143
150
  def self.operator(condition)
144
- name = CGI.escapeHTML(condition[:label] || condition[:operator])
151
+ label = condition.fetch(:label) { condition[:operator] }
152
+ name = HtmlSyntax.label(label)
145
153
  %(<span class="operator" data-operator="">#{name}</span>)
146
154
  end
147
155
 
148
- # One operand's row: its branch, text, and its outcome on
156
+ # One operand's row: its branch, text, coloured as the code it
157
+ # is, written over more than one line read as one, each line
158
+ # break and the indent around it one space, and its outcome on
149
159
  # each path.
150
- # An operand that is only called, eg owner at the end of
151
- # user&.account&.owner, has a tick on each path that called
152
- # it instead.
153
160
  def self.operand_row(operand, branch)
154
- text = CGI.escapeHTML(operand[:text].to_s)
155
- lead = branch_cell(branch) +
161
+ one_line = operand[:text].to_s.gsub(/\s*\n\s*/, ' ')
162
+ text = HtmlSyntax.fragment(one_line)
163
+ lead = HtmlBranch.cell(branch) +
156
164
  %(<span class="text" data-text="">#{text}</span>)
157
165
  attrs = %( data-operand=""#{at(operand)})
158
- names = operand[:called] ? TAKEN : OUTCOMES
159
- row(attrs, lead, cells(operand[:cells], names))
160
- end
161
-
162
- # The cell drawing a row's branch from its operator.
163
- def self.branch_cell(branch)
164
- %(<span class="branch" data-branch="">#{branch}</span>)
166
+ row(attrs, lead, HtmlCells.of(operand[:cells]))
165
167
  end
166
168
  end
167
169
  end
@@ -17,18 +17,27 @@ module PathCoverage
17
17
  Merge.operands(paths.map { |path| read(path, fingerprint) })
18
18
  end
19
19
 
20
- # The method paths of the JSON reports at paths, merged.
21
- def self.method_paths(paths)
22
- merged(paths, 'method_paths')
23
- end
24
-
25
20
  # The paths taken of the JSON reports at paths, as
26
21
  # Render.run takes them: site_paths:, each site's possible
27
- # paths, and outside:, those a treatment rules out, each
28
- # merged.
22
+ # paths, outside:, those a treatment rules out, and reached:,
23
+ # each statement's reaches, each merged.
29
24
  def self.taken(paths)
30
25
  site_paths = Merge.paths(paths.map { |path| paths_of(path) })
31
- { site_paths:, outside: merged(paths, 'outside') }
26
+ outside = merged(paths, 'outside')
27
+ { site_paths:, outside:, reached: reached(paths) }
28
+ end
29
+
30
+ # Example: { "k.rb:2:2" => 2 } and { "k.rb:2:2" => 1 } give
31
+ # { "k.rb:2:2" => 3 }.
32
+ # The reaches of the JSON reports at paths, each statement's
33
+ # summed; nil when none has any, eg reports the :post reporter
34
+ # sent, so a render cannot say what was reached.
35
+ def self.reached(paths)
36
+ reports = paths.map { |path| JSON.parse(File.read(path)) }
37
+ runs = reports.map { |report| report['reached'] }.compact
38
+ return nil if runs.empty?
39
+
40
+ runs.reduce { |all, run| all.merge(run) { |_, a, b| a + b } }
32
41
  end
33
42
 
34
43
  # Raises unless json, the JSON report at path, measured the
@@ -46,14 +55,12 @@ module PathCoverage
46
55
  # The key lists of the JSON reports at paths, as listed_of
47
56
  # gives them, each path's count the sum of every report's.
48
57
  def self.merged(paths, key)
49
- Merge.method_paths(paths.map { |path| listed_of(path, key) })
58
+ Merge.listed(paths.map { |path| listed_of(path, key) })
50
59
  end
51
60
 
52
- # The JSON report at path's list under key, eg its method
53
- # paths as the recorder counts them, { site => { path =>
54
- # count } }; none for a report that has none, eg from a run
55
- # that did not measure methods, so the render claims no
56
- # path taken.
61
+ # The JSON report at path's list under key, eg its paths a
62
+ # treatment rules out, { site => { path => count } }; none
63
+ # for a report that has none, so the render claims none.
57
64
  def self.listed_of(path, key)
58
65
  json = JSON.parse(File.read(path))
59
66
  json.fetch(key, {}).transform_values do |taken|
@@ -6,11 +6,9 @@ module PathCoverage
6
6
  # other reporter can read back.
7
7
  module JsonWriter
8
8
  # Writes operands, as Result gives them, paths, as
9
- # PathsReport gives them, totals, as Kernel.totals gives
10
- # them, and method_paths, as the recorder counts them, to
11
- # path as {"sites": {site: [counts, ...]}, "paths":
12
- # {site: {leaves:, taken:}}, "method_paths": {site:
13
- # [{path:, count:}, ...]}, "totals": {"decisions": [taken,
9
+ # PathsReport gives them, and totals, as Kernel.totals gives
10
+ # them, to path as {"sites": {site: [counts, ...]}, "paths":
11
+ # {site: {leaves:, taken:}}, "totals": {"decisions": [taken,
14
12
  # possible]}}.
15
13
  # parts are json's.
16
14
  def self.write(operands, path, paths = {}, **parts)
@@ -18,25 +16,40 @@ module PathCoverage
18
16
  end
19
17
 
20
18
  # The keys of parts listed as { site => [{path:, count:}] }:
21
- # each method's paths taken, and each path taken that a
22
- # treatment rules out, as Result#outside gives them.
23
- LISTED = %i[method_paths outside].freeze
19
+ # each path taken that a treatment rules out, as
20
+ # Result#outside gives them.
21
+ LISTED = %i[outside].freeze
24
22
 
25
- # operands, and any paths, and parts' method_paths:,
26
- # outside: and totals:, as the JSON report's text, with
23
+ # operands, and any paths, and parts' outside:, totals:,
24
+ # cards:, as WithinMethods.listed gives them, and tree:, as
25
+ # WithinMethods.tree gives it, as the JSON report's text, with
27
26
  # parts' fingerprint:, as Configuration#fingerprint gives
28
- # it, and the names of the kinds measured.
27
+ # it, the names of the kinds measured, and parts' reached:,
28
+ # as Recorder#reached gives it, even empty, as a run that
29
+ # reached no statement says so; none when not given.
29
30
  def self.json(operands, paths = {}, **parts)
30
31
  fingerprint = parts.fetch(:fingerprint, {})
31
32
  report = { 'sites' => operands, **fingerprint }
32
33
  report['paths'] = paths unless paths.empty?
33
34
  report.merge!(lists(parts))
34
- totals = parts.fetch(:totals, {})
35
- report['totals'] = totals unless totals.empty?
35
+ report['reached'] = parts[:reached] if parts.key?(:reached)
36
+ report.merge!(kept(parts))
36
37
  report['kinds'] = Kinds.names
37
38
  JSON.pretty_generate(report)
38
39
  end
39
40
 
41
+ # The keys of parts written as they are, when not empty.
42
+ KEPT = %i[totals cards tree].freeze
43
+
44
+ # { key => its value } of each of parts' KEPT keys that has
45
+ # a value not empty.
46
+ def self.kept(parts)
47
+ KEPT.filter_map do |key|
48
+ value = parts.fetch(key, {})
49
+ [key.to_s, value] unless value.empty?
50
+ end.to_h
51
+ end
52
+
40
53
  # { key => its list } of each of parts' LISTED keys that
41
54
  # has one, as listed gives them.
42
55
  def self.lists(parts)
@@ -4,13 +4,12 @@ require_relative 'configuration'
4
4
  require_relative 'console_summary'
5
5
  require_relative 'json_writer'
6
6
  require_relative 'manifest'
7
- require_relative 'path_gaps'
8
- require_relative 'path_manifest'
9
7
  require_relative 'paths_report'
10
8
  require_relative 'recorder'
11
9
  require_relative 'render'
12
10
  require_relative 'result'
13
11
  require_relative 'rewriter'
12
+ require_relative 'within_methods'
14
13
 
15
14
  # Path coverage for Ruby. This file is the kernel: it loads
16
15
  # before any covered file and is never itself instrumented, since
@@ -23,7 +22,8 @@ module PathCoverage
23
22
  # again.
24
23
  @parsed = {}
25
24
 
26
- # The one recorder that every instrumented operand reports to.
25
+ # The recorder of the latest start, that every instrumented
26
+ # operand reports to.
27
27
  def self.recorder
28
28
  @recorder
29
29
  end
@@ -37,9 +37,12 @@ module PathCoverage
37
37
  # Instruments every file later required that the settings
38
38
  # block covers, and reports when the process exits. on_exit
39
39
  # is Kernel#at_exit except in tests, which must not print
40
- # a report as their own process exits.
40
+ # a report as their own process exits. Each start records
41
+ # afresh, so a later start, as a later test makes, never
42
+ # counts an earlier one's evaluations.
41
43
  def self.start(on_exit: method(:at_exit), &)
42
44
  config = Configuration.new(&)
45
+ @recorder = Recorder.new
43
46
  install_hook if @config.nil?
44
47
  @config = config
45
48
  on_exit.call { report(config) }
@@ -90,43 +93,20 @@ module PathCoverage
90
93
  return unless config.covers?(path)
91
94
 
92
95
  parsed = @parsed[path] = Manifest.parse(path, config)
93
- methods = path_methods(path, parsed[:source], config)
94
- sites = parsed[:sites]
95
- rewritten = Rewriter.rewrite(parsed[:source], sites, methods)
96
+ source = parsed[:source]
97
+ reaches = WithinMethods.reaches(path, parsed, config)
98
+ rewritten = Rewriter.rewrite(source, parsed[:sites], reaches)
96
99
  RubyVM::InstructionSequence.compile(rewritten, path, path)
97
100
  end
98
101
 
99
- # The methods of the covered file at path whose paths are
100
- # counted, none unless config measures methods.
101
- def self.path_methods(path, source, config)
102
- return {} unless config.measures.include?(:methods)
103
-
104
- PathManifest.methods(source, config.relative(path))
105
- end
106
-
107
102
  # Writes the JSON report, then runs the chosen reporters, for
108
103
  # every covered file whether or not the run loaded it, into
109
- # coverage_dir, created first if need be. The paths never
110
- # taken follow the condition gaps.
104
+ # coverage_dir, created first if need be.
111
105
  def self.report(config)
112
106
  files = parsed_files(config)
113
107
  result = collect(files, config)
114
- methods = config.measures.include?(:methods)
115
- paths = methods ? @recorder.paths : nil
116
- Render.run(result.operands, files, config, paths, **result.taken)
117
- return unless config.reporters.include?(:gaps)
118
-
119
- methods = path_methods_of(files, config)
120
- PathGaps.lines(methods, @recorder.paths).each { |line| puts line }
121
- end
122
-
123
- # The methods whose paths are counted of every covered file,
124
- # in one manifest.
125
- def self.path_methods_of(files, config)
126
- all = files.map do |path, parsed|
127
- path_methods(path, parsed[:source], config)
128
- end
129
- all.reduce({}, :merge)
108
+ Render.run(result.operands, files, config,
109
+ **result.taken, reached: @recorder.reached)
130
110
  end
131
111
 
132
112
  # The Result of every covered file, with the counts and site
@@ -141,33 +121,34 @@ module PathCoverage
141
121
  end
142
122
 
143
123
  # Writes result as the JSON report at config's json_path: its
144
- # operands, site paths, paths a treatment rules out, and
145
- # totals, and the method paths the recorder counted, so a
146
- # render from the JSON alone has them, and config's
147
- # fingerprint.
124
+ # operands, site paths, paths a treatment rules out, totals,
125
+ # each def's card, how each card's rows nest, the statements
126
+ # the recorder reached, so a render from the JSON alone has
127
+ # them, and config's fingerprint.
148
128
  def self.write_json(result, files, config)
149
129
  report = PathsReport.of(result.paths, files)
150
- totals = totals(result, files, config)
151
- JsonWriter.write(result.operands, config.json_path, report,
152
- totals:, method_paths: @recorder.paths,
153
- outside: result.outside,
154
- fingerprint: config.fingerprint)
130
+ reached = @recorder.reached
131
+ cards = WithinMethods.listed(files, config, result, reached)
132
+ tree = WithinMethods.tree(files, config, result)
133
+ parts = {
134
+ totals: totals(result, files, config), outside: result.outside,
135
+ cards:, tree:, fingerprint: config.fingerprint, reached:
136
+ }
137
+ path = config.json_path
138
+ JsonWriter.write(result.operands, path, report, **parts)
155
139
  end
156
140
 
157
141
  # The JSON report's totals, each [taken, possible] paths:
158
- # within each decision, and, when config measures methods,
159
- # within each method too; with groups configured, each group's
160
- # own too, under groups.
142
+ # within each decision, and within each method; with groups
143
+ # configured, each group's own too, under groups.
161
144
  def self.totals(result, files, config)
162
- measured = config.measures.include?(:methods)
163
- methods = measured ? path_methods_of(files, config) : {}
164
- paths = measured ? @recorder.paths : nil
165
- totals = ConsoleSummary.totals(result.paths, methods, paths)
145
+ reached = @recorder.reached
146
+ cards = WithinMethods.cards(files, config, result.paths, reached)
147
+ totals = ConsoleSummary.totals(result.paths, cards)
166
148
  groups = config.groups
167
149
  return totals if groups.empty?
168
150
 
169
- summary = ConsoleSummary
170
- own = summary.group_totals(result.paths, methods, paths, groups)
151
+ own = ConsoleSummary.group_totals(result.paths, cards, groups)
171
152
  totals.merge('groups' => own)
172
153
  end
173
154
 
@@ -52,8 +52,9 @@ module PathCoverage
52
52
  'partition' => ALWAYS,
53
53
  'take_while' => lambda do |items, answer|
54
54
  next false if items == ITEMS[0]
55
+ next answer if answer
55
56
 
56
- answer || (items == ITEMS[1] ? false : nil)
57
+ items == ITEMS[1] ? false : nil
57
58
  end
58
59
  }.freeze
59
60
 
@@ -50,26 +50,24 @@ module PathCoverage
50
50
  # matched holds the cells of each when before it, and a
51
51
  # when's own are added.
52
52
  def self.arm_node(source, arm, matched, (outcomes, leaves))
53
- body = arm.body &&
54
- SiteTree.node(source, arm.body, outcomes, leaves)
53
+ condition = arm.body
54
+ body = if condition
55
+ SiteTree.node(source, condition, outcomes, leaves)
56
+ end
55
57
  matched << body[:cells].map { |cell| cell == true } if body
56
58
  cells = body ? matched.last : unmatched(matched, outcomes)
57
59
  text = SiteTree.text_of(source, arm)
58
60
  { arm: arm.kind, text:, cells:, body: }
59
61
  end
60
62
 
61
- # Whether each column matched none of its whens' patterns.
63
+ # Whether each column matched none of its whens' patterns:
64
+ # a when's cell is true where it matched.
62
65
  def self.unmatched(matched, outcomes)
63
66
  outcomes.each_index.map do |column|
64
- matched.none? { |taken| taken[column] }
67
+ !matched.map { |taken| taken[column] }.include?(true)
65
68
  end
66
69
  end
67
70
 
68
- # The name of one of its arms: when or else.
69
- def self.arm_label(arm, _choice)
70
- arm[:arm].to_s
71
- end
72
-
73
71
  # Its patterns need pattern calls.
74
72
  def self.own_calls?
75
73
  true
@@ -52,13 +52,9 @@ module PathCoverage
52
52
  condition.operands.first
53
53
  end
54
54
 
55
- # The name of one of its arms, of a condition named choice:
56
- # then or else for an if or an unless, as its text is
57
- # statements; ? or : for a ternary's, then its condition's
58
- # operator, or its text for a plain arm.
59
- def self.arm_label(arm, choice)
60
- return arm[:arm].to_s if STATEMENT_CHOICES.include?(choice)
61
-
55
+ # The name of one of a ternary's arms: ? or :, then its
56
+ # condition's operator, or its text for a plain arm.
57
+ def self.arm_label(arm)
62
58
  mark = arm[:arm] == :then ? '?' : ':'
63
59
  body = arm[:body]
64
60
  "#{mark} #{body ? body[:operator] : arm[:text]}"
@@ -52,15 +52,19 @@ module PathCoverage
52
52
  receiver = inner.max_by { |site| site.extent.size }
53
53
  return Block.items?(receiver, outcomes)
54
54
  end
55
- whole = inner.find { |site| site.extent == range }
56
- whole && value_on(whole, outcomes, fixed)
55
+ whole = inner.to_h { |site| [site.extent, site] }[range]
56
+ return nil if whole.nil?
57
+
58
+ value_on(whole, outcomes, fixed)
57
59
  end
58
60
 
59
61
  # site's value on its way that took outcomes, one of its
60
- # own, nil when its kind cannot tell.
62
+ # own, nil when its kind cannot tell. outcomes always hold
63
+ # one of site's ways, so one is always found.
61
64
  def self.value_on(site, outcomes, fixed)
62
- ways = SitePaths.walk(site, fixed)
63
- ways.find { |more, _| more <= outcomes }.last
65
+ SitePaths.walk(site, fixed).each do |more, value|
66
+ return value if more <= outcomes
67
+ end
64
68
  end
65
69
 
66
70
  # Its value in each column: the leaf's own, its last operand.
@@ -46,8 +46,7 @@ module PathCoverage
46
46
  # Its value in each column: false where a receiver was nil,
47
47
  # as the chain then gives nil, else its last leaf's.
48
48
  def self.value(_operator, operands)
49
- leaves = operands.reject { |operand| operand[:called] }
50
- columns = leaves.map { |operand| operand[:cells] }.transpose
49
+ columns = operands.map { |operand| operand[:cells] }.transpose
51
50
  columns.map do |column|
52
51
  column.include?(PRESENCE[1]) ? false : column.last
53
52
  end
@@ -104,13 +103,11 @@ module PathCoverage
104
103
  # Its leaves' nodes, each after the first showing only what
105
104
  # it adds to the one before, eg account after user.
106
105
  # A leading &. is left out, as the chain's own row shows it.
107
- # Any of the chain after its last leaf follows, eg owner in
108
- # user&.account&.owner, as a node of its own.
106
+ # The chain after its last leaf, eg owner in
107
+ # user&.account&.owner, has no node: it decides no path, and
108
+ # is called exactly where that leaf is not nil.
109
109
  def self.texted(source, condition, nodes)
110
- ranges = condition.operands
111
- texted = with_added(source, ranges, nodes)
112
- rest = shown(source, ranges.last.end...condition.extent.end)
113
- rest.empty? ? texted : [*texted, called(rest, texted.last)]
110
+ with_added(source, condition.operands, nodes)
114
111
  end
115
112
 
116
113
  # nodes, the nodes of a chain's leaves at ranges, each after
@@ -129,20 +126,11 @@ module PathCoverage
129
126
  source.byteslice(range).delete_prefix('&.')
130
127
  end
131
128
 
132
- # The node of text, the end of a chain after its last
133
- # leaf, eg owner: no leaf, as it decides no path, but marked
134
- # called on each path where last, the last leaf, was not
135
- # nil.
136
- def self.called(text, last)
137
- cells = last[:cells].map { |cell| cell == PRESENCE[0] }
138
- { text:, called: true, cells: }
139
- end
140
-
141
129
  # The rows of its leaves, each under the one before, as each
142
130
  # is reached only when the one before is not nil.
143
131
  def self.rows(chain, indent)
144
132
  chain[:operands].each_with_index.map do |operand, depth|
145
- branch = "#{indent}#{' ' * depth}└─ "
133
+ branch = "#{indent}#{HtmlBranch::GAP * depth}#{HtmlBranch::ELL}"
146
134
  HtmlTree.operand_row(operand, branch)
147
135
  end
148
136
  end
@@ -14,12 +14,6 @@ module PathCoverage
14
14
 
15
15
  OPERATORS = WRITES
16
16
 
17
- # The name of its arm, of a write named choice: the operator
18
- # and its right side, eg ||= key.upcase.
19
- def self.arm_label(arm, choice)
20
- "#{choice} #{arm[:text]}"
21
- end
22
-
23
17
  # Its target needs no record call, and its right side a
24
18
  # written_at call.
25
19
  def self.own_calls?