path_coverage 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +286 -199
  3. data/exe/path_coverage_report +4 -0
  4. data/lib/path_coverage/call_trace.rb +88 -0
  5. data/lib/path_coverage/calls.rb +49 -43
  6. data/lib/path_coverage/card_context.rb +104 -0
  7. data/lib/path_coverage/card_forks.rb +44 -0
  8. data/lib/path_coverage/card_gaps.rb +125 -0
  9. data/lib/path_coverage/card_scopes.rb +37 -0
  10. data/lib/path_coverage/card_tree.rb +79 -66
  11. data/lib/path_coverage/card_way_lists.rb +72 -0
  12. data/lib/path_coverage/card_ways.rb +123 -67
  13. data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +35 -33
  14. data/lib/path_coverage/condition.rb +14 -14
  15. data/lib/path_coverage/configuration.rb +7 -4
  16. data/lib/path_coverage/configuration_limits.rb +52 -0
  17. data/lib/path_coverage/configuration_paths.rb +7 -7
  18. data/lib/path_coverage/console_summary.rb +24 -22
  19. data/lib/path_coverage/{site.rb → decision.rb} +2 -2
  20. data/lib/path_coverage/decision_locations.rb +76 -0
  21. data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +13 -11
  22. data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
  23. data/lib/path_coverage/fold_branches.rb +18 -0
  24. data/lib/path_coverage/fold_cells.rb +42 -0
  25. data/lib/path_coverage/{html_tree.rb → fold_rows.rb} +42 -71
  26. data/lib/path_coverage/gaps.rb +33 -15
  27. data/lib/path_coverage/json_reader.rb +54 -19
  28. data/lib/path_coverage/json_reports.rb +102 -0
  29. data/lib/path_coverage/json_writer.rb +27 -12
  30. data/lib/path_coverage/kernel.rb +56 -58
  31. data/lib/path_coverage/kinds/base.rb +16 -16
  32. data/lib/path_coverage/kinds/block.rb +16 -15
  33. data/lib/path_coverage/kinds/case.rb +14 -12
  34. data/lib/path_coverage/kinds/chain.rb +40 -11
  35. data/lib/path_coverage/kinds/choice.rb +4 -3
  36. data/lib/path_coverage/kinds/contains.rb +40 -32
  37. data/lib/path_coverage/kinds/safe.rb +38 -26
  38. data/lib/path_coverage/kinds/write.rb +6 -6
  39. data/lib/path_coverage/kinds.rb +2 -2
  40. data/lib/path_coverage/leaving.rb +44 -0
  41. data/lib/path_coverage/loop_forks.rb +57 -0
  42. data/lib/path_coverage/loop_nodes.rb +55 -0
  43. data/lib/path_coverage/manifest.rb +92 -58
  44. data/lib/path_coverage/manifest_blocks.rb +5 -26
  45. data/lib/path_coverage/manifest_case.rb +2 -2
  46. data/lib/path_coverage/manifest_chains.rb +8 -8
  47. data/lib/path_coverage/manifest_choices.rb +23 -20
  48. data/lib/path_coverage/manifest_disabled.rb +97 -0
  49. data/lib/path_coverage/manifest_folding.rb +43 -40
  50. data/lib/path_coverage/manifest_ignoring.rb +6 -6
  51. data/lib/path_coverage/manifest_writes.rb +11 -10
  52. data/lib/path_coverage/measured_by.rb +20 -0
  53. data/lib/path_coverage/merge.rb +7 -7
  54. data/lib/path_coverage/method_loops.rb +128 -0
  55. data/lib/path_coverage/method_statements.rb +130 -127
  56. data/lib/path_coverage/method_ways.rb +142 -0
  57. data/lib/path_coverage/paths_report.rb +16 -16
  58. data/lib/path_coverage/recorder.rb +57 -36
  59. data/lib/path_coverage/recorder_blocks.rb +17 -17
  60. data/lib/path_coverage/recorder_cases.rb +12 -11
  61. data/lib/path_coverage/recorder_chains.rb +12 -12
  62. data/lib/path_coverage/recorder_writes.rb +9 -9
  63. data/lib/path_coverage/render.rb +58 -51
  64. data/lib/path_coverage/result.rb +32 -31
  65. data/lib/path_coverage/rewriter.rb +39 -27
  66. data/lib/path_coverage/rewriter_nested.rb +13 -13
  67. data/lib/path_coverage/runs_report.rb +84 -0
  68. data/lib/path_coverage/source_lines.rb +16 -16
  69. data/lib/path_coverage/source_report.rb +100 -0
  70. data/lib/path_coverage/stable_leaves.rb +162 -0
  71. data/lib/path_coverage/statement_arms.rb +31 -30
  72. data/lib/path_coverage/summary.rb +4 -62
  73. data/lib/path_coverage/totals.rb +10 -8
  74. data/lib/path_coverage/version.rb +1 -1
  75. data/lib/path_coverage/walk.rb +106 -0
  76. data/lib/path_coverage/way_algebras.rb +127 -0
  77. data/lib/path_coverage/ways_report.rb +91 -0
  78. data/lib/path_coverage/ways_totals.rb +46 -0
  79. data/lib/path_coverage/within_methods.rb +135 -80
  80. metadata +33 -28
  81. data/lib/path_coverage/card.rb +0 -14
  82. data/lib/path_coverage/html/file.html +0 -33
  83. data/lib/path_coverage/html/index.html +0 -29
  84. data/lib/path_coverage/html/settings.js +0 -50
  85. data/lib/path_coverage/html/side.js +0 -599
  86. data/lib/path_coverage/html/sort.js +0 -34
  87. data/lib/path_coverage/html/style.css +0 -484
  88. data/lib/path_coverage/html/tabs.js +0 -15
  89. data/lib/path_coverage/html/theme.js +0 -27
  90. data/lib/path_coverage/html_branch.rb +0 -30
  91. data/lib/path_coverage/html_cards.rb +0 -88
  92. data/lib/path_coverage/html_cells.rb +0 -71
  93. data/lib/path_coverage/html_file.rb +0 -123
  94. data/lib/path_coverage/html_index.rb +0 -128
  95. data/lib/path_coverage/html_index_head.rb +0 -29
  96. data/lib/path_coverage/html_labels.rb +0 -16
  97. data/lib/path_coverage/html_methods.rb +0 -66
  98. data/lib/path_coverage/html_nav.rb +0 -67
  99. data/lib/path_coverage/html_report.rb +0 -131
  100. data/lib/path_coverage/html_statements.rb +0 -186
  101. data/lib/path_coverage/html_tabs.rb +0 -26
  102. data/lib/path_coverage/html_template.rb +0 -26
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'path_coverage/runs_report'
3
+
4
+ exit PathCoverage::RunsReport.main(ARGV)
@@ -0,0 +1,88 @@
1
+ require_relative 'walk'
2
+
3
+ module PathCoverage
4
+ # The ways each call of a covered method takes: a stack of the
5
+ # calls under way in each thread, each with the way its method
6
+ # is taking, a step going to the innermost call of the method
7
+ # holding its row.
8
+ class CallTrace
9
+ # Starts with no method known and no way taken.
10
+ def initialize
11
+ @entries = {}
12
+ @repeats = {}
13
+ @owners = {}
14
+ @ways = {}
15
+ @stack_key = :"path_coverage_calls_#{object_id}"
16
+ end
17
+
18
+ # { unit's name => { way => count } } of every way ended so
19
+ # far, as Walk counts them.
20
+ attr_reader :ways
21
+
22
+ # Notes the method whose entry is named entry, loops being
23
+ # { its row's name => the names of the loops holding it,
24
+ # outermost first }, and repeats { its row's name => the while
25
+ # or until whose test holds it }, as Walk takes them.
26
+ def know(entry, loops, repeats = {})
27
+ @entries[entry] = loops
28
+ @repeats[entry] = repeats
29
+ loops.each_key { |row| @owners[row] = entry }
30
+ end
31
+
32
+ # Starts a call of a covered method, its method known once it
33
+ # reaches its entry.
34
+ def call
35
+ stack.push(nil)
36
+ end
37
+
38
+ # Notes the statement named name reached: a method's entry
39
+ # starts its call's way, unless no call is under way to take
40
+ # it, as when the call began before this trace did; any other
41
+ # row is a step of the innermost call of the method holding it.
42
+ def reach(name)
43
+ if @entries.key?(name)
44
+ enter(name) unless stack.empty?
45
+ else
46
+ innermost(@owners[name])&.step([name])
47
+ end
48
+ end
49
+
50
+ # Notes the decision named decision taking path, a step of the
51
+ # innermost call of the method holding it.
52
+ def decided(decision, path)
53
+ innermost(@owners[decision])&.step([decision, path])
54
+ end
55
+
56
+ # Notes a pass of the loop named loop, a block called with one
57
+ # more item, in the innermost call of the method holding it.
58
+ def pass(loop)
59
+ innermost(@owners[loop])&.passed(loop)
60
+ end
61
+
62
+ # Ends the innermost call under way, counting its way.
63
+ def return
64
+ stack.pop&.finish
65
+ end
66
+
67
+ private
68
+
69
+ # Starts the way of the innermost call under way, of the
70
+ # method whose entry is named entry.
71
+ def enter(entry)
72
+ stack[-1] = Walk.new(entry, @ways, @entries[entry],
73
+ @repeats[entry])
74
+ end
75
+
76
+ # The calls under way in the current thread, innermost last,
77
+ # each the Walk of its way, or nil before it reaches its entry.
78
+ def stack
79
+ Thread.current[@stack_key] ||= []
80
+ end
81
+
82
+ # The Walk of the innermost call under way of the method
83
+ # whose entry is named entry, else nil.
84
+ def innermost(entry)
85
+ stack.reverse_each.find { |walk| walk&.entry == entry }
86
+ end
87
+ end
88
+ end
@@ -4,96 +4,102 @@
4
4
  module PathCoverage
5
5
  # The call rewritten code makes around each operand; returns
6
6
  # value unchanged.
7
- def self.record(site, index, value)
8
- @recorder.record(site, index, value)
7
+ def self.record(decision, index, value)
8
+ @recorder.record(decision, index, value)
9
9
  end
10
10
 
11
- # The call rewritten code makes before a site's first leaf,
12
- # site having size leaves; returns the evaluation's trail.
13
- def self.enter(site, size)
14
- @recorder.enter(site, size)
11
+ # The call rewritten code makes before a decision's first leaf,
12
+ # decision having size leaves; returns the evaluation's trail.
13
+ def self.enter(decision, size)
14
+ @recorder.enter(decision, size)
15
15
  end
16
16
 
17
- # The call rewritten code makes around a site as a whole,
17
+ # The call rewritten code makes around a decision as a whole,
18
18
  # with the trail enter gave it; returns value unchanged.
19
19
  def self.leave(trail, value)
20
20
  @recorder.leave(trail, value)
21
21
  end
22
22
 
23
23
  # The call rewritten code makes around a case's pattern, the
24
- # index-th of size at site; returns the pattern's stand-in.
25
- def self.when_match(site, index, size, pattern)
26
- @recorder.when_match(site, index, size, pattern)
24
+ # index-th of size at decision; returns the pattern's stand-in.
25
+ def self.when_match(decision, index, size, pattern)
26
+ @recorder.when_match(decision, index, size, pattern)
27
27
  end
28
28
 
29
29
  # The call rewritten code makes around the pattern of a case
30
- # nested in site, the site's index-th leaf; returns the
30
+ # nested in decision, the decision's index-th leaf; returns the
31
31
  # pattern's stand-in.
32
- def self.pattern(site, index, pattern)
33
- @recorder.pattern(site, index, pattern)
32
+ def self.pattern(decision, index, pattern)
33
+ @recorder.pattern(decision, index, pattern)
34
34
  end
35
35
 
36
36
  # The call rewritten code makes around the right side of a
37
- # write nested in site, its target the site's at-th leaf,
37
+ # write nested in decision, its target the decision's at-th leaf,
38
38
  # outcome being the target's when it runs; returns value
39
39
  # unchanged.
40
- def self.written_at(site, at, outcome, value)
41
- @recorder.written_at(site, at, outcome, value)
40
+ def self.written_at(decision, at, outcome, value)
41
+ @recorder.written_at(decision, at, outcome, value)
42
42
  end
43
43
 
44
- # The call rewritten code makes around a write nested in site,
45
- # its target the site's at-th leaf, skipped being the target's
44
+ # The call rewritten code makes around a write nested in decision,
45
+ # its target the decision's at-th leaf, skipped being the target's
46
46
  # outcome when its right side never ran; returns value
47
47
  # unchanged.
48
- def self.settle(site, at, skipped, value)
49
- @recorder.settle(site, at, skipped, value)
48
+ def self.settle(decision, at, skipped, value)
49
+ @recorder.settle(decision, at, skipped, value)
50
50
  end
51
51
 
52
52
  # The call rewritten code makes before a block method's call
53
- # nested in site runs, its receiver the site's at-th leaf;
54
- # returns the site's trail.
55
- def self.begin_items(site, at)
56
- @recorder.begin_items(site, at)
53
+ # nested in decision runs, its receiver the decision's at-th leaf;
54
+ # returns the decision's trail.
55
+ def self.begin_items(decision, at)
56
+ @recorder.begin_items(decision, at)
57
57
  end
58
58
 
59
59
  # The call rewritten code makes around the block's expression
60
- # of a stop-early block method nested in site, its receiver the
61
- # site's at-th leaf; returns value unchanged.
62
- def self.item_at(site, at, value)
63
- @recorder.item_at(site, at, value)
60
+ # of a stop-early block method nested in decision, its receiver the
61
+ # decision's at-th leaf; returns value unchanged.
62
+ def self.item_at(decision, at, value)
63
+ @recorder.item_at(decision, at, value)
64
64
  end
65
65
 
66
66
  # The call rewritten code makes around the block's expression
67
- # of an every-item block method nested in site, its receiver
68
- # the site's at-th leaf; returns value unchanged.
69
- def self.answer_at(site, at, value)
70
- @recorder.answer_at(site, at, value)
67
+ # of an every-item block method nested in decision, its receiver
68
+ # the decision's at-th leaf; returns value unchanged.
69
+ def self.answer_at(decision, at, value)
70
+ @recorder.answer_at(decision, at, value)
71
71
  end
72
72
 
73
73
  # The call rewritten code makes around a block method's call
74
- # nested in site, its receiver the site's at-th leaf, with the
75
- # trail begin_items gave it; returns value unchanged.
76
- def self.end_items(site, at, trail, value)
77
- @recorder.end_items(site, at, trail, value)
74
+ # nested in decision, its receiver the decision's at-th leaf,
75
+ # with the trail begin_items gave it; returns value unchanged.
76
+ def self.end_items(decision, at, trail, value)
77
+ @recorder.end_items(decision, at, trail, value)
78
78
  end
79
79
 
80
80
  # The call rewritten code makes around a &. receiver, the
81
- # index-th leaf of site, its chain's leaves starting at first;
81
+ # index-th leaf of decision, its chain's leaves starting at first;
82
82
  # returns value unchanged.
83
- def self.present(site, index, first, value)
84
- @recorder.present(site, index, first, value)
83
+ def self.present(decision, index, first, value)
84
+ @recorder.present(decision, index, first, value)
85
85
  end
86
86
 
87
87
  # The call rewritten code makes around a &. chain's value, the
88
- # index-th leaf of site, its chain's leaves starting at first;
88
+ # index-th leaf of decision, its chain's leaves starting at first;
89
89
  # returns value unchanged.
90
- def self.chained(site, index, first, value)
91
- @recorder.chained(site, index, first, value)
90
+ def self.chained(decision, index, first, value)
91
+ @recorder.chained(decision, index, first, value)
92
92
  end
93
93
 
94
94
  # The call rewritten code makes before each statement that is
95
- # no site, counting its run.
95
+ # no decision, counting its run.
96
96
  def self.reach(name)
97
97
  @recorder.reach(name)
98
98
  end
99
+
100
+ # The call rewritten code makes first in each block body holding
101
+ # a row: a pass of the loop named name.
102
+ def self.pass(name)
103
+ @recorder.pass(name)
104
+ end
99
105
  end
@@ -0,0 +1,104 @@
1
+ require_relative 'card_tree'
2
+ require_relative 'card_ways'
3
+ require_relative 'method_statements'
4
+ require_relative 'way_algebras'
5
+
6
+ module PathCoverage
7
+ # The paths of each def's card rows that its method can take:
8
+ # those on some way through the card, as CardWays 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 CardContext
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 card 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
+ cards = MethodStatements.of(source, path, decisions)
24
+ cards.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 card'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 card's in source: those on some way
47
+ # through the card, 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 = CardWays.card(source, decisions, rows, labelled)
52
+ by_key = labels.group_by(&:first)
53
+ rows.map do |row|
54
+ can = by_key.fetch(CardTree.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
+ cards = MethodStatements.of(source, path, decisions)
81
+ cards.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 card'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,44 @@
1
+ module PathCoverage
2
+ # How the loops of a card fork when loops are decisions, each of
3
+ # forks being { loop's name => [its bytes, its body's] }, as
4
+ # LoopForks gives them: a loop is placed as a row that is no
5
+ # decision is, by its bytes; its body is a region of it run on
6
+ # its path 1, at least one item, so holds its rows; its path 0
7
+ # is no item. Its module functions place it, for CardTree; its
8
+ # instance method folds it, for CardWays, which includes it.
9
+ module CardForks
10
+ # The region of each loop of forks: its body, run on its path
11
+ # 1.
12
+ def self.regions(forks)
13
+ forks.map do |name, (_, body)|
14
+ CardTree::Region.new(owner: CardTree.key_at(name),
15
+ range: body, arm: 'body', on: [1],
16
+ leaf: nil, leaves: nil)
17
+ end
18
+ end
19
+
20
+ # places, { key => its place }, a card's rows', with the place
21
+ # of each loop of forks too, among regions, all in the order
22
+ # they start; places alone with no loop.
23
+ def self.placed(places, forks, regions)
24
+ return places if forks.empty?
25
+
26
+ loops = forks.to_h do |name, (span, _)|
27
+ key = CardTree.key_at(name)
28
+ [key, CardTree.placed(key, span, regions, nil)]
29
+ end
30
+ merged = places.merge(loops)
31
+ merged.sort_by { _1[0].split(':').map(&:to_i) }.to_h
32
+ end
33
+
34
+ # The pair of the loop keyed key, placed but no row: no pass,
35
+ # going on, or at least one, then the rows of its body one
36
+ # after another, walked once.
37
+ def fork_pair(key)
38
+ pairs = running(key, 1).map { pair_of(_1) }
39
+ own = @ways.unit([key, 1])
40
+ some = sequence(pairs).map { @ways.times(own, _1) }
41
+ added([[@ways.unit([key, 0]), @ways.zero], some])
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,125 @@
1
+ require_relative 'gaps'
2
+
3
+ module PathCoverage
4
+ # The terminal worklist of the untested paths through each
5
+ # method: for each method, a heading, then each path no run
6
+ # took, as the choices a test must make to take it.
7
+ module CardGaps
8
+ # The instruction under a method's untested paths.
9
+ UNTESTED = 'Add tests that take each of these paths:'.freeze
10
+ # The instruction under a method with more paths than
11
+ # --max-paths allows.
12
+ TOO_MANY = 'Split it into smaller functions.'.freeze
13
+ # The instruction under the impossible paths a method's calls
14
+ # took.
15
+ DECLARED = 'Remove the # path: impossible comment that ' \
16
+ 'declares it:'.freeze
17
+
18
+ # The lines of cards, { method's key => card }, as a JSON
19
+ # report has them, each choice's leaves named by their text in
20
+ # texts, { decision => [text, ...] }; a decision whose parent,
21
+ # in parents, { decision => its parent decision }, is stepped
22
+ # on the same path is named there, and the parent left out. A
23
+ # method's card listing no gaps, its ways more than max paths
24
+ # allows, is named with what to do, so it fails and never
25
+ # reads as tested; a top card, run by no call, has no lines.
26
+ def self.lines(cards, texts, parents)
27
+ cards.flat_map do |key, card|
28
+ next [] unless card.key?('taken')
29
+
30
+ card_lines(key, card, texts, parents)
31
+ end
32
+ end
33
+
34
+ # The lines of card, the method's at key, as lines gives them.
35
+ def self.card_lines(key, card, texts, parents)
36
+ where = "#{key.split(':')[..-2].join(':')} #{card['name']}()"
37
+ return too_many(where, card) unless card.key?('gaps')
38
+
39
+ named = Choices.new(card, texts, parents)
40
+ gaps = card['gaps']
41
+ taken = card['impossible_taken']
42
+ section("#{where} has #{paths(gaps.size, 'untested ')}.",
43
+ UNTESTED, gaps, named) +
44
+ section("#{where} took #{paths(taken.size, '')} declared " \
45
+ 'impossible.', DECLARED, taken, named)
46
+ end
47
+
48
+ # The lines of card, the method's at where, with more paths
49
+ # than --max-paths allows: how many, and what to do.
50
+ def self.too_many(where, card)
51
+ ["#{where} has #{card['ways']} paths, more than " \
52
+ '--max-paths allows.', TOO_MANY]
53
+ end
54
+
55
+ # heading and instruction, then each of ways numbered, its
56
+ # choices as named gives them; no lines at all when ways is
57
+ # empty.
58
+ def self.section(heading, instruction, ways, named)
59
+ return [] if ways.empty?
60
+
61
+ numbered = ways.each_with_index.map do |way, index|
62
+ " #{index + 1}. #{named.of(way)}"
63
+ end
64
+ [heading, instruction, *numbered]
65
+ end
66
+
67
+ # count paths, of kind, eg "untested ", singular for one.
68
+ def self.paths(count, kind)
69
+ "#{count} #{kind}#{count == 1 ? 'path' : 'paths'}"
70
+ end
71
+
72
+ # The choices of a card's ways, as a test must make them.
73
+ class Choices
74
+ # The choices of card's ways, its rows naming each step's
75
+ # decision, texts and parents as CardGaps.lines has them.
76
+ def initialize(card, texts, parents)
77
+ @decisions = card['rows'].to_h { [_1['at'], _1['decision']] }
78
+ @texts = texts
79
+ @parents = parents
80
+ end
81
+
82
+ # The choices of way, a path's steps, in order: each stepped
83
+ # decision by its line and its leaves' outcomes, but for a
84
+ # parent of another of them; each loop, a step at no row, by
85
+ # its line and how many items it ran; a step with no choice
86
+ # is left out.
87
+ def of(way)
88
+ stepped = []
89
+ way.each { |step| stepped << step if step.key?('path') }
90
+ implied = implied_by(stepped)
91
+ kept = []
92
+ stepped.each do |step|
93
+ decision = @decisions[step['at']]
94
+ kept << choice(step) unless implied.include?(decision)
95
+ end
96
+ kept.join(', then ')
97
+ end
98
+
99
+ # The parent decisions of the decisions stepped steps, each
100
+ # named on its child's step instead; none of a loop's step,
101
+ # which has no decision.
102
+ def implied_by(stepped)
103
+ implied = Set.new
104
+ stepped.each do |step|
105
+ parent = @parents[@decisions[step['at']]]
106
+ implied << parent if parent
107
+ end
108
+ implied
109
+ end
110
+
111
+ private
112
+
113
+ # One step's choice, after its line: a decision's leaves'
114
+ # outcomes, or a loop's items.
115
+ def choice(step)
116
+ line = step['at'].split(':').first
117
+ decision = @decisions[step['at']]
118
+ path = step['path']
119
+ return "line #{line} loop: #{path.first}" if decision.nil?
120
+
121
+ "line #{line} #{Gaps.steps(path, @texts[decision])}"
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,37 @@
1
+ require 'prism'
2
+
3
+ module PathCoverage
4
+ # The scope a top card is named for: the module or class its
5
+ # lines are in, as a def names a method's card.
6
+ module CardScopes
7
+ # The kinds of node that are a top card'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 card's scope, as a def is a
21
+ # method card'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