path_coverage 0.0.2 → 0.2.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 (58) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +330 -84
  3. data/exe/path_coverage_collector +12 -1
  4. data/lib/path_coverage/calls.rb +106 -0
  5. data/lib/path_coverage/collector.rb +88 -22
  6. data/lib/path_coverage/condition.rb +77 -3
  7. data/lib/path_coverage/configuration.rb +52 -12
  8. data/lib/path_coverage/configuration_paths.rb +56 -0
  9. data/lib/path_coverage/console_summary.rb +77 -0
  10. data/lib/path_coverage/gaps.rb +25 -9
  11. data/lib/path_coverage/html/index.html +3 -14
  12. data/lib/path_coverage/html/side.js +36 -6
  13. data/lib/path_coverage/html/sort.js +27 -21
  14. data/lib/path_coverage/html/style.css +32 -1
  15. data/lib/path_coverage/html/tabs.js +15 -0
  16. data/lib/path_coverage/html_index.rb +105 -19
  17. data/lib/path_coverage/html_index_head.rb +49 -0
  18. data/lib/path_coverage/html_report.rb +44 -12
  19. data/lib/path_coverage/html_tabs.rb +26 -0
  20. data/lib/path_coverage/html_tree.rb +47 -17
  21. data/lib/path_coverage/json_reader.rb +94 -0
  22. data/lib/path_coverage/json_writer.rb +44 -8
  23. data/lib/path_coverage/kernel.rb +53 -45
  24. data/lib/path_coverage/kinds/base.rb +71 -0
  25. data/lib/path_coverage/kinds/block.rb +136 -0
  26. data/lib/path_coverage/kinds/case.rb +89 -0
  27. data/lib/path_coverage/kinds/chain.rb +45 -0
  28. data/lib/path_coverage/kinds/choice.rb +68 -0
  29. data/lib/path_coverage/kinds/contains.rb +111 -0
  30. data/lib/path_coverage/kinds/safe.rb +151 -0
  31. data/lib/path_coverage/kinds/write.rb +54 -0
  32. data/lib/path_coverage/kinds.rb +37 -0
  33. data/lib/path_coverage/manifest.rb +93 -26
  34. data/lib/path_coverage/manifest_blocks.rb +96 -0
  35. data/lib/path_coverage/manifest_case.rb +54 -0
  36. data/lib/path_coverage/manifest_chains.rb +76 -0
  37. data/lib/path_coverage/manifest_choices.rb +64 -32
  38. data/lib/path_coverage/manifest_folding.rb +91 -0
  39. data/lib/path_coverage/manifest_ignoring.rb +35 -0
  40. data/lib/path_coverage/manifest_writes.rb +67 -0
  41. data/lib/path_coverage/merge.rb +19 -3
  42. data/lib/path_coverage/post.rb +8 -8
  43. data/lib/path_coverage/recorder.rb +43 -0
  44. data/lib/path_coverage/recorder_blocks.rb +68 -0
  45. data/lib/path_coverage/recorder_cases.rb +23 -0
  46. data/lib/path_coverage/recorder_chains.rb +40 -0
  47. data/lib/path_coverage/recorder_writes.rb +26 -0
  48. data/lib/path_coverage/render.rb +58 -46
  49. data/lib/path_coverage/result.rb +19 -0
  50. data/lib/path_coverage/rewriter.rb +64 -37
  51. data/lib/path_coverage/rewriter_nested.rb +62 -0
  52. data/lib/path_coverage/site.rb +6 -1
  53. data/lib/path_coverage/site_paths.rb +9 -46
  54. data/lib/path_coverage/site_tree.rb +31 -36
  55. data/lib/path_coverage/summary.rb +64 -17
  56. data/lib/path_coverage/totals.rb +29 -0
  57. data/lib/path_coverage/version.rb +1 -1
  58. metadata +35 -7
@@ -0,0 +1,94 @@
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 method paths of the JSON reports at paths, merged.
21
+ def self.method_paths(paths)
22
+ merged(paths, 'method_paths')
23
+ end
24
+
25
+ # The paths taken of the JSON reports at paths, as
26
+ # Render.run takes them: site_paths:, each site's possible
27
+ # paths, and outside:, those a treatment rules out, each
28
+ # merged.
29
+ def self.taken(paths)
30
+ site_paths = Merge.paths(paths.map { |path| paths_of(path) })
31
+ { site_paths:, outside: merged(paths, 'outside') }
32
+ end
33
+
34
+ # Raises unless json, the JSON report at path, measured the
35
+ # kinds a render does, with fingerprint; one that measured
36
+ # others, or names none, lists its sites' paths differently,
37
+ # so its counts cannot be trusted to the paths a render
38
+ # lists.
39
+ def self.same_kinds(path, json, fingerprint)
40
+ kinds = json['kinds'] == Kinds.names
41
+ return if kinds && json.slice(*FINGERPRINT) == fingerprint
42
+
43
+ raise ArgumentError, "#{path} measured other kinds"
44
+ end
45
+
46
+ # The key lists of the JSON reports at paths, as listed_of
47
+ # gives them, each path's count the sum of every report's.
48
+ def self.merged(paths, key)
49
+ Merge.method_paths(paths.map { |path| listed_of(path, key) })
50
+ end
51
+
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.
57
+ def self.listed_of(path, key)
58
+ json = JSON.parse(File.read(path))
59
+ json.fetch(key, {}).transform_values do |taken|
60
+ taken.to_h { |each| [each['path'], each['count']] }
61
+ end
62
+ end
63
+
64
+ # The site paths of the JSON report at path, as
65
+ # Result#paths gives them; none for a report that has none.
66
+ def self.paths_of(path)
67
+ json = JSON.parse(File.read(path))
68
+ json.fetch('paths', {}).transform_values do |site|
69
+ site['taken'].map do |each|
70
+ { path: each['path'], count: each['count'] }
71
+ end
72
+ end
73
+ end
74
+
75
+ # The operands of the JSON report at path, which raises
76
+ # unless it measured the kinds a render does, with
77
+ # fingerprint.
78
+ def self.read(path, fingerprint)
79
+ json = JSON.parse(File.read(path))
80
+ same_kinds(path, json, fingerprint)
81
+ json['sites'].transform_values do |leaves|
82
+ leaves.map { |leaf| counts(leaf) }
83
+ end
84
+ end
85
+
86
+ # One operand's counts as JSON holds them, with the keys,
87
+ # and any only: value, as the symbols Result#operands uses.
88
+ def self.counts(leaf)
89
+ counts = { truthy: leaf['truthy'], falsy: leaf['falsy'] }
90
+ only = leaf['only']
91
+ only ? counts.merge(only: only.to_sym) : counts
92
+ end
93
+ end
94
+ end
@@ -1,21 +1,57 @@
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
5
6
  # other reporter can read back.
6
7
  module JsonWriter
7
- # Writes operands, as Result gives them, and paths, as
8
- # PathsReport gives them, to path as {"sites": {site:
9
- # [counts, ...]}, "paths": {site: {leaves:, taken:}}}.
10
- def self.write(operands, path, paths = {})
11
- File.write(path, json(operands, paths))
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,
14
+ # possible]}}.
15
+ # parts are json's.
16
+ def self.write(operands, path, paths = {}, **parts)
17
+ File.write(path, json(operands, paths, **parts))
12
18
  end
13
19
 
14
- # operands, and any paths, as the JSON report's text.
15
- def self.json(operands, paths = {})
16
- report = { 'sites' => operands }
20
+ # 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
24
+
25
+ # operands, and any paths, and parts' method_paths:,
26
+ # outside: and totals:, as the JSON report's text, with
27
+ # parts' fingerprint:, as Configuration#fingerprint gives
28
+ # it, and the names of the kinds measured.
29
+ def self.json(operands, paths = {}, **parts)
30
+ fingerprint = parts.fetch(:fingerprint, {})
31
+ report = { 'sites' => operands, **fingerprint }
17
32
  report['paths'] = paths unless paths.empty?
33
+ report.merge!(lists(parts))
34
+ totals = parts.fetch(:totals, {})
35
+ report['totals'] = totals unless totals.empty?
36
+ report['kinds'] = Kinds.names
18
37
  JSON.pretty_generate(report)
19
38
  end
39
+
40
+ # { key => its list } of each of parts' LISTED keys that
41
+ # has one, as listed gives them.
42
+ def self.lists(parts)
43
+ LISTED.filter_map do |key|
44
+ taken = parts.fetch(key, {})
45
+ [key.to_s, listed(taken)] unless taken.empty?
46
+ end.to_h
47
+ end
48
+
49
+ # taken, { site => { path => count } }, as JSON lists it,
50
+ # eg { 'k.rb:2:2' => [{ path: [true, false], count: 1 }] }.
51
+ def self.listed(taken)
52
+ taken.transform_values do |counts|
53
+ counts.map { |path, count| { path:, count: } }
54
+ end
55
+ end
20
56
  end
21
57
  end
@@ -1,5 +1,7 @@
1
1
  require 'fileutils'
2
+ require_relative 'calls'
2
3
  require_relative 'configuration'
4
+ require_relative 'console_summary'
3
5
  require_relative 'json_writer'
4
6
  require_relative 'manifest'
5
7
  require_relative 'path_gaps'
@@ -32,37 +34,6 @@ module PathCoverage
32
34
  @config
33
35
  end
34
36
 
35
- # The call rewritten code makes around each operand; returns
36
- # value unchanged.
37
- def self.record(site, index, value)
38
- @recorder.record(site, index, value)
39
- end
40
-
41
- # The call rewritten code makes before a site's first leaf,
42
- # site having size leaves; returns the evaluation's trail.
43
- def self.enter(site, size)
44
- @recorder.enter(site, size)
45
- end
46
-
47
- # The call rewritten code makes around a site as a whole,
48
- # with the trail enter gave it; returns value unchanged.
49
- def self.leave(trail, value)
50
- @recorder.leave(trail, value)
51
- end
52
-
53
- # The call rewritten code makes as a call reaches its
54
- # method's first decision; returns the call's new path.
55
- def self.path(site, size)
56
- @recorder.path(site, size)
57
- end
58
-
59
- # The call rewritten code makes around each decision's
60
- # predicate, with exits for one whose arm leaves the method;
61
- # returns value unchanged.
62
- def self.decide(path, index, value, exits = nil)
63
- @recorder.decide(path, index, value, exits)
64
- end
65
-
66
37
  # Instruments every file later required that the settings
67
38
  # block covers, and reports when the process exits. on_exit
68
39
  # is Kernel#at_exit except in tests, which must not print
@@ -77,18 +48,26 @@ module PathCoverage
77
48
  # Starts from settings in env alone, for a run whose code
78
49
  # cannot be changed, eg one started by RUBYOPT=-r: covers
79
50
  # the comma-separated PATH_COVERAGE_COVER globs under
80
- # PATH_COVERAGE_ROOT, writing the JSON report into
81
- # PATH_COVERAGE_DIR and POSTing it to
51
+ # PATH_COVERAGE_ROOT, skipping any matching the
52
+ # comma-separated PATH_COVERAGE_SKIP globs, writing the JSON
53
+ # report into PATH_COVERAGE_DIR and POSTing it to
82
54
  # PATH_COVERAGE_COLLECTOR. Without a collector it
83
55
  # starts nothing, so RUBYOPT can be set for every run.
84
56
  def self.start_from_env(env, on_exit: method(:at_exit))
85
57
  return unless env.key?('PATH_COVERAGE_COLLECTOR')
86
58
 
87
- start(on_exit:) do
59
+ start(on_exit:, &env_settings(env))
60
+ end
61
+
62
+ # The start block of the settings in env, as start_from_env
63
+ # reads them.
64
+ def self.env_settings(env)
65
+ covers = env.fetch('PATH_COVERAGE_COVER').split(',')
66
+ skips = env.fetch('PATH_COVERAGE_SKIP', '').split(',')
67
+ proc do
88
68
  root env.fetch('PATH_COVERAGE_ROOT')
89
- env.fetch('PATH_COVERAGE_COVER').split(',').each do |glob|
90
- cover glob
91
- end
69
+ covers.each { |glob| cover glob }
70
+ skips.each { |glob| skip glob }
92
71
  coverage_dir env.fetch('PATH_COVERAGE_DIR')
93
72
  collector env.fetch('PATH_COVERAGE_COLLECTOR')
94
73
  report :post
@@ -118,9 +97,9 @@ module PathCoverage
118
97
  end
119
98
 
120
99
  # The methods of the covered file at path whose paths are
121
- # counted, none unless config measures paths.
100
+ # counted, none unless config measures methods.
122
101
  def self.path_methods(path, source, config)
123
- return {} unless config.measures.include?(:paths)
102
+ return {} unless config.measures.include?(:methods)
124
103
 
125
104
  PathManifest.methods(source, config.relative(path))
126
105
  end
@@ -132,10 +111,9 @@ module PathCoverage
132
111
  def self.report(config)
133
112
  files = parsed_files(config)
134
113
  result = collect(files, config)
135
- paths = config.measures.include?(:paths) ? @recorder.paths : nil
136
- Render.run(
137
- result.operands, files, config, paths, site_paths: result.paths
138
- )
114
+ methods = config.measures.include?(:methods)
115
+ paths = methods ? @recorder.paths : nil
116
+ Render.run(result.operands, files, config, paths, **result.taken)
139
117
  return unless config.reporters.include?(:gaps)
140
118
 
141
119
  methods = path_methods_of(files, config)
@@ -158,11 +136,41 @@ module PathCoverage
158
136
  sites = merged_sites(files)
159
137
  result = Result.new(sites, @recorder.counts, @recorder.site_paths)
160
138
  FileUtils.mkdir_p(config.output_dir)
161
- report = PathsReport.of(result.paths, files)
162
- JsonWriter.write(result.operands, config.json_path, report)
139
+ write_json(result, files, config)
163
140
  result
164
141
  end
165
142
 
143
+ # 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.
148
+ def self.write_json(result, files, config)
149
+ 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)
155
+ end
156
+
157
+ # 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.
161
+ 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)
166
+ groups = config.groups
167
+ return totals if groups.empty?
168
+
169
+ summary = ConsoleSummary
170
+ own = summary.group_totals(result.paths, methods, paths, groups)
171
+ totals.merge('groups' => own)
172
+ end
173
+
166
174
  # { full path => {source:, sites:} } for every covered file:
167
175
  # as parsed when it loaded, or parsed now for a file the run
168
176
  # never loaded.
@@ -0,0 +1,71 @@
1
+ module PathCoverage
2
+ module Kinds
3
+ # The hooks every kind has, each as most kinds need it; a
4
+ # kind extends Base and defines its own in place of any.
5
+ module Base
6
+ # The ways a site of this kind as a whole can be evaluated:
7
+ # as it can be nested in another.
8
+ def root_ways(node, fixed)
9
+ ways(node, fixed)
10
+ end
11
+
12
+ # Whether its leaves, nested in a site, need calls of their
13
+ # own in place of record calls; false for most.
14
+ def own_calls?
15
+ false
16
+ end
17
+
18
+ # The inserts for a site of this kind: its leaves' records,
19
+ # and its leave around them.
20
+ def site_inserts(site, condition)
21
+ Rewriter.record_inserts(site, condition) +
22
+ Rewriter.trail_inserts(site, condition)
23
+ end
24
+
25
+ # What a site of this kind's leave call wraps: the whole of
26
+ # it, for most.
27
+ def trailed(condition)
28
+ condition
29
+ end
30
+
31
+ # Its node in a site's tree, each leaf taking the next index
32
+ # of leaves: its operands' nodes, then its arms', if any.
33
+ def node(source, condition, outcomes, leaves)
34
+ built = [outcomes, leaves]
35
+ operands = SiteTree.operands_of(source, condition, *built)
36
+ SiteTree.arms(source, condition, operands, *built)
37
+ end
38
+
39
+ # The nodes its node stands for among its siblings' in a
40
+ # site's tree on a file's page: itself, for most.
41
+ def unfold(node)
42
+ [node]
43
+ end
44
+
45
+ # The inserts, besides its leaves' calls, for one nested in
46
+ # a site at its at-th leaf: none for most.
47
+ def inserts(_site, _inner, _at, _commands)
48
+ []
49
+ end
50
+
51
+ # nodes, the tree nodes of condition's operands, with any
52
+ # texts that repeat others cut: none for most.
53
+ def texted(_source, _condition, nodes)
54
+ nodes
55
+ end
56
+
57
+ # The text a site of this kind opens and closes with, as
58
+ # [opening, closing], eg ['{', '}'] for a block's braces.
59
+ # Most kinds have none, so ['', ''].
60
+ def brackets(_source, _site)
61
+ ['', '']
62
+ end
63
+
64
+ # Its operands' rows in a site's tree on a file's page, nil
65
+ # for most, whose operands branch from its operator.
66
+ def rows(_condition, _indent)
67
+ nil
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,136 @@
1
+ require_relative 'base'
2
+
3
+ module PathCoverage
4
+ module Kinds
5
+ # A block method's call, eg ids.any? { |id| id.odd? }: its
6
+ # receiver's count of items, and its block's answers, its
7
+ # value following from them. Nothing else is evaluated, eg
8
+ # ids.size.
9
+ module Block
10
+ extend Base
11
+
12
+ OPERATORS = BLOCK_PATHS.keys.freeze
13
+
14
+ # A value that holds whatever the path, eg select's array.
15
+ ALWAYS = ->(*) { true }
16
+
17
+ # nil, nothing found, when its block was called with no
18
+ # items or its last answer was false; else the item found,
19
+ # which may itself be nil, so nil, as no path can tell.
20
+ FOUND = ->(none, answer) { none || !answer ? false : nil }
21
+
22
+ # A block method's value from whether its block was called
23
+ # with no items, and its block's last answer; nil when the
24
+ # path cannot tell.
25
+ VALUES = {
26
+ 'any?' => ->(none, answer) { !none && answer },
27
+ 'all?' => ->(none, answer) { none || answer },
28
+ 'none?' => ->(none, answer) { none || !answer },
29
+ 'detect' => FOUND,
30
+ 'find' => FOUND,
31
+ 'find_index' => ->(none, answer) { !none && answer },
32
+ 'take_while' => ALWAYS,
33
+ **EVERY_ITEM.to_h { |name| [name, ALWAYS] }
34
+ }.freeze
35
+
36
+ # Whether select's result has items: whether any answer
37
+ # was true.
38
+ KEPT = lambda do |items, answers|
39
+ items != ITEMS[0] && answers != ANSWERS[1]
40
+ end
41
+
42
+ # Whether a block method's result has items, from how many
43
+ # items its block was called with and its answers, eg
44
+ # select keeps an item when any answer was true; nil when
45
+ # its path cannot tell, eg take_while's last answer false
46
+ # after 2+ items, which may or may not be its first.
47
+ HAS_ITEMS = {
48
+ 'select' => KEPT, 'filter' => KEPT, 'filter_map' => KEPT,
49
+ 'reject' => lambda do |items, answers|
50
+ items != ITEMS[0] && answers != ANSWERS[0]
51
+ end,
52
+ 'partition' => ALWAYS,
53
+ 'take_while' => lambda do |items, answer|
54
+ next false if items == ITEMS[0]
55
+
56
+ answer || (items == ITEMS[1] ? false : nil)
57
+ end
58
+ }.freeze
59
+
60
+ # Whether site's result has items on the path that took
61
+ # outcomes; nil when its kind cannot tell, eg for a site
62
+ # that is no block method's call.
63
+ def self.items?(site, outcomes)
64
+ receiver, body = site.operands
65
+ rule = HAS_ITEMS[site.operator]
66
+ rule&.call(outcomes[receiver], outcomes[body])
67
+ end
68
+
69
+ # The ways node can be evaluated, fixed by its shape, its
70
+ # value following from each.
71
+ def self.ways(node, _fixed)
72
+ receiver, body = node.operands
73
+ BLOCK_PATHS[node.operator].map do |items, answer|
74
+ value = path_value(node.operator, items, answer)
75
+ [{ receiver => items, body => answer }, value]
76
+ end
77
+ end
78
+
79
+ # Its value on a path: its count of items and its block's
80
+ # answer, eg false for any? called with 0 items, and always
81
+ # truthy for select, which gives an array; nil for find
82
+ # with a last answer true, whose value no path can tell.
83
+ def self.path_value(operator, items, answer)
84
+ VALUES[operator].call(items == ITEMS[0], answer)
85
+ end
86
+
87
+ # Its value in each column, from its count of items and its
88
+ # block's answer there; nil where it was not reached.
89
+ def self.value(operator, operands)
90
+ receiver, body = operands
91
+ receiver[:cells].zip(body[:cells]).map do |items, answer|
92
+ items && path_value(operator, items, answer)
93
+ end
94
+ end
95
+
96
+ # Its block's opening and closing in source, ['{', '}'] or
97
+ # ['do', 'end']: the opening between its receiver and its
98
+ # block's expression, the closing after that expression.
99
+ def self.brackets(source, site)
100
+ receiver, body = site.operands
101
+ between = source.byteslice(receiver.end...body.begin)
102
+ closing = source.byteslice(body.end...site.extent.end)
103
+ [between[/\{|\bdo\b/], closing.strip]
104
+ end
105
+
106
+ # Its receiver needs no record call, and its block's
107
+ # expression an item_at or answer_at call.
108
+ def self.own_calls?
109
+ true
110
+ end
111
+
112
+ # The calls for its leaves: none for its receiver, whose
113
+ # leaf notes the items its block was called with, and an
114
+ # item_at call, noting its last answer, or for a method that
115
+ # calls it with every item an answer_at call, noting every
116
+ # answer, for its block's expression.
117
+ def self.calls(site, inner, leaves)
118
+ receiver, body = inner.operands
119
+ at = leaves.index(receiver)
120
+ every = EVERY_ITEM.include?(inner.operator)
121
+ noter = every ? 'answer_at' : 'item_at'
122
+ { receiver => nil,
123
+ body => "PathCoverage.#{noter}(#{site.inspect}, #{at}, " }
124
+ end
125
+
126
+ # Its call wrapped whole, its receiver the site's at-th leaf,
127
+ # so its items are begun before it runs and counted after.
128
+ def self.inserts(site, inner, at, _commands)
129
+ name = site.inspect
130
+ begun = "PathCoverage.begin_items(#{name}, #{at})"
131
+ call = "PathCoverage.end_items(#{name}, #{at}, #{begun}, "
132
+ Rewriter.wrap(call, inner.extent, false, :kind)
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,89 @@
1
+ require_relative 'base'
2
+ require_relative 'chain'
3
+
4
+ module PathCoverage
5
+ module Kinds
6
+ # A case, its operands its whens' and else's arms, each when
7
+ # trying its patterns in turn until one matches. Each pattern
8
+ # is wrapped in a stand-in, which matches the case's subject
9
+ # against it once, as the case would have.
10
+ module Case
11
+ extend Base
12
+
13
+ OPERATORS = %w[case].freeze
14
+
15
+ # The ways node can be evaluated: as the chain of its whens'
16
+ # conditions.
17
+ def self.ways(node, fixed)
18
+ whens = node.operands.filter_map(&:body)
19
+ Chain.ways(node.with(operands: whens), fixed)
20
+ end
21
+
22
+ # The inserts that wrap each pattern of a case site in a
23
+ # when_match call, its stand-in, which enters and leaves the
24
+ # site itself, as the case tries its patterns one by one;
25
+ # its arms may return or raise, so no leave wraps it.
26
+ def self.site_inserts(site, condition)
27
+ size = condition.leaves.size
28
+ condition.leaves.each_with_index.flat_map do |range, index|
29
+ call = "PathCoverage.when_match(#{site.inspect}, " \
30
+ "#{index}, #{size}, "
31
+ Rewriter.wrap(call, range, false, :leaf)
32
+ end
33
+ end
34
+
35
+ # Its node in a site's tree from its arms, built in order so
36
+ # its whens' patterns pair with path entries left to right:
37
+ # a when is taken on each path one of its patterns matched,
38
+ # and the else on each none did. A case's value is that of
39
+ # no choice, so is nil.
40
+ def self.node(source, condition, outcomes, leaves)
41
+ matched = []
42
+ operands = condition.operands.map do |arm|
43
+ arm_node(source, arm, matched, [outcomes, leaves])
44
+ end
45
+ cells = Array.new(outcomes.size)
46
+ { operator: 'case', cells:, operands: }
47
+ end
48
+
49
+ # One arm's node, its cells whether each column took it;
50
+ # matched holds the cells of each when before it, and a
51
+ # when's own are added.
52
+ def self.arm_node(source, arm, matched, (outcomes, leaves))
53
+ body = arm.body &&
54
+ SiteTree.node(source, arm.body, outcomes, leaves)
55
+ matched << body[:cells].map { |cell| cell == true } if body
56
+ cells = body ? matched.last : unmatched(matched, outcomes)
57
+ text = SiteTree.text_of(source, arm)
58
+ { arm: arm.kind, text:, cells:, body: }
59
+ end
60
+
61
+ # Whether each column matched none of its whens' patterns.
62
+ def self.unmatched(matched, outcomes)
63
+ outcomes.each_index.map do |column|
64
+ matched.none? { |taken| taken[column] }
65
+ end
66
+ end
67
+
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
+ # Its patterns need pattern calls.
74
+ def self.own_calls?
75
+ true
76
+ end
77
+
78
+ # { pattern range => the opening of its pattern call } for
79
+ # the patterns of inner, a case nested in a site whose leaves
80
+ # are leaves.
81
+ def self.calls(site, inner, leaves)
82
+ inner.leaves.to_h do |range|
83
+ index = leaves.index(range)
84
+ [range, "PathCoverage.pattern(#{site.inspect}, #{index}, "]
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,45 @@
1
+ require_relative 'base'
2
+
3
+ module PathCoverage
4
+ module Kinds
5
+ # A chain of one operator, eg a && b && c, or a when's
6
+ # patterns, each operand reached only while every one before
7
+ # it gave the outcome its operator goes on with.
8
+ module Chain
9
+ extend Base
10
+
11
+ OPERATORS = %w[&& and || or when].freeze
12
+
13
+ # The outcome of an operand on which each operator goes on
14
+ # to its next operand; any other ends the chain there. A
15
+ # when's patterns, and a case's whens, stop at a match.
16
+ CONTINUES_ON = {
17
+ '&&' => true, 'and' => true, '||' => false, 'or' => false,
18
+ 'when' => false, 'case' => false
19
+ }.freeze
20
+
21
+ # The ways node can be evaluated: each way of its first
22
+ # operand, going on to the next while each outcome is the
23
+ # one its operator goes on with.
24
+ def self.ways(node, fixed)
25
+ first, *rest = node.operands
26
+ goes_on = CONTINUES_ON.fetch(node.operator)
27
+ rest.reduce(SitePaths.walk(first, fixed)) do |ways, operand|
28
+ ways.flat_map do |outcomes, value|
29
+ next [[outcomes, value]] unless value == goes_on
30
+
31
+ SitePaths.walk(operand, fixed).map do |more, after|
32
+ [outcomes.merge(more), after]
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ # Its value in each column: the last operand it reached.
39
+ def self.value(_operator, operands)
40
+ operands.map { |operand| operand[:cells] }
41
+ .transpose.map { |column| column.compact.last }
42
+ end
43
+ end
44
+ end
45
+ end