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,30 +1,64 @@
1
- require 'json'
1
+ require_relative 'console_summary'
2
2
  require_relative 'gaps'
3
3
  require_relative 'html_report'
4
+ require_relative 'json_reader'
4
5
  require_relative 'manifest'
5
- require_relative 'merge'
6
6
  require_relative 'paths_report'
7
7
  require_relative 'post'
8
8
  require_relative 'site_paths'
9
+ require_relative 'within_methods'
9
10
 
10
11
  module PathCoverage
11
12
  # The reporters, run from operands alone: needing nothing the
12
13
  # run recorded beyond them, they can run in another process.
13
14
  module Render
14
- # Runs each reporter chosen in config. operands and
15
- # site_paths are as Result gives them; files is { full path
16
- # => {source:, sites:} } for each covered file.
17
- def self.run(operands, files, config, paths = nil, site_paths: {})
18
- site_paths = every_site(site_paths, files)
15
+ # Runs each reporter chosen in config. operands, and taken's
16
+ # site_paths: and outside:, are as Result gives them, and its
17
+ # reached: as Recorder#reached gives it; files is
18
+ # { full path => {source:, sites:} } for each covered file.
19
+ def self.run(operands, files, config, **taken)
20
+ site_paths = every_site(taken.fetch(:site_paths, {}), files)
19
21
  reporters = config.reporters
20
- gaps(operands, files, site_paths) if reporters.include?(:gaps)
21
- if reporters.include?(:html)
22
- HtmlReport.write(operands, files, config, paths, site_paths:)
22
+ if reporters.include?(:gaps)
23
+ gaps(operands, files, site_paths, taken.fetch(:outside, {}))
23
24
  end
25
+ html_and_summary(operands, files, config,
26
+ [site_paths, taken[:reached]])
24
27
  return unless reporters.include?(:post)
25
28
 
29
+ post(operands, files, config, site_paths)
30
+ end
31
+
32
+ # Runs the :post reporter: operands, and the paths report of
33
+ # site_paths in files, with config's fingerprint.
34
+ def self.post(operands, files, config, site_paths)
26
35
  report = PathsReport.of(site_paths, files)
27
- Post.deliver(operands, config.collector_url, report)
36
+ url = config.collector_url
37
+ fingerprint = config.fingerprint
38
+ Post.deliver(operands, url, report, fingerprint:)
39
+ end
40
+
41
+ # Runs the :html and :summary reporters, each when chosen.
42
+ def self.html_and_summary(operands, files, config, recorded)
43
+ site_paths, reached = recorded
44
+ reporters = config.reporters
45
+ if reporters.include?(:html)
46
+ HtmlReport.write(operands, files, config, recorded)
47
+ end
48
+ return unless reporters.include?(:summary)
49
+
50
+ cards = WithinMethods.cards(files, config, site_paths, reached)
51
+ summary(config, site_paths, cards)
52
+ end
53
+
54
+ # Prints the summary lines: the paths taken of all possible
55
+ # within decisions, and, when cards are given, as they are
56
+ # when methods are measured, within methods; then any
57
+ # operators ignored.
58
+ def self.summary(config, site_paths, cards)
59
+ groups = config.groups
60
+ puts ConsoleSummary.lines(site_paths, cards, groups) +
61
+ ConsoleSummary.ignored_lines(config.ignored)
28
62
  end
29
63
 
30
64
  # site_paths with every site in files that it lacks, eg
@@ -42,51 +76,26 @@ module PathCoverage
42
76
  none.merge(site_paths)
43
77
  end
44
78
 
45
- # Prints the gap lines: each path a site never took, each
46
- # leaf named by its text in files.
47
- def self.gaps(operands, files, site_paths)
79
+ # Prints the gap lines: each path a site never took, and each
80
+ # path taken that a treatment rules out, as outside lists
81
+ # them, each leaf named by its text in files.
82
+ def self.gaps(operands, files, site_paths, outside)
48
83
  texts = PathsReport.texts(files)
49
- lines = Gaps.lines(operands, paths: site_paths, texts:)
84
+ paths = site_paths
85
+ lines = Gaps.lines(operands, paths:, texts:, outside:)
50
86
  lines.each { |line| puts line }
51
87
  end
52
88
 
53
89
  # Runs the reporters chosen in config from the JSON reports
54
- # at paths, their counts merged, parsing each covered file
55
- # afresh. paths is config.json_path alone by default.
90
+ # at paths, their counts and reaches merged, parsing each
91
+ # covered file afresh. paths is config.json_path alone by
92
+ # default.
56
93
  def self.from_json(config, paths = [config.json_path])
57
- operands = Merge.operands(paths.map { |path| read(path) })
58
- site_paths = Merge.paths(paths.map { |path| paths_of(path) })
94
+ operands = JsonReader.operands(paths, config.fingerprint)
59
95
  files = config.files.to_h do |path|
60
96
  [path, Manifest.parse(path, config)]
61
97
  end
62
- run(operands, files, config, site_paths:)
63
- end
64
-
65
- # The site paths of the JSON report at path, as
66
- # Result#paths gives them; none for a report that has none.
67
- def self.paths_of(path)
68
- json = JSON.parse(File.read(path))
69
- json.fetch('paths', {}).transform_values do |site|
70
- site['taken'].map do |each|
71
- { path: each['path'], count: each['count'] }
72
- end
73
- end
74
- end
75
-
76
- # The operands of the JSON report at path.
77
- def self.read(path)
78
- json = JSON.parse(File.read(path))
79
- json['sites'].transform_values do |leaves|
80
- leaves.map { |leaf| counts(leaf) }
81
- end
82
- end
83
-
84
- # One operand's counts as JSON holds them, with the keys,
85
- # and any only: value, as the symbols Result#operands uses.
86
- def self.counts(leaf)
87
- counts = { truthy: leaf['truthy'], falsy: leaf['falsy'] }
88
- only = leaf['only']
89
- only ? counts.merge(only: only.to_sym) : counts
98
+ run(operands, files, config, **JsonReader.taken(paths))
90
99
  end
91
100
  end
92
101
  end
@@ -30,6 +30,25 @@ module PathCoverage
30
30
  end
31
31
  end
32
32
 
33
+ # { site => { path => evaluations } } of each path taken
34
+ # that is none of its site's possible paths, eg a &. receiver
35
+ # nil though treated as present; only sites with one. Such a
36
+ # path shows a treatment to be wrong, so it is reported, never
37
+ # dropped.
38
+ def outside
39
+ all = @sites.to_h do |site, condition|
40
+ taken = @site_paths.fetch(site, {})
41
+ [site, taken.except(*SitePaths.possible(condition))]
42
+ end
43
+ all.reject { |_, paths| paths.empty? }
44
+ end
45
+
46
+ # The paths taken, as Render.run takes them: site_paths:,
47
+ # as paths gives them, and outside:.
48
+ def taken
49
+ { site_paths: paths, outside: }
50
+ end
51
+
33
52
  # { site => [counts of operand 0, counts of operand 1, ...] },
34
53
  # with zero counts for an operand that never ran, and
35
54
  # only: naming the one outcome a fixed leaf can have.
@@ -1,75 +1,63 @@
1
1
  require_relative 'condition'
2
+ require_relative 'kinds'
3
+ require_relative 'rewriter_nested'
2
4
 
3
5
  module PathCoverage
4
6
  # Instruments source text so that each leaf operand records its
5
7
  # outcome when the code runs. It inserts text at the byte
6
8
  # ranges the manifest found, and never parses.
7
9
  module Rewriter
8
- PATH_NAME = '__path_coverage_path'.freeze
9
-
10
10
  # Returns source with each operand in sites wrapped in a
11
11
  # record call, each site as a whole in a leave call that
12
- # enters it first, and each method in methods starting a
13
- # path that its decisions are wrapped in decide calls to add
14
- # to. Only text is inserted, never a newline, so backtrace
15
- # line numbers still match the file on disk.
16
- def self.rewrite(source, sites, methods = {})
12
+ # enters it first, and each statement in reaches, { name =>
13
+ # offset }, after a reach call that counts its runs. Only
14
+ # text is inserted, never a newline, so backtrace line
15
+ # numbers still match the file on disk.
16
+ def self.rewrite(source, sites, reaches = {})
17
17
  inserts = sites.flat_map do |site, condition|
18
18
  site_inserts(site, condition)
19
19
  end
20
- apply(source.dup, inserts + path_inserts(methods))
21
- end
22
-
23
- # The inserts for one site, by its kind: a case's patterns'
24
- # stand-ins, a block method's call and block, or any other
25
- # site's leaves' records and its leave.
26
- def self.site_inserts(site, condition)
27
- kind = condition.operator
28
- return case_inserts(site, condition) if kind == 'case'
29
-
30
- blocks = BLOCK_PATHS.key?(kind)
31
- return block_inserts(site, condition) if blocks
32
-
33
- record_inserts(site, condition) + trail_inserts(site, condition)
20
+ apply(source.dup, inserts + reach_inserts(reaches))
34
21
  end
35
22
 
36
- # The inserts that wrap a block method's call whole in a
37
- # leave_items call that enters it first, inside any leave of
38
- # a site it is the predicate of, and its block's expression,
39
- # outside any site's leave there, in an item call, noting its
40
- # last answer, or for a method that calls it with every item
41
- # an answer call, noting every answer.
42
- def self.block_inserts(site, condition)
43
- _receiver, body = condition.leaves
44
- enter = "PathCoverage.enter_items(#{site.inspect})"
45
- call = "PathCoverage.leave_items(#{enter}, "
46
- every = EVERY_ITEM.include?(condition.operator)
47
- noter = every ? 'answer' : 'item'
48
- item = "PathCoverage.#{noter}(#{site.inspect}, "
49
- command = condition.commands.include?(body)
50
- wrap(call, condition.extent, false, 0.5) +
51
- wrap(item, body, command, 1.5)
23
+ # Example: { "k.rb:2:2" => 18 } puts a reach call at 18.
24
+ # The inserts that put a reach call, a statement of its
25
+ # own, before each statement in reaches, outside all else
26
+ # there. A statement reached by
27
+ # its byte range, eg an endless def's body, is wrapped in
28
+ # parentheses with its call, being one expression.
29
+ def self.reach_inserts(reaches)
30
+ rank = [Float::INFINITY, LAYERS.index(:reach)]
31
+ reaches.flat_map do |name, at|
32
+ call = "PathCoverage.reach(#{name.inspect}); "
33
+ next [[at, call, rank]] unless at.is_a?(Range)
34
+
35
+ [[at.begin, "(#{call}", rank], [at.end, ')', rank, :close]]
36
+ end
52
37
  end
53
38
 
54
- # The inserts that wrap each pattern of a case site in a
55
- # when_match call, its stand-in, which enters and leaves the
56
- # site itself, as the case tries its patterns one by one.
57
- def self.case_inserts(site, condition)
58
- size = condition.leaves.size
59
- condition.leaves.each_with_index.flat_map do |range, index|
60
- call = "PathCoverage.when_match(#{site.inspect}, " \
61
- "#{index}, #{size}, "
62
- wrap(call, range, false, 0)
63
- end
39
+ # The inserts for one site, as its kind gives them, eg its
40
+ # leaves' records and its leave; a block method's call or a
41
+ # write is noted as if nested at leaf 0.
42
+ def self.site_inserts(site, condition)
43
+ Kinds.of(condition.operator).site_inserts(site, condition)
64
44
  end
65
45
 
66
46
  # The inserts that wrap each of site's leaves in a record
67
- # call, innermost of all.
47
+ # call, innermost of all; a leaf of a nested site that needs
48
+ # a call of its own, eg a &. chain's, in the call
49
+ # RewriterNested gives it, or in none, and that site's own
50
+ # inserts too.
68
51
  def self.record_inserts(site, condition)
69
- condition.leaves.each_with_index.flat_map do |range, index|
70
- command = condition.commands.include?(range)
71
- wrap(record_call(site, index), range, command, 0)
52
+ nested = RewriterNested.calls(site, condition)
53
+ leaves = condition.leaves
54
+ records = leaves.each_with_index.flat_map do |range, i|
55
+ call = nested.fetch(range) { record_call(site, i) }
56
+ next [] unless call
57
+
58
+ wrap(call, range, condition.commands.include?(range), :leaf)
72
59
  end
60
+ records + RewriterNested.inserts(site, condition)
73
61
  end
74
62
 
75
63
  WORD_OPERATORS = %w[and or].freeze
@@ -83,22 +71,10 @@ module PathCoverage
83
71
  enter = "PathCoverage.enter(#{site.inspect}, " \
84
72
  "#{condition.leaves.size})"
85
73
  call = "PathCoverage.leave(#{enter}, "
86
- wrapped = trailed(condition)
74
+ wrapped = Kinds.of(condition.operator).trailed(condition)
87
75
  nested = wrapped.is_a?(Condition)
88
76
  word = nested && WORD_OPERATORS.include?(wrapped.operator)
89
- wrap(call, nested ? wrapped.extent : wrapped, word, 1)
90
- end
91
-
92
- # What a site's leave call wraps: an if's or an unless's
93
- # predicate, a leaf's byte range or a nested Condition, as
94
- # its path is decided there and its arms are statements that
95
- # may return or raise; any other site as a whole.
96
- def self.trailed(condition)
97
- unless STATEMENT_CHOICES.include?(condition.operator)
98
- return condition
99
- end
100
-
101
- condition.operands.first
77
+ wrap(call, nested ? wrapped.extent : wrapped, word, :site)
102
78
  end
103
79
 
104
80
  # The opening of the record call around operand index of
@@ -107,55 +83,41 @@ module PathCoverage
107
83
  "PathCoverage.record(#{site.inspect}, #{index}, "
108
84
  end
109
85
 
110
- # The inserts that start each method's path at its entry,
111
- # outermost of all there, and wrap each of its decisions'
112
- # predicates in a decide call, outside any record or
113
- # outcome call.
114
- def self.path_inserts(methods)
115
- methods.flat_map do |site, method|
116
- decisions = method.decisions
117
- start = path_start(site, decisions.size)
118
- decides = decisions.each_with_index.flat_map do |decision, i|
119
- call = "PathCoverage.decide(#{PATH_NAME}, #{i}, "
120
- tail = decision.exits.nil? ? '' : ", #{decision.exits}"
121
- wrap(call, decision.range, decision.command, 2, tail)
122
- end
123
- [[method.entry, start, 3]] + decides
124
- end
125
- end
126
-
127
- # The statement that starts a call's path through the
128
- # method at site, which has size decisions.
129
- def self.path_start(site, size)
130
- call = "PathCoverage.path(#{site.inspect}, #{size})"
131
- "#{PATH_NAME} = #{call}; "
132
- end
133
-
134
- # The two inserts that wrap the code at range in call, of
135
- # rank among inserts at the same offset, with tail as the
136
- # call's further arguments. A command call is parenthesised,
137
- # as it is no bare argument; nothing else is, since
138
- # parentheses make a statement, which gives its line a
139
- # line-coverage count of its own.
140
- def self.wrap(call, range, command, rank, tail = '')
141
- open, close = command ? ['(', ")#{tail})"] : ['', "#{tail})"]
86
+ # The layers of inserts, innermost first, that break a tie
87
+ # between inserts wrapping the same range: a leaf's call, a
88
+ # nested kind's call around its whole, eg end_items, a site's
89
+ # leave, and a statement's reach.
90
+ LAYERS = %i[leaf kind site reach].freeze
91
+
92
+ # The two inserts that wrap the code at range in call. Their
93
+ # order, [size, rank], nests them among inserts at the same
94
+ # offset: the longer range outside, the rank of layer in
95
+ # LAYERS breaking a tie. A command call is parenthesised, as
96
+ # it is no bare argument; nothing else is, since parentheses
97
+ # make a statement, which gives its line a line-coverage
98
+ # count of its own.
99
+ def self.wrap(call, range, command, layer)
100
+ open, close = command ? ['(', '))'] : ['', ')']
101
+ order = [range.size, LAYERS.index(layer)]
142
102
  [
143
- [range.begin, call + open, rank],
144
- [range.end, close, rank, :close]
103
+ [range.begin, call + open, order],
104
+ [range.end, close, order, :close]
145
105
  ]
146
106
  end
147
107
 
148
- # Splices each [offset, text, rank] insert into source, last
149
- # offset first so earlier offsets stay valid; a closing
108
+ # Splices each [offset, text, order] insert into source,
109
+ # last offset first so earlier offsets stay valid; a closing
150
110
  # insert is marked :close. Text spliced in later at an
151
111
  # offset lands before text already there, so at one offset
152
- # opening inserts go in lowest rank first, leaving the
153
- # highest outermost, then closing ones highest rank first,
154
- # so the innermost call closes first and each tail stays
155
- # with its own call.
112
+ # opening inserts go in lowest order first, leaving the
113
+ # highest outermost, then closing ones highest order first,
114
+ # so the innermost call closes first. Eg a ternary whose
115
+ # predicate is a leaf: the site's leave, wrapping the whole
116
+ # ternary, opens outside the record wrapping its predicate.
156
117
  def self.apply(source, inserts)
157
- sorted = inserts.sort_by do |offset, _, rank, close|
158
- close ? [-offset, 1, -rank] : [-offset, 0, rank]
118
+ sorted = inserts.sort_by do |offset, _, order, close|
119
+ size, rank = order
120
+ close ? [-offset, 1, -size, -rank] : [-offset, 0, size, rank]
159
121
  end
160
122
  sorted.each do |offset, text, _|
161
123
  source.bytesplice(offset, 0, text)
@@ -0,0 +1,62 @@
1
+ require_relative 'condition'
2
+ require_relative 'kinds'
3
+
4
+ module PathCoverage
5
+ # The part of the rewriter that instruments the sites nested in
6
+ # another, eg the &. chain in ok && user&.admin?. The site
7
+ # around them enters and leaves; each nested site's leaves are
8
+ # noted as that site's, at their place among its leaves, by the
9
+ # calls its kind gives in place of plain records.
10
+ module RewriterNested
11
+ # { leaf range => the opening of its call, nil for none } for
12
+ # the leaves of every site nested in condition whose kind has
13
+ # calls of its own, condition itself included.
14
+ def self.calls(site, condition)
15
+ leaves = condition.leaves
16
+ rooted(condition).reduce({}) do |calls, inner|
17
+ kind = Kinds.of(inner.operator)
18
+ calls.merge(kind.calls(site, inner, leaves))
19
+ end
20
+ end
21
+
22
+ # The inserts, besides its leaves' calls, for every site
23
+ # nested in condition whose kind has calls of its own,
24
+ # condition itself included, as its kind gives them.
25
+ def self.inserts(site, condition)
26
+ leaves = condition.leaves
27
+ rooted(condition).flat_map do |inner|
28
+ at = leaves.index(inner.operands.first)
29
+ kind = Kinds.of(inner.operator)
30
+ kind.inserts(site, inner, at, condition.commands)
31
+ end
32
+ end
33
+
34
+ # condition alone when its kind has calls of its own, eg a
35
+ # block method's call that is a site of its own, its leaves
36
+ # then noted at their places from 0; else the sites nested in
37
+ # it.
38
+ def self.rooted(condition)
39
+ return [condition] if own_calls?(condition)
40
+
41
+ nested(condition)
42
+ end
43
+
44
+ # The sites nested in condition whose kind has calls of its
45
+ # own, in its operands and in its arms' conditions.
46
+ def self.nested(condition)
47
+ condition.operands.flat_map do |operand|
48
+ case operand
49
+ when Condition
50
+ own_calls?(operand) ? [operand] : nested(operand)
51
+ when Arm then operand.body ? nested(operand.body) : []
52
+ else []
53
+ end
54
+ end
55
+ end
56
+
57
+ # Whether condition's kind gives its leaves calls of their own.
58
+ def self.own_calls?(condition)
59
+ Kinds.of(condition.operator).own_calls?
60
+ end
61
+ end
62
+ end
@@ -1,78 +1,29 @@
1
1
  require_relative 'condition'
2
+ require_relative 'kinds'
2
3
 
3
4
  module PathCoverage
4
5
  # The paths an evaluation of a site can take, worked out
5
6
  # from its condition alone: the outcome of each leaf, in leaf
6
7
  # order, nil for a leaf that evaluation never reaches.
7
8
  module SitePaths
8
- # Every possible path through site's condition, true
9
- # before false; a stop-early block method's are fixed by its
10
- # shape alone.
9
+ # Every possible path through site's condition, true before
10
+ # false, as its kind gives the ways a site of it can be
11
+ # evaluated.
11
12
  def self.possible(site)
12
- shape = BLOCK_PATHS[site.operator]
13
- return shape if shape
14
-
15
- walk(site, site.fixed).map do |outcomes, _value|
13
+ ways = Kinds.of(site.operator).root_ways(site, site.fixed)
14
+ ways.map do |outcomes, _value|
16
15
  site.leaves.map { |range| outcomes[range] }
17
16
  end
18
17
  end
19
18
 
20
- # The outcome of an operand on which each operator goes on
21
- # to its next operand; any other ends the chain there. A
22
- # when's patterns, and a case's whens, stop at a match.
23
- CONTINUES_ON = {
24
- '&&' => true, 'and' => true, '||' => false, 'or' => false,
25
- 'when' => false, 'case' => false
26
- }.freeze
27
-
28
19
  # [[{ leaf range => outcome }, value], ...] for each way
29
20
  # node can be evaluated, value being the outcome of node as
30
- # a whole; fixed is the site's { leaf range => its one
31
- # outcome }. A case is the chain of its whens' conditions.
21
+ # a whole, as its kind gives them; fixed is the site's
22
+ # { leaf range => its one outcome }.
32
23
  def self.walk(node, fixed)
33
24
  return leaf(node, fixed) unless node.is_a?(Condition)
34
- return ternary(node, fixed) if CHOICES.include?(node.operator)
35
-
36
- if node.operator == 'case'
37
- whens = node.operands.filter_map(&:body)
38
- return chain(node.with(operands: whens), fixed)
39
- end
40
-
41
- chain(node, fixed)
42
- end
43
25
 
44
- # The ways a chain of one operator can be evaluated: each
45
- # way of its first operand, going on to the next while each
46
- # outcome is the one its operator goes on with.
47
- def self.chain(node, fixed)
48
- first, *rest = node.operands
49
- goes_on = CONTINUES_ON.fetch(node.operator)
50
- rest.reduce(walk(first, fixed)) do |ways, operand|
51
- ways.flat_map do |outcomes, value|
52
- next [[outcomes, value]] unless value == goes_on
53
-
54
- walk(operand, fixed).map do |more, after|
55
- [outcomes.merge(more), after]
56
- end
57
- end
58
- end
59
- end
60
-
61
- # The ways a ternary, an if or an unless can be evaluated:
62
- # each way of its predicate, then each way of the condition
63
- # the arm it picks is, if that arm is one; one with no else
64
- # arm picks none. Its value is that of no choice, so is nil.
65
- def self.ternary(node, fixed)
66
- predicate, then_arm, else_arm = node.operands
67
- then_on = THEN_ON[node.operator]
68
- walk(predicate, fixed).flat_map do |outcomes, value|
69
- body = (value == then_on ? then_arm : else_arm)&.body
70
- next [[outcomes, nil]] if body.nil?
71
-
72
- walk(body, fixed).map do |more, _|
73
- [outcomes.merge(more), nil]
74
- end
75
- end
26
+ Kinds.of(node.operator).ways(node, fixed)
76
27
  end
77
28
 
78
29
  # The ways a leaf can be evaluated: true, then false, or