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
@@ -0,0 +1,76 @@
1
+ require_relative 'condition'
2
+
3
+ module PathCoverage
4
+ # The part of the manifest's visitor that builds the sites of
5
+ # safe-navigation chains, eg user&.account&.owner. It relies
6
+ # on its includer's extent_of.
7
+ module ManifestChains
8
+ # The calls of node's chain, outermost first, eg owner then
9
+ # account for user&.account&.owner; none for a node that is
10
+ # no call.
11
+ def chain_calls(node)
12
+ calls = []
13
+ while node&.type == :call_node
14
+ calls << node
15
+ node = node.receiver
16
+ end
17
+ calls
18
+ end
19
+
20
+ # True for a call whose chain has a &. in it, eg
21
+ # user&.name.upcase.
22
+ def safe_chain?(node)
23
+ chain_calls(node).any?(&:safe_navigation?)
24
+ end
25
+
26
+ # A call that is a site as a Condition: a &. chain's, or a
27
+ # block method call's. It relies on its includer's
28
+ # block_condition.
29
+ def call_condition(node)
30
+ return block_condition(node) unless safe_chain?(node)
31
+
32
+ chain_condition(node, value: false)
33
+ end
34
+
35
+ # A &. chain as a Condition named &.: its leaves are the
36
+ # receivers of its &. calls, innermost first, each nil or
37
+ # not nil, and, with value, the whole chain's value last, as
38
+ # when it is an operand of an && or an if's predicate. Any
39
+ # site in a call's arguments or block, or in the chain's
40
+ # first receiver, is a site of its own. A chain that is no
41
+ # bare argument, eg a command call, is noted among the
42
+ # site's commands, to be parenthesised.
43
+ def chain_condition(node, value:)
44
+ calls = chain_calls(node)
45
+ visit_chain(calls)
46
+ safe = calls.select(&:safe_navigation?).reverse
47
+ receivers = safe.map { |call| receiver_of(call) }
48
+ extent = extent_of(node)
49
+ @notes[:commands] << extent if Manifest.command?(node)
50
+ operands = value ? [*receivers, extent] : receivers
51
+ Condition.new(operator: SAFE, operands:, extent:)
52
+ end
53
+
54
+ # The byte range of a &. call's receiver, noted among its
55
+ # site's fixed leaves as :present, never nil, when it is a
56
+ # call to a method treated as present.
57
+ def receiver_of(call)
58
+ receiver = call.receiver
59
+ range = extent_of(receiver)
60
+ named = receiver.type == :call_node &&
61
+ @present.include?(receiver.name.to_s)
62
+ @notes[:fixed][range] = :present if named
63
+ range
64
+ end
65
+
66
+ # Visits the sites inside a chain's calls' arguments and
67
+ # blocks, and inside its first receiver, eg (a || b) in
68
+ # (a || b)&.size, but never a call of the chain itself.
69
+ def visit_chain(calls)
70
+ calls.each do |call|
71
+ [call.arguments, call.block].compact.each { _1.accept(self) }
72
+ end
73
+ calls.last.receiver&.accept(self)
74
+ end
75
+ end
76
+ end
@@ -11,10 +11,12 @@ module PathCoverage
11
11
  # being an if_node with no if keyword.
12
12
  CHOICE_TYPES = %i[if_node unless_node].freeze
13
13
 
14
- # Where node's site starts: an if's or an unless's keyword,
15
- # which a modifier's statement comes before; anything
16
- # else's own start.
14
+ # Where node's site starts: a &. chain's start; a block
15
+ # method's call at its method; an if's or an unless's
16
+ # keyword, which a modifier's statement comes before;
17
+ # anything else's own start.
17
18
  def site_loc(node)
19
+ return node.location if safe_chain?(node)
18
20
  return node.message_loc if node.type == :call_node
19
21
  return node.location unless CHOICE_TYPES.include?(node.type)
20
22
  return node.location if ternary?(node)
@@ -69,11 +71,11 @@ module PathCoverage
69
71
  end
70
72
 
71
73
  # An if's arm, statements its code, which never folds into
72
- # the if's site; any site inside it is a site of its own.
73
- # An arm with no statements has no extent.
74
+ # the if's site; as a new scope, any site inside it is a site
75
+ # of its own. An arm with no statements has no extent.
74
76
  def statement_arm(kind, statements)
75
- statements&.accept(self)
76
- extent = statements && extent_of(statements)
77
+ scoped { statements&.accept(self) }
78
+ extent = statements ? extent_of(statements) : nil
77
79
  Arm.new(kind:, extent:, body: nil)
78
80
  end
79
81
 
@@ -85,7 +87,7 @@ module PathCoverage
85
87
  return [statement_arm(:else, subsequent.statements)]
86
88
  end
87
89
 
88
- subsequent.accept(self)
90
+ scoped { subsequent.accept(self) }
89
91
  extent = extent_of(subsequent)
90
92
  [Arm.new(kind: :else, extent:, body: nil)]
91
93
  end
@@ -100,19 +102,26 @@ module PathCoverage
100
102
  end
101
103
 
102
104
  # One arm of a ternary, statements its code: a condition
103
- # folds in as its body, being part of the one expression;
104
- # any site inside a plain arm is a site of its own.
105
+ # folds in as its body, being part of the one expression, as
106
+ # does a plain arm with sites inside it, its value its last
107
+ # leaf; a plain arm with none has no body.
105
108
  def arm_of(kind, statements)
106
109
  written = grouped(statements.body.first)
107
- body = condition_of(written) if foldable?(written)
108
- statements.accept(self) if body.nil?
109
- Arm.new(kind:, extent: extent_of(statements), body:)
110
+ extent = extent_of(statements)
111
+ body = if foldable?(written)
112
+ condition_of(written)
113
+ else
114
+ inner = collected { statements.accept(self) }
115
+ inner.empty? ? nil : contained(inner, extent)
116
+ end
117
+ Arm.new(kind:, extent:, body:)
110
118
  end
111
119
 
112
- # True for a condition that folds into a ternary's site:
113
- # an &&, ||, and, or, or a ternary.
120
+ # True for a condition that folds into a ternary's site: a
121
+ # ternary, never ignored, as the ternary it folds into is
122
+ # not; or an &&, ||, and or or not ignored.
114
123
  def foldable?(node)
115
- operator?(node) || ternary?(node)
124
+ ternary?(node) || (operator?(node) && !ignored?(node))
116
125
  end
117
126
 
118
127
  # True for a ternary, an if with no if keyword.
@@ -121,10 +130,15 @@ module PathCoverage
121
130
  end
122
131
 
123
132
  # A ternary's or an if's predicate as its operand: an && or
124
- # || nested as a Condition, anything else a leaf.
133
+ # || nested as a Condition, as is a &. chain, its value its
134
+ # last leaf, anything else a leaf.
125
135
  def predicate_of(node)
126
- predicate = node.predicate
136
+ predicate = grouped(node.predicate)
137
+ return leaf(predicate) if ignored?(predicate)
127
138
  return condition_of(predicate) if operator?(predicate)
139
+ if safe_chain?(predicate)
140
+ return chain_condition(predicate, value: true)
141
+ end
128
142
 
129
143
  leaf(predicate)
130
144
  end
@@ -0,0 +1,91 @@
1
+ require_relative 'condition'
2
+
3
+ module PathCoverage
4
+ # The part of the manifest's visitor that folds every site in one
5
+ # expression into one site. While a leaf is visited, each site
6
+ # found in it, the leaf itself included, is collected as a nested
7
+ # Condition of that leaf, rather than noted as a site of its own.
8
+ # A new scope, eg a block's body or an if's arm, stops the
9
+ # collecting, so a site in it is a site of its own. It relies on
10
+ # its includer's condition_of, chain_condition, safe_chain? and
11
+ # extent_of.
12
+ module ManifestFolding
13
+ # node's Condition as a site nested in a leaf: a &. chain's
14
+ # with its value as its last leaf, as its leaf's outcome
15
+ # follows from it.
16
+ def nested_condition_of(node)
17
+ return chain_condition(node, value: true) if safe_chain?(node)
18
+
19
+ condition_of(node)
20
+ end
21
+
22
+ # The Conditions of the sites found while yielding, each one
23
+ # collected rather than noted as a site of its own.
24
+ def collected
25
+ outer = @inner
26
+ @inner = []
27
+ yield
28
+ @inner
29
+ ensure
30
+ @inner = outer
31
+ end
32
+
33
+ # Yields with no collecting, so a site found is one of its own:
34
+ # for a new scope, eg a block's body or an if's arm.
35
+ def scoped
36
+ outer = @inner
37
+ @inner = nil
38
+ yield
39
+ ensure
40
+ @inner = outer
41
+ end
42
+
43
+ # range as a leaf, or, with inner sites found in it, a Condition
44
+ # named contains: those sites, then range, the leaf's outcome,
45
+ # fixed as :nonempty when nonempty. A leaf that is wholly one
46
+ # block method's call whose value follows from its path is
47
+ # that call alone, needing no outcome of its own.
48
+ def contained(inner, range, nonempty: false)
49
+ return range if inner.empty?
50
+
51
+ only = inner.first
52
+ if inner.size == 1 && only.extent == range &&
53
+ VALUED.include?(only.operator)
54
+ return only
55
+ end
56
+
57
+ fixed = nonempty ? { range => :nonempty } : {}
58
+ Condition.new(operator: CONTAINS, operands: [*inner, range],
59
+ extent: range, fixed:)
60
+ end
61
+
62
+ # True for a call, with no arguments or block, to a method
63
+ # treated as nonempty, eg any?, whose receiver, parenthesised
64
+ # or not, is one of inner, the sites found in it: its outcome
65
+ # then follows from that site's path. It relies on its
66
+ # includer's grouped.
67
+ def nonempty_call?(node, inner)
68
+ return false unless node.type == :call_node
69
+ return false if node.arguments || node.block || !node.receiver
70
+
71
+ receiver = extent_of(grouped(node.receiver))
72
+ @nonempty.include?(node.name.to_s) &&
73
+ inner.map(&:extent).include?(receiver)
74
+ end
75
+
76
+ # A block's body is a new scope: it runs once per item.
77
+ def visit_block_node(node)
78
+ scoped { super }
79
+ end
80
+
81
+ # A lambda's body is a new scope: it runs when called.
82
+ def visit_lambda_node(node)
83
+ scoped { super }
84
+ end
85
+
86
+ # A def's body is a new scope: it runs when called.
87
+ def visit_def_node(node)
88
+ scoped { super }
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,35 @@
1
+ module PathCoverage
2
+ # The part of the manifest's visitor that leaves out the sites
3
+ # of ignored operators, eg ignore '&&='. An ignored node is no
4
+ # site, though a site inside it may be; nested in a site, it
5
+ # is a leaf. It relies on its includer's @ignored, ternary?
6
+ # and safe_chain?.
7
+ module ManifestIgnoring
8
+ # The operator of each node type that always names one kind
9
+ # of site.
10
+ NAMED = { unless_node: 'unless', case_node: 'case' }.freeze
11
+
12
+ # True for a node whose operator is ignored.
13
+ def ignored?(node)
14
+ @ignored.include?(operator_of(node))
15
+ end
16
+
17
+ # The operator naming node's kind of site, eg ?: for a
18
+ # ternary or any? for a block method's call; nil for a
19
+ # node with none.
20
+ def operator_of(node)
21
+ type = node.type
22
+ return NAMED[type] if NAMED.key?(type)
23
+ return ternary?(node) ? '?:' : 'if' if type == :if_node
24
+ return call_operator(node) if type == :call_node
25
+
26
+ node.respond_to?(:operator) ? node.operator : nil
27
+ end
28
+
29
+ # The operator of a call: &. for a &. chain, else its
30
+ # method's name, eg any?.
31
+ def call_operator(node)
32
+ safe_chain?(node) ? '&.' : node.name.to_s
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,67 @@
1
+ require_relative 'condition'
2
+
3
+ module PathCoverage
4
+ # The part of the manifest's visitor that builds the sites of
5
+ # ||= and &&= writes, eg @clock ||= Clock.new. It relies on
6
+ # its includer's add_site, bare_less? and extent_of.
7
+ module ManifestWrites
8
+ # Every node type of a ||= or &&= write, by its target: a
9
+ # variable of each kind, a constant, a constant path, an
10
+ # attribute, eg self.size, and an index, eg store[key].
11
+ WRITE_TYPES = %w[
12
+ local_variable instance_variable class_variable
13
+ global_variable constant constant_path call index
14
+ ].flat_map do |target|
15
+ %W[#{target}_or_write_node #{target}_and_write_node]
16
+ end.map(&:to_sym).freeze
17
+
18
+ # Notes each kind of write as a site, eg
19
+ # visit_index_or_write_node for store[key] ||= 0.
20
+ WRITE_TYPES.each do |type|
21
+ define_method(:"visit_#{type}") do |node|
22
+ write?(node) ? add_site(node) : visit_child_nodes(node)
23
+ end
24
+ end
25
+
26
+ # True for a ||= or &&= write that is a site: any but one
27
+ # through &., eg box&.size ||= 3, which a nil box skips whole,
28
+ # its target never looked at, so neither of its paths taken.
29
+ def write?(node)
30
+ WRITE_TYPES.include?(node.type) && !safe_write?(node)
31
+ end
32
+
33
+ # True for a write through &., eg box&.size ||= 3.
34
+ def safe_write?(node)
35
+ node.respond_to?(:safe_navigation?) && node.safe_navigation?
36
+ end
37
+
38
+ # A write as a Condition, named by its operator: its one leaf
39
+ # its target, then its right side as an arm, taken when the
40
+ # target is false for ||=, true for &&=. The target is a
41
+ # leaf the rewriter never reads again, as reading it could
42
+ # call a method twice; its outcome follows from whether the
43
+ # arm ran. Any site inside the write, eg in an index or on
44
+ # the right side, is a site of its own. A right side that is
45
+ # no bare argument, eg a command call, is noted among the
46
+ # site's commands, to be parenthesised.
47
+ def write_condition(node)
48
+ visit_child_nodes(node)
49
+ value = extent_of(node.value)
50
+ @notes[:commands] << value if bare_less?(node.value)
51
+ arm = Arm.new(kind: :then, extent: value, body: nil)
52
+ Condition.new(
53
+ operator: node.operator, operands: [target_of(node), arm],
54
+ extent: extent_of(node)
55
+ )
56
+ end
57
+
58
+ # The byte range of a write's target: from its start to its
59
+ # operator, less any space between, eg `store[key]`.
60
+ def target_of(node)
61
+ start = node.location.start_offset
62
+ before = node.operator_loc.start_offset - start
63
+ text = node.location.slice.byteslice(0, before)
64
+ start...(start + text.rstrip.bytesize)
65
+ end
66
+ end
67
+ end
@@ -12,11 +12,15 @@ module PathCoverage
12
12
  end
13
13
 
14
14
  # One site paths hash, as Result#paths gives them, each
15
- # path's count the sum of every run's; runs of the same
16
- # source list the same paths in the same order.
15
+ # path's count the sum of every run's. Runs that list a
16
+ # site's paths differently raise, as adding their counts by
17
+ # position would credit one path's count to another.
17
18
  def self.paths(runs)
18
19
  runs.reduce do |total, run|
19
- total.merge(run) do |_site, mine, theirs|
20
+ total.merge(run) do |site, mine, theirs|
21
+ same = mine.map { _1[:path] } == theirs.map { _1[:path] }
22
+ raise ArgumentError, "runs differ at #{site}" unless same
23
+
20
24
  mine.zip(theirs).map do |a, b|
21
25
  { path: a[:path], count: a[:count] + b[:count] }
22
26
  end
@@ -24,6 +28,18 @@ module PathCoverage
24
28
  end
25
29
  end
26
30
 
31
+ # One listed hash, { site => { path => count } }, eg the
32
+ # paths a treatment rules out, each path's count the sum of
33
+ # every run's, eg [true, false] taken once in each of two
34
+ # runs is taken twice.
35
+ def self.listed(runs)
36
+ runs.reduce do |total, run|
37
+ total.merge(run) do |_site, mine, theirs|
38
+ mine.merge(theirs) { |_path, a, b| a + b }
39
+ end
40
+ end
41
+ end
42
+
27
43
  # One operand's counts from two runs, added; a fixed leaf's
28
44
  # only comes from the source, so either run's will do.
29
45
  def self.sum(mine, theirs)