path_coverage 0.4.0 → 0.5.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 (102) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +286 -199
  3. data/exe/path_coverage_report +4 -0
  4. data/lib/path_coverage/call_trace.rb +88 -0
  5. data/lib/path_coverage/calls.rb +49 -43
  6. data/lib/path_coverage/card_context.rb +104 -0
  7. data/lib/path_coverage/card_forks.rb +44 -0
  8. data/lib/path_coverage/card_gaps.rb +125 -0
  9. data/lib/path_coverage/card_scopes.rb +37 -0
  10. data/lib/path_coverage/card_tree.rb +79 -66
  11. data/lib/path_coverage/card_way_lists.rb +72 -0
  12. data/lib/path_coverage/card_ways.rb +123 -67
  13. data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +35 -33
  14. data/lib/path_coverage/condition.rb +14 -14
  15. data/lib/path_coverage/configuration.rb +7 -4
  16. data/lib/path_coverage/configuration_limits.rb +52 -0
  17. data/lib/path_coverage/configuration_paths.rb +7 -7
  18. data/lib/path_coverage/console_summary.rb +24 -22
  19. data/lib/path_coverage/{site.rb → decision.rb} +2 -2
  20. data/lib/path_coverage/decision_locations.rb +76 -0
  21. data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +13 -11
  22. data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
  23. data/lib/path_coverage/fold_branches.rb +18 -0
  24. data/lib/path_coverage/fold_cells.rb +42 -0
  25. data/lib/path_coverage/{html_tree.rb → fold_rows.rb} +42 -71
  26. data/lib/path_coverage/gaps.rb +33 -15
  27. data/lib/path_coverage/json_reader.rb +54 -19
  28. data/lib/path_coverage/json_reports.rb +102 -0
  29. data/lib/path_coverage/json_writer.rb +27 -12
  30. data/lib/path_coverage/kernel.rb +56 -58
  31. data/lib/path_coverage/kinds/base.rb +16 -16
  32. data/lib/path_coverage/kinds/block.rb +16 -15
  33. data/lib/path_coverage/kinds/case.rb +14 -12
  34. data/lib/path_coverage/kinds/chain.rb +40 -11
  35. data/lib/path_coverage/kinds/choice.rb +4 -3
  36. data/lib/path_coverage/kinds/contains.rb +40 -32
  37. data/lib/path_coverage/kinds/safe.rb +38 -26
  38. data/lib/path_coverage/kinds/write.rb +6 -6
  39. data/lib/path_coverage/kinds.rb +2 -2
  40. data/lib/path_coverage/leaving.rb +44 -0
  41. data/lib/path_coverage/loop_forks.rb +57 -0
  42. data/lib/path_coverage/loop_nodes.rb +55 -0
  43. data/lib/path_coverage/manifest.rb +92 -58
  44. data/lib/path_coverage/manifest_blocks.rb +5 -26
  45. data/lib/path_coverage/manifest_case.rb +2 -2
  46. data/lib/path_coverage/manifest_chains.rb +8 -8
  47. data/lib/path_coverage/manifest_choices.rb +23 -20
  48. data/lib/path_coverage/manifest_disabled.rb +97 -0
  49. data/lib/path_coverage/manifest_folding.rb +43 -40
  50. data/lib/path_coverage/manifest_ignoring.rb +6 -6
  51. data/lib/path_coverage/manifest_writes.rb +11 -10
  52. data/lib/path_coverage/measured_by.rb +20 -0
  53. data/lib/path_coverage/merge.rb +7 -7
  54. data/lib/path_coverage/method_loops.rb +128 -0
  55. data/lib/path_coverage/method_statements.rb +130 -127
  56. data/lib/path_coverage/method_ways.rb +142 -0
  57. data/lib/path_coverage/paths_report.rb +16 -16
  58. data/lib/path_coverage/recorder.rb +57 -36
  59. data/lib/path_coverage/recorder_blocks.rb +17 -17
  60. data/lib/path_coverage/recorder_cases.rb +12 -11
  61. data/lib/path_coverage/recorder_chains.rb +12 -12
  62. data/lib/path_coverage/recorder_writes.rb +9 -9
  63. data/lib/path_coverage/render.rb +58 -51
  64. data/lib/path_coverage/result.rb +32 -31
  65. data/lib/path_coverage/rewriter.rb +39 -27
  66. data/lib/path_coverage/rewriter_nested.rb +13 -13
  67. data/lib/path_coverage/runs_report.rb +84 -0
  68. data/lib/path_coverage/source_lines.rb +16 -16
  69. data/lib/path_coverage/source_report.rb +100 -0
  70. data/lib/path_coverage/stable_leaves.rb +162 -0
  71. data/lib/path_coverage/statement_arms.rb +31 -30
  72. data/lib/path_coverage/summary.rb +4 -62
  73. data/lib/path_coverage/totals.rb +10 -8
  74. data/lib/path_coverage/version.rb +1 -1
  75. data/lib/path_coverage/walk.rb +106 -0
  76. data/lib/path_coverage/way_algebras.rb +127 -0
  77. data/lib/path_coverage/ways_report.rb +91 -0
  78. data/lib/path_coverage/ways_totals.rb +46 -0
  79. data/lib/path_coverage/within_methods.rb +135 -80
  80. metadata +33 -28
  81. data/lib/path_coverage/card.rb +0 -14
  82. data/lib/path_coverage/html/file.html +0 -33
  83. data/lib/path_coverage/html/index.html +0 -29
  84. data/lib/path_coverage/html/settings.js +0 -50
  85. data/lib/path_coverage/html/side.js +0 -599
  86. data/lib/path_coverage/html/sort.js +0 -34
  87. data/lib/path_coverage/html/style.css +0 -484
  88. data/lib/path_coverage/html/tabs.js +0 -15
  89. data/lib/path_coverage/html/theme.js +0 -27
  90. data/lib/path_coverage/html_branch.rb +0 -30
  91. data/lib/path_coverage/html_cards.rb +0 -88
  92. data/lib/path_coverage/html_cells.rb +0 -71
  93. data/lib/path_coverage/html_file.rb +0 -123
  94. data/lib/path_coverage/html_index.rb +0 -128
  95. data/lib/path_coverage/html_index_head.rb +0 -29
  96. data/lib/path_coverage/html_labels.rb +0 -16
  97. data/lib/path_coverage/html_methods.rb +0 -66
  98. data/lib/path_coverage/html_nav.rb +0 -67
  99. data/lib/path_coverage/html_report.rb +0 -131
  100. data/lib/path_coverage/html_statements.rb +0 -186
  101. data/lib/path_coverage/html_tabs.rb +0 -26
  102. data/lib/path_coverage/html_template.rb +0 -26
@@ -1,41 +1,32 @@
1
- require 'cgi/escape'
2
1
  require_relative 'condition'
3
- require_relative 'html_branch'
4
- require_relative 'html_cells'
5
- require_relative 'html_labels'
6
- require_relative 'html_syntax'
2
+ require_relative 'code_tokens'
3
+ require_relative 'fold_branches'
4
+ require_relative 'fold_cells'
7
5
  require_relative 'kinds'
8
6
 
9
7
  module PathCoverage
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.
12
- module HtmlTree
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.
16
- def self.site_rows(site)
17
- counts = HtmlCells.counts(site[:counts])
18
- attrs = at(site) + else_attr(site[:else_at])
19
- [row(attrs, operator(site), counts), *operand_rows(site, '')]
20
- end
21
-
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(',')}") : ''
26
- end
27
-
28
- # SPIKE: the data-at attribute of a node located in its
29
- # source, '' for one that is not.
30
- def self.at(node)
31
- node[:at] ? %( data-at="#{node[:at].join(',')}") : ''
8
+ # The tree of a decision, from DecisionTree.of, as its row's
9
+ # fold in a card shows it: a grid whose columns are the
10
+ # decision's paths, as page data holds its rows, each a kind,
11
+ # a branch, a label, where its code is and a cell per path.
12
+ module FoldRows
13
+ # The rows of decision's fold, as page data holds them:
14
+ # decision's own row, naming its operator and each path's
15
+ # count, and where its else keyword is, if it has one; then
16
+ # the rows of its operands.
17
+ def self.rows(decision)
18
+ text = decision.fetch(:label) { decision[:operator] }
19
+ label = CodeTokens.label_code(text)
20
+ own = { kind: 'decision', branch: '', label:, at: decision[:at],
21
+ else: decision[:else_at], counts: decision[:counts] }
22
+ [own, *operand_rows(decision, '')]
32
23
  end
33
24
 
34
25
  # The rows of a condition's operands, each branching from
35
26
  # its operator after indent; a nested condition's operands,
36
27
  # and those of an arm's condition, follow its own row,
37
- # indented under its branch. A leaf with sites inside it
38
- # branches as those sites, then the leaf, in the order they
28
+ # indented under its branch. A leaf with decisions inside it
29
+ # branches as those decisions, then the leaf, in the order they
39
30
  # run.
40
31
  def self.operand_rows(condition, indent)
41
32
  own = Kinds.of(condition[:operator]).rows(condition, indent)
@@ -45,8 +36,8 @@ module PathCoverage
45
36
  last = operands.size - 1
46
37
  operands.each_with_index.flat_map do |operand, i|
47
38
  tip = i == last
48
- branch = indent + (tip ? HtmlBranch::ELL : HtmlBranch::TEE)
49
- under = indent + (tip ? HtmlBranch::GAP : HtmlBranch::BAR)
39
+ branch = indent + (tip ? FoldBranches::ELL : FoldBranches::TEE)
40
+ under = indent + (tip ? FoldBranches::GAP : FoldBranches::BAR)
50
41
  node_rows(operand, branch, under)
51
42
  end
52
43
  end
@@ -65,11 +56,9 @@ module PathCoverage
65
56
  # it: return or raise, coloured as code, marking each path
66
57
  # that leaves there, placed at its code.
67
58
  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)
59
+ cells = FoldCells.values(node[:cells], FoldCells::LEAVES)
60
+ label = CodeTokens.code(node[:leaves])
61
+ { kind: 'leaves', branch:, label:, at: node[:at], cells: }
73
62
  end
74
63
 
75
64
  # A ternary's arm's row, ? for then and : for else: an arm
@@ -78,20 +67,18 @@ module PathCoverage
78
67
  # the paths taking it.
79
68
  def self.arm_rows(arm, branch, under)
80
69
  body = arm[:body]
81
- values = arm_cells(arm, body)
82
- label = HtmlLabels.arm(arm)
83
- lead = HtmlBranch.cell(branch) +
84
- %(<span class="arm" data-label="">#{label}</span>)
85
- row = row(%( data-arm=""#{at(arm)}), lead, values)
70
+ label = CodeTokens.ternary_arm_code(Kinds::Choice.arm_label(arm))
71
+ row = { kind: 'arm', branch:, label:, at: arm[:at],
72
+ cells: arm_cells(arm, body) }
86
73
  body ? [row, *operand_rows(body, under)] : [row]
87
74
  end
88
75
 
89
76
  # The cells of arm's row: its body's values, when its body is
90
77
  # a condition; else a tick on each path that took it.
91
78
  def self.arm_cells(arm, body)
92
- return HtmlCells.of(body[:cells]) if body
79
+ return FoldCells.values(body[:cells]) if body
93
80
 
94
- HtmlCells.of(arm[:cells], HtmlCells::TAKEN)
81
+ FoldCells.values(arm[:cells], FoldCells::TAKEN)
95
82
  end
96
83
 
97
84
  # The operands of condition a card's fold shows: a ?:'s arms,
@@ -116,13 +103,13 @@ module PathCoverage
116
103
  # it, so left the method, at where its code is, so hovering it
117
104
  # lights the row; nil for an arm that does neither.
118
105
  def self.leaving(arm)
119
- word = arm[:text].to_s[LEAVES, 1]
106
+ word = arm[:leaves]
120
107
  word && { leaves: word, cells: arm[:cells], at: arm[:at] }
121
108
  end
122
109
 
123
110
  # operands with each node standing for the nodes its kind
124
- # unfolds it into, eg a leaf with sites inside it for those
125
- # sites, then the leaf; a leaf or an arm for itself.
111
+ # unfolds it into, eg a leaf with decisions inside it for those
112
+ # decisions, then the leaf; a leaf or an arm for itself.
126
113
  def self.unfolded(operands)
127
114
  operands.flat_map do |operand|
128
115
  operator = operand[:operator]
@@ -130,27 +117,14 @@ module PathCoverage
130
117
  end
131
118
  end
132
119
 
133
- # A nested condition's row: its branch, its operator, and
134
- # its value on each path.
120
+ # A nested condition's row: its branch, its operator, or its
121
+ # label when it has one, eg select { for a decision inside a leaf,
122
+ # and its value on each path.
135
123
  def self.subtree_row(condition, branch)
136
- lead = HtmlBranch.cell(branch) + operator(condition)
137
- attrs = %( data-subtree=""#{at(condition)})
138
- row(attrs, lead, HtmlCells.of(condition[:cells]))
139
- end
140
-
141
- # A row of the grid, with attrs, lead's cells naming it,
142
- # then values, its cells for each path.
143
- def self.row(attrs, lead, values)
144
- %(<div class="row"#{attrs}>#{lead}#{values}</div>)
145
- end
146
-
147
- # The cell naming a condition's operator, or its label when
148
- # it has one, eg select { for a site inside a leaf, coloured
149
- # as the code it is part of.
150
- def self.operator(condition)
151
- label = condition.fetch(:label) { condition[:operator] }
152
- name = HtmlSyntax.label(label)
153
- %(<span class="operator" data-operator="">#{name}</span>)
124
+ text = condition.fetch(:label) { condition[:operator] }
125
+ cells = FoldCells.values(condition[:cells])
126
+ { kind: 'subtree', branch:, label: CodeTokens.label_code(text),
127
+ at: condition[:at], cells: }
154
128
  end
155
129
 
156
130
  # One operand's row: its branch, text, coloured as the code it
@@ -159,11 +133,8 @@ module PathCoverage
159
133
  # each path.
160
134
  def self.operand_row(operand, branch)
161
135
  one_line = operand[:text].to_s.gsub(/\s*\n\s*/, ' ')
162
- text = HtmlSyntax.fragment(one_line)
163
- lead = HtmlBranch.cell(branch) +
164
- %(<span class="text" data-text="">#{text}</span>)
165
- attrs = %( data-operand=""#{at(operand)})
166
- row(attrs, lead, HtmlCells.of(operand[:cells]))
136
+ { kind: 'operand', branch:, label: CodeTokens.code(one_line),
137
+ at: operand[:at], cells: FoldCells.values(operand[:cells]) }
167
138
  end
168
139
  end
169
140
  end
@@ -1,32 +1,50 @@
1
1
  module PathCoverage
2
- # The terminal worklist: one line per path a site never
3
- # took, and nothing for a site that took them all. Every
2
+ # The terminal worklist: one line per path a decision never
3
+ # took, and nothing for a decision that took them all. Every
4
4
  # count stays in the operands and paths for reporters that
5
5
  # show them all.
6
6
  module Gaps
7
7
  # The gap lines of operands, paths and outside, as Result
8
- # gives them, in manifest order; texts is { site => each
9
- # leaf's source text }. A site's first leaf runs before any
10
- # other, so a site whose first never ran was never reached.
8
+ # gives them, in manifest order; texts is { decision => each
9
+ # leaf's source text }. A decision's first leaf runs before any
10
+ # other, so a decision whose first never ran was never reached.
11
11
  def self.lines(operands, paths: {}, texts: {}, outside: {})
12
- operands.flat_map do |site, counts|
13
- next ["#{site} never reached"] if never_ran?(counts.first)
12
+ operands.flat_map do |decision, counts|
13
+ next ["#{decision} never reached"] if never_ran?(counts.first)
14
14
 
15
- path_gaps(site, paths.fetch(site, []), texts[site]) +
16
- ruled_out(site, outside.fetch(site, {}), texts[site])
15
+ path_gaps(decision, paths.fetch(decision, []),
16
+ texts[decision]) +
17
+ ruled_out(decision, outside.fetch(decision, {}),
18
+ texts[decision])
17
19
  end
18
20
  end
19
21
 
20
- # One line per path site took that a treatment rules out,
22
+ # One line per path decision took that a treatment rules out,
21
23
  # as Result#outside gives them, texts being each leaf's
22
24
  # source text.
23
- def self.ruled_out(site, taken, texts)
25
+ def self.ruled_out(decision, taken, texts)
24
26
  taken.keys.map do |path|
25
- "#{site} path taken that a treatment rules out: " \
27
+ "#{decision} path taken that a treatment rules out: " \
26
28
  "#{steps(path, texts)}"
27
29
  end
28
30
  end
29
31
 
32
+ # Example: ["k.rb:5:11 path taken that its method rules out:
33
+ # `a` true"].
34
+ # One line per path taken, or return or raise reached, that
35
+ # its method rules out, ruled as CardContext.ruled_out gives
36
+ # them, texts being { decision => each leaf's source text }.
37
+ # Such a line shows a fact derived from the code to be wrong.
38
+ def self.ruled_by_method(ruled, texts)
39
+ ruled.map do |name, path|
40
+ reached = "#{name} reached, though its method rules it out"
41
+ next reached if path.nil?
42
+
43
+ "#{name} path taken that its method rules out: " \
44
+ "#{steps(path, texts[name])}"
45
+ end
46
+ end
47
+
30
48
  # The outcome of each leaf path reaches, each named by its
31
49
  # text, eg "`u.name` nil". A path's first leaf always has an
32
50
  # outcome, so it always has a step.
@@ -36,15 +54,15 @@ module PathCoverage
36
54
  end.join(', ')
37
55
  end
38
56
 
39
- # One line per path site never took, as Result#paths gives
57
+ # One line per path decision never took, as Result#paths gives
40
58
  # them, naming the outcome of each leaf the path reaches,
41
59
  # texts being each leaf's source text.
42
- def self.path_gaps(site, paths, texts)
60
+ def self.path_gaps(decision, paths, texts)
43
61
  paths.each_with_object([]) do |each, lines|
44
62
  next unless each[:count].zero?
45
63
 
46
64
  named = steps(each[:path], texts)
47
- lines << "#{site} path never taken: #{named}"
65
+ lines << "#{decision} path never taken: #{named}"
48
66
  end
49
67
  end
50
68
 
@@ -1,11 +1,13 @@
1
1
  require 'json'
2
2
  require_relative 'kinds'
3
+ require_relative 'measured_by'
3
4
  require_relative 'merge'
5
+ require_relative 'ways_report'
4
6
 
5
7
  module PathCoverage
6
8
  # Reads back the JSON reports JsonWriter writes, merging the
7
9
  # counts of several runs, and refusing any report whose
8
- # sites' paths are listed differently from a render's.
10
+ # decisions' paths are listed differently from a render's.
9
11
  module JsonReader
10
12
  # The keys of a JSON report's fingerprint.
11
13
  FINGERPRINT = %w[ignored treats].freeze
@@ -18,13 +20,42 @@ module PathCoverage
18
20
  end
19
21
 
20
22
  # 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.
23
+ # Render.run takes them: decision_paths:, each decision's possible
24
+ # paths, outside:, those a treatment rules out, reached:,
25
+ # each statement's reaches, and ways:, each method's calls'
26
+ # ways and each loop's passes', each merged.
24
27
  def self.taken(paths)
25
- site_paths = Merge.paths(paths.map { |path| paths_of(path) })
28
+ decision_paths = Merge.paths(paths.map do |path|
29
+ paths_of(path)
30
+ end)
26
31
  outside = merged(paths, 'outside')
27
- { site_paths:, outside:, reached: reached(paths) }
32
+ { decision_paths:, outside:, reached: reached(paths),
33
+ ways: ways(paths) }
34
+ end
35
+
36
+ # The ways of the JSON reports at paths, as WaysReport.ways
37
+ # reads them, each unit's way's counts summed; nil when none
38
+ # lists any, eg reports the :post reporter sent, so a render
39
+ # cannot say which ways were taken.
40
+ def self.ways(paths)
41
+ runs = paths.map do |path|
42
+ report = JSON.parse(File.read(path))
43
+ listed_ways?(report) ? WaysReport.ways(report) : nil
44
+ end.compact
45
+ return nil if runs.empty?
46
+
47
+ runs.reduce do |all, run|
48
+ all.merge(run) do |_, ways, more|
49
+ ways.merge(more) { |_, a, b| a + b }
50
+ end
51
+ end
52
+ end
53
+
54
+ # True when report, a JSON report, lists its cards' ways: one a
55
+ # run wrote, which names the max_paths its cards were listed
56
+ # with; not one the :post reporter sent, which has no cards.
57
+ def self.listed_ways?(report)
58
+ report.key?('max_paths')
28
59
  end
29
60
 
30
61
  # Example: { "k.rb:2:2" => 2 } and { "k.rb:2:2" => 1 } give
@@ -41,15 +72,19 @@ module PathCoverage
41
72
  end
42
73
 
43
74
  # 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)
75
+ # kinds a render does, with fingerprint, and was measured by
76
+ # the same tools at the same versions; one that differs in
77
+ # either, or names none, can list its decisions' paths
78
+ # differently, so its counts cannot be trusted to the paths a
79
+ # render lists.
80
+ def self.same_measuring(path, json, fingerprint)
49
81
  kinds = json['kinds'] == Kinds.names
50
- return if kinds && json.slice(*FINGERPRINT) == fingerprint
82
+ unless kinds && json.slice(*FINGERPRINT) == fingerprint
83
+ raise ArgumentError, "#{path} measured other kinds"
84
+ end
85
+ return if json['measured_by'] == MeasuredBy.versions
51
86
 
52
- raise ArgumentError, "#{path} measured other kinds"
87
+ raise ArgumentError, "#{path} was measured by other tools"
53
88
  end
54
89
 
55
90
  # The key lists of the JSON reports at paths, as listed_of
@@ -59,7 +94,7 @@ module PathCoverage
59
94
  end
60
95
 
61
96
  # The JSON report at path's list under key, eg its paths a
62
- # treatment rules out, { site => { path => count } }; none
97
+ # treatment rules out, { decision => { path => count } }; none
63
98
  # for a report that has none, so the render claims none.
64
99
  def self.listed_of(path, key)
65
100
  json = JSON.parse(File.read(path))
@@ -68,12 +103,12 @@ module PathCoverage
68
103
  end
69
104
  end
70
105
 
71
- # The site paths of the JSON report at path, as
106
+ # The decision paths of the JSON report at path, as
72
107
  # Result#paths gives them; none for a report that has none.
73
108
  def self.paths_of(path)
74
109
  json = JSON.parse(File.read(path))
75
- json.fetch('paths', {}).transform_values do |site|
76
- site['taken'].map do |each|
110
+ json.fetch('paths', {}).transform_values do |decision|
111
+ decision['taken'].map do |each|
77
112
  { path: each['path'], count: each['count'] }
78
113
  end
79
114
  end
@@ -84,8 +119,8 @@ module PathCoverage
84
119
  # fingerprint.
85
120
  def self.read(path, fingerprint)
86
121
  json = JSON.parse(File.read(path))
87
- same_kinds(path, json, fingerprint)
88
- json['sites'].transform_values do |leaves|
122
+ same_measuring(path, json, fingerprint)
123
+ json['decisions'].transform_values do |leaves|
89
124
  leaves.map { |leaf| counts(leaf) }
90
125
  end
91
126
  end
@@ -0,0 +1,102 @@
1
+ require 'fileutils'
2
+ require 'json'
3
+ require_relative 'card_gaps'
4
+ require_relative 'console_summary'
5
+ require_relative 'json_writer'
6
+ require_relative 'paths_report'
7
+ require_relative 'source_report'
8
+ require_relative 'within_methods'
9
+
10
+ module PathCoverage
11
+ # The two JSON reports every run writes, and every render of
12
+ # runs merged, into coverage_dir: path_coverage.json, the
13
+ # counts, and path_coverage.source.json, what drawing each
14
+ # file's code needs, for html-reporter to draw the pages from.
15
+ module JsonReports
16
+ # What WithinMethods reads of a run: operands, decision paths,
17
+ # and paths a treatment rules out, as Result gives them.
18
+ Recorded = Data.define(:operands, :paths, :outside)
19
+
20
+ # Writes both reports into config's coverage_dir, created
21
+ # first if need be. operands, and taken's decision_paths: and
22
+ # outside:, are as Result gives them, taken's reached: as
23
+ # Recorder#reached gives it, nil when not known; files is
24
+ # { full path => {source:, decisions:} } for each covered file.
25
+ def self.write(operands, files, config, **taken)
26
+ FileUtils.mkdir_p(config.output_dir)
27
+ paths = taken.values_at(:decision_paths, :outside)
28
+ recorded = Recorded.new(operands, *paths)
29
+ reached = taken[:reached]
30
+ cards = WithinMethods.listed(files, config, recorded,
31
+ [reached, taken[:ways]])
32
+ tree = WithinMethods.tree(files, config, recorded)
33
+ write_json(recorded, files, config, [cards, reached, tree])
34
+ write_ways(cards, files, config, tree)
35
+ write_source(files, config, recorded.paths, cards)
36
+ end
37
+
38
+ # Writes recorded as the JSON report at config's json_path:
39
+ # its operands, decision paths, paths a treatment rules out,
40
+ # totals, cards, each def's card, as WithinMethods.listed
41
+ # gives them, tree, how each card's rows nest, the statements
42
+ # reached, when known, and config's fingerprint and limits.
43
+ def self.write_json(recorded, files, config, listed)
44
+ cards, reached, tree = listed
45
+ report = PathsReport.of(recorded.paths, files)
46
+ totals = totals(recorded.paths, files, config, reached)
47
+ limits = { 'loops' => config.loops?,
48
+ 'max_paths' => config.path_limit }
49
+ parts = { totals:, outside: recorded.outside, cards:, tree:,
50
+ fingerprint: config.fingerprint, limits: }
51
+ parts[:reached] = reached if reached
52
+ path = config.json_path
53
+ JsonWriter.write(recorded.operands, path, report, **parts)
54
+ end
55
+
56
+ # Writes the ways lines, each method's untested paths, as
57
+ # CardGaps.lines gives them for cards, beside the JSON report,
58
+ # at ways_path, so a later check, or the :ways reporter, can
59
+ # show them; tree is how each card's rows nest, as
60
+ # WithinMethods.tree gives it.
61
+ def self.write_ways(cards, files, config, tree)
62
+ parents = tree.flat_map do |path, nested|
63
+ nested.values.flat_map do |rows|
64
+ rows.filter_map do |at, place|
65
+ next unless place['parent']
66
+
67
+ ["#{path}:#{at}", "#{path}:#{place['parent']}"]
68
+ end
69
+ end
70
+ end.to_h
71
+ lines = CardGaps.lines(cards, PathsReport.texts(files), parents)
72
+ File.write(ways_path(config), lines.map { "#{_1}\n" }.join)
73
+ end
74
+
75
+ # Where the ways lines go, beside the JSON report.
76
+ def self.ways_path(config)
77
+ config.json_path.sub(/\.json\z/, '.ways.txt')
78
+ end
79
+
80
+ # Writes the source report beside the JSON report, from
81
+ # files, decision_paths and cards, as write has them.
82
+ def self.write_source(files, config, decision_paths, cards)
83
+ source = SourceReport.of(files, config, decision_paths, cards)
84
+ path = config.json_path.sub(/\.json\z/, '.source.json')
85
+ File.write(path, JSON.generate(source))
86
+ end
87
+
88
+ # The JSON report's totals, each [taken, possible] paths:
89
+ # within each decision, and within each method; with groups
90
+ # configured, each group's own too, under groups.
91
+ def self.totals(decision_paths, files, config, reached)
92
+ cards = WithinMethods.cards(files, config, decision_paths,
93
+ reached)
94
+ totals = ConsoleSummary.totals(decision_paths, cards)
95
+ groups = config.groups
96
+ return totals if groups.empty?
97
+
98
+ own = ConsoleSummary.group_totals(decision_paths, cards, groups)
99
+ totals.merge('groups' => own)
100
+ end
101
+ end
102
+ end
@@ -1,21 +1,26 @@
1
1
  require 'json'
2
2
  require_relative 'kinds'
3
+ require_relative 'measured_by'
3
4
 
4
5
  module PathCoverage
5
6
  # The base reporter: the result as raw JSON, which every
6
7
  # other reporter can read back.
7
8
  module JsonWriter
9
+ # The URL of the schema the JSON report follows.
10
+ SCHEMA = 'https://path-coverage.github.io/schemas/v1/' \
11
+ 'path_coverage.schema.json'.freeze
12
+
8
13
  # Writes operands, as Result gives them, paths, as
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,
12
- # possible]}}.
14
+ # PathsReport gives them, and totals, as JsonReports.totals
15
+ # gives them, to path as {"decisions": {decision: [counts, ...]},
16
+ # "paths": {decision: {leaves:, taken:}}, "totals": {"alone":
17
+ # [taken, possible]}}.
13
18
  # parts are json's.
14
19
  def self.write(operands, path, paths = {}, **parts)
15
20
  File.write(path, json(operands, paths, **parts))
16
21
  end
17
22
 
18
- # The keys of parts listed as { site => [{path:, count:}] }:
23
+ # The keys of parts listed as { decision => [{path:, count:}] }:
19
24
  # each path taken that a treatment rules out, as
20
25
  # Result#outside gives them.
21
26
  LISTED = %i[outside].freeze
@@ -24,20 +29,30 @@ module PathCoverage
24
29
  # cards:, as WithinMethods.listed gives them, and tree:, as
25
30
  # WithinMethods.tree gives it, as the JSON report's text, with
26
31
  # parts' fingerprint:, as Configuration#fingerprint gives
27
- # it, the names of the kinds measured, and parts' reached:,
32
+ # it, the names of the kinds measured, the tools that measured
33
+ # it and their versions, and parts' reached:,
28
34
  # as Recorder#reached gives it, even empty, as a run that
29
- # reached no statement says so; none when not given.
35
+ # reached no statement says so; none when not given; parts'
36
+ # limits:, { "loops" =>, "max_paths" => } the cards were
37
+ # listed with; headed by its schema's URL.
30
38
  def self.json(operands, paths = {}, **parts)
31
39
  fingerprint = parts.fetch(:fingerprint, {})
32
- report = { 'sites' => operands, **fingerprint }
40
+ report = { '$schema' => SCHEMA, 'decisions' => operands,
41
+ **fingerprint }
33
42
  report['paths'] = paths unless paths.empty?
34
- report.merge!(lists(parts))
35
- report['reached'] = parts[:reached] if parts.key?(:reached)
36
- report.merge!(kept(parts))
43
+ report.merge!(lists(parts), counted(parts), kept(parts))
37
44
  report['kinds'] = Kinds.names
45
+ report['measured_by'] = MeasuredBy.versions
38
46
  JSON.pretty_generate(report)
39
47
  end
40
48
 
49
+ # { "reached" =>, "loops" =>, "max_paths" => } of parts'
50
+ # reached:, when given, and limits:, when given.
51
+ def self.counted(parts)
52
+ reached = parts.slice(:reached).transform_keys(&:to_s)
53
+ reached.merge(parts.fetch(:limits, {}))
54
+ end
55
+
41
56
  # The keys of parts written as they are, when not empty.
42
57
  KEPT = %i[totals cards tree].freeze
43
58
 
@@ -59,7 +74,7 @@ module PathCoverage
59
74
  end.to_h
60
75
  end
61
76
 
62
- # taken, { site => { path => count } }, as JSON lists it,
77
+ # taken, { decision => { path => count } }, as JSON lists it,
63
78
  # eg { 'k.rb:2:2' => [{ path: [true, false], count: 1 }] }.
64
79
  def self.listed(taken)
65
80
  taken.transform_values do |counts|