path_coverage 0.4.0 → 0.6.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 (108) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +260 -300
  3. data/exe/path_coverage_report +4 -0
  4. data/lib/path_coverage/call_trace.rb +109 -0
  5. data/lib/path_coverage/calls.rb +73 -43
  6. data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +35 -33
  7. data/lib/path_coverage/condition.rb +14 -14
  8. data/lib/path_coverage/configuration.rb +7 -4
  9. data/lib/path_coverage/configuration_limits.rb +52 -0
  10. data/lib/path_coverage/configuration_paths.rb +7 -7
  11. data/lib/path_coverage/console_summary.rb +28 -26
  12. data/lib/path_coverage/{site.rb → decision.rb} +2 -2
  13. data/lib/path_coverage/decision_locations.rb +76 -0
  14. data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +13 -11
  15. data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
  16. data/lib/path_coverage/fold_branches.rb +18 -0
  17. data/lib/path_coverage/fold_cells.rb +42 -0
  18. data/lib/path_coverage/{html_tree.rb → fold_rows.rb} +43 -72
  19. data/lib/path_coverage/gaps.rb +33 -15
  20. data/lib/path_coverage/json_reader.rb +54 -19
  21. data/lib/path_coverage/json_reports.rb +109 -0
  22. data/lib/path_coverage/json_writer.rb +29 -14
  23. data/lib/path_coverage/kernel.rb +77 -58
  24. data/lib/path_coverage/kinds/base.rb +16 -16
  25. data/lib/path_coverage/kinds/block.rb +16 -15
  26. data/lib/path_coverage/kinds/case.rb +14 -12
  27. data/lib/path_coverage/kinds/chain.rb +40 -11
  28. data/lib/path_coverage/kinds/choice.rb +4 -3
  29. data/lib/path_coverage/kinds/contains.rb +40 -32
  30. data/lib/path_coverage/kinds/safe.rb +38 -26
  31. data/lib/path_coverage/kinds/write.rb +6 -6
  32. data/lib/path_coverage/kinds.rb +2 -2
  33. data/lib/path_coverage/leaving.rb +52 -0
  34. data/lib/path_coverage/loop_forks.rb +57 -0
  35. data/lib/path_coverage/loop_nodes.rb +91 -0
  36. data/lib/path_coverage/manifest.rb +92 -58
  37. data/lib/path_coverage/manifest_blocks.rb +5 -26
  38. data/lib/path_coverage/manifest_case.rb +2 -2
  39. data/lib/path_coverage/manifest_chains.rb +8 -8
  40. data/lib/path_coverage/manifest_choices.rb +23 -20
  41. data/lib/path_coverage/manifest_disabled.rb +97 -0
  42. data/lib/path_coverage/manifest_folding.rb +43 -40
  43. data/lib/path_coverage/manifest_ignoring.rb +6 -6
  44. data/lib/path_coverage/manifest_writes.rb +11 -10
  45. data/lib/path_coverage/measured_by.rb +20 -0
  46. data/lib/path_coverage/merge.rb +7 -7
  47. data/lib/path_coverage/method_loops.rb +172 -0
  48. data/lib/path_coverage/method_statements.rb +150 -134
  49. data/lib/path_coverage/method_ways.rb +170 -0
  50. data/lib/path_coverage/paths_context.rb +104 -0
  51. data/lib/path_coverage/paths_fitting.rb +31 -0
  52. data/lib/path_coverage/paths_forks.rb +72 -0
  53. data/lib/path_coverage/paths_gaps.rb +137 -0
  54. data/lib/path_coverage/paths_leaving.rb +40 -0
  55. data/lib/path_coverage/paths_report.rb +16 -16
  56. data/lib/path_coverage/paths_rescues.rb +146 -0
  57. data/lib/path_coverage/paths_scopes.rb +37 -0
  58. data/lib/path_coverage/paths_tree.rb +203 -0
  59. data/lib/path_coverage/paths_way_lists.rb +72 -0
  60. data/lib/path_coverage/paths_ways.rb +193 -0
  61. data/lib/path_coverage/recorder.rb +71 -36
  62. data/lib/path_coverage/recorder_blocks.rb +17 -17
  63. data/lib/path_coverage/recorder_cases.rb +12 -11
  64. data/lib/path_coverage/recorder_chains.rb +12 -12
  65. data/lib/path_coverage/recorder_writes.rb +9 -9
  66. data/lib/path_coverage/render.rb +58 -51
  67. data/lib/path_coverage/rescue_forks.rb +174 -0
  68. data/lib/path_coverage/result.rb +32 -31
  69. data/lib/path_coverage/rewriter.rb +116 -35
  70. data/lib/path_coverage/rewriter_nested.rb +13 -13
  71. data/lib/path_coverage/runs_report.rb +84 -0
  72. data/lib/path_coverage/source_lines.rb +16 -16
  73. data/lib/path_coverage/source_report.rb +100 -0
  74. data/lib/path_coverage/stable_leaves.rb +162 -0
  75. data/lib/path_coverage/statement_arms.rb +53 -38
  76. data/lib/path_coverage/summary.rb +4 -62
  77. data/lib/path_coverage/totals.rb +12 -10
  78. data/lib/path_coverage/version.rb +1 -1
  79. data/lib/path_coverage/walk.rb +106 -0
  80. data/lib/path_coverage/way_algebras.rb +127 -0
  81. data/lib/path_coverage/ways_report.rb +91 -0
  82. data/lib/path_coverage/ways_totals.rb +46 -0
  83. data/lib/path_coverage/within_methods.rb +152 -97
  84. metadata +39 -30
  85. data/lib/path_coverage/card.rb +0 -14
  86. data/lib/path_coverage/card_tree.rb +0 -186
  87. data/lib/path_coverage/card_ways.rb +0 -140
  88. data/lib/path_coverage/html/file.html +0 -33
  89. data/lib/path_coverage/html/index.html +0 -29
  90. data/lib/path_coverage/html/settings.js +0 -50
  91. data/lib/path_coverage/html/side.js +0 -599
  92. data/lib/path_coverage/html/sort.js +0 -34
  93. data/lib/path_coverage/html/style.css +0 -484
  94. data/lib/path_coverage/html/tabs.js +0 -15
  95. data/lib/path_coverage/html/theme.js +0 -27
  96. data/lib/path_coverage/html_branch.rb +0 -30
  97. data/lib/path_coverage/html_cards.rb +0 -88
  98. data/lib/path_coverage/html_cells.rb +0 -71
  99. data/lib/path_coverage/html_file.rb +0 -123
  100. data/lib/path_coverage/html_index.rb +0 -128
  101. data/lib/path_coverage/html_index_head.rb +0 -29
  102. data/lib/path_coverage/html_labels.rb +0 -16
  103. data/lib/path_coverage/html_methods.rb +0 -66
  104. data/lib/path_coverage/html_nav.rb +0 -67
  105. data/lib/path_coverage/html_report.rb +0 -131
  106. data/lib/path_coverage/html_statements.rb +0 -186
  107. data/lib/path_coverage/html_tabs.rb +0 -26
  108. data/lib/path_coverage/html_template.rb +0 -26
@@ -0,0 +1,104 @@
1
+ require_relative 'paths_tree'
2
+ require_relative 'paths_ways'
3
+ require_relative 'method_statements'
4
+ require_relative 'way_algebras'
5
+
6
+ module PathCoverage
7
+ # The paths of each def's flow rows that its method can take:
8
+ # those on some way through the flow, as PathsWays folds it,
9
+ # and any taken that it rules out, which are counted, never
10
+ # dropped, and reported. Paths combined count these, where paths
11
+ # alone count each decision's paths on its own.
12
+ # paths is as Result#paths gives it, and reached as
13
+ # Recorder#reached does, nil when not known.
14
+ module PathsContext
15
+ # Example: { "k.rb:1:0" => [2, 3] } for ship, its first row's
16
+ # 2 paths both taken, its second's 1 path, of 2, that its
17
+ # method can take, not taken.
18
+ # Each def's flow total in source, [taken, size], its rows'
19
+ # taken as MethodStatements.taken gives them, a row not known
20
+ # counting as not taken, and its size their paths as rows
21
+ # gives them.
22
+ def self.totals(source, path, paths, reached, decisions)
23
+ flows = MethodStatements.of(source, path, decisions)
24
+ flows.transform_values do |rows|
25
+ shown = rows(source, decisions, rows, paths, reached)
26
+ seen = shown.filter_map(&:last).sum
27
+ [seen, MethodStatements.size(shown.map { _1.first.paths })]
28
+ end
29
+ end
30
+
31
+ # [row, taken] for each of rows, a flow's in source: row with
32
+ # as its paths how many of them its method can take or took,
33
+ # as of gives them, and taken as MethodStatements.taken gives
34
+ # it.
35
+ def self.rows(source, decisions, rows, paths, reached)
36
+ context = of(source, decisions, rows, paths, reached)
37
+ shown = rows.zip(context).map do |row, (can, took)|
38
+ row.with(paths: (can | took).size)
39
+ end
40
+ shown.zip(MethodStatements.taken(rows, paths, reached))
41
+ end
42
+
43
+ # Example: [[[1], [1]], [[0, 1], [0]]] for return 0 if a, then
44
+ # a ?: of b, called once with a false and b true.
45
+ # [the indices of its paths its method can take, those taken]
46
+ # for each of rows, a flow's in source: those on some way
47
+ # through the flow, as WayAlgebras::Labelled folds them; and
48
+ # those taken, a return's or a raise's one path index 0.
49
+ def self.of(source, decisions, rows, paths, reached)
50
+ labelled = WayAlgebras::Labelled
51
+ labels = PathsWays.flow(source, decisions, rows, labelled)
52
+ by_key = labels.group_by(&:first)
53
+ rows.map do |row|
54
+ can = by_key.fetch(PathsTree.key_of(row), []).map(&:last)
55
+ [can, taken_paths(row, paths, reached)]
56
+ end
57
+ end
58
+
59
+ # The indices of the paths of row taken: of its decision's
60
+ # paths, those with a count in paths; for a return or a raise,
61
+ # its one path when reached counts it, none when not, or when
62
+ # reached is nil, not known.
63
+ def self.taken_paths(row, paths, reached)
64
+ decision = row.decision
65
+ return reached.to_h.key?(row.name) ? [0] : [] if decision.nil?
66
+
67
+ counts = paths.fetch(decision).map { _1[:count] }
68
+ counts.each_with_index.map { |n, at| at if n.positive? }.compact
69
+ end
70
+
71
+ # Example: [["k.rb:5:11", [true]]], a ?:'s true path taken past
72
+ # return 0 if a, a's local having been set by other code.
73
+ # [name, path] for each path taken in files, { full path =>
74
+ # {source:, decisions:} }, under config, that its method rules
75
+ # out, as of finds them, as ruled_in gives them.
76
+ def self.ruled_out(files, config, paths, reached)
77
+ files.flat_map do |full_path, parsed|
78
+ source, decisions = parsed.values_at(:source, :decisions)
79
+ path = config.relative(full_path)
80
+ flows = MethodStatements.of(source, path, decisions)
81
+ flows.values.flat_map do |rows|
82
+ ruled_in(source, decisions, rows, paths, reached)
83
+ end
84
+ end
85
+ end
86
+
87
+ # [name, path] for each path of rows, a flow's in source, taken
88
+ # though its method rules it out: a decision's name and the
89
+ # path, as paths has it; a return's or a raise's and nil.
90
+ def self.ruled_in(source, decisions, rows, paths, reached)
91
+ context = of(source, decisions, rows, paths, reached)
92
+ rows.zip(context).flat_map do |row, (can, took)|
93
+ (took - can).map { [row.name, path_at(row, _1, paths)] }
94
+ end
95
+ end
96
+
97
+ # The path at index among those of row's decision, as paths
98
+ # has it; nil for a return or a raise, which has no decision.
99
+ def self.path_at(row, index, paths)
100
+ decision = row.decision
101
+ decision ? paths.fetch(decision)[index][:path] : nil
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,31 @@
1
+ module PathCoverage
2
+ # Which paths of a decision row fit the path its parent takes.
3
+ # PathsWays includes it. A row that is the whole of its parent's
4
+ # leaf runs only the paths that give that leaf the outcome its
5
+ # parent's path has.
6
+ module PathsFitting
7
+ # The block's value on each path of the decision row keyed
8
+ # key, by index. A path that does not fit outcomes gets none
9
+ # instead. fits_of decides which paths fit.
10
+ def each_fitting(key, outcomes, none)
11
+ fits = fits_of(key, outcomes)
12
+ possible(@rows[key].decision).each_index.map do |path|
13
+ fits.call(path) ? yield(path) : none
14
+ end
15
+ end
16
+
17
+ # A test of whether a path index of the decision row keyed key
18
+ # fits outcomes, its parent's path's. At the top outcomes is
19
+ # nil, and every path fits. Every path fits a row that gives
20
+ # nothing too. Otherwise a path fits when it gives its leaf the
21
+ # outcome the parent's path has, or gives what is not known.
22
+ def fits_of(key, outcomes)
23
+ place = @places[key]
24
+ gives = place['gives']
25
+ return ->(_) { true } if outcomes.nil? || gives.nil?
26
+
27
+ fits = [nil, outcomes[place['leaf']]]
28
+ ->(path) { fits.include?(gives[path]) }
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,72 @@
1
+ module PathCoverage
2
+ # The loops of a flow. forks is { loop's name => [its bytes, its
3
+ # body's] }, as LoopForks gives them. Each loop is a scope: a
4
+ # next or a break in its body ends a pass, and the way goes on
5
+ # after the loop. When loops are decisions, each loop is also a
6
+ # fork. Its path 0 is no item. Its path 1 is at least one item,
7
+ # and runs its body, which holds its rows. A loop is placed by
8
+ # its bytes, as a row that is no decision is. The module
9
+ # functions place loops, for PathsTree. The instance methods fold
10
+ # them, for PathsWays, which includes this module.
11
+ module PathsForks
12
+ # The region of each loop of forks, its body, run on its path
13
+ # 1. Then the regions of each rescue fork of rescues, as
14
+ # PathsRescues gives them.
15
+ def self.regions(forks, rescues)
16
+ forks.map do |name, (_, body)|
17
+ PathsTree::Region.new(owner: PathsTree.key_at(name),
18
+ range: body, arm: 'body', on: [1],
19
+ leaf: nil, leaves: nil)
20
+ end + PathsRescues.regions(rescues)
21
+ end
22
+
23
+ # places, { key => its place }, a flow's rows', with the place
24
+ # of each scope too, among regions. The scopes are each loop of
25
+ # forks and each rescue fork of rescues. All are in the order
26
+ # they start. With no scope it is places alone.
27
+ def self.placed(places, (forks, rescues), regions)
28
+ spans = forks.transform_values(&:first)
29
+ .merge(rescues.transform_values { _1[1] })
30
+ return places if spans.empty?
31
+
32
+ scopes = spans.to_h do |name, span|
33
+ key = PathsTree.key_at(name)
34
+ [key, PathsTree.placed(key, span, regions, nil)]
35
+ end
36
+ merged = places.merge(scopes)
37
+ merged.sort_by { _1[0].split(':').map(&:to_i) }.to_h
38
+ end
39
+
40
+ # The parts of the loop keyed key, which is placed but is no
41
+ # row. Its body's rows run one after another, walked once. A
42
+ # pass ended by a next or a break goes on after the loop. When
43
+ # loops fork, the loop either makes no pass, or makes at least
44
+ # one and runs its body.
45
+ def fork_pair(key)
46
+ body = rejoined(sequence(running(key, 1).map { pair_of(_1) }))
47
+ return body unless @forking
48
+
49
+ own = @ways.unit([key, 1])
50
+ some = body.map { @ways.times(own, _1) }
51
+ added([[@ways.unit([key, 0]), *nothing.drop(1)], some])
52
+ end
53
+
54
+ # The parts of ways through a loop's body, [on, returned, raised,
55
+ # passed], once its scope ends. The ways that ended a pass by a
56
+ # next or a break join those that go on.
57
+ def rejoined((on, returned, raised, passed))
58
+ [@ways.plus(on, passed), returned, raised, @ways.zero]
59
+ end
60
+
61
+ # many, each a fold's parts, added up part by part. No parts
62
+ # add up to no ways.
63
+ def added(many)
64
+ many.reduce(nothing) do |sum, parts|
65
+ sum.zip(parts).map { @ways.plus(*_1) }
66
+ end
67
+ end
68
+
69
+ # The parts, [on, returned, raised, passed], of no ways.
70
+ def nothing = Array.new(4) { @ways.zero }
71
+ end
72
+ end
@@ -0,0 +1,137 @@
1
+ require_relative 'gaps'
2
+ require_relative 'method_ways'
3
+
4
+ module PathCoverage
5
+ # The terminal worklist of the untested paths through each
6
+ # method: for each method, a heading, then each path no run
7
+ # took, as the choices a test must make to take it.
8
+ module PathsGaps
9
+ # The instruction under a method's untested paths.
10
+ UNTESTED = 'Add tests that take each of these paths:'.freeze
11
+ # The instruction under a method with more paths than
12
+ # --max-paths allows.
13
+ TOO_MANY = 'Split it into smaller functions.'.freeze
14
+ # The instruction under the impossible paths a method's calls
15
+ # took.
16
+ DECLARED = 'Remove the # path: impossible comment that ' \
17
+ 'declares it:'.freeze
18
+
19
+ # The lines of flows, { method's key => flow }, as a JSON
20
+ # report has them, each choice's leaves named by their text in
21
+ # texts, { decision => [text, ...] }; a decision whose parent,
22
+ # in parents, { decision => its parent decision }, is stepped
23
+ # on the same path is named there, and the parent left out. A
24
+ # method's flow listing no gaps, its ways more than max paths
25
+ # allows, is named with what to do, so it fails and never
26
+ # reads as tested; a top flow, run by no call, has no lines.
27
+ def self.lines(flows, texts, parents)
28
+ flows.flat_map do |key, flow|
29
+ next [] unless flow.key?('taken')
30
+
31
+ method_lines(key, flow, texts, parents)
32
+ end
33
+ end
34
+
35
+ # The lines of flow, the method's at key, as lines gives them.
36
+ def self.method_lines(key, flow, texts, parents)
37
+ where = "#{key.split(':')[..-2].join(':')} #{flow['name']}()"
38
+ return too_many(where, flow) unless flow.key?('gaps')
39
+
40
+ named = Choices.new(flow, texts, parents)
41
+ gaps = flow['gaps']
42
+ taken = flow['impossible_taken']
43
+ section("#{where} has #{paths(gaps.size, 'untested ')}.",
44
+ UNTESTED, gaps, named) +
45
+ section("#{where} took #{paths(taken.size, '')} declared " \
46
+ 'impossible.', DECLARED, taken, named)
47
+ end
48
+
49
+ # The lines of flow, the method's at where, with more paths
50
+ # than --max-paths allows: how many, and what to do.
51
+ def self.too_many(where, flow)
52
+ ["#{where} has #{flow['ways']} paths, more than " \
53
+ '--max-paths allows.', TOO_MANY]
54
+ end
55
+
56
+ # heading and instruction, then each of ways numbered, its
57
+ # choices as named gives them; no lines at all when ways is
58
+ # empty.
59
+ def self.section(heading, instruction, ways, named)
60
+ return [] if ways.empty?
61
+
62
+ numbered = ways.each_with_index.map do |way, index|
63
+ " #{index + 1}. #{named.of(way)}"
64
+ end
65
+ [heading, instruction, *numbered]
66
+ end
67
+
68
+ # count paths, of kind, eg "untested ", singular for one.
69
+ def self.paths(count, kind)
70
+ "#{count} #{kind}#{count == 1 ? 'path' : 'paths'}"
71
+ end
72
+
73
+ # The choices of a flow's ways, as a test must make them.
74
+ class Choices
75
+ # The choices of flow's ways, its rows naming each step's
76
+ # decision, texts and parents as PathsGaps.lines has them.
77
+ def initialize(flow, texts, parents)
78
+ @decisions = flow['rows'].to_h { [_1['at'], _1['decision']] }
79
+ @texts = texts
80
+ @parents = parents
81
+ end
82
+
83
+ # The choices of way, a path's steps, in order: each stepped
84
+ # decision by its line and its leaves' outcomes, but for a
85
+ # parent of another of them; each loop, a step at no row, by
86
+ # its line and how many items it ran; a step with no choice
87
+ # is left out.
88
+ def of(way)
89
+ stepped = []
90
+ way.each { |step| stepped << step if step.key?('path') }
91
+ implied = implied_by(stepped)
92
+ kept = []
93
+ stepped.each do |step|
94
+ decision = @decisions[step['at']]
95
+ kept << choice(step) unless implied.include?(decision)
96
+ end
97
+ kept.join(', then ')
98
+ end
99
+
100
+ # The parent decisions of the decisions stepped steps, each
101
+ # named on its child's step instead; none of a loop's step,
102
+ # which has no decision.
103
+ def implied_by(stepped)
104
+ implied = Set.new
105
+ stepped.each do |step|
106
+ parent = @parents[@decisions[step['at']]]
107
+ implied << parent if parent
108
+ end
109
+ implied
110
+ end
111
+
112
+ private
113
+
114
+ # One step's choice, after its line. For a decision it is its
115
+ # leaves' outcomes. For a fork, a loop or a rescue, it is its
116
+ # path.
117
+ def choice(step)
118
+ line = step['at'].split(':').first
119
+ decision = @decisions[step['at']]
120
+ path = step['path']
121
+ return "line #{line} #{forked(path.first)}" if decision.nil?
122
+
123
+ "line #{line} #{Gaps.steps(path, @texts[decision])}"
124
+ end
125
+
126
+ # A fork's path, from its label, as MethodWays labels it. A
127
+ # label in FORKS is a loop's items. Any other is a rescue's:
128
+ # its body completing, or which of its rescues runs.
129
+ def forked(label)
130
+ return "loop: #{label}" if MethodWays::FORKS.include?(label)
131
+ return 'rescue: body completes' if label == 'completed'
132
+
133
+ "rescue: #{label} runs"
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,40 @@
1
+ module PathCoverage
2
+ # How ways leave a flow. PathsWays includes it. A return or a
3
+ # throw returns, and no rescue catches it. A raise or a fail
4
+ # raises, and a rescue can catch it. A next, a break or a retry
5
+ # ends its pass, and the way goes on after its loop.
6
+ module PathsLeaving
7
+ # Where the ways that leave by each word go, as an index into a
8
+ # fold's parts, [on, returned, raised, passed].
9
+ PARTS = { 'return' => 1, 'throw' => 1, 'raise' => 2, 'fail' => 2,
10
+ 'next' => 3, 'break' => 3, 'retry' => 3 }.freeze
11
+
12
+ # { path index => word } for the decision row keyed key. It
13
+ # holds each path whose arm leaves, with the word it leaves
14
+ # by, eg return or raise.
15
+ def leaving(key)
16
+ decision = @decisions.fetch(@rows[key].decision)
17
+ regions = PathsTree.regions_of(@source, key, decision)
18
+ regions.select(&:leaves).flat_map do |region|
19
+ region.on.map { [_1, region.leaves] }
20
+ end.to_h
21
+ end
22
+
23
+ # The parts of ways, [on, returned, raised, passed]. With word
24
+ # nil they are unchanged. Otherwise every way leaves, and those
25
+ # that went on leave by word.
26
+ def leaving_by((on, *out), word)
27
+ return [on, *out] if word.nil?
28
+
29
+ all = left(word, on).drop(1).zip(out).map { @ways.plus(*_1) }
30
+ [@ways.zero, *all]
31
+ end
32
+
33
+ # The parts holding ways, put where word sends them, as PARTS
34
+ # says. Every other part is empty.
35
+ def left(word, ways)
36
+ part = PARTS.fetch(word)
37
+ (0..3).map { |index| index == part ? ways : @ways.zero }
38
+ end
39
+ end
40
+ end
@@ -1,49 +1,49 @@
1
1
  module PathCoverage
2
- # What the reports say about each site's paths beyond their
2
+ # What the reports say about each decision's paths beyond their
3
3
  # counts, worked out from each covered file's source: where
4
4
  # each leaf is, and what it says.
5
5
  module PathsReport
6
- # { site => { leaves:, taken: } }: taken is each path and
6
+ # { decision => { leaves:, taken: } }: taken is each path and
7
7
  # its count, as Result#paths gives them, and leaves each
8
8
  # leaf's ["line:column", "line:column"] span, so a tool can
9
9
  # recover each leaf exactly. files is { full path =>
10
- # {source:, sites:} }.
11
- def self.of(site_paths, files)
10
+ # {source:, decisions:} }.
11
+ def self.of(decision_paths, files)
12
12
  spans = leaf_spans(files)
13
- site_paths.to_h do |site, taken|
14
- [site, { leaves: spans.fetch(site), taken: }]
13
+ decision_paths.to_h do |decision, taken|
14
+ [decision, { leaves: spans.fetch(decision), taken: }]
15
15
  end
16
16
  end
17
17
 
18
- # { site => [each leaf's source text] } for every site in
18
+ # { decision => [each leaf's source text] } for every decision in
19
19
  # files.
20
20
  def self.texts(files)
21
- each_site(files) do |source, condition|
21
+ each_decision(files) do |source, condition|
22
22
  condition.leaves.map { |range| source.byteslice(range) }
23
23
  end
24
24
  end
25
25
 
26
- # { site => [each leaf's span] } for every site in files.
26
+ # { decision => [each leaf's span] } for every decision in files.
27
27
  def self.leaf_spans(files)
28
- each_site(files) do |source, condition|
28
+ each_decision(files) do |source, condition|
29
29
  condition.leaves.map do |range|
30
30
  [at(source, range.begin), at(source, range.end)]
31
31
  end
32
32
  end
33
33
  end
34
34
 
35
- # { site => what the block makes of its file's source and
36
- # its condition } for every site in files.
37
- def self.each_site(files)
35
+ # { decision => what the block makes of its file's source and
36
+ # its condition } for every decision in files.
37
+ def self.each_decision(files)
38
38
  files.values.each_with_object({}) do |parsed, all|
39
- parsed[:sites].each do |site, condition|
40
- all[site] = yield(parsed[:source], condition)
39
+ parsed[:decisions].each do |decision, condition|
40
+ all[decision] = yield(parsed[:source], condition)
41
41
  end
42
42
  end
43
43
  end
44
44
 
45
45
  # "line:column" of a byte offset in source, the line from
46
- # 1 and the column, in characters, from 0, as site names
46
+ # 1 and the column, in characters, from 0, as decision names
47
47
  # count them.
48
48
  def self.at(source, offset)
49
49
  before = source.byteslice(0, offset)
@@ -0,0 +1,146 @@
1
+ module PathCoverage
2
+ # How a begin with a rescue forks its flow. PathsWays includes
3
+ # it. The begin is a scope. Its body either completes, or a
4
+ # rescue runs. A rescue can run after any prefix of a way through
5
+ # the body, because a raise can come from anywhere. A prefix can
6
+ # end at any row, including one inside an arm or a loop's body.
7
+ module PathsRescues
8
+ # { key => its rescues } of each rescue fork of rescues, as
9
+ # RescueForks gives them.
10
+ def self.counts(rescues)
11
+ rescues.to_h do |name, (count, *)|
12
+ [PathsTree.key_at(name), count]
13
+ end
14
+ end
15
+
16
+ # The regions of each rescue fork of rescues, as RescueForks
17
+ # gives them. Its body is run on its path 0. The body of its
18
+ # rescue numbered n is run on its path n.
19
+ def self.regions(rescues)
20
+ rescues.flat_map do |name, (_, _, body, clauses)|
21
+ owner = PathsTree.key_at(name)
22
+ bodies = [['body', body], *clauses.map { ['rescue', _1] }]
23
+ bodies.each_with_index.map do |(arm, range), path|
24
+ PathsTree::Region.new(owner:, range:, arm:, on: [path],
25
+ leaf: nil, leaves: nil)
26
+ end
27
+ end
28
+ end
29
+
30
+ # The parts of the rescue fork keyed key, [on, returned, raised,
31
+ # passed]. Its body's ways that go on complete, labelled [key,
32
+ # 0]. A prefix of the body, or a way that raises in it, then
33
+ # runs rescue n, labelled [key, n], then that rescue's rows. A
34
+ # raise written in the body is taken as caught there. Ways that
35
+ # return or pass still leave.
36
+ def rescue_pair(key)
37
+ on, returned, _, passed = body = run_on(key, 0)
38
+ completed = [@ways.times(on, @ways.unit([key, 0])), returned,
39
+ @ways.zero, passed]
40
+ ran = (1..@rescues.fetch(key)).map do |path|
41
+ prefix = rescue_prefix(key, body, path)
42
+ run_on(key, path).map { @ways.times(prefix, _1) }
43
+ end
44
+ added([completed, *ran])
45
+ end
46
+
47
+ # The ways up to and including the step of the rescue numbered
48
+ # path of the fork keyed key. They start with each prefix of
49
+ # its body, or with one of body, its parts, that goes through
50
+ # the whole of it or raises in it.
51
+ def rescue_prefix(key, (on, _, raised, _), path)
52
+ whole = @ways.plus(on, raised)
53
+ ended = @ways.plus(opened(running(key, 0)), whole)
54
+ @ways.times(ended, @ways.unit([key, path]))
55
+ end
56
+
57
+ # The parts of the rows the fork keyed key runs on its path at
58
+ # index path, one after another: its body on path 0, else the
59
+ # body of its rescue numbered path.
60
+ def run_on(key, path)
61
+ sequence(running(key, path).map { pair_of(_1) })
62
+ end
63
+
64
+ # The ways that stop partway through the rows keyed keys,
65
+ # before the last of them completes. Each goes on through the
66
+ # rows before one row, then stops before that row or inside
67
+ # it. outcomes is the parent's path's outcomes, or nil at the
68
+ # top. Only a row's paths that fit outcomes are taken.
69
+ def opened(keys, outcomes = nil)
70
+ on = @ways.one
71
+ keys.reduce(@ways.zero) do |cut, key|
72
+ stop = @ways.plus(@ways.one, inside(key, outcomes))
73
+ here = @ways.times(on, stop)
74
+ on = @ways.times(on, fitted_pair(key, outcomes)[0])
75
+ @ways.plus(cut, here)
76
+ end
77
+ end
78
+
79
+ # The ways that stop inside the row keyed key, after its own
80
+ # step. For a decision, each fitting path's step, then a stop
81
+ # partway through the rows its arm runs. For a loop, a stop
82
+ # partway through its body. A return or a raise has none,
83
+ # because it leaves.
84
+ def inside(key, outcomes)
85
+ return rescue_inside(key) if @rescues.key?(key)
86
+ return looped(key) unless @rows.key?(key)
87
+
88
+ decision = @rows[key].decision
89
+ return @ways.zero if decision.nil?
90
+
91
+ cuts = each_fitting(key, outcomes, @ways.zero) do |path|
92
+ inside_on(key, path)
93
+ end
94
+ cuts.reduce(@ways.zero) { |all, cut| @ways.plus(all, cut) }
95
+ end
96
+
97
+ # The ways that stop inside the decision row keyed key, on its
98
+ # path at index path. The path's step comes with the whole of
99
+ # the decisions in its condition. No cut divides those. Then
100
+ # comes a stop partway through the rows its arms run.
101
+ def inside_on(key, path)
102
+ outcomes = possible(@rows[key].decision)[path]
103
+ by = running(key, path).group_by { @places[_1]['leaf'].nil? }
104
+ whole = whole_on(key, path, outcomes, by.fetch(false, []))
105
+ @ways.times(whole, opened(by.fetch(true, []), outcomes))
106
+ end
107
+
108
+ # The ways through the decision row keyed key on its path at
109
+ # index path. They include the whole of leaves, the rows in its
110
+ # condition, one after another.
111
+ def whole_on(key, path, outcomes, leaves)
112
+ pairs = leaves.map { fitted_pair(_1, outcomes) }
113
+ @ways.times(own(key, path, outcomes), sequence(pairs)[0])
114
+ end
115
+
116
+ # The ways that stop inside the loop keyed key, partway through
117
+ # its body. When loops fork, they start with its step of at
118
+ # least one item.
119
+ def looped(key)
120
+ partway = opened(running(key, 1))
121
+ return partway unless @forking
122
+
123
+ @ways.times(@ways.unit([key, 1]), partway)
124
+ end
125
+
126
+ # The ways that stop inside the rescue fork keyed key. They stop
127
+ # partway through its body, or partway through the rows of one
128
+ # of its rescues, after that rescue's step.
129
+ def rescue_inside(key)
130
+ body = run_on(key, 0)
131
+ ran = (1..@rescues.fetch(key)).map do |path|
132
+ prefix = rescue_prefix(key, body, path)
133
+ @ways.times(prefix, opened(running(key, path)))
134
+ end
135
+ ran.reduce(opened(running(key, 0))) do |all, cut|
136
+ @ways.plus(all, cut)
137
+ end
138
+ end
139
+
140
+ # The parts of the row keyed key. outcomes is its parent's
141
+ # path's outcomes, or nil at the top.
142
+ def fitted_pair(key, outcomes)
143
+ outcomes.nil? ? pair_of(key) : child_pair(key, outcomes)
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,37 @@
1
+ require 'prism'
2
+
3
+ module PathCoverage
4
+ # The scope a top flow is named for: the module or class its
5
+ # lines are in, as a def names a method's flow.
6
+ module PathsScopes
7
+ # The kinds of node that are a top flow's scope.
8
+ HOLDERS = [Prism::ModuleNode, Prism::ClassNode].freeze
9
+
10
+ # A lambda giving the scope of a line of source, as scope
11
+ # gives it, its modules and classes found once.
12
+ def self.of(source)
13
+ holders = holders_in(Prism.parse(source).value)
14
+ ->(line) { scope(holders, line) }
15
+ end
16
+
17
+ # Example: line 2, inside module Shop on lines 1 to 9, gives
18
+ # Shop.
19
+ # The name of the innermost of holders, modules and classes,
20
+ # whose lines hold line, a top flow's scope, as a def is a
21
+ # method flow's; file for a line in none.
22
+ def self.scope(holders, line)
23
+ holding = holders.select do |node|
24
+ location = node.location
25
+ (location.start_line..location.end_line).cover?(line)
26
+ end
27
+ holding.empty? ? 'file' : holding.last.name.to_s
28
+ end
29
+
30
+ # Every module and class node in the tree at node, each
31
+ # before those nested in it.
32
+ def self.holders_in(node)
33
+ own = HOLDERS.include?(node.class) ? [node] : []
34
+ own + node.compact_child_nodes.flat_map { holders_in(_1) }
35
+ end
36
+ end
37
+ end