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
@@ -1,16 +1,15 @@
1
1
  require 'fileutils'
2
2
  require_relative 'calls'
3
3
  require_relative 'configuration'
4
+ require_relative 'console_summary'
4
5
  require_relative 'json_writer'
5
6
  require_relative 'manifest'
6
- require_relative 'path_gaps'
7
- require_relative 'path_manifest'
8
7
  require_relative 'paths_report'
9
8
  require_relative 'recorder'
10
9
  require_relative 'render'
11
10
  require_relative 'result'
12
11
  require_relative 'rewriter'
13
- require_relative 'totals'
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) }
@@ -48,18 +51,26 @@ module PathCoverage
48
51
  # Starts from settings in env alone, for a run whose code
49
52
  # cannot be changed, eg one started by RUBYOPT=-r: covers
50
53
  # the comma-separated PATH_COVERAGE_COVER globs under
51
- # PATH_COVERAGE_ROOT, writing the JSON report into
52
- # PATH_COVERAGE_DIR and POSTing it to
54
+ # PATH_COVERAGE_ROOT, skipping any matching the
55
+ # comma-separated PATH_COVERAGE_SKIP globs, writing the JSON
56
+ # report into PATH_COVERAGE_DIR and POSTing it to
53
57
  # PATH_COVERAGE_COLLECTOR. Without a collector it
54
58
  # starts nothing, so RUBYOPT can be set for every run.
55
59
  def self.start_from_env(env, on_exit: method(:at_exit))
56
60
  return unless env.key?('PATH_COVERAGE_COLLECTOR')
57
61
 
58
- start(on_exit:) do
62
+ start(on_exit:, &env_settings(env))
63
+ end
64
+
65
+ # The start block of the settings in env, as start_from_env
66
+ # reads them.
67
+ def self.env_settings(env)
68
+ covers = env.fetch('PATH_COVERAGE_COVER').split(',')
69
+ skips = env.fetch('PATH_COVERAGE_SKIP', '').split(',')
70
+ proc do
59
71
  root env.fetch('PATH_COVERAGE_ROOT')
60
- env.fetch('PATH_COVERAGE_COVER').split(',').each do |glob|
61
- cover glob
62
- end
72
+ covers.each { |glob| cover glob }
73
+ skips.each { |glob| skip glob }
63
74
  coverage_dir env.fetch('PATH_COVERAGE_DIR')
64
75
  collector env.fetch('PATH_COVERAGE_COLLECTOR')
65
76
  report :post
@@ -82,45 +93,20 @@ module PathCoverage
82
93
  return unless config.covers?(path)
83
94
 
84
95
  parsed = @parsed[path] = Manifest.parse(path, config)
85
- methods = path_methods(path, parsed[:source], config)
86
- sites = parsed[:sites]
87
- 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)
88
99
  RubyVM::InstructionSequence.compile(rewritten, path, path)
89
100
  end
90
101
 
91
- # The methods of the covered file at path whose paths are
92
- # counted, none unless config measures methods.
93
- def self.path_methods(path, source, config)
94
- return {} unless config.measures.include?(:methods)
95
-
96
- PathManifest.methods(source, config.relative(path))
97
- end
98
-
99
102
  # Writes the JSON report, then runs the chosen reporters, for
100
103
  # every covered file whether or not the run loaded it, into
101
- # coverage_dir, created first if need be. The paths never
102
- # taken follow the condition gaps.
104
+ # coverage_dir, created first if need be.
103
105
  def self.report(config)
104
106
  files = parsed_files(config)
105
107
  result = collect(files, config)
106
- methods = config.measures.include?(:methods)
107
- paths = methods ? @recorder.paths : nil
108
- Render.run(
109
- result.operands, files, config, paths, site_paths: result.paths
110
- )
111
- return unless config.reporters.include?(:gaps)
112
-
113
- methods = path_methods_of(files, config)
114
- PathGaps.lines(methods, @recorder.paths).each { |line| puts line }
115
- end
116
-
117
- # The methods whose paths are counted of every covered file,
118
- # in one manifest.
119
- def self.path_methods_of(files, config)
120
- all = files.map do |path, parsed|
121
- path_methods(path, parsed[:source], config)
122
- end
123
- all.reduce({}, :merge)
108
+ Render.run(result.operands, files, config,
109
+ **result.taken, reached: @recorder.reached)
124
110
  end
125
111
 
126
112
  # The Result of every covered file, with the counts and site
@@ -130,23 +116,39 @@ module PathCoverage
130
116
  sites = merged_sites(files)
131
117
  result = Result.new(sites, @recorder.counts, @recorder.site_paths)
132
118
  FileUtils.mkdir_p(config.output_dir)
119
+ write_json(result, files, config)
120
+ result
121
+ end
122
+
123
+ # Writes result as the JSON report at config's json_path: its
124
+ # operands, site paths, paths a treatment rules out, totals,
125
+ # each def's card, the statements the recorder reached, so a
126
+ # render from the JSON alone has them, and config's
127
+ # fingerprint.
128
+ def self.write_json(result, files, config)
133
129
  report = PathsReport.of(result.paths, files)
134
- totals = totals(result, files, config)
130
+ reached = @recorder.reached
131
+ cards = WithinMethods.listed(files, config, result, reached)
132
+ parts = {
133
+ totals: totals(result, files, config), outside: result.outside,
134
+ cards:, fingerprint: config.fingerprint, reached:
135
+ }
135
136
  path = config.json_path
136
- JsonWriter.write(result.operands, path, report, totals:)
137
- result
137
+ JsonWriter.write(result.operands, path, report, **parts)
138
138
  end
139
139
 
140
140
  # The JSON report's totals, each [taken, possible] paths:
141
- # within each decision, and, when config measures methods,
142
- # across each method's decisions too.
141
+ # within each decision, and within each method; with groups
142
+ # configured, each group's own too, under groups.
143
143
  def self.totals(result, files, config)
144
- totals = { 'decisions' => Totals.decisions(result.paths) }
145
- return totals unless config.measures.include?(:methods)
146
-
147
- methods = path_methods_of(files, config)
148
- taken = Totals.methods(methods, @recorder.paths)
149
- totals.merge('methods' => taken)
144
+ reached = @recorder.reached
145
+ cards = WithinMethods.cards(files, config, result.paths, reached)
146
+ totals = ConsoleSummary.totals(result.paths, cards)
147
+ groups = config.groups
148
+ return totals if groups.empty?
149
+
150
+ own = ConsoleSummary.group_totals(result.paths, cards, groups)
151
+ totals.merge('groups' => own)
150
152
  end
151
153
 
152
154
  # { full path => {source:, sites:} } for every covered file:
@@ -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,137 @@
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
+ next answer if answer
56
+
57
+ items == ITEMS[1] ? false : nil
58
+ end
59
+ }.freeze
60
+
61
+ # Whether site's result has items on the path that took
62
+ # outcomes; nil when its kind cannot tell, eg for a site
63
+ # that is no block method's call.
64
+ def self.items?(site, outcomes)
65
+ receiver, body = site.operands
66
+ rule = HAS_ITEMS[site.operator]
67
+ rule&.call(outcomes[receiver], outcomes[body])
68
+ end
69
+
70
+ # The ways node can be evaluated, fixed by its shape, its
71
+ # value following from each.
72
+ def self.ways(node, _fixed)
73
+ receiver, body = node.operands
74
+ BLOCK_PATHS[node.operator].map do |items, answer|
75
+ value = path_value(node.operator, items, answer)
76
+ [{ receiver => items, body => answer }, value]
77
+ end
78
+ end
79
+
80
+ # Its value on a path: its count of items and its block's
81
+ # answer, eg false for any? called with 0 items, and always
82
+ # truthy for select, which gives an array; nil for find
83
+ # with a last answer true, whose value no path can tell.
84
+ def self.path_value(operator, items, answer)
85
+ VALUES[operator].call(items == ITEMS[0], answer)
86
+ end
87
+
88
+ # Its value in each column, from its count of items and its
89
+ # block's answer there; nil where it was not reached.
90
+ def self.value(operator, operands)
91
+ receiver, body = operands
92
+ receiver[:cells].zip(body[:cells]).map do |items, answer|
93
+ items && path_value(operator, items, answer)
94
+ end
95
+ end
96
+
97
+ # Its block's opening and closing in source, ['{', '}'] or
98
+ # ['do', 'end']: the opening between its receiver and its
99
+ # block's expression, the closing after that expression.
100
+ def self.brackets(source, site)
101
+ receiver, body = site.operands
102
+ between = source.byteslice(receiver.end...body.begin)
103
+ closing = source.byteslice(body.end...site.extent.end)
104
+ [between[/\{|\bdo\b/], closing.strip]
105
+ end
106
+
107
+ # Its receiver needs no record call, and its block's
108
+ # expression an item_at or answer_at call.
109
+ def self.own_calls?
110
+ true
111
+ end
112
+
113
+ # The calls for its leaves: none for its receiver, whose
114
+ # leaf notes the items its block was called with, and an
115
+ # item_at call, noting its last answer, or for a method that
116
+ # calls it with every item an answer_at call, noting every
117
+ # answer, for its block's expression.
118
+ def self.calls(site, inner, leaves)
119
+ receiver, body = inner.operands
120
+ at = leaves.index(receiver)
121
+ every = EVERY_ITEM.include?(inner.operator)
122
+ noter = every ? 'answer_at' : 'item_at'
123
+ { receiver => nil,
124
+ body => "PathCoverage.#{noter}(#{site.inspect}, #{at}, " }
125
+ end
126
+
127
+ # Its call wrapped whole, its receiver the site's at-th leaf,
128
+ # so its items are begun before it runs and counted after.
129
+ def self.inserts(site, inner, at, _commands)
130
+ name = site.inspect
131
+ begun = "PathCoverage.begin_items(#{name}, #{at})"
132
+ call = "PathCoverage.end_items(#{name}, #{at}, #{begun}, "
133
+ Rewriter.wrap(call, inner.extent, false, :kind)
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,92 @@
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
+ condition = arm.body
54
+ body = if condition
55
+ SiteTree.node(source, condition, outcomes, leaves)
56
+ end
57
+ matched << body[:cells].map { |cell| cell == true } if body
58
+ cells = body ? matched.last : unmatched(matched, outcomes)
59
+ text = SiteTree.text_of(source, arm)
60
+ { arm: arm.kind, text:, cells:, body: }
61
+ end
62
+
63
+ # Whether each column matched none of its whens' patterns:
64
+ # a when's cell is true where it matched.
65
+ def self.unmatched(matched, outcomes)
66
+ outcomes.each_index.map do |column|
67
+ !matched.map { |taken| taken[column] }.include?(true)
68
+ end
69
+ end
70
+
71
+ # The name of one of its arms: when or else.
72
+ def self.arm_label(arm, _choice)
73
+ arm[:arm].to_s
74
+ end
75
+
76
+ # Its patterns need pattern calls.
77
+ def self.own_calls?
78
+ true
79
+ end
80
+
81
+ # { pattern range => the opening of its pattern call } for
82
+ # the patterns of inner, a case nested in a site whose leaves
83
+ # are leaves.
84
+ def self.calls(site, inner, leaves)
85
+ inner.leaves.to_h do |range|
86
+ index = leaves.index(range)
87
+ [range, "PathCoverage.pattern(#{site.inspect}, #{index}, "]
88
+ end
89
+ end
90
+ end
91
+ end
92
+ 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
@@ -0,0 +1,68 @@
1
+ require_relative 'base'
2
+
3
+ module PathCoverage
4
+ module Kinds
5
+ # The ways and value of a condition whose predicate picks an
6
+ # arm, which Choice and Write share.
7
+ module Arms
8
+ # The ways node can be evaluated: each way of its predicate,
9
+ # then each way of the condition the arm it picks is, if
10
+ # that arm is one; one with no else arm picks none. Its
11
+ # value is that of no choice, so is nil.
12
+ def ways(node, fixed)
13
+ predicate, then_arm, else_arm = node.operands
14
+ then_on = THEN_ON[node.operator]
15
+ SitePaths.walk(predicate, fixed).flat_map do |outcomes, value|
16
+ body = (value == then_on ? then_arm : else_arm)&.body
17
+ next [[outcomes, nil]] if body.nil?
18
+
19
+ SitePaths.walk(body, fixed).map do |more, _|
20
+ [outcomes.merge(more), nil]
21
+ end
22
+ end
23
+ end
24
+
25
+ # Its value in each column: its taken arm's body's, nil for
26
+ # a plain arm.
27
+ def value(_operator, operands)
28
+ _, *arms = operands
29
+ bodies = arms.map { |arm| arm[:body]&.fetch(:cells) }
30
+ arms.first[:cells].each_index.map do |column|
31
+ taken = arms.index { |arm| arm[:cells][column] }
32
+ taken && bodies[taken]&.fetch(column)
33
+ end
34
+ end
35
+ end
36
+
37
+ # A condition whose predicate picks an arm: a ternary, an if
38
+ # or an unless.
39
+ module Choice
40
+ extend Base
41
+ extend Arms
42
+
43
+ OPERATORS = CHOICES
44
+
45
+ # What its site's leave call wraps: an if's or an unless's
46
+ # predicate, a leaf's byte range or a nested Condition, as
47
+ # its path is decided there and its arms are statements that
48
+ # may return or raise; a ternary as a whole.
49
+ def self.trailed(condition)
50
+ return condition if condition.operator == '?:'
51
+
52
+ condition.operands.first
53
+ end
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
+
62
+ mark = arm[:arm] == :then ? '?' : ':'
63
+ body = arm[:body]
64
+ "#{mark} #{body ? body[:operator] : arm[:text]}"
65
+ end
66
+ end
67
+ end
68
+ end