path_coverage 0.7.1 → 0.7.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3aba82007768b3ee01a089934083c833dcc35d9dffbde4998c04559e4d93e257
4
- data.tar.gz: 13e4c34a101f2ef4143c1c39a2be3dfd3da871d483a7b675980d944fccac002d
3
+ metadata.gz: 606de09991d4d1b5030cf4aaf30a6c2c611504ec9264056edcf060c0805cc2e3
4
+ data.tar.gz: e388a444fd0d651a640a3703ed3df432ebee62ad631b00edebc5f389c736360e
5
5
  SHA512:
6
- metadata.gz: c2098204eb8f2b5a75bead720fcc93a2518db3621c1a0223fedda9dbf8a57cc09cc4ceac226ec83d242901fe7b77e4104077a14584e0d9de07c1637d6863b77d
7
- data.tar.gz: 407f0209d11862e23b65744ece44d197b17484a27c3293afac0eb617a4c81b3c39f5076e6a5df23d23c01c3502b111589b8d64908d943bc169dcf76c30364f30
6
+ metadata.gz: aae97e3b6a3d8c704a49bbde46aa341ae7d7e923d1e9d98fb6f63c53fca88a794b1b4a9f652649168b7e6854f847e9f7b7cf05e3418e93a933d6b40f4920f8a7
7
+ data.tar.gz: c49ef7782c1d2d5b3b20f0c9452bf4f535e71dba869fc306113d295912d10bfe7d9bf952dd2a25c4a15fbbd22ad1781472a783274159ed541c53d2c73bbe223a
@@ -74,6 +74,12 @@ module PathCoverage
74
74
  # raise, nil for an arm that does not leave the method.
75
75
  Arm = Data.define(:kind, :extent, :body, :leaves)
76
76
 
77
+ # The members a Condition need not be given: no command-call
78
+ # or fixed leaves, no extent and no site.
79
+ CONDITION_OPTIONAL = {
80
+ commands: [].freeze, fixed: {}.freeze, extent: nil, site: nil
81
+ }.freeze
82
+
77
83
  # One decision's condition as the manifest found it: its operator
78
84
  # and its operands, each the byte range of a leaf or a nested
79
85
  # Condition. A decision's commands are the byte ranges of its
@@ -82,16 +88,18 @@ module PathCoverage
82
88
  # outcome only, eg `Clock.new`. Its extent is the byte range
83
89
  # of the whole condition as written, eg a ternary's from its
84
90
  # predicate to its else arm, which its leaves alone cannot
85
- # give.
91
+ # give. Its site is the node_id Prism gives the node it is the
92
+ # decision of, the same in every parse of the same source, so
93
+ # that node, and no other starting where it does, is known as
94
+ # the decision; nil for a Condition that is no decision of its
95
+ # own, eg one nested in another's leaf, which no node is.
86
96
  Condition = Data.define(
87
- :operator, :operands, :commands, :fixed, :extent
97
+ :operator, :operands, :commands, :fixed, :extent, :site
88
98
  ) do
89
- # A condition with no command-call or fixed leaves, and no
90
- # extent, by default.
91
- def initialize(
92
- operator:, operands:, commands: [], fixed: {}, extent: nil
93
- )
94
- super
99
+ # A condition of operator and operands, each member of
100
+ # CONDITION_OPTIONAL given, or else its default.
101
+ def initialize(operator:, operands:, **given)
102
+ super(operator:, operands:, **CONDITION_OPTIONAL, **given)
95
103
  end
96
104
 
97
105
  # The byte range of every leaf operand, left to right, a
@@ -12,5 +12,12 @@ module PathCoverage
12
12
  def self.at(path, loc)
13
13
  "#{path}:#{loc.start_line}:#{loc.start_column}"
14
14
  end
15
+
16
+ # Example: "src/a:b.rb" for "src/a:b.rb:2:2".
17
+ # The path name names a decision in, all of it before its
18
+ # line and column, a colon in the path kept.
19
+ def self.path_of(name)
20
+ name.sub(/:\d+:\d+\z/, '')
21
+ end
15
22
  end
16
23
  end
@@ -0,0 +1,40 @@
1
+ require_relative 'decision'
2
+
3
+ module PathCoverage
4
+ # Decision mode's decisions in one file, each known by its site:
5
+ # the one node it is the decision of, by the node_id Prism gives
6
+ # it in every parse of the file. A node is a decision exactly
7
+ # when it is a site, never because it starts where one does, as
8
+ # the statements holding an if first do.
9
+ class DecisionSites
10
+ # The sites of decisions, { decision => Condition }, as
11
+ # Manifest.decisions gives them for the file at path. A
12
+ # decision named in another file is refused: a row would be
13
+ # named by one path and its decision by another.
14
+ def initialize(path, decisions)
15
+ @decisions = decisions
16
+ @names = decisions.to_h do |name, condition|
17
+ unless Decision.path_of(name) == path
18
+ raise ArgumentError, "decision #{name} is not in #{path}"
19
+ end
20
+
21
+ [condition.site, name]
22
+ end
23
+ end
24
+
25
+ # The name of the decision node is the site of, eg "k.rb:2:5";
26
+ # nil when node is the site of none.
27
+ def name_of(node)
28
+ @names[node.node_id]
29
+ end
30
+
31
+ # The Condition of the decision node is the site of; nil when
32
+ # node is the site of none.
33
+ def condition_of(node)
34
+ name = name_of(node)
35
+ return nil if name.nil?
36
+
37
+ @decisions.fetch(name)
38
+ end
39
+ end
40
+ end
@@ -67,14 +67,17 @@ module PathCoverage
67
67
  KEYWORDS.include?(loop.type) ? loop.statements : loop.body
68
68
  end
69
69
 
70
- # True for a keyword loop written after its body, eg x += 1
71
- # while x < 9, where a call put first in its body would run
72
- # once, not each pass, so none goes there.
70
+ # The types of node that are loops written after their body
71
+ # when they have no end of their own.
72
+ MODIFIABLE = %i[while_node until_node].freeze
73
+
74
+ # True for a loop written after its body, eg x += 1 while
75
+ # x < 9, or begin ... end while x < 9: a while or until with no
76
+ # end of its own, where a call put first in its body would run
77
+ # once, not each pass, so none goes there. A for always has
78
+ # its end, and an empty loop is no modifier.
73
79
  def self.modifier?(loop)
74
- return false unless KEYWORDS.include?(loop.type)
75
-
76
- body = loop.statements.location
77
- loop.keyword_loc.start_offset > body.start_offset
80
+ MODIFIABLE.include?(loop.type) && loop.closing_loc.nil?
78
81
  end
79
82
 
80
83
  # The first statement of a loop's body, a do ... rescue ... end
@@ -181,7 +181,8 @@ module PathCoverage
181
181
  end
182
182
 
183
183
  # Notes node as a decision, named where it starts: an if or an
184
- # unless at its keyword. It claims its place first, so a
184
+ # unless at its keyword; its site node's node_id, so node is
185
+ # the one node known as it. It claims its place first, so a
185
186
  # decision found inside one of its leaves or arms follows it.
186
187
  # While a leaf is being visited, any decision is collected as
187
188
  # part of that leaf instead, as it is part of one expression.
@@ -194,7 +195,8 @@ module PathCoverage
194
195
  outer = @notes
195
196
  @notes = { commands: [], fixed: {} }
196
197
  condition = scoped { condition_of(node) }
197
- @decisions[decision] = condition.with(**@notes)
198
+ @decisions[decision] =
199
+ condition.with(**@notes, site: node.node_id)
198
200
  @notes = outer
199
201
  end
200
202
 
@@ -4,9 +4,10 @@ require_relative 'manifest_chains'
4
4
  require_relative 'manifest_choices'
5
5
  require_relative 'decision'
6
6
  require_relative 'decision_paths'
7
+ require_relative 'decision_sites'
7
8
  require_relative 'decision_tree'
8
- require_relative 'leaving'
9
9
  require_relative 'statement_arms'
10
+ require_relative 'statement_tops'
10
11
  require_relative 'totals'
11
12
 
12
13
  module PathCoverage
@@ -152,12 +153,15 @@ module PathCoverage
152
153
  include ManifestChains
153
154
  include ManifestChoices
154
155
  include StatementArms
156
+ include StatementTops
155
157
 
156
158
  # Starts with no methods found; decisions are decision mode's,
157
- # { decision => Condition }.
159
+ # { decision => Condition }, each known by its site, as
160
+ # DecisionSites has them.
158
161
  def initialize(path, decisions)
159
162
  @path = path
160
163
  @decisions = decisions
164
+ @sites = DecisionSites.new(path, decisions)
161
165
  @methods = {}
162
166
  @names = {}
163
167
  @lasts = {}
@@ -229,19 +233,33 @@ module PathCoverage
229
233
  end
230
234
  end
231
235
 
232
- # The Statement row of node. A decision is where decision mode
233
- # names it, eg a modifier if at its keyword, with as many
234
- # paths as decision mode gives it. Anything else is where
235
- # it starts, with 1 path.
236
+ # The Statement row of node: a decision's row for the site of
237
+ # a decision, a plain row for any other node.
236
238
  def row_of(node)
237
- decision = decision_at(node)
238
- loc = decision ? decision_loc(node) : node.location
239
- name = Decision.at(@path, loc)
239
+ name = @sites.name_of(node)
240
+ name.nil? ? plain_row(node) : decision_row(node, name)
241
+ end
242
+
243
+ # The row of node, the site of the decision named name: at its
244
+ # decision_loc, eg a modifier if at its keyword, with as many
245
+ # paths as decision mode gives the decision.
246
+ def decision_row(node, name)
247
+ loc = decision_loc(node)
248
+ paths = DecisionPaths.possible(@decisions.fetch(name)).size
249
+ Statement.new(
250
+ line: loc.start_line, column: loc.start_column,
251
+ top: top_of(node), paths:, decision: name, name:
252
+ )
253
+ end
254
+
255
+ # The row of node, the site of no decision: named where it
256
+ # starts, with 1 path.
257
+ def plain_row(node)
258
+ loc = node.location
240
259
  Statement.new(
241
260
  line: loc.start_line, column: loc.start_column,
242
- top: top_of(node),
243
- paths: decision ? DecisionPaths.possible(decision).size : 1,
244
- decision: decision ? name : nil, name:
261
+ top: top_of(node), paths: 1, decision: nil,
262
+ name: Decision.at(@path, loc)
245
263
  )
246
264
  end
247
265
 
@@ -266,55 +284,16 @@ module PathCoverage
266
284
  end
267
285
  end
268
286
 
269
- # True when decision mode has a decision named where node's
270
- # decision would start.
287
+ # True when node is the site of one of decision mode's
288
+ # decisions.
271
289
  def decision?(node)
272
- !decision_at(node).nil?
290
+ !@sites.name_of(node).nil?
273
291
  end
274
292
 
275
- # Decision mode's Condition for the decision named where
276
- # node's decision would start; nil when it has none there.
293
+ # Decision mode's Condition for the decision node is the site
294
+ # of; nil when node is the site of none.
277
295
  def decision_at(node)
278
- @decisions[decision_name(node)]
279
- end
280
-
281
- # The name node's decision would have, by where it would start.
282
- def decision_name(node)
283
- Decision.at(@path, decision_loc(node))
284
- end
285
-
286
- TOPS = { call_node: '()', def_node: 'def' }.freeze
287
-
288
- # What statement is as written: a decision's operator, eg if,
289
- # when decision mode has a decision where it starts, an elsif
290
- # being elsif, as written, an if or unless with an else
291
- # if/else or unless/else; else = for a write, Leaving's word,
292
- # return or raise, for a statement that leaves the method,
293
- # () for a call, def for a def inside it, value for anything
294
- # else.
295
- def top_of(statement)
296
- decision = decision_at(statement)
297
- return 'elsif' if decision && elsif?(statement)
298
- return with_else(decision.operator, statement) if decision
299
- return '=' if write?(statement)
300
-
301
- word = Leaving.word(statement)
302
- return word if word
303
-
304
- TOPS.fetch(statement.type, 'value')
305
- end
306
-
307
- # True for an if written as an elsif.
308
- def elsif?(statement)
309
- statement.type == :if_node && statement.if_keyword == 'elsif'
310
- end
311
-
312
- # True for a write of any kind: of a variable, eg @x = 1
313
- # or x += 1, or a call Ruby makes of an index or attribute
314
- # write, eg a[i] = x or a.b = x.
315
- def write?(statement)
316
- statement.type.end_with?('_write_node') ||
317
- (statement.type == :call_node && statement.attribute_write?)
296
+ @sites.condition_of(node)
318
297
  end
319
298
  end
320
299
  end
@@ -3,7 +3,7 @@ module PathCoverage
3
3
  # body, the arms of each decision in it whose arms are statements,
4
4
  # and the body of each block in it, as the flow lays them out
5
5
  # flat. It relies on its includer's rows_in, decision? and
6
- # decision_name.
6
+ # top_of.
7
7
  module StatementArms
8
8
  # The nodes a statement's decisions are never looked for in: a
9
9
  # block's, a lambda's and a begin's statements are rows of
@@ -12,17 +12,6 @@ module PathCoverage
12
12
  # flow.
13
13
  UNSEEN = %i[block_node begin_node def_node lambda_node].freeze
14
14
 
15
- # Example: if/else for if a then 1 else 2 end.
16
- # operator, a decision node's, with /else after it when node is an
17
- # if or an unless with an else, so its row names both arms;
18
- # operator alone for any other, eg a ?:, whose arms are ? and :.
19
- def with_else(operator, node)
20
- return operator unless %w[if unless].include?(operator)
21
-
22
- clause = operator == 'if' ? node.subsequent : node.else_clause
23
- clause&.type == :else_node ? "#{operator}/else" : operator
24
- end
25
-
26
15
  # The tops of statements that end a path. A return or a raise
27
16
  # leaves the method. A next, a break or a retry leaves its pass
28
17
  # or its loop.
@@ -64,17 +53,13 @@ module PathCoverage
64
53
  # own, in the order they are written: in its leaves, eg a
65
54
  # modifier if's arm or a &. call's receiver; not in its arms
66
55
  # of statements, nor in a body, which have rows of their own.
67
- # A decision is named where it starts, so a node starting
68
- # where a decision does, eg a ?:'s predicate, names that
69
- # decision: each is kept once, and decision itself not at
70
- # all. None for a top that is no decision, eg a def inside a
71
- # def, whose decisions are on its own flow.
56
+ # Each is its own site node, found once. None for a top that
57
+ # is no decision, eg a def inside a def, whose decisions are
58
+ # on its own flow.
72
59
  def inner_decisions(decision)
73
60
  return [] unless decision?(decision)
74
61
 
75
- found = heads(decision).flat_map { decisions_within(_1) }
76
- named = found.to_h { [decision_name(_1), _1] }
77
- named.except(decision_name(decision)).values
62
+ heads(decision).flat_map { decisions_within(_1) }
78
63
  end
79
64
 
80
65
  # node when it is a decision, then each decision within it, in
@@ -0,0 +1,53 @@
1
+ require_relative 'leaving'
2
+
3
+ module PathCoverage
4
+ # What each statement of a method's flow is as written, the top
5
+ # its row is named by, eg if/else or =. It relies on its
6
+ # includer's decision_at.
7
+ module StatementTops
8
+ TOPS = { call_node: '()', def_node: 'def' }.freeze
9
+
10
+ # What statement is as written: a decision's operator, eg if,
11
+ # when statement is the site of a decision, an elsif
12
+ # being elsif, as written, an if or unless with an else
13
+ # if/else or unless/else; else = for a write, Leaving's word,
14
+ # return or raise, for a statement that leaves the method,
15
+ # () for a call, def for a def inside it, value for anything
16
+ # else.
17
+ def top_of(statement)
18
+ decision = decision_at(statement)
19
+ return 'elsif' if decision && elsif?(statement)
20
+ return with_else(decision.operator, statement) if decision
21
+ return '=' if write?(statement)
22
+
23
+ word = Leaving.word(statement)
24
+ return word if word
25
+
26
+ TOPS.fetch(statement.type, 'value')
27
+ end
28
+
29
+ # Example: if/else for if a then 1 else 2 end.
30
+ # operator, a decision node's, with /else after it when node is an
31
+ # if or an unless with an else, so its row names both arms;
32
+ # operator alone for any other, eg a ?:, whose arms are ? and :.
33
+ def with_else(operator, node)
34
+ return operator unless %w[if unless].include?(operator)
35
+
36
+ clause = operator == 'if' ? node.subsequent : node.else_clause
37
+ clause&.type == :else_node ? "#{operator}/else" : operator
38
+ end
39
+
40
+ # True for an if written as an elsif.
41
+ def elsif?(statement)
42
+ statement.type == :if_node && statement.if_keyword == 'elsif'
43
+ end
44
+
45
+ # True for a write of any kind: of a variable, eg @x = 1
46
+ # or x += 1, or a call Ruby makes of an index or attribute
47
+ # write, eg a[i] = x or a.b = x.
48
+ def write?(statement)
49
+ statement.type.end_with?('_write_node') ||
50
+ (statement.type == :call_node && statement.attribute_write?)
51
+ end
52
+ end
53
+ end
@@ -1,3 +1,3 @@
1
1
  module PathCoverage
2
- VERSION = '0.7.1'.freeze
2
+ VERSION = '0.7.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: path_coverage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Jagger
@@ -49,6 +49,7 @@ files:
49
49
  - lib/path_coverage/decision.rb
50
50
  - lib/path_coverage/decision_locations.rb
51
51
  - lib/path_coverage/decision_paths.rb
52
+ - lib/path_coverage/decision_sites.rb
52
53
  - lib/path_coverage/decision_tree.rb
53
54
  - lib/path_coverage/fold_branches.rb
54
55
  - lib/path_coverage/fold_cells.rb
@@ -111,6 +112,7 @@ files:
111
112
  - lib/path_coverage/source_report.rb
112
113
  - lib/path_coverage/stable_leaves.rb
113
114
  - lib/path_coverage/statement_arms.rb
115
+ - lib/path_coverage/statement_tops.rb
114
116
  - lib/path_coverage/summary.rb
115
117
  - lib/path_coverage/totals.rb
116
118
  - lib/path_coverage/version.rb