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.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +172 -121
  3. data/lib/path_coverage/calls.rb +4 -11
  4. data/lib/path_coverage/card.rb +14 -0
  5. data/lib/path_coverage/card_tree.rb +186 -0
  6. data/lib/path_coverage/card_ways.rb +140 -0
  7. data/lib/path_coverage/condition.rb +11 -4
  8. data/lib/path_coverage/configuration.rb +1 -17
  9. data/lib/path_coverage/configuration_paths.rb +4 -2
  10. data/lib/path_coverage/console_summary.rb +15 -17
  11. data/lib/path_coverage/gaps.rb +12 -9
  12. data/lib/path_coverage/html/file.html +5 -5
  13. data/lib/path_coverage/html/index.html +3 -1
  14. data/lib/path_coverage/html/settings.js +50 -0
  15. data/lib/path_coverage/html/side.js +470 -432
  16. data/lib/path_coverage/html/style.css +345 -226
  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 +88 -0
  20. data/lib/path_coverage/html_cells.rb +71 -0
  21. data/lib/path_coverage/html_file.rb +82 -68
  22. data/lib/path_coverage/html_index.rb +28 -29
  23. data/lib/path_coverage/html_index_head.rb +10 -30
  24. data/lib/path_coverage/html_labels.rb +16 -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 +45 -49
  28. data/lib/path_coverage/html_statements.rb +186 -0
  29. data/lib/path_coverage/html_syntax.rb +167 -0
  30. data/lib/path_coverage/html_template.rb +6 -0
  31. data/lib/path_coverage/html_tree.rb +90 -88
  32. data/lib/path_coverage/json_reader.rb +21 -14
  33. data/lib/path_coverage/json_writer.rb +26 -13
  34. data/lib/path_coverage/kernel.rb +32 -51
  35. data/lib/path_coverage/kinds/block.rb +2 -1
  36. data/lib/path_coverage/kinds/case.rb +7 -9
  37. data/lib/path_coverage/kinds/choice.rb +3 -7
  38. data/lib/path_coverage/kinds/contains.rb +9 -5
  39. data/lib/path_coverage/kinds/safe.rb +6 -18
  40. data/lib/path_coverage/kinds/write.rb +0 -6
  41. data/lib/path_coverage/manifest.rb +5 -3
  42. data/lib/path_coverage/manifest_blocks.rb +3 -4
  43. data/lib/path_coverage/manifest_case.rb +2 -3
  44. data/lib/path_coverage/manifest_choices.rb +41 -20
  45. data/lib/path_coverage/manifest_folding.rb +48 -11
  46. data/lib/path_coverage/merge.rb +3 -3
  47. data/lib/path_coverage/method_statements.rb +305 -0
  48. data/lib/path_coverage/recorder.rb +12 -31
  49. data/lib/path_coverage/render.rb +19 -22
  50. data/lib/path_coverage/rewriter.rb +36 -55
  51. data/lib/path_coverage/site_paths.rb +8 -0
  52. data/lib/path_coverage/site_tree.rb +9 -0
  53. data/lib/path_coverage/statement_arms.rb +194 -0
  54. data/lib/path_coverage/summary.rb +11 -15
  55. data/lib/path_coverage/totals.rb +6 -12
  56. data/lib/path_coverage/version.rb +1 -1
  57. data/lib/path_coverage/within_methods.rb +177 -0
  58. metadata +19 -12
  59. data/lib/path_coverage/html/toggle.js +0 -12
  60. data/lib/path_coverage/html_paths.rb +0 -138
  61. data/lib/path_coverage/path_gaps.rb +0 -41
  62. data/lib/path_coverage/path_manifest.rb +0 -110
@@ -0,0 +1,140 @@
1
+ require_relative 'card_tree'
2
+ require_relative 'manifest'
3
+ require_relative 'method_statements'
4
+ require_relative 'site_paths'
5
+
6
+ module PathCoverage
7
+ # The number of ways one call can go through each card, folded
8
+ # over its tree, as CardTree places its rows. Each row gives a
9
+ # pair, [ways that go on, ways that leave the method]: a return
10
+ # or a raise leaves, and so does each path of a site taking an
11
+ # arm that returns or raises, eg return 0 if a. Rows one after
12
+ # another multiply, but only the ways that go on reach the next
13
+ # row; a site adds up its paths, each with the rows it runs on
14
+ # that path one after another; and a row that is the whole of a
15
+ # leaf counts only those of its paths that give the leaf the
16
+ # outcome the path has, so no impossible way is counted where
17
+ # gives knows it. A block's body counts once.
18
+ class CardWays
19
+ # Example: { "k.rb:1:0" => 3 } for def f(a, b), return 0 if a,
20
+ # then a ?: of b: 1 way leaves, 2 go on.
21
+ # { def site => its ways } for each def in source, its sites
22
+ # those of no settings.
23
+ def self.of(source, path)
24
+ sites = Manifest.sites(source, path)
25
+ cards = MethodStatements.of(source, path, sites)
26
+ cards.transform_values { |rows| card(source, sites, rows) }
27
+ end
28
+
29
+ # The ways through a card of rows in source, sites being
30
+ # { site => Condition }, as Manifest.sites gives them.
31
+ def self.card(source, sites, rows)
32
+ places = CardTree.nested(source, sites, rows)
33
+ new(source, rows, places, sites).total
34
+ end
35
+
36
+ # The pair of rows one after another, from their pairs: the
37
+ # ways that go on through them all, and those that leave at
38
+ # one of them, having gone on through every one before it.
39
+ def self.sequence(pairs)
40
+ pairs.reduce([1, 0]) do |(on, left), (more, leaving)|
41
+ [on * more, left + (on * leaving)]
42
+ end
43
+ end
44
+
45
+ # The fold of rows, a card's in source, placed as places has
46
+ # them, each site row's Condition among sites.
47
+ def initialize(source, rows, places, sites)
48
+ @source = source
49
+ @rows = rows.to_h { |row| [CardTree.key_of(row), row] }
50
+ @places = places
51
+ @sites = sites
52
+ @children = places.keys.group_by { places[_1]['parent'] }
53
+ end
54
+
55
+ # The ways through the card: its top rows' pairs, one after
56
+ # another, those that go on and those that leave.
57
+ def total
58
+ tops = @children.fetch(nil, []).map { |key| pair_of(key) }
59
+ CardWays.sequence(tops).sum
60
+ end
61
+
62
+ # The pair of the row keyed key: a site's adds up its paths'
63
+ # pairs; any other row is a return or a raise, as no other is
64
+ # a row, so leaves.
65
+ def pair_of(key)
66
+ row = @rows[key]
67
+ return [0, 1] unless row.site
68
+
69
+ pairs = possible(row.site).each_index.map do |path|
70
+ pair_on(key, path)
71
+ end
72
+ added(pairs)
73
+ end
74
+
75
+ # The pair of the site row keyed key on its path at index
76
+ # path: the rows it runs on that path, one after another, each
77
+ # given that path's outcomes; every way of it leaving when the
78
+ # path takes an arm that leaves.
79
+ def pair_on(key, path)
80
+ outcomes = possible(@rows[key].site)[path]
81
+ pairs = running(key, path).map do |child|
82
+ child_pair(child, outcomes)
83
+ end
84
+ pair = CardWays.sequence(pairs)
85
+ leaving(key).include?(path) ? [0, pair.sum] : pair
86
+ end
87
+
88
+ # The keys of the rows the site row keyed key runs on its path
89
+ # at index path, in the order they are written.
90
+ def running(key, path)
91
+ runs = @children.fetch(key, []).group_by do |child|
92
+ @places[child]['on'].include?(path)
93
+ end
94
+ runs.fetch(true, [])
95
+ end
96
+
97
+ # The pair of the row keyed child, its parent's path having
98
+ # outcomes: all of its own, for a row that gives nothing; else
99
+ # those of only its paths that give its leaf the outcome that
100
+ # path has, or that give what is not known.
101
+ def child_pair(child, outcomes)
102
+ place = @places[child]
103
+ gives = place['gives']
104
+ return pair_of(child) if gives.nil?
105
+
106
+ fits = [nil, outcome_of(outcomes, place)]
107
+ pairs = gives.each_index.map do |path|
108
+ fits.include?(gives[path]) ? pair_on(child, path) : [0, 0]
109
+ end
110
+ added(pairs)
111
+ end
112
+
113
+ # The outcome on a parent's path, outcomes, of the leaf place
114
+ # names.
115
+ def outcome_of(outcomes, place)
116
+ outcomes[place['leaf']]
117
+ end
118
+
119
+ # The indices of the paths of the site row keyed key that take
120
+ # an arm whose code returns or raises.
121
+ def leaving(key)
122
+ site = @sites.fetch(@rows[key].site)
123
+ regions = CardTree.regions_of(@source, key, site)
124
+ left = regions.group_by do |region|
125
+ region.arm && @source.byteslice(region.range).match?(LEAVES)
126
+ end
127
+ left.fetch(true, []).flat_map(&:on)
128
+ end
129
+
130
+ # pairs added up, each part on its own.
131
+ def added(pairs)
132
+ pairs.reduce([0, 0]) { |(a, b), (c, d)| [a + c, b + d] }
133
+ end
134
+
135
+ # The possible paths of the site named site.
136
+ def possible(site)
137
+ SitePaths.possible(@sites.fetch(site))
138
+ end
139
+ end
140
+ end
@@ -2,9 +2,9 @@ module PathCoverage
2
2
  # The operators whose predicate picks an arm.
3
3
  CHOICES = %w[?: if unless].freeze
4
4
 
5
- # The choices whose arms are statements, so whose path is
6
- # decided by their predicate alone.
7
- STATEMENT_CHOICES = %w[if unless].freeze
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
8
 
9
9
  # The operator of a safe-navigation chain, eg user&.account.
10
10
  SAFE = '&.'.freeze
@@ -110,10 +110,17 @@ module PathCoverage
110
110
  operands.flat_map do |operand|
111
111
  case operand
112
112
  when Condition then operand.leaves
113
- when Arm then operand.body&.leaves || []
113
+ when Arm then arm_leaves(operand)
114
114
  else [operand]
115
115
  end
116
116
  end
117
117
  end
118
+
119
+ # The leaves of arm's body, none for an arm whose body is no
120
+ # condition.
121
+ def arm_leaves(arm)
122
+ body = arm.body
123
+ body ? body.leaves : []
124
+ end
118
125
  end
119
126
  end
@@ -10,11 +10,10 @@ module PathCoverage
10
10
 
11
11
  GLOB_FLAGS = File::FNM_PATHNAME | File::FNM_EXTGLOB
12
12
  SETTINGS = %w[
13
- collector cover coverage_dir group ignore measure report root
13
+ collector cover coverage_dir group ignore report root
14
14
  skip treat
15
15
  ].freeze
16
16
  REPORTERS = %w[gaps html post summary].freeze
17
- MEASURES = %w[decisions methods].freeze
18
17
 
19
18
  # Evaluates the settings block against a new configuration.
20
19
  def initialize(&)
@@ -22,7 +21,6 @@ module PathCoverage
22
21
  @skips = []
23
22
  @groups = {}
24
23
  @reporters = [:gaps]
25
- @measures = [:decisions]
26
24
  @ignored = []
27
25
  @treats = []
28
26
  instance_eval(&)
@@ -31,20 +29,6 @@ module PathCoverage
31
29
  # The reporters chosen with report, :gaps when none were.
32
30
  attr_reader :reporters
33
31
 
34
- # What is measured, chosen with measure.
35
- # :decisions, the paths within each decision, when nothing
36
- # was chosen.
37
- attr_reader :measures
38
-
39
- # Chooses what is measured.
40
- # Eg measure :decisions, :methods also measures the paths
41
- # within each method, through its decisions.
42
- # Refuses a name there is no measure for.
43
- def measure(*names)
44
- check('measure', names, MEASURES)
45
- @measures = names.map(&:to_sym)
46
- end
47
-
48
32
  # The dir the reports are written to.
49
33
  def output_dir
50
34
  @coverage_dir
@@ -49,8 +49,10 @@ module PathCoverage
49
49
  # each only when set: runs whose fingerprints differ list
50
50
  # their sites' paths differently.
51
51
  def fingerprint
52
- all = { 'ignored' => @ignored, 'treats' => @treats }
53
- all.reject { |_, setting| setting.empty? }
52
+ print = {}
53
+ print['ignored'] = @ignored unless @ignored.empty?
54
+ print['treats'] = @treats unless @treats.empty?
55
+ print
54
56
  end
55
57
  end
56
58
  end
@@ -8,36 +8,34 @@ module PathCoverage
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 site_paths and, when paths are given,
12
- # as they are when methods are measured, of methods; groups
13
- # is { name => [glob, ...] }.
14
- def self.lines(site_paths, methods, paths, groups = {})
15
- if groups.empty?
16
- return totals_lines(totals(site_paths, methods, paths))
17
- end
11
+ # The summary lines of site_paths and, when cards, { def
12
+ # site => [taken, size] }, are given, as they are when
13
+ # methods are measured, of cards; groups is { name =>
14
+ # [glob, ...] }.
15
+ def self.lines(site_paths, cards, groups = {})
16
+ return totals_lines(totals(site_paths, cards)) if groups.empty?
18
17
 
19
- all = group_totals(site_paths, methods, paths, groups)
18
+ all = group_totals(site_paths, cards, groups)
20
19
  all.flat_map do |name, totals|
21
20
  ["#{name}:", *totals_lines(totals).map { |line| " #{line}" }]
22
21
  end
23
22
  end
24
23
 
25
24
  # { 'decisions' => [taken, possible] } of site_paths, with
26
- # 'methods' => [taken, possible] of methods when paths are
27
- # given.
28
- def self.totals(site_paths, methods, paths)
25
+ # 'methods' => [taken, possible] of cards when given.
26
+ def self.totals(site_paths, cards)
29
27
  totals = { 'decisions' => Totals.decisions(site_paths) }
30
- return totals if paths.nil?
28
+ return totals if cards.nil?
31
29
 
32
- totals.merge('methods' => Totals.methods(methods, paths))
30
+ totals.merge('methods' => Totals.methods(cards))
33
31
  end
34
32
 
35
33
  # { group name => totals } of each group's own sites and
36
- # methods alone.
37
- def self.group_totals(site_paths, methods, paths, groups)
34
+ # cards alone.
35
+ def self.group_totals(site_paths, cards, groups)
38
36
  groups.to_h do |name, globs|
39
- own_sites = own(site_paths, globs)
40
- [name, totals(own_sites, own(methods, globs), paths)]
37
+ own_cards = cards ? own(cards, globs) : nil
38
+ [name, totals(own(site_paths, globs), own_cards)]
41
39
  end
42
40
  end
43
41
 
@@ -6,12 +6,11 @@ module PathCoverage
6
6
  module Gaps
7
7
  # The gap lines of operands, paths and outside, as Result
8
8
  # gives them, in manifest order; texts is { site => each
9
- # leaf's source text }.
9
+ # leaf's source text }. A site's first leaf runs before any
10
+ # other, so a site whose first never ran was never reached.
10
11
  def self.lines(operands, paths: {}, texts: {}, outside: {})
11
12
  operands.flat_map do |site, counts|
12
- if counts.all? { |each| never_ran?(each) }
13
- next ["#{site} never reached"]
14
- end
13
+ next ["#{site} never reached"] if never_ran?(counts.first)
15
14
 
16
15
  path_gaps(site, paths.fetch(site, []), texts[site]) +
17
16
  ruled_out(site, outside.fetch(site, {}), texts[site])
@@ -29,10 +28,11 @@ module PathCoverage
29
28
  end
30
29
 
31
30
  # The outcome of each leaf path reaches, each named by its
32
- # text, eg "`u.name` nil".
31
+ # text, eg "`u.name` nil". A path's first leaf always has an
32
+ # outcome, so it always has a step.
33
33
  def self.steps(path, texts)
34
- path.zip(texts).filter_map do |outcome, text|
35
- "#{code(text)} #{outcome}" unless outcome.nil?
34
+ path.zip(texts).each_with_object([]) do |(outcome, text), all|
35
+ all << "#{code(text)} #{outcome}" unless outcome.nil?
36
36
  end.join(', ')
37
37
  end
38
38
 
@@ -40,8 +40,11 @@ module PathCoverage
40
40
  # them, naming the outcome of each leaf the path reaches,
41
41
  # texts being each leaf's source text.
42
42
  def self.path_gaps(site, paths, texts)
43
- paths.select { |each| each[:count].zero? }.map do |each|
44
- "#{site} path never taken: #{steps(each[:path], texts)}"
43
+ paths.each_with_object([]) do |each, lines|
44
+ next unless each[:count].zero?
45
+
46
+ named = steps(each[:path], texts)
47
+ lines << "#{site} path never taken: #{named}"
45
48
  end
46
49
  end
47
50
 
@@ -13,15 +13,15 @@
13
13
  <body>
14
14
  <main class="page">
15
15
  <header class="head" data-head="">
16
- <p class="back"><a data-index="" href="{{index}}">Path coverage</a></p>
17
- <h1 class="path">{{path}}</h1>
16
+ <div class="top"><p class="title"><a data-index="" href="{{index}}">Path coverage</a></p><h1 class="path"><a data-row="" href="{{index}}#row-{{path}}">{{path}}</a></h1><button type="button" class="theme" aria-label="Light or dark"></button></div>
18
17
  {{jumps}}
19
18
  </header>
20
19
  <div class="box">
21
- {{listing}}
20
+ <div class="rail">
21
+ {{rail}}
22
22
  </div>
23
- <div class="pathgrids" hidden="hidden">
24
- {{grids}}
23
+ {{listing}}
24
+ <div class="divider" role="separator" aria-orientation="vertical" title="Drag to share the width between paths and code"></div>
25
25
  </div>
26
26
  </main>
27
27
  <script>
@@ -12,7 +12,9 @@
12
12
  </head>
13
13
  <body>
14
14
  <main class="page">
15
- <h1>Path coverage</h1>
15
+ <header class="head" data-head="">
16
+ <div class="top"><h1 class="title">Path coverage</h1><button type="button" class="theme" aria-label="Light or dark"></button></div>
17
+ </header>
16
18
  {{tabs}}
17
19
  <div class="box">
18
20
  {{tables}}
@@ -0,0 +1,50 @@
1
+ // The report's settings, eg its theme. Each is carried by
2
+ // every link to another page of the report, as ?name=, since some
3
+ // browsers keep nothing shared between a report's local files, and
4
+ // is also kept in the browser where it can be, for a page opened
5
+ // afresh.
6
+ window.pathCoverageSettings = (function () {
7
+ // The value of setting name the link that opened this page
8
+ // carried, else the one last kept, when it is one of allowed;
9
+ // else null.
10
+ function given(name, allowed) {
11
+ var match = new RegExp('[?&]' + name + '=([a-z]+)').exec(window.location.search);
12
+ if (match && allowed.indexOf(match[1]) >= 0) { return match[1]; }
13
+ try {
14
+ var kept = window.localStorage.getItem('path_coverage.' + name);
15
+ return allowed.indexOf(kept) >= 0 ? kept : null;
16
+ } catch (e) { return null; }
17
+ }
18
+
19
+ // Keeps value as setting name's, where the browser allows it.
20
+ function keep(name, value) {
21
+ try { window.localStorage.setItem('path_coverage.' + name, value); } catch (e) { /* carried by links only */ }
22
+ }
23
+
24
+ // href, a link to another page of the report, with its ?name=
25
+ // set to value, before any #fragment, its other ?settings kept.
26
+ function carrying(href, name, value) {
27
+ var hash = href.indexOf('#');
28
+ var fragment = hash < 0 ? '' : href.slice(hash);
29
+ var rest = hash < 0 ? href : href.slice(0, hash);
30
+ var query = rest.indexOf('?');
31
+ var page = query < 0 ? rest : rest.slice(0, query);
32
+ var settings = query < 0 ? [] : rest.slice(query + 1).split('&').filter(function (pair) {
33
+ return pair && pair.split('=')[0] !== name;
34
+ });
35
+ settings.push(name + '=' + value);
36
+ return page + '?' + settings.join('&') + fragment;
37
+ }
38
+
39
+ // Makes every link to another of the report's pages carry value
40
+ // as setting name's.
41
+ function carry(name, value) {
42
+ document.querySelectorAll('a[href]').forEach(function (link) {
43
+ var href = link.getAttribute('href');
44
+ if (/^[a-z]+:|^#/.test(href) || !/\.html(\?|#|$)/.test(href)) { return; }
45
+ link.setAttribute('href', carrying(href, name, value));
46
+ });
47
+ }
48
+
49
+ return { given: given, keep: keep, carry: carry };
50
+ })();