path_coverage 0.1.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 (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +315 -107
  3. data/lib/path_coverage/calls.rb +55 -23
  4. data/lib/path_coverage/card.rb +14 -0
  5. data/lib/path_coverage/condition.rb +33 -3
  6. data/lib/path_coverage/configuration.rb +43 -22
  7. data/lib/path_coverage/configuration_paths.rb +58 -0
  8. data/lib/path_coverage/console_summary.rb +75 -0
  9. data/lib/path_coverage/gaps.rb +32 -13
  10. data/lib/path_coverage/html/file.html +5 -5
  11. data/lib/path_coverage/html/index.html +6 -18
  12. data/lib/path_coverage/html/settings.js +50 -0
  13. data/lib/path_coverage/html/side.js +341 -413
  14. data/lib/path_coverage/html/sort.js +24 -21
  15. data/lib/path_coverage/html/style.css +339 -207
  16. data/lib/path_coverage/html/tabs.js +15 -0
  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 +111 -0
  20. data/lib/path_coverage/html_cells.rb +66 -0
  21. data/lib/path_coverage/html_file.rb +82 -68
  22. data/lib/path_coverage/html_index.rb +95 -41
  23. data/lib/path_coverage/html_index_head.rb +29 -0
  24. data/lib/path_coverage/html_labels.rb +23 -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 +69 -56
  28. data/lib/path_coverage/html_statements.rb +166 -0
  29. data/lib/path_coverage/html_syntax.rb +156 -0
  30. data/lib/path_coverage/html_tabs.rb +26 -0
  31. data/lib/path_coverage/html_template.rb +6 -0
  32. data/lib/path_coverage/html_tree.rb +59 -74
  33. data/lib/path_coverage/json_reader.rb +101 -0
  34. data/lib/path_coverage/json_writer.rb +52 -7
  35. data/lib/path_coverage/kernel.rb +55 -53
  36. data/lib/path_coverage/kinds/base.rb +71 -0
  37. data/lib/path_coverage/kinds/block.rb +137 -0
  38. data/lib/path_coverage/kinds/case.rb +92 -0
  39. data/lib/path_coverage/kinds/chain.rb +45 -0
  40. data/lib/path_coverage/kinds/choice.rb +68 -0
  41. data/lib/path_coverage/kinds/contains.rb +113 -0
  42. data/lib/path_coverage/kinds/safe.rb +153 -0
  43. data/lib/path_coverage/kinds/write.rb +54 -0
  44. data/lib/path_coverage/kinds.rb +37 -0
  45. data/lib/path_coverage/manifest.rb +60 -20
  46. data/lib/path_coverage/manifest_blocks.rb +48 -10
  47. data/lib/path_coverage/manifest_case.rb +2 -3
  48. data/lib/path_coverage/manifest_chains.rb +76 -0
  49. data/lib/path_coverage/manifest_choices.rb +32 -18
  50. data/lib/path_coverage/manifest_folding.rb +91 -0
  51. data/lib/path_coverage/manifest_ignoring.rb +35 -0
  52. data/lib/path_coverage/manifest_writes.rb +67 -0
  53. data/lib/path_coverage/merge.rb +19 -3
  54. data/lib/path_coverage/method_statements.rb +300 -0
  55. data/lib/path_coverage/post.rb +8 -8
  56. data/lib/path_coverage/recorder.rb +20 -79
  57. data/lib/path_coverage/recorder_blocks.rb +68 -0
  58. data/lib/path_coverage/recorder_cases.rb +23 -0
  59. data/lib/path_coverage/recorder_chains.rb +40 -0
  60. data/lib/path_coverage/recorder_writes.rb +26 -0
  61. data/lib/path_coverage/render.rb +56 -47
  62. data/lib/path_coverage/result.rb +19 -0
  63. data/lib/path_coverage/rewriter.rb +69 -107
  64. data/lib/path_coverage/rewriter_nested.rb +62 -0
  65. data/lib/path_coverage/site_paths.rb +9 -58
  66. data/lib/path_coverage/site_tree.rb +21 -57
  67. data/lib/path_coverage/statement_arms.rb +161 -0
  68. data/lib/path_coverage/summary.rb +41 -17
  69. data/lib/path_coverage/totals.rb +6 -12
  70. data/lib/path_coverage/version.rb +1 -1
  71. data/lib/path_coverage/within_methods.rb +92 -0
  72. metadata +40 -11
  73. data/lib/path_coverage/html/toggle.js +0 -12
  74. data/lib/path_coverage/html_paths.rb +0 -138
  75. data/lib/path_coverage/path_gaps.rb +0 -41
  76. data/lib/path_coverage/path_manifest.rb +0 -110
@@ -0,0 +1,156 @@
1
+ require 'cgi/escape'
2
+ require 'prism'
3
+
4
+ module PathCoverage
5
+ # The colours of a file's code on its page: each token Prism
6
+ # lexes, as Ruby itself does, gets the class of its kind, eg k
7
+ # for a keyword, so the page's CSS colours it.
8
+ module HtmlSyntax
9
+ # The class of each coloured token type that is not a keyword
10
+ # or a number: c comment, s string, t constant, v instance
11
+ # variable, o an operator that decides a path.
12
+ CLASSES = {
13
+ COMMENT: 'c', CONSTANT: 't', INSTANCE_VARIABLE: 'v',
14
+ **%i[
15
+ AMPERSAND_AMPERSAND PIPE_PIPE AMPERSAND_DOT QUESTION_MARK
16
+ COLON AMPERSAND_AMPERSAND_EQUAL PIPE_PIPE_EQUAL
17
+ ].to_h { |type| [type, 'o'] },
18
+ **%i[
19
+ STRING_BEGIN STRING_CONTENT STRING_END SYMBOL_BEGIN LABEL
20
+ PERCENT_LOWER_W WORDS_SEP REGEXP_BEGIN REGEXP_END
21
+ HEREDOC_START HEREDOC_END
22
+ ].to_h { |type| [type, 's'] }
23
+ }.freeze
24
+
25
+ # { line number => [[from, to, class], ...] } for the lines of
26
+ # source that have coloured tokens, columns counting
27
+ # characters, a token over many lines having a range on each,
28
+ # touching ranges of one class joined.
29
+ def self.of(source)
30
+ layout = Layout.new(source)
31
+ pairs = classed(source).flat_map do |from, to, css|
32
+ layout.ranges(from, to).map do |number, first, last|
33
+ [number, [first, last, css]]
34
+ end
35
+ end
36
+ pairs.group_by(&:first).transform_values do |line|
37
+ joined(line.map(&:last).sort)
38
+ end
39
+ end
40
+
41
+ # A source's lines, by the byte offset each starts at.
42
+ class Layout
43
+ # The lines of source, line 1 starting at byte 0.
44
+ def initialize(source)
45
+ @source = source
46
+ @starts = [0]
47
+ source.each_line do |line|
48
+ @starts << (@starts.last + line.bytesize)
49
+ end
50
+ end
51
+
52
+ # [[line number, first column, last column], ...] for each
53
+ # line the bytes from...to reach, columns counting characters
54
+ # and kept within the line: from the line holding from to the
55
+ # last starting before to. The last start is the source's
56
+ # size, so both searches always find a line.
57
+ def ranges(from, to)
58
+ first = @starts.bsearch_index { |start| start > from } - 1
59
+ last = @starts.bsearch_index { |start| start >= to }
60
+ reached = (first...last).map { |n| columns(n, from, to) }
61
+ reached.compact
62
+ end
63
+
64
+ # [line number, first column, last column] of the bytes
65
+ # from...to on the index-th line; nil when none are on it.
66
+ def columns(index, from, to)
67
+ start = @starts[index]
68
+ size = @starts[index + 1] - start
69
+ text = @source.byteslice(start, size).chomp
70
+ first = chars(text, from - start)
71
+ last = chars(text, to - start)
72
+ first < last ? [index + 1, first, last] : nil
73
+ end
74
+
75
+ # How many characters of text its first bytes bytes hold.
76
+ def chars(text, bytes)
77
+ text.byteslice(0, bytes.clamp(0, text.bytesize)).length
78
+ end
79
+ end
80
+
81
+ # [[from, to, class], ...], the byte range and class of each
82
+ # coloured token of source; a symbol's name is coloured as
83
+ # its colon is.
84
+ def self.classed(source)
85
+ previous = nil
86
+ Prism.lex(source).value.filter_map do |token, _|
87
+ css = class_of(token.type, previous)
88
+ previous = token.type
89
+ location = token.location
90
+ css && [location.start_offset, location.end_offset, css]
91
+ end
92
+ end
93
+
94
+ # The class of a token of type, after one of type previous;
95
+ # nil for a token left uncoloured.
96
+ def self.class_of(type, previous)
97
+ return 's' if previous == :SYMBOL_BEGIN
98
+
99
+ name = type.to_s
100
+ return 'k' if name.start_with?('KEYWORD_')
101
+ return 'n' if name.start_with?('INTEGER', 'FLOAT')
102
+
103
+ CLASSES[type]
104
+ end
105
+
106
+ # ranges, in order, each touching one of the same class joined
107
+ # to it.
108
+ def self.joined(ranges)
109
+ ranges.each_with_object([]) do |(from, to, css), out|
110
+ last = out.last
111
+ if last && last[1] == from && last[2] == css
112
+ last[1] = to
113
+ else
114
+ out << [from, to, css]
115
+ end
116
+ end
117
+ end
118
+
119
+ # The names of the operators that decide a path, as a site
120
+ # tree's labels write them. Alone, some lex as other tokens,
121
+ # eg ?: as a character and || as two pipes, so each is
122
+ # coloured as an operator by name.
123
+ DECIDING = %w[&& || &. ?: &&= ||=].freeze
124
+
125
+ # text, a fragment of code on one line, eg a site tree's
126
+ # operator or operand, escaped and coloured as the code it is
127
+ # part of; one of DECIDING as the operator it names.
128
+ def self.fragment(text)
129
+ if DECIDING.include?(text)
130
+ return %(<span class="o">#{CGI.escapeHTML(text)}</span>)
131
+ end
132
+
133
+ html(text, of(text).fetch(1, []))
134
+ end
135
+
136
+ # A ternary's arm's label, eg ? total > 100, as HTML: its ? or
137
+ # : coloured as an operator, the code after it as code.
138
+ def self.ternary_arm(label)
139
+ mark, code = label.split(' ', 2)
140
+ %(<span class="o">#{mark}</span> #{fragment(code.to_s)})
141
+ end
142
+
143
+ # text, escaped, each of tokens' ranges, [[from, to, class],
144
+ # ...], in a span of its class.
145
+ def self.html(text, tokens)
146
+ at = 0
147
+ parts = tokens.map do |from, to, css|
148
+ plain = CGI.escapeHTML(text[at...from])
149
+ at = to
150
+ token = CGI.escapeHTML(text[from...to])
151
+ %(#{plain}<span class="#{css}">#{token}</span>)
152
+ end
153
+ parts.join + CGI.escapeHTML(text[at..])
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,26 @@
1
+ require 'cgi/escape'
2
+
3
+ module PathCoverage
4
+ # The index page's tab bar, one tab per table: All files,
5
+ # then each group.
6
+ module HtmlTabs
7
+ # The tab bar of names, the first tab selected, each tab
8
+ # controlling the table of the same index.
9
+ def self.bar(names)
10
+ tabs = names.each_with_index.map { |name, i| tab(name, i) }
11
+ nav = %(<nav class="tabs" role="tablist" aria-label="Groups">)
12
+ "#{nav}#{tabs.join}</nav>"
13
+ end
14
+
15
+ # The index-th tab, named name.
16
+ def self.tab(name, index)
17
+ selected = index.zero?
18
+ [
19
+ %(<button type="button" role="tab"),
20
+ %( aria-selected="#{selected}"),
21
+ %( aria-controls="files-#{index}">),
22
+ CGI.escapeHTML(name), '</button>'
23
+ ].join
24
+ end
25
+ end
26
+ end
@@ -10,6 +10,12 @@ module PathCoverage
10
10
  File.read(File.join(DIR, name))
11
11
  end
12
12
 
13
+ # The page template named name, its {{style}} the report's
14
+ # style sheet, and each other {{name}} values[name].
15
+ def self.page(name, values)
16
+ fill(asset(name), values.merge('style' => asset('style.css')))
17
+ end
18
+
13
19
  # template with each {{name}} replaced by values[name].
14
20
  def self.fill(template, values)
15
21
  template.gsub(/\{\{(\w+)\}\}/) do
@@ -1,57 +1,22 @@
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'
7
+ require_relative 'kinds'
3
8
 
4
9
  module PathCoverage
5
- # The tree of a span's sites on a file's page, from SiteTree.of
6
- # 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.
7
12
  module HtmlTree
8
- OUTCOMES = {
9
- true => 'T', false => 'F', nil => '',
10
- ITEMS[0] => '0', ITEMS[1] => '1', ITEMS[2] => '*',
11
- ANSWERS[0] => 'T', ANSWERS[1] => 'F', ANSWERS[2] => 'TF'
12
- }.freeze
13
- # The choices whose arms are named by their kind, eg then,
14
- # else or when, as their text is statements.
15
- NAMED_ARMS = [*STATEMENT_CHOICES, 'case'].freeze
16
-
17
- # A bullet marks a path taking an arm.
18
- TAKEN = { true => [0x2022].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
13
  # A site's row, naming its operator and each path's count,
33
14
  # then the rows of its operands.
34
15
  def self.site_rows(site)
35
- counts = count_cells(site[:counts])
16
+ counts = HtmlCells.counts(site[:counts])
36
17
  [row(at(site), operator(site), counts), *operand_rows(site, '')]
37
18
  end
38
19
 
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
46
- end
47
-
48
- # One cell per path, naming each with names.
49
- def self.cells(values, names = OUTCOMES)
50
- values.map do |value|
51
- %(<span class="cell" data-cell="">#{names[value]}</span>)
52
- end.join
53
- end
54
-
55
20
  # SPIKE: the data-at attribute of a node located in its
56
21
  # source, '' for one that is not.
57
22
  def self.at(node)
@@ -61,13 +26,19 @@ module PathCoverage
61
26
  # The rows of a condition's operands, each branching from
62
27
  # its operator after indent; a nested condition's operands,
63
28
  # and those of an arm's condition, follow its own row,
64
- # indented under its branch.
29
+ # indented under its branch. A leaf with sites inside it
30
+ # branches as those sites, then the leaf, in the order they
31
+ # run.
65
32
  def self.operand_rows(condition, indent)
66
- last = condition[:operands].size - 1
67
- condition[:operands].each_with_index.flat_map do |operand, i|
33
+ own = Kinds.of(condition[:operator]).rows(condition, indent)
34
+ return own if own
35
+
36
+ operands = unfolded(condition[:operands])
37
+ last = operands.size - 1
38
+ operands.each_with_index.flat_map do |operand, i|
68
39
  tip = i == last
69
- branch = indent + (tip ? '└─ ' : '├─ ')
70
- under = indent + (tip ? ' ' : '│ ')
40
+ branch = indent + (tip ? HtmlBranch::ELL : HtmlBranch::TEE)
41
+ under = indent + (tip ? HtmlBranch::GAP : HtmlBranch::BAR)
71
42
  node_rows(operand, branch, under, condition[:operator])
72
43
  end
73
44
  end
@@ -87,33 +58,45 @@ module PathCoverage
87
58
  # An arm's row: a ternary's, ? for then and : for else, an
88
59
  # arm holding a condition showing the condition's operator
89
60
  # and value, its operands below, a plain arm its text; an
90
- # if's named then or else, as its text is statements. A
91
- # plain arm marks the paths taking it.
61
+ # if's named then or else, as its text is statements; its then
62
+ # label marked as a name, as no then is written in its code,
63
+ # where its else is. A plain arm marks the paths taking it.
92
64
  def self.arm_rows(arm, branch, under, choice)
93
65
  body = arm[:body]
94
- values = cells((body || arm)[:cells], body ? OUTCOMES : TAKEN)
95
- text = CGI.escapeHTML(arm_label(arm, choice))
96
- lead = branch_cell(branch) +
97
- %(<span class="arm" data-label="">#{text}</span>)
66
+ values = arm_cells(arm, body)
67
+ named = STATEMENT_CHOICES.include?(choice) && arm[:arm] == :then
68
+ text = HtmlLabels.arm(arm, choice, named)
69
+ flag = named ? ' data-named=""' : ''
70
+ lead = HtmlBranch.cell(branch) +
71
+ %(<span class="arm" data-label=""#{flag}>#{text}</span>)
98
72
  row = row(%( data-arm=""#{at(arm)}), lead, values)
99
73
  body ? [row, *operand_rows(body, under)] : [row]
100
74
  end
101
75
 
102
- # The name of an arm of a condition whose operator is choice.
103
- def self.arm_label(arm, choice)
104
- return arm[:arm].to_s if NAMED_ARMS.include?(choice)
76
+ # The cells of arm's row: its body's values, when its body is
77
+ # a condition; else a tick on each path that took it.
78
+ def self.arm_cells(arm, body)
79
+ return HtmlCells.of(body[:cells]) if body
105
80
 
106
- mark = arm[:arm] == :then ? '?' : ':'
107
- body = arm[:body]
108
- "#{mark} #{body ? body[:operator] : arm[:text]}"
81
+ HtmlCells.of(arm[:cells], HtmlCells::TAKEN)
82
+ end
83
+
84
+ # operands with each node standing for the nodes its kind
85
+ # unfolds it into, eg a leaf with sites inside it for those
86
+ # sites, then the leaf; a leaf or an arm for itself.
87
+ def self.unfolded(operands)
88
+ operands.flat_map do |operand|
89
+ operator = operand[:operator]
90
+ operator ? Kinds.of(operator).unfold(operand) : [operand]
91
+ end
109
92
  end
110
93
 
111
94
  # A nested condition's row: its branch, its operator, and
112
95
  # its value on each path.
113
96
  def self.subtree_row(condition, branch)
114
- lead = branch_cell(branch) + operator(condition)
97
+ lead = HtmlBranch.cell(branch) + operator(condition)
115
98
  attrs = %( data-subtree=""#{at(condition)})
116
- row(attrs, lead, cells(condition[:cells]))
99
+ row(attrs, lead, HtmlCells.of(condition[:cells]))
117
100
  end
118
101
 
119
102
  # A row of the grid, with attrs, lead's cells naming it,
@@ -122,25 +105,27 @@ module PathCoverage
122
105
  %(<div class="row"#{attrs}>#{lead}#{values}</div>)
123
106
  end
124
107
 
125
- # The cell naming a condition's operator.
108
+ # The cell naming a condition's operator, or its label when
109
+ # it has one, eg select { for a site inside a leaf, coloured
110
+ # as the code it is part of.
126
111
  def self.operator(condition)
127
- name = CGI.escapeHTML(condition[:operator])
112
+ label = condition.fetch(:label) { condition[:operator] }
113
+ name = HtmlSyntax.fragment(label)
128
114
  %(<span class="operator" data-operator="">#{name}</span>)
129
115
  end
130
116
 
131
- # One operand's row: its branch, text, and its outcome on
132
- # each path.
117
+ # One operand's row: its branch, text, coloured as the code it
118
+ # is, and its outcome on each path.
119
+ # An operand that is only called, eg owner at the end of
120
+ # user&.account&.owner, has a tick on each path that called
121
+ # it instead.
133
122
  def self.operand_row(operand, branch)
134
- text = CGI.escapeHTML(operand[:text].to_s)
135
- lead = branch_cell(branch) +
123
+ text = HtmlSyntax.fragment(operand[:text].to_s)
124
+ lead = HtmlBranch.cell(branch) +
136
125
  %(<span class="text" data-text="">#{text}</span>)
137
126
  attrs = %( data-operand=""#{at(operand)})
138
- row(attrs, lead, cells(operand[:cells]))
139
- end
140
-
141
- # The cell drawing a row's branch from its operator.
142
- def self.branch_cell(branch)
143
- %(<span class="branch" data-branch="">#{branch}</span>)
127
+ names = operand[:called] ? HtmlCells::TAKEN : HtmlCells::OUTCOMES
128
+ row(attrs, lead, HtmlCells.of(operand[:cells], names))
144
129
  end
145
130
  end
146
131
  end
@@ -0,0 +1,101 @@
1
+ require 'json'
2
+ require_relative 'kinds'
3
+ require_relative 'merge'
4
+
5
+ module PathCoverage
6
+ # Reads back the JSON reports JsonWriter writes, merging the
7
+ # counts of several runs, and refusing any report whose
8
+ # sites' paths are listed differently from a render's.
9
+ module JsonReader
10
+ # The keys of a JSON report's fingerprint.
11
+ FINGERPRINT = %w[ignored treats].freeze
12
+
13
+ # The operands of the JSON reports at paths, merged; raises
14
+ # unless each measured the kinds a render does, with
15
+ # fingerprint, as Configuration#fingerprint gives it.
16
+ def self.operands(paths, fingerprint)
17
+ Merge.operands(paths.map { |path| read(path, fingerprint) })
18
+ end
19
+
20
+ # The paths taken of the JSON reports at paths, as
21
+ # Render.run takes them: site_paths:, each site's possible
22
+ # paths, outside:, those a treatment rules out, and reached:,
23
+ # each statement's reaches, each merged.
24
+ def self.taken(paths)
25
+ site_paths = Merge.paths(paths.map { |path| paths_of(path) })
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 } }
41
+ end
42
+
43
+ # Raises unless json, the JSON report at path, measured the
44
+ # kinds a render does, with fingerprint; one that measured
45
+ # others, or names none, lists its sites' paths differently,
46
+ # so its counts cannot be trusted to the paths a render
47
+ # lists.
48
+ def self.same_kinds(path, json, fingerprint)
49
+ kinds = json['kinds'] == Kinds.names
50
+ return if kinds && json.slice(*FINGERPRINT) == fingerprint
51
+
52
+ raise ArgumentError, "#{path} measured other kinds"
53
+ end
54
+
55
+ # The key lists of the JSON reports at paths, as listed_of
56
+ # gives them, each path's count the sum of every report's.
57
+ def self.merged(paths, key)
58
+ Merge.listed(paths.map { |path| listed_of(path, key) })
59
+ end
60
+
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.
64
+ def self.listed_of(path, key)
65
+ json = JSON.parse(File.read(path))
66
+ json.fetch(key, {}).transform_values do |taken|
67
+ taken.to_h { |each| [each['path'], each['count']] }
68
+ end
69
+ end
70
+
71
+ # The site paths of the JSON report at path, as
72
+ # Result#paths gives them; none for a report that has none.
73
+ def self.paths_of(path)
74
+ json = JSON.parse(File.read(path))
75
+ json.fetch('paths', {}).transform_values do |site|
76
+ site['taken'].map do |each|
77
+ { path: each['path'], count: each['count'] }
78
+ end
79
+ end
80
+ end
81
+
82
+ # The operands of the JSON report at path, which raises
83
+ # unless it measured the kinds a render does, with
84
+ # fingerprint.
85
+ def self.read(path, fingerprint)
86
+ json = JSON.parse(File.read(path))
87
+ same_kinds(path, json, fingerprint)
88
+ json['sites'].transform_values do |leaves|
89
+ leaves.map { |leaf| counts(leaf) }
90
+ end
91
+ end
92
+
93
+ # One operand's counts as JSON holds them, with the keys,
94
+ # and any only: value, as the symbols Result#operands uses.
95
+ def self.counts(leaf)
96
+ counts = { truthy: leaf['truthy'], falsy: leaf['falsy'] }
97
+ only = leaf['only']
98
+ only ? counts.merge(only: only.to_sym) : counts
99
+ end
100
+ end
101
+ end
@@ -1,4 +1,5 @@
1
1
  require 'json'
2
+ require_relative 'kinds'
2
3
 
3
4
  module PathCoverage
4
5
  # The base reporter: the result as raw JSON, which every
@@ -9,17 +10,61 @@ module PathCoverage
9
10
  # them, to path as {"sites": {site: [counts, ...]}, "paths":
10
11
  # {site: {leaves:, taken:}}, "totals": {"decisions": [taken,
11
12
  # possible]}}.
12
- def self.write(operands, path, paths = {}, totals: {})
13
- File.write(path, json(operands, paths, totals:))
13
+ # parts are json's.
14
+ def self.write(operands, path, paths = {}, **parts)
15
+ File.write(path, json(operands, paths, **parts))
14
16
  end
15
17
 
16
- # operands, and any paths and totals, as the JSON report's
17
- # text.
18
- def self.json(operands, paths = {}, totals: {})
19
- report = { 'sites' => operands }
18
+ # The keys of parts listed as { site => [{path:, count:}] }:
19
+ # each path taken that a treatment rules out, as
20
+ # Result#outside gives them.
21
+ LISTED = %i[outside].freeze
22
+
23
+ # operands, and any paths, and parts' outside:, totals: and
24
+ # cards:, as WithinMethods.listed gives them, as the JSON
25
+ # report's text, with
26
+ # parts' fingerprint:, as Configuration#fingerprint gives
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.
30
+ def self.json(operands, paths = {}, **parts)
31
+ fingerprint = parts.fetch(:fingerprint, {})
32
+ report = { 'sites' => operands, **fingerprint }
20
33
  report['paths'] = paths unless paths.empty?
21
- report['totals'] = totals unless totals.empty?
34
+ report.merge!(lists(parts))
35
+ report['reached'] = parts[:reached] if parts.key?(:reached)
36
+ report.merge!(kept(parts))
37
+ report['kinds'] = Kinds.names
22
38
  JSON.pretty_generate(report)
23
39
  end
40
+
41
+ # The keys of parts written as they are, when not empty.
42
+ KEPT = %i[totals cards].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
+
53
+ # { key => its list } of each of parts' LISTED keys that
54
+ # has one, as listed gives them.
55
+ def self.lists(parts)
56
+ LISTED.filter_map do |key|
57
+ taken = parts.fetch(key, {})
58
+ [key.to_s, listed(taken)] unless taken.empty?
59
+ end.to_h
60
+ end
61
+
62
+ # taken, { site => { path => count } }, as JSON lists it,
63
+ # eg { 'k.rb:2:2' => [{ path: [true, false], count: 1 }] }.
64
+ def self.listed(taken)
65
+ taken.transform_values do |counts|
66
+ counts.map { |path, count| { path:, count: } }
67
+ end
68
+ end
24
69
  end
25
70
  end