path_coverage 0.4.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.
- checksums.yaml +4 -4
- data/README.md +286 -199
- data/exe/path_coverage_report +4 -0
- data/lib/path_coverage/call_trace.rb +88 -0
- data/lib/path_coverage/calls.rb +49 -43
- data/lib/path_coverage/card_context.rb +104 -0
- data/lib/path_coverage/card_forks.rb +44 -0
- data/lib/path_coverage/card_gaps.rb +125 -0
- data/lib/path_coverage/card_scopes.rb +37 -0
- data/lib/path_coverage/card_tree.rb +79 -66
- data/lib/path_coverage/card_way_lists.rb +72 -0
- data/lib/path_coverage/card_ways.rb +123 -67
- data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +35 -33
- data/lib/path_coverage/condition.rb +14 -14
- data/lib/path_coverage/configuration.rb +7 -4
- data/lib/path_coverage/configuration_limits.rb +52 -0
- data/lib/path_coverage/configuration_paths.rb +7 -7
- data/lib/path_coverage/console_summary.rb +24 -22
- data/lib/path_coverage/{site.rb → decision.rb} +2 -2
- data/lib/path_coverage/decision_locations.rb +76 -0
- data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +13 -11
- data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
- data/lib/path_coverage/fold_branches.rb +18 -0
- data/lib/path_coverage/fold_cells.rb +42 -0
- data/lib/path_coverage/{html_tree.rb → fold_rows.rb} +42 -71
- data/lib/path_coverage/gaps.rb +33 -15
- data/lib/path_coverage/json_reader.rb +54 -19
- data/lib/path_coverage/json_reports.rb +102 -0
- data/lib/path_coverage/json_writer.rb +27 -12
- data/lib/path_coverage/kernel.rb +56 -58
- data/lib/path_coverage/kinds/base.rb +16 -16
- data/lib/path_coverage/kinds/block.rb +16 -15
- data/lib/path_coverage/kinds/case.rb +14 -12
- data/lib/path_coverage/kinds/chain.rb +40 -11
- data/lib/path_coverage/kinds/choice.rb +4 -3
- data/lib/path_coverage/kinds/contains.rb +40 -32
- data/lib/path_coverage/kinds/safe.rb +38 -26
- data/lib/path_coverage/kinds/write.rb +6 -6
- data/lib/path_coverage/kinds.rb +2 -2
- data/lib/path_coverage/leaving.rb +44 -0
- data/lib/path_coverage/loop_forks.rb +57 -0
- data/lib/path_coverage/loop_nodes.rb +55 -0
- data/lib/path_coverage/manifest.rb +92 -58
- data/lib/path_coverage/manifest_blocks.rb +5 -26
- data/lib/path_coverage/manifest_case.rb +2 -2
- data/lib/path_coverage/manifest_chains.rb +8 -8
- data/lib/path_coverage/manifest_choices.rb +23 -20
- data/lib/path_coverage/manifest_disabled.rb +97 -0
- data/lib/path_coverage/manifest_folding.rb +43 -40
- data/lib/path_coverage/manifest_ignoring.rb +6 -6
- data/lib/path_coverage/manifest_writes.rb +11 -10
- data/lib/path_coverage/measured_by.rb +20 -0
- data/lib/path_coverage/merge.rb +7 -7
- data/lib/path_coverage/method_loops.rb +128 -0
- data/lib/path_coverage/method_statements.rb +130 -127
- data/lib/path_coverage/method_ways.rb +142 -0
- data/lib/path_coverage/paths_report.rb +16 -16
- data/lib/path_coverage/recorder.rb +57 -36
- data/lib/path_coverage/recorder_blocks.rb +17 -17
- data/lib/path_coverage/recorder_cases.rb +12 -11
- data/lib/path_coverage/recorder_chains.rb +12 -12
- data/lib/path_coverage/recorder_writes.rb +9 -9
- data/lib/path_coverage/render.rb +58 -51
- data/lib/path_coverage/result.rb +32 -31
- data/lib/path_coverage/rewriter.rb +39 -27
- data/lib/path_coverage/rewriter_nested.rb +13 -13
- data/lib/path_coverage/runs_report.rb +84 -0
- data/lib/path_coverage/source_lines.rb +16 -16
- data/lib/path_coverage/source_report.rb +100 -0
- data/lib/path_coverage/stable_leaves.rb +162 -0
- data/lib/path_coverage/statement_arms.rb +31 -30
- data/lib/path_coverage/summary.rb +4 -62
- data/lib/path_coverage/totals.rb +10 -8
- data/lib/path_coverage/version.rb +1 -1
- data/lib/path_coverage/walk.rb +106 -0
- data/lib/path_coverage/way_algebras.rb +127 -0
- data/lib/path_coverage/ways_report.rb +91 -0
- data/lib/path_coverage/ways_totals.rb +46 -0
- data/lib/path_coverage/within_methods.rb +135 -80
- metadata +33 -28
- data/lib/path_coverage/card.rb +0 -14
- data/lib/path_coverage/html/file.html +0 -33
- data/lib/path_coverage/html/index.html +0 -29
- data/lib/path_coverage/html/settings.js +0 -50
- data/lib/path_coverage/html/side.js +0 -599
- data/lib/path_coverage/html/sort.js +0 -34
- data/lib/path_coverage/html/style.css +0 -484
- data/lib/path_coverage/html/tabs.js +0 -15
- data/lib/path_coverage/html/theme.js +0 -27
- data/lib/path_coverage/html_branch.rb +0 -30
- data/lib/path_coverage/html_cards.rb +0 -88
- data/lib/path_coverage/html_cells.rb +0 -71
- data/lib/path_coverage/html_file.rb +0 -123
- data/lib/path_coverage/html_index.rb +0 -128
- data/lib/path_coverage/html_index_head.rb +0 -29
- data/lib/path_coverage/html_labels.rb +0 -16
- data/lib/path_coverage/html_methods.rb +0 -66
- data/lib/path_coverage/html_nav.rb +0 -67
- data/lib/path_coverage/html_report.rb +0 -131
- data/lib/path_coverage/html_statements.rb +0 -186
- data/lib/path_coverage/html_tabs.rb +0 -26
- data/lib/path_coverage/html_template.rb +0 -26
|
@@ -2,16 +2,12 @@ module PathCoverage
|
|
|
2
2
|
# The operators whose predicate picks an arm.
|
|
3
3
|
CHOICES = %w[?: if unless].freeze
|
|
4
4
|
|
|
5
|
-
# An arm whose code leaves the method, by its code's start: a
|
|
6
|
-
# return or a raise, captured.
|
|
7
|
-
LEAVES = /\A(return|raise)\b/
|
|
8
|
-
|
|
9
5
|
# The operator of a safe-navigation chain, eg user&.account.
|
|
10
6
|
SAFE = '&.'.freeze
|
|
11
7
|
|
|
12
|
-
# The operator of a leaf with
|
|
8
|
+
# The operator of a leaf with decisions inside it, eg
|
|
13
9
|
# ids.select { |i| i.odd? }.any? as an operand of an &&: its
|
|
14
|
-
# operands are those
|
|
10
|
+
# operands are those decisions' Conditions, then the leaf's own byte
|
|
15
11
|
# range, whose outcome is the leaf's value.
|
|
16
12
|
CONTAINS = 'contains'.freeze
|
|
17
13
|
|
|
@@ -25,9 +21,12 @@ module PathCoverage
|
|
|
25
21
|
WRITES = %w[||= &&=].freeze
|
|
26
22
|
|
|
27
23
|
# The predicate outcome on which each choice takes its then
|
|
28
|
-
# arm: an unless's is false, as is a ||= write's
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
# arm: an unless's is false, as is a ||= write's, and an || or
|
|
25
|
+
# or's, whose leaving right side, eg x or return t, runs when
|
|
26
|
+
# the operand before it is false.
|
|
27
|
+
THEN_ON = Hash.new(true).merge(
|
|
28
|
+
'unless' => false, '||=' => false, '||' => false, 'or' => false
|
|
29
|
+
).freeze
|
|
31
30
|
|
|
32
31
|
# The methods that call their block an item at a time until
|
|
33
32
|
# it decides, its last answer deciding the result.
|
|
@@ -70,7 +69,7 @@ module PathCoverage
|
|
|
70
69
|
# all but find and detect, whose found item may itself be nil.
|
|
71
70
|
VALUED = [*STOP_EARLY - %w[find detect], *EVERY_ITEM].freeze
|
|
72
71
|
|
|
73
|
-
# Every block method whose call is a
|
|
72
|
+
# Every block method whose call is a decision, by its paths.
|
|
74
73
|
BLOCK_PATHS = [
|
|
75
74
|
*STOP_EARLY.map { |name| [name, STOP_EARLY_PATHS] },
|
|
76
75
|
*EVERY_ITEM.map { |name| [name, EVERY_ITEM_PATHS] }
|
|
@@ -80,12 +79,13 @@ module PathCoverage
|
|
|
80
79
|
# :else, extent its byte range, nil for an arm with no
|
|
81
80
|
# statements, and body the Condition it is, when it is one,
|
|
82
81
|
# else nil for a plain value or statements, which decide
|
|
83
|
-
# nothing
|
|
84
|
-
|
|
82
|
+
# nothing; leaves is Leaving's word for its code, return or
|
|
83
|
+
# raise, nil for an arm that does not leave the method.
|
|
84
|
+
Arm = Data.define(:kind, :extent, :body, :leaves)
|
|
85
85
|
|
|
86
|
-
# One
|
|
86
|
+
# One decision's condition as the manifest found it: its operator
|
|
87
87
|
# and its operands, each the byte range of a leaf or a nested
|
|
88
|
-
# Condition. A
|
|
88
|
+
# Condition. A decision's commands are the byte ranges of its
|
|
89
89
|
# leaves that are command calls, eg `ok? id`; its fixed are
|
|
90
90
|
# { byte range => :truthy } for leaves that can have that one
|
|
91
91
|
# outcome only, eg `Clock.new`. Its extent is the byte range
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
require_relative 'configuration_limits'
|
|
1
2
|
require_relative 'configuration_paths'
|
|
2
3
|
|
|
3
4
|
module PathCoverage
|
|
@@ -6,14 +7,15 @@ module PathCoverage
|
|
|
6
7
|
|
|
7
8
|
# The settings given in a PathCoverage.start block.
|
|
8
9
|
class Configuration
|
|
10
|
+
include ConfigurationLimits
|
|
9
11
|
include ConfigurationPaths
|
|
10
12
|
|
|
11
13
|
GLOB_FLAGS = File::FNM_PATHNAME | File::FNM_EXTGLOB
|
|
12
14
|
SETTINGS = %w[
|
|
13
|
-
collector cover coverage_dir group ignore
|
|
14
|
-
skip treat
|
|
15
|
+
collector cover coverage_dir group ignore loops max_paths
|
|
16
|
+
report root skip treat
|
|
15
17
|
].freeze
|
|
16
|
-
REPORTERS = %w[gaps
|
|
18
|
+
REPORTERS = %w[gaps post summary ways].freeze
|
|
17
19
|
|
|
18
20
|
# Evaluates the settings block against a new configuration.
|
|
19
21
|
def initialize(&)
|
|
@@ -23,6 +25,7 @@ module PathCoverage
|
|
|
23
25
|
@reporters = [:gaps]
|
|
24
26
|
@ignored = []
|
|
25
27
|
@treats = []
|
|
28
|
+
default_limits
|
|
26
29
|
instance_eval(&)
|
|
27
30
|
end
|
|
28
31
|
|
|
@@ -42,7 +45,7 @@ module PathCoverage
|
|
|
42
45
|
@collector_url = url
|
|
43
46
|
end
|
|
44
47
|
|
|
45
|
-
# Sets the dir that cover globs and
|
|
48
|
+
# Sets the dir that cover globs and decision names are
|
|
46
49
|
# relative to.
|
|
47
50
|
def root(dir)
|
|
48
51
|
@root = dir
|
|
@@ -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
|
|
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
|
|
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
|
|
16
|
-
# ignore '&&=' measures no &&= write; a
|
|
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
|
|
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
|
|
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
|
|
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
|
-
# "
|
|
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
|
|
12
|
-
#
|
|
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(
|
|
16
|
-
|
|
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(
|
|
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
|
-
# { '
|
|
25
|
-
# '
|
|
26
|
-
def self.totals(
|
|
27
|
-
totals = { '
|
|
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('
|
|
33
|
+
totals.merge('combined' => Totals.combined(cards))
|
|
31
34
|
end
|
|
32
35
|
|
|
33
|
-
# { group name => totals } of each group's own
|
|
36
|
+
# { group name => totals } of each group's own decisions and
|
|
34
37
|
# cards alone.
|
|
35
|
-
def self.group_totals(
|
|
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(
|
|
41
|
+
[name, totals(own(decision_paths, globs), own_cards)]
|
|
39
42
|
end
|
|
40
43
|
end
|
|
41
44
|
|
|
42
|
-
# The entries of
|
|
43
|
-
# any of globs.
|
|
44
|
-
def self.own(
|
|
45
|
-
|
|
46
|
-
Summary.matches?(Summary.path_of(
|
|
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 "
|
|
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
|
-
"
|
|
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
|
|
4
|
-
module
|
|
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,31 +2,33 @@ require_relative 'condition'
|
|
|
2
2
|
require_relative 'kinds'
|
|
3
3
|
|
|
4
4
|
module PathCoverage
|
|
5
|
-
# The paths an evaluation of a
|
|
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
|
|
9
|
-
# Every possible path through
|
|
10
|
-
# false, as its kind gives the ways a
|
|
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(
|
|
13
|
-
ways = Kinds.of(
|
|
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
|
-
|
|
16
|
+
decision.leaves.map { |range| outcomes[range] }
|
|
16
17
|
end
|
|
17
18
|
end
|
|
18
19
|
|
|
19
20
|
# Example: [false, true, false, true, false] for an any?.
|
|
20
|
-
#
|
|
21
|
+
# decision's value on each of its possible paths, in the order
|
|
21
22
|
# possible gives them, nil where not known.
|
|
22
|
-
def self.values(
|
|
23
|
-
ways = Kinds.of(
|
|
23
|
+
def self.values(decision)
|
|
24
|
+
ways = Kinds.of(decision.operator).root_ways(decision,
|
|
25
|
+
decision.fixed)
|
|
24
26
|
ways.map(&:last)
|
|
25
27
|
end
|
|
26
28
|
|
|
27
29
|
# [[{ leaf range => outcome }, value], ...] for each way
|
|
28
30
|
# node can be evaluated, value being the outcome of node as
|
|
29
|
-
# a whole, as its kind gives them; fixed is the
|
|
31
|
+
# a whole, as its kind gives them; fixed is the decision's
|
|
30
32
|
# { leaf range => its one outcome }.
|
|
31
33
|
def self.walk(node, fixed)
|
|
32
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
|
|
6
|
-
# grid whose columns are the
|
|
7
|
-
module
|
|
8
|
-
# { operator:, counts:, cells:, operands: } for one
|
|
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
|
-
|
|
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
|
-
#
|
|
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
|
|
56
|
-
#
|
|
57
|
-
#
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
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
|