path_coverage 0.2.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.
- checksums.yaml +4 -4
- data/README.md +172 -121
- data/lib/path_coverage/calls.rb +4 -11
- data/lib/path_coverage/card.rb +14 -0
- data/lib/path_coverage/card_tree.rb +186 -0
- data/lib/path_coverage/card_ways.rb +140 -0
- data/lib/path_coverage/condition.rb +11 -4
- data/lib/path_coverage/configuration.rb +1 -17
- data/lib/path_coverage/configuration_paths.rb +4 -2
- data/lib/path_coverage/console_summary.rb +15 -17
- data/lib/path_coverage/gaps.rb +12 -9
- data/lib/path_coverage/html/file.html +5 -5
- data/lib/path_coverage/html/index.html +3 -1
- data/lib/path_coverage/html/settings.js +50 -0
- data/lib/path_coverage/html/side.js +470 -432
- data/lib/path_coverage/html/style.css +345 -226
- data/lib/path_coverage/html/theme.js +27 -0
- data/lib/path_coverage/html_branch.rb +30 -0
- data/lib/path_coverage/html_cards.rb +88 -0
- data/lib/path_coverage/html_cells.rb +71 -0
- data/lib/path_coverage/html_file.rb +82 -68
- data/lib/path_coverage/html_index.rb +28 -29
- data/lib/path_coverage/html_index_head.rb +10 -30
- data/lib/path_coverage/html_labels.rb +16 -0
- data/lib/path_coverage/html_methods.rb +66 -0
- data/lib/path_coverage/html_nav.rb +50 -50
- data/lib/path_coverage/html_report.rb +45 -49
- data/lib/path_coverage/html_statements.rb +186 -0
- data/lib/path_coverage/html_syntax.rb +167 -0
- data/lib/path_coverage/html_template.rb +6 -0
- data/lib/path_coverage/html_tree.rb +90 -88
- data/lib/path_coverage/json_reader.rb +21 -14
- data/lib/path_coverage/json_writer.rb +26 -13
- data/lib/path_coverage/kernel.rb +32 -51
- data/lib/path_coverage/kinds/block.rb +2 -1
- data/lib/path_coverage/kinds/case.rb +7 -9
- data/lib/path_coverage/kinds/choice.rb +3 -7
- data/lib/path_coverage/kinds/contains.rb +9 -5
- data/lib/path_coverage/kinds/safe.rb +6 -18
- data/lib/path_coverage/kinds/write.rb +0 -6
- data/lib/path_coverage/manifest.rb +5 -3
- data/lib/path_coverage/manifest_blocks.rb +3 -4
- data/lib/path_coverage/manifest_case.rb +2 -3
- data/lib/path_coverage/manifest_choices.rb +41 -20
- data/lib/path_coverage/manifest_folding.rb +48 -11
- data/lib/path_coverage/merge.rb +3 -3
- data/lib/path_coverage/method_statements.rb +305 -0
- data/lib/path_coverage/recorder.rb +12 -31
- data/lib/path_coverage/render.rb +19 -22
- data/lib/path_coverage/rewriter.rb +36 -55
- data/lib/path_coverage/site_paths.rb +8 -0
- data/lib/path_coverage/site_tree.rb +9 -0
- data/lib/path_coverage/statement_arms.rb +194 -0
- data/lib/path_coverage/summary.rb +11 -15
- data/lib/path_coverage/totals.rb +6 -12
- data/lib/path_coverage/version.rb +1 -1
- data/lib/path_coverage/within_methods.rb +177 -0
- metadata +19 -12
- data/lib/path_coverage/html/toggle.js +0 -12
- data/lib/path_coverage/html_paths.rb +0 -138
- data/lib/path_coverage/path_gaps.rb +0 -41
- data/lib/path_coverage/path_manifest.rb +0 -110
data/lib/path_coverage/render.rb
CHANGED
|
@@ -6,21 +6,24 @@ require_relative 'manifest'
|
|
|
6
6
|
require_relative 'paths_report'
|
|
7
7
|
require_relative 'post'
|
|
8
8
|
require_relative 'site_paths'
|
|
9
|
+
require_relative 'within_methods'
|
|
9
10
|
|
|
10
11
|
module PathCoverage
|
|
11
12
|
# The reporters, run from operands alone: needing nothing the
|
|
12
13
|
# run recorded beyond them, they can run in another process.
|
|
13
14
|
module Render
|
|
14
15
|
# Runs each reporter chosen in config. operands, and taken's
|
|
15
|
-
# site_paths: and outside:, are as Result gives them
|
|
16
|
-
#
|
|
17
|
-
|
|
16
|
+
# site_paths: and outside:, are as Result gives them, and its
|
|
17
|
+
# reached: as Recorder#reached gives it; files is
|
|
18
|
+
# { full path => {source:, sites:} } for each covered file.
|
|
19
|
+
def self.run(operands, files, config, **taken)
|
|
18
20
|
site_paths = every_site(taken.fetch(:site_paths, {}), files)
|
|
19
21
|
reporters = config.reporters
|
|
20
22
|
if reporters.include?(:gaps)
|
|
21
23
|
gaps(operands, files, site_paths, taken.fetch(:outside, {}))
|
|
22
24
|
end
|
|
23
|
-
html_and_summary(operands, files, config,
|
|
25
|
+
html_and_summary(operands, files, config,
|
|
26
|
+
[site_paths, taken[:reached]])
|
|
24
27
|
return unless reporters.include?(:post)
|
|
25
28
|
|
|
26
29
|
post(operands, files, config, site_paths)
|
|
@@ -36,29 +39,25 @@ module PathCoverage
|
|
|
36
39
|
end
|
|
37
40
|
|
|
38
41
|
# Runs the :html and :summary reporters, each when chosen.
|
|
39
|
-
def self.html_and_summary(operands, files, config,
|
|
40
|
-
|
|
42
|
+
def self.html_and_summary(operands, files, config, recorded)
|
|
43
|
+
site_paths, reached = recorded
|
|
41
44
|
reporters = config.reporters
|
|
42
45
|
if reporters.include?(:html)
|
|
43
|
-
HtmlReport.write(operands, files, config,
|
|
46
|
+
HtmlReport.write(operands, files, config, recorded)
|
|
44
47
|
end
|
|
45
48
|
return unless reporters.include?(:summary)
|
|
46
49
|
|
|
47
|
-
|
|
50
|
+
cards = WithinMethods.cards(files, config, site_paths, reached)
|
|
51
|
+
summary(config, site_paths, cards)
|
|
48
52
|
end
|
|
49
53
|
|
|
50
54
|
# Prints the summary lines: the paths taken of all possible
|
|
51
|
-
# within decisions, and, when
|
|
55
|
+
# within decisions, and, when cards are given, as they are
|
|
52
56
|
# when methods are measured, within methods; then any
|
|
53
57
|
# operators ignored.
|
|
54
|
-
def self.summary(
|
|
55
|
-
methods = files.map do |full_path, parsed|
|
|
56
|
-
path = config.relative(full_path)
|
|
57
|
-
PathManifest.methods(parsed[:source], path)
|
|
58
|
-
end
|
|
59
|
-
all = methods.reduce({}, :merge)
|
|
58
|
+
def self.summary(config, site_paths, cards)
|
|
60
59
|
groups = config.groups
|
|
61
|
-
puts ConsoleSummary.lines(site_paths,
|
|
60
|
+
puts ConsoleSummary.lines(site_paths, cards, groups) +
|
|
62
61
|
ConsoleSummary.ignored_lines(config.ignored)
|
|
63
62
|
end
|
|
64
63
|
|
|
@@ -88,17 +87,15 @@ module PathCoverage
|
|
|
88
87
|
end
|
|
89
88
|
|
|
90
89
|
# Runs the reporters chosen in config from the JSON reports
|
|
91
|
-
# at paths, their counts merged, parsing each
|
|
92
|
-
#
|
|
93
|
-
#
|
|
90
|
+
# at paths, their counts and reaches merged, parsing each
|
|
91
|
+
# covered file afresh. paths is config.json_path alone by
|
|
92
|
+
# default.
|
|
94
93
|
def self.from_json(config, paths = [config.json_path])
|
|
95
94
|
operands = JsonReader.operands(paths, config.fingerprint)
|
|
96
95
|
files = config.files.to_h do |path|
|
|
97
96
|
[path, Manifest.parse(path, config)]
|
|
98
97
|
end
|
|
99
|
-
|
|
100
|
-
taken = methods ? JsonReader.method_paths(paths) : nil
|
|
101
|
-
run(operands, files, config, taken, **JsonReader.taken(paths))
|
|
98
|
+
run(operands, files, config, **JsonReader.taken(paths))
|
|
102
99
|
end
|
|
103
100
|
end
|
|
104
101
|
end
|
|
@@ -7,19 +7,33 @@ module PathCoverage
|
|
|
7
7
|
# outcome when the code runs. It inserts text at the byte
|
|
8
8
|
# ranges the manifest found, and never parses.
|
|
9
9
|
module Rewriter
|
|
10
|
-
PATH_NAME = '__path_coverage_path'.freeze
|
|
11
|
-
|
|
12
10
|
# Returns source with each operand in sites wrapped in a
|
|
13
11
|
# record call, each site as a whole in a leave call that
|
|
14
|
-
# enters it first, and each
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
def self.rewrite(source, sites,
|
|
12
|
+
# enters it first, and each statement in reaches, { name =>
|
|
13
|
+
# offset }, after a reach call that counts its runs. Only
|
|
14
|
+
# text is inserted, never a newline, so backtrace line
|
|
15
|
+
# numbers still match the file on disk.
|
|
16
|
+
def self.rewrite(source, sites, reaches = {})
|
|
19
17
|
inserts = sites.flat_map do |site, condition|
|
|
20
18
|
site_inserts(site, condition)
|
|
21
19
|
end
|
|
22
|
-
apply(source.dup, inserts +
|
|
20
|
+
apply(source.dup, inserts + reach_inserts(reaches))
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Example: { "k.rb:2:2" => 18 } puts a reach call at 18.
|
|
24
|
+
# The inserts that put a reach call, a statement of its
|
|
25
|
+
# own, before each statement in reaches, outside all else
|
|
26
|
+
# there. A statement reached by
|
|
27
|
+
# its byte range, eg an endless def's body, is wrapped in
|
|
28
|
+
# parentheses with its call, being one expression.
|
|
29
|
+
def self.reach_inserts(reaches)
|
|
30
|
+
rank = [Float::INFINITY, LAYERS.index(:reach)]
|
|
31
|
+
reaches.flat_map do |name, at|
|
|
32
|
+
call = "PathCoverage.reach(#{name.inspect}); "
|
|
33
|
+
next [[at, call, rank]] unless at.is_a?(Range)
|
|
34
|
+
|
|
35
|
+
[[at.begin, "(#{call}", rank], [at.end, ')', rank, :close]]
|
|
36
|
+
end
|
|
23
37
|
end
|
|
24
38
|
|
|
25
39
|
# The inserts for one site, as its kind gives them, eg its
|
|
@@ -69,53 +83,21 @@ module PathCoverage
|
|
|
69
83
|
"PathCoverage.record(#{site.inspect}, #{index}, "
|
|
70
84
|
end
|
|
71
85
|
|
|
72
|
-
# The inserts that start each method's path at its entry,
|
|
73
|
-
# outermost of all there, and wrap each of its decisions'
|
|
74
|
-
# predicates in a decide call, outside any record or
|
|
75
|
-
# outcome call.
|
|
76
|
-
def self.path_inserts(methods)
|
|
77
|
-
methods.flat_map do |site, method|
|
|
78
|
-
decisions = method.decisions
|
|
79
|
-
decides = decisions.each_with_index.flat_map do |decision, i|
|
|
80
|
-
call = "PathCoverage.decide(#{PATH_NAME}, #{i}, "
|
|
81
|
-
tail = decision.exits.nil? ? '' : ", #{decision.exits}"
|
|
82
|
-
command = decision.command
|
|
83
|
-
wrap(call, decision.range, command, :decision, tail)
|
|
84
|
-
end
|
|
85
|
-
[start_insert(site, method)] + decides
|
|
86
|
-
end
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
# The insert at method's entry that starts a call's path,
|
|
90
|
-
# outermost of all there.
|
|
91
|
-
def self.start_insert(site, method)
|
|
92
|
-
start = path_start(site, method.decisions.size)
|
|
93
|
-
[method.entry, start, [Float::INFINITY, LAYERS.index(:path)]]
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
# The statement that starts a call's path through the
|
|
97
|
-
# method at site, which has size decisions.
|
|
98
|
-
def self.path_start(site, size)
|
|
99
|
-
call = "PathCoverage.path(#{site.inspect}, #{size})"
|
|
100
|
-
"#{PATH_NAME} = #{call}; "
|
|
101
|
-
end
|
|
102
|
-
|
|
103
86
|
# The layers of inserts, innermost first, that break a tie
|
|
104
87
|
# between inserts wrapping the same range: a leaf's call, a
|
|
105
88
|
# nested kind's call around its whole, eg end_items, a site's
|
|
106
|
-
# leave,
|
|
107
|
-
LAYERS = %i[leaf kind site
|
|
89
|
+
# leave, and a statement's reach.
|
|
90
|
+
LAYERS = %i[leaf kind site reach].freeze
|
|
108
91
|
|
|
109
|
-
# The two inserts that wrap the code at range in call
|
|
110
|
-
#
|
|
111
|
-
#
|
|
112
|
-
#
|
|
113
|
-
#
|
|
114
|
-
#
|
|
115
|
-
#
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
open, close = command ? ['(', ")#{tail})"] : ['', "#{tail})"]
|
|
92
|
+
# The two inserts that wrap the code at range in call. Their
|
|
93
|
+
# order, [size, rank], nests them among inserts at the same
|
|
94
|
+
# offset: the longer range outside, the rank of layer in
|
|
95
|
+
# LAYERS breaking a tie. A command call is parenthesised, as
|
|
96
|
+
# it is no bare argument; nothing else is, since parentheses
|
|
97
|
+
# make a statement, which gives its line a line-coverage
|
|
98
|
+
# count of its own.
|
|
99
|
+
def self.wrap(call, range, command, layer)
|
|
100
|
+
open, close = command ? ['(', '))'] : ['', ')']
|
|
119
101
|
order = [range.size, LAYERS.index(layer)]
|
|
120
102
|
[
|
|
121
103
|
[range.begin, call + open, order],
|
|
@@ -129,10 +111,9 @@ module PathCoverage
|
|
|
129
111
|
# offset lands before text already there, so at one offset
|
|
130
112
|
# opening inserts go in lowest order first, leaving the
|
|
131
113
|
# highest outermost, then closing ones highest order first,
|
|
132
|
-
# so the innermost call closes first
|
|
133
|
-
#
|
|
134
|
-
#
|
|
135
|
-
# ternary, opens outside the decide wrapping its predicate.
|
|
114
|
+
# so the innermost call closes first. Eg a ternary whose
|
|
115
|
+
# predicate is a leaf: the site's leave, wrapping the whole
|
|
116
|
+
# ternary, opens outside the record wrapping its predicate.
|
|
136
117
|
def self.apply(source, inserts)
|
|
137
118
|
sorted = inserts.sort_by do |offset, _, order, close|
|
|
138
119
|
size, rank = order
|
|
@@ -16,6 +16,14 @@ module PathCoverage
|
|
|
16
16
|
end
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
+
# Example: [false, true, false, true, false] for an any?.
|
|
20
|
+
# site's value on each of its possible paths, in the order
|
|
21
|
+
# possible gives them, nil where not known.
|
|
22
|
+
def self.values(site)
|
|
23
|
+
ways = Kinds.of(site.operator).root_ways(site, site.fixed)
|
|
24
|
+
ways.map(&:last)
|
|
25
|
+
end
|
|
26
|
+
|
|
19
27
|
# [[{ leaf range => outcome }, value], ...] for each way
|
|
20
28
|
# node can be evaluated, value being the outcome of node as
|
|
21
29
|
# a whole, as its kind gives them; fixed is the site's
|
|
@@ -13,15 +13,24 @@ module PathCoverage
|
|
|
13
13
|
# an arm { arm:, text:, cells:, body: } whether the column
|
|
14
14
|
# took it, body being its condition's tree, nil for a plain
|
|
15
15
|
# arm. Texts are cut from source at the manifest's ranges.
|
|
16
|
+
# An elsif's tree is labelled elsif, as it is written.
|
|
16
17
|
def self.of(source, condition, paths)
|
|
17
18
|
columns = paths.sort_by.with_index do |taken, i|
|
|
18
19
|
[taken[:count].zero? ? 0 : 1, i]
|
|
19
20
|
end
|
|
20
21
|
outcomes = columns.map { |taken| taken[:path] }
|
|
21
22
|
tree = node(source, condition, outcomes, (0..).each)
|
|
23
|
+
tree = tree.merge(label: 'elsif') if elsif?(source, condition)
|
|
22
24
|
tree.merge(counts: columns.map { |taken| taken[:count] })
|
|
23
25
|
end
|
|
24
26
|
|
|
27
|
+
# True for an if site written as an elsif: an if's extent
|
|
28
|
+
# starts at its keyword.
|
|
29
|
+
def self.elsif?(source, condition)
|
|
30
|
+
condition.operator == 'if' &&
|
|
31
|
+
source.byteslice(condition.extent.begin, 5) == 'elsif'
|
|
32
|
+
end
|
|
33
|
+
|
|
25
34
|
# The tree of condition, as its kind builds it, each leaf
|
|
26
35
|
# taking the next index of leaves, so leaves pair with path
|
|
27
36
|
# entries left to right.
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
module PathCoverage
|
|
2
|
+
# The lists of statements a method's card has rows for: a def's
|
|
3
|
+
# body, the arms of each site in it whose arms are statements,
|
|
4
|
+
# and the body of each block in it, as the card lays them out
|
|
5
|
+
# flat. It relies on its includer's rows_in, site? and
|
|
6
|
+
# site_name.
|
|
7
|
+
module StatementArms
|
|
8
|
+
# The nodes a statement's sites are never looked for in: a
|
|
9
|
+
# block's, a lambda's and a begin's statements are rows of
|
|
10
|
+
# their own, after the statement holding them, and a def's
|
|
11
|
+
# body runs only when it is called, its sites rows of its own
|
|
12
|
+
# card.
|
|
13
|
+
UNSEEN = %i[block_node begin_node def_node lambda_node].freeze
|
|
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
|
+
|
|
48
|
+
# Example: return a ? 1 : 2 if x gives the ?:.
|
|
49
|
+
# The sites inside site that are sites of their own, in the
|
|
50
|
+
# order they are written: in its leaves, eg a modifier if's
|
|
51
|
+
# arm or a &. call's receiver; not in its arms of statements,
|
|
52
|
+
# nor in a body, which have rows of their own. A site is
|
|
53
|
+
# named where it starts, so a node starting where a site
|
|
54
|
+
# does, eg a ?:'s predicate, names that site: each is kept
|
|
55
|
+
# once, and site itself not at all. None for a top that is no
|
|
56
|
+
# site, eg a def inside a def, whose sites are on its own card.
|
|
57
|
+
def inner_sites(site)
|
|
58
|
+
return [] unless site?(site)
|
|
59
|
+
|
|
60
|
+
found = heads(site).flat_map { sites_within(_1) }
|
|
61
|
+
named = found.to_h { [site_name(_1), _1] }
|
|
62
|
+
named.except(site_name(site)).values
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# node when it is a site, then each site within it, in
|
|
66
|
+
# order; none in a body or a def.
|
|
67
|
+
def sites_within(node)
|
|
68
|
+
return [] if UNSEEN.include?(node.type)
|
|
69
|
+
|
|
70
|
+
own = site?(node) ? [node] : []
|
|
71
|
+
own + heads(node).flat_map { sites_within(_1) }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Example: items.map { |i| i.price } gives the price row.
|
|
75
|
+
# The rows of the bodies in statement: of each block's and
|
|
76
|
+
# lambda's statements, a block method's included,
|
|
77
|
+
# as its block's sites are sites of their own, and a reach
|
|
78
|
+
# call before its one expression sits outside the
|
|
79
|
+
# expression's own record call; and of each begin's parts.
|
|
80
|
+
def body_rows(statement)
|
|
81
|
+
bodies_in(statement).flat_map do |body|
|
|
82
|
+
rows_in(statements_of(body))
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# The body of each block and lambda in node, run 0 or more
|
|
87
|
+
# times, and each begin: none inside an arm of statements or
|
|
88
|
+
# another body, whose rows are their own, nor in a def, whose
|
|
89
|
+
# rows are its own card's.
|
|
90
|
+
def bodies_in(node)
|
|
91
|
+
case node.type
|
|
92
|
+
when :def_node then []
|
|
93
|
+
when :block_node, :lambda_node then [node.body]
|
|
94
|
+
when :begin_node then [node]
|
|
95
|
+
else heads(node).flat_map { bodies_in(_1) }
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# node's children outside its arms of statements: all of
|
|
100
|
+
# them for a node with none, else its predicate, and a
|
|
101
|
+
# case's patterns too.
|
|
102
|
+
def heads(node)
|
|
103
|
+
return node.compact_child_nodes if arms_of(node).empty?
|
|
104
|
+
return [node.predicate] unless node.type == :case_node
|
|
105
|
+
|
|
106
|
+
[node.predicate, *node.conditions.flat_map(&:conditions)]
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# The statements of node's arms, then's before else's; none
|
|
110
|
+
# when node has no arms of statements, as a modifier's arm,
|
|
111
|
+
# and a ?:'s, are part of the site's own expression.
|
|
112
|
+
def arms_of(node)
|
|
113
|
+
case node.type
|
|
114
|
+
when :if_node then if_arms(node)
|
|
115
|
+
when :unless_node then unless_arms(node)
|
|
116
|
+
when :case_node then case_arms(node)
|
|
117
|
+
else []
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# A case's arms' statements: each when's in turn, then its
|
|
122
|
+
# else's.
|
|
123
|
+
def case_arms(node)
|
|
124
|
+
whens = node.conditions.flat_map do |clause|
|
|
125
|
+
statements_of(clause.statements)
|
|
126
|
+
end
|
|
127
|
+
whens + else_arms(node.else_clause)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# An if's arms' statements, an elsif standing as a statement
|
|
131
|
+
# of its if's else; none for a ?: or a modifier if.
|
|
132
|
+
def if_arms(node)
|
|
133
|
+
return [] if node.if_keyword_loc.nil? || then_first?(node)
|
|
134
|
+
|
|
135
|
+
statements_of(node.statements) + else_arms(node.subsequent)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# An unless's arms' statements; none for a modifier unless.
|
|
139
|
+
def unless_arms(node)
|
|
140
|
+
return [] if then_first?(node)
|
|
141
|
+
|
|
142
|
+
statements_of(node.statements) + else_arms(node.else_clause)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# True for an if or unless whose then arm is written before
|
|
146
|
+
# its predicate: a modifier, eg x if y.
|
|
147
|
+
def then_first?(node)
|
|
148
|
+
statements = node.statements
|
|
149
|
+
return false if statements.nil?
|
|
150
|
+
|
|
151
|
+
predicate = node.predicate.location.start_offset
|
|
152
|
+
statements.location.start_offset < predicate
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# The statements of an if's else, an elsif as one of them,
|
|
156
|
+
# none for none.
|
|
157
|
+
def else_arms(node)
|
|
158
|
+
return [node] if node&.type == :if_node
|
|
159
|
+
|
|
160
|
+
clause_statements(node)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# The statements of clause, an else or an ensure; none for
|
|
164
|
+
# none.
|
|
165
|
+
def clause_statements(clause)
|
|
166
|
+
clause ? statements_of(clause.statements) : []
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# The top-level statements of a def's body, a block's, or an
|
|
170
|
+
# arm's; with a rescue, those of each part in turn.
|
|
171
|
+
def statements_of(body)
|
|
172
|
+
return begun(body) if body&.type == :begin_node
|
|
173
|
+
|
|
174
|
+
body ? body.body : []
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# A begin's statements, then each rescue's, as each runs when
|
|
178
|
+
# its exception is raised, its else's, run when none is, and
|
|
179
|
+
# its ensure's, run either way.
|
|
180
|
+
def begun(node)
|
|
181
|
+
statements_of(node.statements) + rescued(node.rescue_clause) +
|
|
182
|
+
clause_statements(node.else_clause) +
|
|
183
|
+
clause_statements(node.ensure_clause)
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# The statements of clause, a rescue, then of each rescue
|
|
187
|
+
# after it; none for none.
|
|
188
|
+
def rescued(clause)
|
|
189
|
+
return [] if clause.nil?
|
|
190
|
+
|
|
191
|
+
statements_of(clause.statements) + rescued(clause.subsequent)
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
end
|
|
@@ -7,36 +7,32 @@ module PathCoverage
|
|
|
7
7
|
module Summary
|
|
8
8
|
# One row per file that has sites in site_paths, as
|
|
9
9
|
# Result#paths gives them, in path order: its sites, and
|
|
10
|
-
# its decisions total
|
|
11
|
-
#
|
|
12
|
-
|
|
13
|
-
# methods are measured.
|
|
14
|
-
def self.files(site_paths, methods = {}, paths = nil)
|
|
10
|
+
# its decisions total, and its methods total, its defs' cards
|
|
11
|
+
# in cards, { def site => [taken, size] }, added up.
|
|
12
|
+
def self.files(site_paths, cards)
|
|
15
13
|
by_path = site_paths.group_by { |site, _| path_of(site) }
|
|
16
14
|
by_path.sort.map do |path, sites|
|
|
17
15
|
{
|
|
18
16
|
path:, sites: sites.size,
|
|
19
17
|
decisions: Totals.decisions(sites.to_h),
|
|
20
|
-
methods:
|
|
18
|
+
methods: methods_of(path, cards)
|
|
21
19
|
}
|
|
22
20
|
end
|
|
23
21
|
end
|
|
24
22
|
|
|
25
|
-
# The methods total of the file at path
|
|
26
|
-
#
|
|
27
|
-
def self.methods_of(path,
|
|
28
|
-
|
|
29
|
-
Totals.methods(own, paths)
|
|
23
|
+
# The methods total of the file at path, its defs' cards
|
|
24
|
+
# added up; [0, 0] for a file with no def.
|
|
25
|
+
def self.methods_of(path, cards)
|
|
26
|
+
Totals.methods(cards.select { |site, _| path_of(site) == path })
|
|
30
27
|
end
|
|
31
28
|
|
|
32
|
-
# The sum of each column over rows,
|
|
33
|
-
#
|
|
29
|
+
# The sum of each column over rows; no rows, as for a group
|
|
30
|
+
# no file is in, gives 0 sites and [0, 0] totals.
|
|
34
31
|
def self.total(rows)
|
|
35
|
-
methods = rows.map { |row| row[:methods] }
|
|
36
32
|
{
|
|
37
33
|
sites: rows.sum { |row| row[:sites] },
|
|
38
34
|
decisions: add(rows.map { |row| row[:decisions] }),
|
|
39
|
-
methods:
|
|
35
|
+
methods: add(rows.map { |row| row[:methods] })
|
|
40
36
|
}
|
|
41
37
|
end
|
|
42
38
|
|
data/lib/path_coverage/totals.rb
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
require_relative 'path_gaps'
|
|
2
|
-
|
|
3
1
|
module PathCoverage
|
|
4
2
|
# The two coverage totals, each [taken, possible] paths over
|
|
5
3
|
# every covered file. They count the very paths the gap
|
|
@@ -13,16 +11,12 @@ module PathCoverage
|
|
|
13
11
|
[every.count { |each| each[:count].positive? }, every.size]
|
|
14
12
|
end
|
|
15
13
|
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
every = PathGaps.possible(method.decisions)
|
|
23
|
-
seen = paths.fetch(site, {})
|
|
24
|
-
[taken + every.count { |path| seen.key?(path) },
|
|
25
|
-
possible + every.size]
|
|
14
|
+
# Example: cards [4, 6] and [17, 21] give [21, 27].
|
|
15
|
+
# Paths within each method: cards is { def site => [taken,
|
|
16
|
+
# size] }, each method's card's paths taken of its size.
|
|
17
|
+
def self.methods(cards)
|
|
18
|
+
cards.values.reduce([0, 0]) do |(taken, possible), (more, of)|
|
|
19
|
+
[taken + more, possible + of]
|
|
26
20
|
end
|
|
27
21
|
end
|
|
28
22
|
end
|