path_coverage 0.3.0 → 0.4.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.
@@ -12,6 +12,39 @@ module PathCoverage
12
12
  # card.
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 site 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
+ # The tops of statements that end a path, leaving the method.
27
+ LEAVING = %w[return raise].freeze
28
+
29
+ # True for a row that shapes its method's paths: a site, or a
30
+ # return or a raise, which ends a path. Any other, eg x = 1,
31
+ # runs exactly where what holds it does, so is no row.
32
+ def shapes?(node)
33
+ site?(node) || LEAVING.include?(top_of(node))
34
+ end
35
+
36
+ # Where node, a def, is entered: its body's start, or, for an
37
+ # endless def, whose body is one expression, its byte range,
38
+ # to be wrapped; before its end for an empty body.
39
+ def entry_at(node)
40
+ body = node.body
41
+ return node.end_keyword_loc.start_offset if body.nil?
42
+
43
+ loc = body.location
44
+ start = loc.start_offset
45
+ node.equal_loc ? start...loc.end_offset : start
46
+ end
47
+
15
48
  # Example: return a ? 1 : 2 if x gives the ?:.
16
49
  # The sites inside site that are sites of their own, in the
17
50
  # order they are written: in its leaves, eg a modifier if's
@@ -1,3 +1,3 @@
1
1
  module PathCoverage
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
@@ -1,4 +1,6 @@
1
- require_relative 'html_cards'
1
+ require 'prism'
2
+ require_relative 'card_tree'
3
+ require_relative 'card_ways'
2
4
  require_relative 'html_statements'
3
5
  require_relative 'method_statements'
4
6
  require_relative 'source_lines'
@@ -39,23 +41,101 @@ module PathCoverage
39
41
  def self.listed(files, config, result, reached)
40
42
  files.map do |full_path, parsed|
41
43
  path = config.relative(full_path)
44
+ recorded = [result.operands, result.paths]
42
45
  listed_in(path, parsed, result.paths, reached)
43
- .merge(listed_tops(path, parsed, result))
46
+ .merge(listed_tops(path, parsed, recorded))
44
47
  end.reduce({}, :merge)
45
48
  end
46
49
 
50
+ # Example: { "k.rb" => { "1:0" => { "2:2" => { "parent" =>
51
+ # nil, "in" => nil, "on" => nil } } } }.
52
+ # How the rows of each card nest, in every file in files: by
53
+ # the file's path, then its card's line:column, a def's card
54
+ # and each top card, as listed names them, then each row's,
55
+ # as CardTree places them; result a Result.
56
+ def self.tree(files, config, result)
57
+ files.to_h do |full_path, parsed|
58
+ path = config.relative(full_path)
59
+ [path, tree_in(path, parsed, result)]
60
+ end
61
+ end
62
+
63
+ # How the rows of each card of the file at path nest, parsed
64
+ # as Manifest.parse gives it, as tree gives them.
65
+ def self.tree_in(path, parsed, result)
66
+ source, sites = parsed.values_at(:source, :sites)
67
+ cards = CardTree.of(source, path, sites).to_a +
68
+ tree_tops(path, parsed, result)
69
+ cards.to_h do |site, rows|
70
+ [site.split(':').last(2) * ':', rows]
71
+ end
72
+ end
73
+
74
+ # [[card site, its rows' places]] for each top card of the
75
+ # file at path, as listed_tops names them.
76
+ def self.tree_tops(path, parsed, result)
77
+ source, sites = parsed.values_at(:source, :sites)
78
+ lines = SourceLines.of(source, sites, result.operands)
79
+ spans(path, parsed, lines).map do |span, rows|
80
+ nested = CardTree.nested(source, sites, rows)
81
+ ["#{path}:#{span.begin}:0", nested]
82
+ end
83
+ end
84
+
47
85
  # The top cards of the file at path, as its page draws them:
48
86
  # each under the site of its first line's start, named for
49
- # its scope, as listed gives them.
50
- def self.listed_tops(path, parsed, result)
87
+ # its scope, as listed gives them; operands and site_paths as
88
+ # Result gives them.
89
+ def self.listed_tops(path, parsed, (operands, site_paths))
51
90
  source, sites = parsed.values_at(:source, :sites)
52
- lines = SourceLines.of(source, sites, result.operands)
53
- scopes = HtmlCards.scopes(source)
54
- HtmlCards.spans(path, parsed, lines).to_h do |span, rows|
91
+ lines = SourceLines.of(source, sites, operands)
92
+ scopes = scopes(source)
93
+ spans(path, parsed, lines).to_h do |span, rows|
55
94
  site = "#{path}:#{span.begin}:0"
56
- taken = rows.zip(MethodStatements.taken(rows, result.paths))
57
- [site, card(site, scopes[span.begin], span.end, taken)]
95
+ taken = rows.zip(MethodStatements.taken(rows, site_paths))
96
+ ways = CardWays.card(source, sites, rows)
97
+ named = [scopes[span.begin], span.end, ways, nil]
98
+ [site, card(site, named, taken)]
99
+ end
100
+ end
101
+
102
+ # { span => its top-level rows } of the file at path, each
103
+ # row under the span its line is in, lines as SourceLines.of
104
+ # gives them.
105
+ def self.spans(path, parsed, lines)
106
+ source, sites = parsed.values_at(:source, :sites)
107
+ rows = MethodStatements.top_rows(source, path, sites)
108
+ rows.group_by { lines[_1.line - 1][:span] }
109
+ end
110
+
111
+ # A lambda giving the scope of a line of source, as scope
112
+ # gives it, its modules and classes found once.
113
+ def self.scopes(source)
114
+ holders = holders_in(Prism.parse(source).value)
115
+ ->(line) { scope(holders, line) }
116
+ end
117
+
118
+ # Example: line 2, inside module Shop on lines 1 to 9, gives
119
+ # Shop.
120
+ # The name of the innermost of holders, modules and classes,
121
+ # whose lines hold line, a top card's scope, as a def is a
122
+ # method card's; file for a line in none.
123
+ def self.scope(holders, line)
124
+ holding = holders.select do |node|
125
+ location = node.location
126
+ (location.start_line..location.end_line).cover?(line)
58
127
  end
128
+ holding.empty? ? 'file' : holding.last.name.to_s
129
+ end
130
+
131
+ # The kinds of node that are a top card's scope.
132
+ HOLDERS = [Prism::ModuleNode, Prism::ClassNode].freeze
133
+
134
+ # Every module and class node in the tree at node, each
135
+ # before those nested in it.
136
+ def self.holders_in(node)
137
+ own = HOLDERS.include?(node.class) ? [node] : []
138
+ own + node.compact_child_nodes.flat_map { holders_in(_1) }
59
139
  end
60
140
 
61
141
  # The cards of the file at path, parsed as Manifest.parse
@@ -66,18 +146,23 @@ module PathCoverage
66
146
  lasts = MethodStatements.lasts(source, path, sites)
67
147
  MethodStatements.of(source, path, sites).to_h do |site, rows|
68
148
  taken = MethodStatements.taken(rows, paths, reached)
69
- [site, card(site, names[site], lasts[site], rows.zip(taken))]
149
+ ways = CardWays.card(source, sites, rows)
150
+ calls = reached&.fetch(site, 0)
151
+ named = [names[site], lasts[site], ways, calls]
152
+ [site, card(site, named, rows.zip(taken))]
70
153
  end
71
154
  end
72
155
 
73
- # The card of the def at site named name, ending on line
74
- # last: its first line, from site, and a row for each
75
- # [statement, taken] of rows.
76
- def self.card(site, name, last, rows)
156
+ # The card of the def at site, named name, ending on line
157
+ # last, with ways through it, as ways folds them, and calls,
158
+ # how many times it was called, nil for a top card, which is no
159
+ # method's, or when not known: its first line, from site, and
160
+ # a row for each [statement, taken] of rows.
161
+ def self.card(site, (name, last, ways, calls), rows)
77
162
  first = Integer(site.split(':')[-2])
78
163
  listed = rows.map { |statement, count| row(statement, count) }
79
164
  { 'name' => name, 'first' => first, 'last' => last,
80
- 'rows' => listed }
165
+ 'ways' => ways, 'calls' => calls, 'rows' => listed }
81
166
  end
82
167
 
83
168
  # A card's row: statement's line:column, as its page labels
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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Jagger
@@ -37,6 +37,8 @@ files:
37
37
  - lib/path_coverage/auto.rb
38
38
  - lib/path_coverage/calls.rb
39
39
  - lib/path_coverage/card.rb
40
+ - lib/path_coverage/card_tree.rb
41
+ - lib/path_coverage/card_ways.rb
40
42
  - lib/path_coverage/collector.rb
41
43
  - lib/path_coverage/condition.rb
42
44
  - lib/path_coverage/configuration.rb
@@ -108,7 +110,7 @@ files:
108
110
  - lib/path_coverage/totals.rb
109
111
  - lib/path_coverage/version.rb
110
112
  - lib/path_coverage/within_methods.rb
111
- homepage: https://github.com/cyber-dojo/path_coverage
113
+ homepage: https://github.com/path-coverage/ruby
112
114
  licenses:
113
115
  - BSD-2-Clause
114
116
  metadata: