path_coverage 0.3.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 (103) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +316 -177
  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 +199 -0
  11. data/lib/path_coverage/card_way_lists.rb +72 -0
  12. data/lib/path_coverage/card_ways.rb +196 -0
  13. data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +38 -25
  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} +18 -8
  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/fold_rows.rb +140 -0
  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 +31 -16
  30. data/lib/path_coverage/kernel.rb +56 -57
  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 -17
  34. data/lib/path_coverage/kinds/chain.rb +40 -11
  35. data/lib/path_coverage/kinds/choice.rb +7 -10
  36. data/lib/path_coverage/kinds/contains.rb +43 -33
  37. data/lib/path_coverage/kinds/safe.rb +44 -46
  38. data/lib/path_coverage/kinds/write.rb +6 -12
  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 +94 -59
  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 +52 -29
  48. data/lib/path_coverage/manifest_disabled.rb +97 -0
  49. data/lib/path_coverage/manifest_folding.rb +66 -26
  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 +147 -139
  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 +61 -27
  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 +184 -44
  80. metadata +36 -29
  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 -470
  86. data/lib/path_coverage/html/sort.js +0 -34
  87. data/lib/path_coverage/html/style.css +0 -470
  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 -111
  92. data/lib/path_coverage/html_cells.rb +0 -66
  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 -23
  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 -166
  101. data/lib/path_coverage/html_tabs.rb +0 -26
  102. data/lib/path_coverage/html_template.rb +0 -26
  103. data/lib/path_coverage/html_tree.rb +0 -131
@@ -0,0 +1,52 @@
1
+ module PathCoverage
2
+ # The settings of a PathCoverage.start block that limit a card's
3
+ # ways: the most paths a method may have and still be checked,
4
+ # and whether a loop counts as a decision. It relies on its
5
+ # includer's ConfigurationError, and sets @max_paths and @loops
6
+ # from their defaults first.
7
+ module ConfigurationLimits
8
+ # The most paths a method may have and still be checked, by
9
+ # default.
10
+ MAX_PATHS = 4096
11
+ # The most max_paths may be: 65536 paths keep analysing each
12
+ # method's paths to about 2 seconds and 120 MB of JSON.
13
+ PATHS_CAP = 65_536
14
+
15
+ # Sets each limit to its default: a method with more than
16
+ # MAX_PATHS paths fails, and no loop is a decision.
17
+ def default_limits
18
+ @max_paths = MAX_PATHS
19
+ @loops = false
20
+ end
21
+
22
+ # The most paths a method may have and still be checked, as
23
+ # max_paths sets it: one with more lists none of its ways, and
24
+ # fails.
25
+ def path_limit = @max_paths
26
+
27
+ # Sets the most paths a method may have and still be checked;
28
+ # refuses more than PATHS_CAP.
29
+ def max_paths(count)
30
+ if count > PATHS_CAP
31
+ raise ConfigurationError, cap_refusal(count)
32
+ end
33
+
34
+ @max_paths = count
35
+ end
36
+
37
+ # Why max_paths refuses count.
38
+ def cap_refusal(count)
39
+ "max paths #{count} is more than #{PATHS_CAP}, the cap that " \
40
+ "keeps analysing each method's paths to about 2 seconds " \
41
+ 'and 120 MB of JSON'
42
+ end
43
+
44
+ # True when each loop counts as a decision, 0 items or 1+.
45
+ def loops? = @loops
46
+
47
+ # Counts each loop as a decision: 0 items or 1+ items.
48
+ def loops
49
+ @loops = true
50
+ end
51
+ end
52
+ end
@@ -2,20 +2,20 @@ require_relative 'kinds'
2
2
 
3
3
  module PathCoverage
4
4
  # The settings of a PathCoverage.start block that change which
5
- # paths a site has: ignore, eg ignore '&&=', and treat, eg
5
+ # paths a decision has: ignore, eg ignore '&&=', and treat, eg
6
6
  # treat 'name', as: :present. It relies on its includer's
7
7
  # @ignored, @treats and check.
8
8
  module ConfigurationPaths
9
9
  TREATMENTS = %w[present nonempty].freeze
10
10
 
11
- # The operators whose sites are not measured, chosen with
11
+ # The operators whose decisions are not measured, chosen with
12
12
  # ignore, eg ['&&=']; none when nothing was chosen.
13
13
  attr_reader :ignored
14
14
 
15
- # Chooses operators whose sites are not measured, eg
16
- # ignore '&&=' measures no &&= write; a site inside one, eg
15
+ # Chooses operators whose decisions are not measured, eg
16
+ # ignore '&&=' measures no &&= write; a decision inside one, eg
17
17
  # on its right side, still is. Refuses an operator there
18
- # is no kind of site for.
18
+ # is no kind of decision for.
19
19
  def ignore(*operators)
20
20
  check('operator', operators, Kinds::BY_OPERATOR.keys)
21
21
  @ignored = operators
@@ -43,11 +43,11 @@ module PathCoverage
43
43
  end
44
44
  end
45
45
 
46
- # The settings that change which paths a site has, eg
46
+ # The settings that change which paths a decision has, eg
47
47
  # { 'ignored' => ['&&='],
48
48
  # 'treats' => [['name', 'present', nil]] },
49
49
  # each only when set: runs whose fingerprints differ list
50
- # their sites' paths differently.
50
+ # their decisions' paths differently.
51
51
  def fingerprint
52
52
  print = {}
53
53
  print['ignored'] = @ignored unless @ignored.empty?
@@ -4,46 +4,49 @@ require_relative 'totals'
4
4
  module PathCoverage
5
5
  # The :summary reporter's lines: each total as SimpleCov prints
6
6
  # its line and branch coverage, eg
7
- # "Path coverage within decisions: 519 / 623 (83.31%)". With
7
+ # "Paths alone: 519 / 623 (83.31%)". With
8
8
  # groups configured each group is a heading, its own totals
9
9
  # indented under it, and there is no total over every group.
10
10
  module ConsoleSummary
11
- # The summary lines of site_paths and, when cards, { def
12
- # site => [taken, size] }, are given, as they are when
11
+ # The summary lines of decision_paths and, when cards, { def
12
+ # decision => [taken, size] }, are given, as they are when
13
13
  # methods are measured, of cards; groups is { name =>
14
14
  # [glob, ...] }.
15
- def self.lines(site_paths, cards, groups = {})
16
- return totals_lines(totals(site_paths, cards)) if groups.empty?
15
+ def self.lines(decision_paths, cards, groups = {})
16
+ if groups.empty?
17
+ return totals_lines(totals(decision_paths,
18
+ cards))
19
+ end
17
20
 
18
- all = group_totals(site_paths, cards, groups)
21
+ all = group_totals(decision_paths, cards, groups)
19
22
  all.flat_map do |name, totals|
20
23
  ["#{name}:", *totals_lines(totals).map { |line| " #{line}" }]
21
24
  end
22
25
  end
23
26
 
24
- # { 'decisions' => [taken, possible] } of site_paths, with
25
- # 'methods' => [taken, possible] of cards when given.
26
- def self.totals(site_paths, cards)
27
- totals = { 'decisions' => Totals.decisions(site_paths) }
27
+ # { 'alone' => [taken, possible] } of decision_paths, with
28
+ # 'combined' => [taken, possible] of cards when given.
29
+ def self.totals(decision_paths, cards)
30
+ totals = { 'alone' => Totals.alone(decision_paths) }
28
31
  return totals if cards.nil?
29
32
 
30
- totals.merge('methods' => Totals.methods(cards))
33
+ totals.merge('combined' => Totals.combined(cards))
31
34
  end
32
35
 
33
- # { group name => totals } of each group's own sites and
36
+ # { group name => totals } of each group's own decisions and
34
37
  # cards alone.
35
- def self.group_totals(site_paths, cards, groups)
38
+ def self.group_totals(decision_paths, cards, groups)
36
39
  groups.to_h do |name, globs|
37
40
  own_cards = cards ? own(cards, globs) : nil
38
- [name, totals(own(site_paths, globs), own_cards)]
41
+ [name, totals(own(decision_paths, globs), own_cards)]
39
42
  end
40
43
  end
41
44
 
42
- # The entries of sites, keyed by site name, in a file matching
43
- # any of globs.
44
- def self.own(sites, globs)
45
- sites.select do |site, _|
46
- Summary.matches?(Summary.path_of(site), globs)
45
+ # The entries of decisions, keyed by decision name, in a file
46
+ # matching any of globs.
47
+ def self.own(decisions, globs)
48
+ decisions.select do |decision, _|
49
+ Summary.matches?(Summary.path_of(decision), globs)
47
50
  end
48
51
  end
49
52
 
@@ -55,11 +58,10 @@ module PathCoverage
55
58
  ["Path coverage ignores: #{ignored.join(', ')}"]
56
59
  end
57
60
 
58
- # A line per total, eg "Path coverage within methods: 1 / 4
59
- # (25.00%)".
61
+ # A line per total, eg "Paths combined: 1 / 4 (25.00%)".
60
62
  def self.totals_lines(totals)
61
63
  totals.map do |name, pair|
62
- "Path coverage within #{name}: #{figure(pair)}"
64
+ "Paths #{name}: #{figure(pair)}"
63
65
  end
64
66
  end
65
67
 
@@ -1,7 +1,7 @@
1
1
  module PathCoverage
2
2
  # Names a condition by where it starts in its file, so the
3
- # rewriter and the manifest agree on which site is which.
4
- module Site
3
+ # rewriter and the manifest agree on which decision is which.
4
+ module Decision
5
5
  # "path:line:column" of node, eg "k.rb:2:2".
6
6
  def self.of(path, node)
7
7
  at(path, node.location)
@@ -0,0 +1,76 @@
1
+ require_relative 'condition'
2
+
3
+ module PathCoverage
4
+ # Where in its source each part of a decision's tree is, as
5
+ # [line, column, line, column], so a page can light the code
6
+ # a fold row stands for.
7
+ module DecisionLocations
8
+ # simplecov:disable
9
+ # SPIKE: tree with an at: of [line, column, line, column]
10
+ # on it and on each of its operands: a leaf's from its byte
11
+ # range, a condition's and an arm's from its extent, the
12
+ # whole of it as written. Columns count characters, not
13
+ # bytes. A node past the condition's operands, eg owner at
14
+ # the end of user&.account&.owner, is kept as it is.
15
+ def self.located(source, condition, tree)
16
+ pairs = condition.operands.zip(tree[:operands])
17
+ operands = pairs.map do |operand, node|
18
+ located_operand(source, operand, node)
19
+ end
20
+ rest = tree[:operands].drop(pairs.size)
21
+ tree.merge(at: at(source, condition.extent),
22
+ operands: operands + rest)
23
+ end
24
+
25
+ # SPIKE: the node of operand with its at:, as located gives
26
+ # a condition's, an arm's or a leaf's.
27
+ def self.located_operand(source, operand, node)
28
+ case operand
29
+ when Condition then located(source, operand, node)
30
+ when Arm then located_arm(source, operand, node)
31
+ else node.merge(at: at(source, operand))
32
+ end
33
+ end
34
+
35
+ # An arm's node with its at:, nil for an arm with no
36
+ # statements, and its body's tree located.
37
+ def self.located_arm(source, arm, node)
38
+ body = arm.body ? located(source, arm.body, node[:body]) : nil
39
+ at = arm.extent ? at(source, arm.extent) : nil
40
+ node.merge(at:, body:)
41
+ end
42
+
43
+ # [line, column, line, column] of the byte range in source.
44
+ def self.at(source, range)
45
+ [*position(source, range.begin), *position(source, range.end)]
46
+ end
47
+
48
+ # [line, column] of a byte offset in source.
49
+ def self.position(source, offset)
50
+ before = source.byteslice(0, offset)
51
+ newline = before.rindex("\n")
52
+ column = before.length - (newline ? newline + 1 : 0)
53
+ [before.count("\n") + 1, column]
54
+ end
55
+ # simplecov:enable
56
+
57
+ # [line, column, line, column] of condition's else keyword in
58
+ # source, so hovering it can open condition's fold, as
59
+ # hovering its if does; nil for one with no else, or one whose
60
+ # else has no code, or a ?:, whose else is no keyword. The
61
+ # keyword is what comes just before its arm's code, so an
62
+ # elsif, or an else with a comment after it, has none.
63
+ def self.else_at(source, condition)
64
+ return nil if condition.operator == '?:'
65
+
66
+ arm = condition.operands.grep(Arm).to_h { [_1.kind, _1] }[:else]
67
+ extent = arm&.extent
68
+ return nil unless extent
69
+
70
+ start = source.b.byteslice(0, extent.begin) =~ /\belse\s*\z/
71
+ return nil unless start
72
+
73
+ at(source, start...(start + 4))
74
+ end
75
+ end
76
+ end
@@ -2,23 +2,33 @@ require_relative 'condition'
2
2
  require_relative 'kinds'
3
3
 
4
4
  module PathCoverage
5
- # The paths an evaluation of a site can take, worked out
5
+ # The paths an evaluation of a decision can take, worked out
6
6
  # from its condition alone: the outcome of each leaf, in leaf
7
7
  # order, nil for a leaf that evaluation never reaches.
8
- module SitePaths
9
- # Every possible path through site's condition, true before
10
- # false, as its kind gives the ways a site of it can be
8
+ module DecisionPaths
9
+ # Every possible path through decision's condition, true before
10
+ # false, as its kind gives the ways a decision of it can be
11
11
  # evaluated.
12
- def self.possible(site)
13
- ways = Kinds.of(site.operator).root_ways(site, site.fixed)
12
+ def self.possible(decision)
13
+ ways = Kinds.of(decision.operator).root_ways(decision,
14
+ decision.fixed)
14
15
  ways.map do |outcomes, _value|
15
- site.leaves.map { |range| outcomes[range] }
16
+ decision.leaves.map { |range| outcomes[range] }
16
17
  end
17
18
  end
18
19
 
20
+ # Example: [false, true, false, true, false] for an any?.
21
+ # decision's value on each of its possible paths, in the order
22
+ # possible gives them, nil where not known.
23
+ def self.values(decision)
24
+ ways = Kinds.of(decision.operator).root_ways(decision,
25
+ decision.fixed)
26
+ ways.map(&:last)
27
+ end
28
+
19
29
  # [[{ leaf range => outcome }, value], ...] for each way
20
30
  # node can be evaluated, value being the outcome of node as
21
- # a whole, as its kind gives them; fixed is the site's
31
+ # a whole, as its kind gives them; fixed is the decision's
22
32
  # { leaf range => its one outcome }.
23
33
  def self.walk(node, fixed)
24
34
  return leaf(node, fixed) unless node.is_a?(Condition)
@@ -2,10 +2,10 @@ require_relative 'condition'
2
2
  require_relative 'kinds'
3
3
 
4
4
  module PathCoverage
5
- # The tree shown under a site's line on its file's page: a
6
- # grid whose columns are the site's paths, never taken first.
7
- module SiteTree
8
- # { operator:, counts:, cells:, operands: } for one site,
5
+ # The tree shown under a decision's line on its file's page: a
6
+ # grid whose columns are the decision's paths, never taken first.
7
+ module DecisionTree
8
+ # { operator:, counts:, cells:, operands: } for one decision,
9
9
  # from its paths, [{path:, count:}, ...]. counts are the
10
10
  # columns' counts. Every node has cells, one per column: a
11
11
  # leaf { text:, cells: } its outcome, nil where unreached;
@@ -18,13 +18,20 @@ module PathCoverage
18
18
  columns = paths.sort_by.with_index do |taken, i|
19
19
  [taken[:count].zero? ? 0 : 1, i]
20
20
  end
21
- outcomes = columns.map { |taken| taken[:path] }
22
- tree = node(source, condition, outcomes, (0..).each)
23
- tree = tree.merge(label: 'elsif') if elsif?(source, condition)
21
+ tree = built(source, condition, columns.map { _1[:path] })
24
22
  tree.merge(counts: columns.map { |taken| taken[:count] })
25
23
  end
26
24
 
27
- # True for an if site written as an elsif: an if's extent
25
+ # The tree of condition with no counts, a column for each of
26
+ # outcomes, each path's outcomes, in the order given.
27
+ def self.built(source, condition, outcomes)
28
+ tree = node(source, condition, outcomes, (0..).each)
29
+ return tree unless elsif?(source, condition)
30
+
31
+ tree.merge(label: 'elsif')
32
+ end
33
+
34
+ # True for an if decision written as an elsif: an if's extent
28
35
  # starts at its keyword.
29
36
  def self.elsif?(source, condition)
30
37
  condition.operator == 'if' &&
@@ -52,15 +59,17 @@ module PathCoverage
52
59
  Kinds.of(condition.operator).texted(source, condition, nodes)
53
60
  end
54
61
 
55
- # condition's node from its operands' nodes, its ternary
56
- # arms built in order after its predicate, as each arm's
57
- # cells follow from the predicate's.
58
- def self.arms(source, condition, operands, outcomes, leaves)
59
- predicate = operands.first
60
- operands = operands.map do |operand|
62
+ # condition's node from its operands' nodes, its arms built in
63
+ # order, as each arm's cells follow from those of the operand
64
+ # that decides it: the last operand before it that is no arm,
65
+ # eg a ternary's predicate, or x in x or return t.
66
+ def self.arms(source, condition, built, outcomes, leaves)
67
+ then_on = THEN_ON[condition.operator]
68
+ operands = built.each_with_index.map do |operand, index|
61
69
  next operand unless operand.is_a?(Arm)
62
70
 
63
- decided = [predicate[:cells], THEN_ON[condition.operator]]
71
+ deciding = built.take(index).grep_v(Arm).last
72
+ decided = [deciding[:cells], then_on]
64
73
  arm(source, operand, decided, outcomes, leaves)
65
74
  end
66
75
  operator = condition.operator
@@ -82,7 +91,7 @@ module PathCoverage
82
91
  picked = arm.kind == :then ? then_on : !then_on
83
92
  body = arm.body ? node(source, arm.body, outcomes, leaves) : nil
84
93
  {
85
- arm: arm.kind, text: text_of(source, arm),
94
+ arm: arm.kind, text: text_of(source, arm), leaves: arm.leaves,
86
95
  cells: decided.map { |outcome| outcome == picked }, body:
87
96
  }
88
97
  end
@@ -0,0 +1,18 @@
1
+ module PathCoverage
2
+ # The branches of a decision's tree on a file's page, joining each
3
+ # operand's row to its operator's. A branch is a string of
4
+ # kinds, one letter per cell: t a tee, l an ell, v a bar, h a
5
+ # dash, s a space. The page draws each kind as lines a whole
6
+ # row tall, so a branch joins the rows above and below it.
7
+ module FoldBranches
8
+ # The branch to an operand with more after it.
9
+ TEE = 'ths'.freeze
10
+ # The branch to the last operand.
11
+ ELL = 'lhs'.freeze
12
+ # The indent under an operand with more after it, its bar
13
+ # running on down to them.
14
+ BAR = 'vss'.freeze
15
+ # The indent under the last operand.
16
+ GAP = 'sss'.freeze
17
+ end
18
+ end
@@ -0,0 +1,42 @@
1
+ require_relative 'kinds'
2
+
3
+ module PathCoverage
4
+ # The cells of a row of a decision's tree on a file's page, one per
5
+ # path, as page data holds them. A cell says what its path did:
6
+ # its text a letter, eg T or F, or empty with a mark the page
7
+ # draws, eg a tick; with text to show on hover when its mark
8
+ # alone may not say enough.
9
+ module FoldCells
10
+ # [text, mark, hover] of a leaf's or a condition's cell, by
11
+ # the value it took on the path: a letter for each value
12
+ # but a &. receiver's, which is a mark, present or absent,
13
+ # hovering as not nil or nil.
14
+ OUTCOMES = {
15
+ true => ['T'], false => ['F'], nil => [''],
16
+ ITEMS[0] => ['0'], ITEMS[1] => ['1'], ITEMS[2] => ['*'],
17
+ ANSWERS[0] => ['T'], ANSWERS[1] => ['F'], ANSWERS[2] => ['TF'],
18
+ PRESENCE[0] => ['', 'present', PRESENCE[0]],
19
+ PRESENCE[1] => ['', 'absent', PRESENCE[1]]
20
+ }.freeze
21
+
22
+ # [text, mark] of a ternary's arm's cell: taken on a path
23
+ # that took it, else blank.
24
+ TAKEN = { true => ['', 'taken'], false => [''] }.freeze
25
+
26
+ # [text, mark] of the cell of an arm that returns or raises:
27
+ # leaves on a path that took it, so left the method there,
28
+ # else blank.
29
+ LEAVES = { true => ['', 'leaves'], false => [''] }.freeze
30
+
31
+ # Example: true and false give ['T', nil, nil], ['F', nil, nil].
32
+ # The cells of values, as page data holds them, each [text,
33
+ # mark, hover] as names gives it, nil for a mark or hover it
34
+ # has none of.
35
+ def self.values(values, names = OUTCOMES)
36
+ values.map do |value|
37
+ text, mark, hover = names[value]
38
+ [text, mark, hover]
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,140 @@
1
+ require_relative 'condition'
2
+ require_relative 'code_tokens'
3
+ require_relative 'fold_branches'
4
+ require_relative 'fold_cells'
5
+ require_relative 'kinds'
6
+
7
+ module PathCoverage
8
+ # The tree of a decision, from DecisionTree.of, as its row's
9
+ # fold in a card shows it: a grid whose columns are the
10
+ # decision's paths, as page data holds its rows, each a kind,
11
+ # a branch, a label, where its code is and a cell per path.
12
+ module FoldRows
13
+ # The rows of decision's fold, as page data holds them:
14
+ # decision's own row, naming its operator and each path's
15
+ # count, and where its else keyword is, if it has one; then
16
+ # the rows of its operands.
17
+ def self.rows(decision)
18
+ text = decision.fetch(:label) { decision[:operator] }
19
+ label = CodeTokens.label_code(text)
20
+ own = { kind: 'decision', branch: '', label:, at: decision[:at],
21
+ else: decision[:else_at], counts: decision[:counts] }
22
+ [own, *operand_rows(decision, '')]
23
+ end
24
+
25
+ # The rows of a condition's operands, each branching from
26
+ # its operator after indent; a nested condition's operands,
27
+ # and those of an arm's condition, follow its own row,
28
+ # indented under its branch. A leaf with decisions inside it
29
+ # branches as those decisions, then the leaf, in the order they
30
+ # run.
31
+ def self.operand_rows(condition, indent)
32
+ own = Kinds.of(condition[:operator]).rows(condition, indent)
33
+ return own if own
34
+
35
+ operands = unfolded(shown(condition))
36
+ last = operands.size - 1
37
+ operands.each_with_index.flat_map do |operand, i|
38
+ tip = i == last
39
+ branch = indent + (tip ? FoldBranches::ELL : FoldBranches::TEE)
40
+ under = indent + (tip ? FoldBranches::GAP : FoldBranches::BAR)
41
+ node_rows(operand, branch, under)
42
+ end
43
+ end
44
+
45
+ # The rows of one operand of a condition, branching after
46
+ # branch, any operands of its own indented to under.
47
+ def self.node_rows(operand, branch, under)
48
+ return arm_rows(operand, branch, under) if operand.key?(:arm)
49
+ return [leaves_row(operand, branch)] if operand.key?(:leaves)
50
+ return [operand_row(operand, branch)] unless operand[:operator]
51
+
52
+ [subtree_row(operand, branch), *operand_rows(operand, under)]
53
+ end
54
+
55
+ # The row of an arm that leaves the method, as leaving gives
56
+ # it: return or raise, coloured as code, marking each path
57
+ # that leaves there, placed at its code.
58
+ def self.leaves_row(node, branch)
59
+ cells = FoldCells.values(node[:cells], FoldCells::LEAVES)
60
+ label = CodeTokens.code(node[:leaves])
61
+ { kind: 'leaves', branch:, label:, at: node[:at], cells: }
62
+ end
63
+
64
+ # A ternary's arm's row, ? for then and : for else: an arm
65
+ # holding a condition showing the condition's operator and
66
+ # value, its operands below; a plain arm its text, marking
67
+ # the paths taking it.
68
+ def self.arm_rows(arm, branch, under)
69
+ body = arm[:body]
70
+ label = CodeTokens.ternary_arm_code(Kinds::Choice.arm_label(arm))
71
+ row = { kind: 'arm', branch:, label:, at: arm[:at],
72
+ cells: arm_cells(arm, body) }
73
+ body ? [row, *operand_rows(body, under)] : [row]
74
+ end
75
+
76
+ # The cells of arm's row: its body's values, when its body is
77
+ # a condition; else a tick on each path that took it.
78
+ def self.arm_cells(arm, body)
79
+ return FoldCells.values(body[:cells]) if body
80
+
81
+ FoldCells.values(arm[:cells], FoldCells::TAKEN)
82
+ end
83
+
84
+ # The operands of condition a card's fold shows: a ?:'s arms,
85
+ # as its arms are part of its one expression; for any other,
86
+ # eg an if, a ||= or a case, no arm, as which arm a path takes
87
+ # follows from its leaves, but each arm's body, eg a when's
88
+ # patterns, which are leaves of its own, and, for an arm that
89
+ # returns or raises, where paths leave the method.
90
+ def self.shown(condition)
91
+ operands = condition[:operands]
92
+ return operands if condition[:operator] == '?:'
93
+
94
+ operands.flat_map do |operand|
95
+ next [operand] unless operand.key?(:arm)
96
+
97
+ [operand[:body], leaving(operand)].compact
98
+ end
99
+ end
100
+
101
+ # { leaves: return or raise, cells:, at: } for arm when its
102
+ # code returns or raises, cells marking the paths that took
103
+ # it, so left the method, at where its code is, so hovering it
104
+ # lights the row; nil for an arm that does neither.
105
+ def self.leaving(arm)
106
+ word = arm[:leaves]
107
+ word && { leaves: word, cells: arm[:cells], at: arm[:at] }
108
+ end
109
+
110
+ # operands with each node standing for the nodes its kind
111
+ # unfolds it into, eg a leaf with decisions inside it for those
112
+ # decisions, then the leaf; a leaf or an arm for itself.
113
+ def self.unfolded(operands)
114
+ operands.flat_map do |operand|
115
+ operator = operand[:operator]
116
+ operator ? Kinds.of(operator).unfold(operand) : [operand]
117
+ end
118
+ end
119
+
120
+ # A nested condition's row: its branch, its operator, or its
121
+ # label when it has one, eg select { for a decision inside a leaf,
122
+ # and its value on each path.
123
+ def self.subtree_row(condition, branch)
124
+ text = condition.fetch(:label) { condition[:operator] }
125
+ cells = FoldCells.values(condition[:cells])
126
+ { kind: 'subtree', branch:, label: CodeTokens.label_code(text),
127
+ at: condition[:at], cells: }
128
+ end
129
+
130
+ # One operand's row: its branch, text, coloured as the code it
131
+ # is, written over more than one line read as one, each line
132
+ # break and the indent around it one space, and its outcome on
133
+ # each path.
134
+ def self.operand_row(operand, branch)
135
+ one_line = operand[:text].to_s.gsub(/\s*\n\s*/, ' ')
136
+ { kind: 'operand', branch:, label: CodeTokens.code(one_line),
137
+ at: operand[:at], cells: FoldCells.values(operand[:cells]) }
138
+ end
139
+ end
140
+ end
@@ -1,32 +1,50 @@
1
1
  module PathCoverage
2
- # The terminal worklist: one line per path a site never
3
- # took, and nothing for a site that took them all. Every
2
+ # The terminal worklist: one line per path a decision never
3
+ # took, and nothing for a decision that took them all. Every
4
4
  # count stays in the operands and paths for reporters that
5
5
  # show them all.
6
6
  module Gaps
7
7
  # The gap lines of operands, paths and outside, as Result
8
- # gives them, in manifest order; texts is { site => each
9
- # leaf's source text }. A site's first leaf runs before any
10
- # other, so a site whose first never ran was never reached.
8
+ # gives them, in manifest order; texts is { decision => each
9
+ # leaf's source text }. A decision's first leaf runs before any
10
+ # other, so a decision whose first never ran was never reached.
11
11
  def self.lines(operands, paths: {}, texts: {}, outside: {})
12
- operands.flat_map do |site, counts|
13
- next ["#{site} never reached"] if never_ran?(counts.first)
12
+ operands.flat_map do |decision, counts|
13
+ next ["#{decision} never reached"] if never_ran?(counts.first)
14
14
 
15
- path_gaps(site, paths.fetch(site, []), texts[site]) +
16
- ruled_out(site, outside.fetch(site, {}), texts[site])
15
+ path_gaps(decision, paths.fetch(decision, []),
16
+ texts[decision]) +
17
+ ruled_out(decision, outside.fetch(decision, {}),
18
+ texts[decision])
17
19
  end
18
20
  end
19
21
 
20
- # One line per path site took that a treatment rules out,
22
+ # One line per path decision took that a treatment rules out,
21
23
  # as Result#outside gives them, texts being each leaf's
22
24
  # source text.
23
- def self.ruled_out(site, taken, texts)
25
+ def self.ruled_out(decision, taken, texts)
24
26
  taken.keys.map do |path|
25
- "#{site} path taken that a treatment rules out: " \
27
+ "#{decision} path taken that a treatment rules out: " \
26
28
  "#{steps(path, texts)}"
27
29
  end
28
30
  end
29
31
 
32
+ # Example: ["k.rb:5:11 path taken that its method rules out:
33
+ # `a` true"].
34
+ # One line per path taken, or return or raise reached, that
35
+ # its method rules out, ruled as CardContext.ruled_out gives
36
+ # them, texts being { decision => each leaf's source text }.
37
+ # Such a line shows a fact derived from the code to be wrong.
38
+ def self.ruled_by_method(ruled, texts)
39
+ ruled.map do |name, path|
40
+ reached = "#{name} reached, though its method rules it out"
41
+ next reached if path.nil?
42
+
43
+ "#{name} path taken that its method rules out: " \
44
+ "#{steps(path, texts[name])}"
45
+ end
46
+ end
47
+
30
48
  # The outcome of each leaf path reaches, each named by its
31
49
  # text, eg "`u.name` nil". A path's first leaf always has an
32
50
  # outcome, so it always has a step.
@@ -36,15 +54,15 @@ module PathCoverage
36
54
  end.join(', ')
37
55
  end
38
56
 
39
- # One line per path site never took, as Result#paths gives
57
+ # One line per path decision never took, as Result#paths gives
40
58
  # them, naming the outcome of each leaf the path reaches,
41
59
  # texts being each leaf's source text.
42
- def self.path_gaps(site, paths, texts)
60
+ def self.path_gaps(decision, paths, texts)
43
61
  paths.each_with_object([]) do |each, lines|
44
62
  next unless each[:count].zero?
45
63
 
46
64
  named = steps(each[:path], texts)
47
- lines << "#{site} path never taken: #{named}"
65
+ lines << "#{decision} path never taken: #{named}"
48
66
  end
49
67
  end
50
68