path_coverage 0.0.2 → 0.2.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 (58) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +330 -84
  3. data/exe/path_coverage_collector +12 -1
  4. data/lib/path_coverage/calls.rb +106 -0
  5. data/lib/path_coverage/collector.rb +88 -22
  6. data/lib/path_coverage/condition.rb +77 -3
  7. data/lib/path_coverage/configuration.rb +52 -12
  8. data/lib/path_coverage/configuration_paths.rb +56 -0
  9. data/lib/path_coverage/console_summary.rb +77 -0
  10. data/lib/path_coverage/gaps.rb +25 -9
  11. data/lib/path_coverage/html/index.html +3 -14
  12. data/lib/path_coverage/html/side.js +36 -6
  13. data/lib/path_coverage/html/sort.js +27 -21
  14. data/lib/path_coverage/html/style.css +32 -1
  15. data/lib/path_coverage/html/tabs.js +15 -0
  16. data/lib/path_coverage/html_index.rb +105 -19
  17. data/lib/path_coverage/html_index_head.rb +49 -0
  18. data/lib/path_coverage/html_report.rb +44 -12
  19. data/lib/path_coverage/html_tabs.rb +26 -0
  20. data/lib/path_coverage/html_tree.rb +47 -17
  21. data/lib/path_coverage/json_reader.rb +94 -0
  22. data/lib/path_coverage/json_writer.rb +44 -8
  23. data/lib/path_coverage/kernel.rb +53 -45
  24. data/lib/path_coverage/kinds/base.rb +71 -0
  25. data/lib/path_coverage/kinds/block.rb +136 -0
  26. data/lib/path_coverage/kinds/case.rb +89 -0
  27. data/lib/path_coverage/kinds/chain.rb +45 -0
  28. data/lib/path_coverage/kinds/choice.rb +68 -0
  29. data/lib/path_coverage/kinds/contains.rb +111 -0
  30. data/lib/path_coverage/kinds/safe.rb +151 -0
  31. data/lib/path_coverage/kinds/write.rb +54 -0
  32. data/lib/path_coverage/kinds.rb +37 -0
  33. data/lib/path_coverage/manifest.rb +93 -26
  34. data/lib/path_coverage/manifest_blocks.rb +96 -0
  35. data/lib/path_coverage/manifest_case.rb +54 -0
  36. data/lib/path_coverage/manifest_chains.rb +76 -0
  37. data/lib/path_coverage/manifest_choices.rb +64 -32
  38. data/lib/path_coverage/manifest_folding.rb +91 -0
  39. data/lib/path_coverage/manifest_ignoring.rb +35 -0
  40. data/lib/path_coverage/manifest_writes.rb +67 -0
  41. data/lib/path_coverage/merge.rb +19 -3
  42. data/lib/path_coverage/post.rb +8 -8
  43. data/lib/path_coverage/recorder.rb +43 -0
  44. data/lib/path_coverage/recorder_blocks.rb +68 -0
  45. data/lib/path_coverage/recorder_cases.rb +23 -0
  46. data/lib/path_coverage/recorder_chains.rb +40 -0
  47. data/lib/path_coverage/recorder_writes.rb +26 -0
  48. data/lib/path_coverage/render.rb +58 -46
  49. data/lib/path_coverage/result.rb +19 -0
  50. data/lib/path_coverage/rewriter.rb +64 -37
  51. data/lib/path_coverage/rewriter_nested.rb +62 -0
  52. data/lib/path_coverage/site.rb +6 -1
  53. data/lib/path_coverage/site_paths.rb +9 -46
  54. data/lib/path_coverage/site_tree.rb +31 -36
  55. data/lib/path_coverage/summary.rb +64 -17
  56. data/lib/path_coverage/totals.rb +29 -0
  57. data/lib/path_coverage/version.rb +1 -1
  58. metadata +35 -7
@@ -0,0 +1,106 @@
1
+ # The calls rewritten code makes, each handed to the kernel's
2
+ # one recorder. Like the kernel, this file loads before any
3
+ # covered file and is never itself instrumented.
4
+ module PathCoverage
5
+ # The call rewritten code makes around each operand; returns
6
+ # value unchanged.
7
+ def self.record(site, index, value)
8
+ @recorder.record(site, index, value)
9
+ end
10
+
11
+ # The call rewritten code makes before a site's first leaf,
12
+ # site having size leaves; returns the evaluation's trail.
13
+ def self.enter(site, size)
14
+ @recorder.enter(site, size)
15
+ end
16
+
17
+ # The call rewritten code makes around a site as a whole,
18
+ # with the trail enter gave it; returns value unchanged.
19
+ def self.leave(trail, value)
20
+ @recorder.leave(trail, value)
21
+ end
22
+
23
+ # The call rewritten code makes around a case's pattern, the
24
+ # index-th of size at site; returns the pattern's stand-in.
25
+ def self.when_match(site, index, size, pattern)
26
+ @recorder.when_match(site, index, size, pattern)
27
+ end
28
+
29
+ # The call rewritten code makes around the pattern of a case
30
+ # nested in site, the site's index-th leaf; returns the
31
+ # pattern's stand-in.
32
+ def self.pattern(site, index, pattern)
33
+ @recorder.pattern(site, index, pattern)
34
+ end
35
+
36
+ # The call rewritten code makes around the right side of a
37
+ # write nested in site, its target the site's at-th leaf,
38
+ # outcome being the target's when it runs; returns value
39
+ # unchanged.
40
+ def self.written_at(site, at, outcome, value)
41
+ @recorder.written_at(site, at, outcome, value)
42
+ end
43
+
44
+ # The call rewritten code makes around a write nested in site,
45
+ # its target the site's at-th leaf, skipped being the target's
46
+ # outcome when its right side never ran; returns value
47
+ # unchanged.
48
+ def self.settle(site, at, skipped, value)
49
+ @recorder.settle(site, at, skipped, value)
50
+ end
51
+
52
+ # The call rewritten code makes before a block method's call
53
+ # nested in site runs, its receiver the site's at-th leaf;
54
+ # returns the site's trail.
55
+ def self.begin_items(site, at)
56
+ @recorder.begin_items(site, at)
57
+ end
58
+
59
+ # The call rewritten code makes around the block's expression
60
+ # of a stop-early block method nested in site, its receiver the
61
+ # site's at-th leaf; returns value unchanged.
62
+ def self.item_at(site, at, value)
63
+ @recorder.item_at(site, at, value)
64
+ end
65
+
66
+ # The call rewritten code makes around the block's expression
67
+ # of an every-item block method nested in site, its receiver
68
+ # the site's at-th leaf; returns value unchanged.
69
+ def self.answer_at(site, at, value)
70
+ @recorder.answer_at(site, at, value)
71
+ end
72
+
73
+ # The call rewritten code makes around a block method's call
74
+ # nested in site, its receiver the site's at-th leaf, with the
75
+ # trail begin_items gave it; returns value unchanged.
76
+ def self.end_items(site, at, trail, value)
77
+ @recorder.end_items(site, at, trail, value)
78
+ end
79
+
80
+ # The call rewritten code makes around a &. receiver, the
81
+ # index-th leaf of site, its chain's leaves starting at first;
82
+ # returns value unchanged.
83
+ def self.present(site, index, first, value)
84
+ @recorder.present(site, index, first, value)
85
+ end
86
+
87
+ # The call rewritten code makes around a &. chain's value, the
88
+ # index-th leaf of site, its chain's leaves starting at first;
89
+ # returns value unchanged.
90
+ def self.chained(site, index, first, value)
91
+ @recorder.chained(site, index, first, value)
92
+ end
93
+
94
+ # The call rewritten code makes as a call reaches its
95
+ # method's first decision; returns the call's new path.
96
+ def self.path(site, size)
97
+ @recorder.path(site, size)
98
+ end
99
+
100
+ # The call rewritten code makes around each decision's
101
+ # predicate, with exits for one whose arm leaves the method;
102
+ # returns value unchanged.
103
+ def self.decide(path, index, value, exits = nil)
104
+ @recorder.decide(path, index, value, exits)
105
+ end
106
+ end
@@ -3,44 +3,110 @@ module PathCoverage
3
3
  # runs that sent them, eg failing docker build steps, can
4
4
  # be merged and rendered once they are all done.
5
5
  module Collector
6
- # Answers every request on server, forever, saving each
7
- # body in dir as run-0001.json, run-0002.json, and so on,
8
- # in the order they arrive. A connection sending no
9
- # request, eg a check that the collector is up, is skipped.
6
+ # Answers every request on server until server is closed,
7
+ # then returns.
8
+ # Each report is saved in dir as run-0001.json,
9
+ # run-0002.json, and so on, in the order they arrive.
10
+ # A connection sending no request is skipped.
10
11
  def self.serve(server, dir)
11
12
  n = 0
12
- loop do
13
- body = request_body(server.accept)
14
- next if body.nil?
15
-
16
- n += 1
17
- File.write(format('%<dir>s/run-%<n>04d.json', dir:, n:), body)
13
+ while (client = accept(server))
14
+ saved = receive(client, dir) do |body|
15
+ save(dir, format('run-%<n>04d.json', n: n + 1), body)
16
+ end
17
+ n += 1 if saved
18
18
  end
19
19
  end
20
20
 
21
- # The body of the one HTTP request on client, after
22
- # answering it 200; nil, unanswered, when client closes
23
- # without sending a request.
24
- def self.request_body(client)
21
+ # The next connection to server; nil once server is closed,
22
+ # which is how serve is told to return.
23
+ def self.accept(server)
24
+ server.accept
25
+ rescue IOError
26
+ nil
27
+ end
28
+
29
+ # Saves body as dir/name whole or not at all: it is written
30
+ # to name.tmp and renamed, so a collector stopped mid-write
31
+ # leaves no partial report under a run's name.
32
+ def self.save(dir, name, body)
33
+ path = "#{dir}/#{name}"
34
+ File.write("#{path}.tmp", body)
35
+ File.rename("#{path}.tmp", path)
36
+ end
37
+
38
+ # Hands the body of the one HTTP request on client to the
39
+ # block, which saves it, and returns whether it was saved.
40
+ # dir is where reports are saved, which GET /ready checks.
41
+ # A client that closes without sending a request, eg a
42
+ # check that the collector is up, is closed unanswered and
43
+ # nothing is saved.
44
+ def self.receive(client, dir, &)
25
45
  headers = []
26
46
  while (line = client.gets) && line != "\r\n"
27
47
  headers << line
28
48
  end
29
- body = headers.empty? ? nil : answer(client, headers)
49
+ saved = !headers.empty? && respond(client, headers, dir, &)
30
50
  client.close
31
- body
51
+ saved
52
+ end
53
+
54
+ # The check each GET path makes of the collector's dir.
55
+ # GET /alive passes whenever the collector answers.
56
+ # GET /ready passes only when dir is a writable directory,
57
+ # so a report sent now could be saved.
58
+ PROBES = {
59
+ '/alive' => ->(_dir) { true },
60
+ '/ready' => lambda do |dir|
61
+ File.directory?(dir) && File.writable?(dir)
62
+ end
63
+ }.freeze
64
+
65
+ # Answers the request with headers on client, and returns
66
+ # whether a report was saved in dir.
67
+ # A GET of a path in PROBES is answered 200 when its check
68
+ # passes, 503 when not. Nothing is saved for it.
69
+ # Any other request is a report, handed to answer.
70
+ def self.respond(client, headers, dir, &)
71
+ probe = PROBES[headers.first[/\AGET (\S+) /, 1]]
72
+ return answer(client, headers, &) if probe.nil?
73
+
74
+ up = probe.call(dir)
75
+ reply(client, up ? '200 OK' : '503 Service Unavailable')
76
+ false
32
77
  end
33
78
 
34
- # The body of a request with headers, read from client,
35
- # which is then answered 200.
79
+ # Reads the body of a request with headers from client and
80
+ # yields it to be saved. Returns whether it was saved.
81
+ # The 200 is sent only once it is saved.
82
+ # So a 200 always means the report is on disk.
83
+ # A body shorter than its Content-Length is answered 400.
84
+ # Eg its sender died mid-send. It is not saved.
85
+ # A save that fails is answered 500.
36
86
  def self.answer(client, headers)
37
- length = headers.sum do |line|
87
+ length = content_length(headers)
88
+ body = client.read(length).to_s
89
+ whole = body.bytesize == length
90
+ yield body if whole
91
+ reply(client, whole ? '200 OK' : '400 Bad Request')
92
+ whole
93
+ rescue SystemCallError
94
+ reply(client, '500 Internal Server Error')
95
+ false
96
+ end
97
+
98
+ # The byte length headers give the request's body; 0 when
99
+ # they give none.
100
+ def self.content_length(headers)
101
+ headers.sum do |line|
38
102
  name, value = line.split(':', 2)
39
103
  name.casecmp?('content-length') ? value.to_i : 0
40
104
  end
41
- body = client.read(length)
42
- client.write("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n")
43
- body
105
+ end
106
+
107
+ # Writes an HTTP response of status, with no body, to client.
108
+ def self.reply(client, status)
109
+ client.write("HTTP/1.1 #{status}\r\nContent-Length: 0\r\n\r\n")
44
110
  end
45
111
  end
46
112
  end
@@ -1,9 +1,83 @@
1
1
  module PathCoverage
2
2
  # The operators whose predicate picks an arm.
3
- CHOICES = %w[?: if].freeze
3
+ CHOICES = %w[?: if unless].freeze
4
4
 
5
- # One arm of a ternary or an if: kind is :then or :else,
6
- # extent its byte range, nil for an if's arm with no
5
+ # The choices whose arms are statements, so whose path is
6
+ # decided by their predicate alone.
7
+ STATEMENT_CHOICES = %w[if unless].freeze
8
+
9
+ # The operator of a safe-navigation chain, eg user&.account.
10
+ SAFE = '&.'.freeze
11
+
12
+ # The operator of a leaf with sites inside it, eg
13
+ # ids.select { |i| i.odd? }.any? as an operand of an &&: its
14
+ # operands are those sites' Conditions, then the leaf's own byte
15
+ # range, whose outcome is the leaf's value.
16
+ CONTAINS = 'contains'.freeze
17
+
18
+ # A &. receiver's outcome: not nil, so the call is made, false
19
+ # included, or nil, so the rest of the chain is skipped.
20
+ PRESENCE = ['not nil', 'nil'].freeze
21
+
22
+ # The writes whose target decides whether their right side
23
+ # runs, as an unless or an if decides its arm: x ||= v runs v
24
+ # when x is false, x &&= v when x is true.
25
+ WRITES = %w[||= &&=].freeze
26
+
27
+ # The predicate outcome on which each choice takes its then
28
+ # arm: an unless's is false, as is a ||= write's.
29
+ THEN_ON = Hash.new(true).merge('unless' => false, '||=' => false)
30
+ .freeze
31
+
32
+ # The methods that call their block an item at a time until
33
+ # it decides, its last answer deciding the result.
34
+ STOP_EARLY = %w[
35
+ any? all? none? find detect find_index take_while
36
+ ].freeze
37
+
38
+ # How many items a block method's block was called with, as
39
+ # its receiver's leaf outcome: none, one, or two or more.
40
+ ITEMS = ['0 items', '1 item', '2+ items'].freeze
41
+
42
+ # Every path a stop-early block method's call can take: how
43
+ # many items its block was called with, and its last answer,
44
+ # nil when it was never called.
45
+ STOP_EARLY_PATHS = [
46
+ [ITEMS[0], nil],
47
+ [ITEMS[1], true], [ITEMS[1], false],
48
+ [ITEMS[2], true], [ITEMS[2], false]
49
+ ].freeze
50
+
51
+ # The methods that call their block with every item.
52
+ EVERY_ITEM = %w[
53
+ select filter reject partition count filter_map
54
+ ].freeze
55
+
56
+ # A block's answers over every item it was called with.
57
+ ANSWERS = ['all true', 'all false', 'both'].freeze
58
+
59
+ # Every path an every-item block method's call can take: how
60
+ # many items its block was called with, and its answers,
61
+ # which can be both only with two or more items.
62
+ EVERY_ITEM_PATHS = [
63
+ [ITEMS[0], nil],
64
+ [ITEMS[1], ANSWERS[0]], [ITEMS[1], ANSWERS[1]],
65
+ [ITEMS[2], ANSWERS[0]], [ITEMS[2], ANSWERS[1]],
66
+ [ITEMS[2], ANSWERS[2]]
67
+ ].freeze
68
+
69
+ # The block methods whose value follows from their path alone:
70
+ # all but find and detect, whose found item may itself be nil.
71
+ VALUED = [*STOP_EARLY - %w[find detect], *EVERY_ITEM].freeze
72
+
73
+ # Every block method whose call is a site, by its paths.
74
+ BLOCK_PATHS = [
75
+ *STOP_EARLY.map { |name| [name, STOP_EARLY_PATHS] },
76
+ *EVERY_ITEM.map { |name| [name, EVERY_ITEM_PATHS] }
77
+ ].to_h.freeze
78
+
79
+ # One arm of a ternary, an if or an unless: kind is :then or
80
+ # :else, extent its byte range, nil for an arm with no
7
81
  # statements, and body the Condition it is, when it is one,
8
82
  # else nil for a plain value or statements, which decide
9
83
  # nothing.
@@ -1,33 +1,45 @@
1
+ require_relative 'configuration_paths'
2
+
1
3
  module PathCoverage
2
4
  # Raised for a start block that cannot be applied.
3
5
  class ConfigurationError < StandardError; end
4
6
 
5
7
  # The settings given in a PathCoverage.start block.
6
8
  class Configuration
9
+ include ConfigurationPaths
10
+
7
11
  GLOB_FLAGS = File::FNM_PATHNAME | File::FNM_EXTGLOB
8
12
  SETTINGS = %w[
9
- collector cover coverage_dir measure report root
13
+ collector cover coverage_dir group ignore measure report root
14
+ skip treat
10
15
  ].freeze
11
- REPORTERS = %w[gaps html post].freeze
12
- MEASURES = %w[conditions paths].freeze
16
+ REPORTERS = %w[gaps html post summary].freeze
17
+ MEASURES = %w[decisions methods].freeze
13
18
 
14
19
  # Evaluates the settings block against a new configuration.
15
20
  def initialize(&)
16
21
  @globs = []
22
+ @skips = []
23
+ @groups = {}
17
24
  @reporters = [:gaps]
18
- @measures = [:conditions]
25
+ @measures = [:decisions]
26
+ @ignored = []
27
+ @treats = []
19
28
  instance_eval(&)
20
29
  end
21
30
 
22
31
  # The reporters chosen with report, :gaps when none were.
23
32
  attr_reader :reporters
24
33
 
25
- # What is measured, chosen with measure, :conditions when
26
- # nothing was.
34
+ # What is measured, chosen with measure.
35
+ # :decisions, the paths within each decision, when nothing
36
+ # was chosen.
27
37
  attr_reader :measures
28
38
 
29
- # Chooses what is measured, eg measure :conditions, :paths;
30
- # refuses a name there is no measure for.
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.
31
43
  def measure(*names)
32
44
  check('measure', names, MEASURES)
33
45
  @measures = names.map(&:to_sym)
@@ -57,6 +69,23 @@ module PathCoverage
57
69
  @globs << glob
58
70
  end
59
71
 
72
+ # Adds a glob, relative to root, of files not to instrument,
73
+ # though a cover glob matches them, eg skip 'source/vendor/**'.
74
+ def skip(glob)
75
+ @skips << glob
76
+ end
77
+
78
+ # { name => [glob, ...] } of each group given, in order.
79
+ # Eg { 'Models' => ['source/models/**/*.rb'] }.
80
+ attr_reader :groups
81
+
82
+ # Names a group of covered files, those matching any of
83
+ # globs, relative to root, so the HTML index gives them a
84
+ # tab of their own, eg group 'Models', 'source/models/**'.
85
+ def group(name, *globs)
86
+ @groups[name] = globs
87
+ end
88
+
60
89
  # Chooses the reporters run at exit, eg report :gaps;
61
90
  # refuses a name there is no reporter for.
62
91
  def report(*names)
@@ -83,10 +112,11 @@ module PathCoverage
83
112
  @coverage_dir = dir
84
113
  end
85
114
 
86
- # Every file the cover globs match under root, as full
87
- # paths, whether or not the run loads it.
115
+ # Every file the cover globs match under root, and no skip
116
+ # glob does, as full paths, whether or not the run loads it.
88
117
  def files
89
- Dir.glob(@globs, base: @root).sort.map { |f| "#{@root}/#{f}" }
118
+ found = Dir.glob(@globs, base: @root).sort
119
+ found.reject { |f| skipped?(f) }.map { |f| "#{@root}/#{f}" }
90
120
  end
91
121
 
92
122
  # Where the JSON report goes.
@@ -99,13 +129,23 @@ module PathCoverage
99
129
  path.start_with?("#{@root}/") && matches?(relative(path))
100
130
  end
101
131
 
102
- # True when the root-relative path matches a cover glob.
132
+ # True when the root-relative path matches a cover glob and
133
+ # no skip glob.
103
134
  def matches?(relative_path)
135
+ return false if skipped?(relative_path)
136
+
104
137
  @globs.any? do |glob|
105
138
  File.fnmatch?(glob, relative_path, GLOB_FLAGS)
106
139
  end
107
140
  end
108
141
 
142
+ # True when the root-relative path matches a skip glob.
143
+ def skipped?(relative_path)
144
+ @skips.any? do |glob|
145
+ File.fnmatch?(glob, relative_path, GLOB_FLAGS)
146
+ end
147
+ end
148
+
109
149
  # path relative to root, eg "source/kata.rb".
110
150
  def relative(path)
111
151
  path.delete_prefix("#{@root}/")
@@ -0,0 +1,56 @@
1
+ require_relative 'kinds'
2
+
3
+ module PathCoverage
4
+ # The settings of a PathCoverage.start block that change which
5
+ # paths a site has: ignore, eg ignore '&&=', and treat, eg
6
+ # treat 'name', as: :present. It relies on its includer's
7
+ # @ignored, @treats and check.
8
+ module ConfigurationPaths
9
+ TREATMENTS = %w[present nonempty].freeze
10
+
11
+ # The operators whose sites are not measured, chosen with
12
+ # ignore, eg ['&&=']; none when nothing was chosen.
13
+ attr_reader :ignored
14
+
15
+ # Chooses operators whose sites are not measured, eg
16
+ # ignore '&&=' measures no &&= write; a site inside one, eg
17
+ # on its right side, still is. Refuses an operator there
18
+ # is no kind of site for.
19
+ def ignore(*operators)
20
+ check('operator', operators, Kinds::BY_OPERATOR.keys)
21
+ @ignored = operators
22
+ end
23
+
24
+ # Treats calls to the method named name as never nil, with
25
+ # as: :present, so a &. after such a call has no nil path;
26
+ # or as true exactly when its receiver has items, with as:
27
+ # :nonempty, eg any?. It applies in each covered file
28
+ # matching glob, relative to root, or in every one with no
29
+ # glob. Refuses any other treatment.
30
+ def treat(name, as:, in: nil)
31
+ check('treatment', [as], TREATMENTS)
32
+ @treats << [name, as.to_s, binding.local_variable_get(:in)]
33
+ end
34
+
35
+ # The names of the methods treated as as, eg 'present', in
36
+ # the covered file at relative_path.
37
+ def treated(as, relative_path)
38
+ flags = Configuration::GLOB_FLAGS
39
+ @treats.filter_map do |name, treatment, glob|
40
+ next unless treatment == as
41
+
42
+ name if glob.nil? || File.fnmatch?(glob, relative_path, flags)
43
+ end
44
+ end
45
+
46
+ # The settings that change which paths a site has, eg
47
+ # { 'ignored' => ['&&='],
48
+ # 'treats' => [['name', 'present', nil]] },
49
+ # each only when set: runs whose fingerprints differ list
50
+ # their sites' paths differently.
51
+ def fingerprint
52
+ all = { 'ignored' => @ignored, 'treats' => @treats }
53
+ all.reject { |_, setting| setting.empty? }
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,77 @@
1
+ require_relative 'summary'
2
+ require_relative 'totals'
3
+
4
+ module PathCoverage
5
+ # The :summary reporter's lines: each total as SimpleCov prints
6
+ # its line and branch coverage, eg
7
+ # "Path coverage within decisions: 519 / 623 (83.31%)". With
8
+ # groups configured each group is a heading, its own totals
9
+ # indented under it, and there is no total over every group.
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
18
+
19
+ all = group_totals(site_paths, methods, paths, groups)
20
+ all.flat_map do |name, totals|
21
+ ["#{name}:", *totals_lines(totals).map { |line| " #{line}" }]
22
+ end
23
+ end
24
+
25
+ # { '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)
29
+ totals = { 'decisions' => Totals.decisions(site_paths) }
30
+ return totals if paths.nil?
31
+
32
+ totals.merge('methods' => Totals.methods(methods, paths))
33
+ end
34
+
35
+ # { group name => totals } of each group's own sites and
36
+ # methods alone.
37
+ def self.group_totals(site_paths, methods, paths, groups)
38
+ groups.to_h do |name, globs|
39
+ own_sites = own(site_paths, globs)
40
+ [name, totals(own_sites, own(methods, globs), paths)]
41
+ end
42
+ end
43
+
44
+ # The entries of sites, keyed by site name, in a file matching
45
+ # any of globs.
46
+ def self.own(sites, globs)
47
+ sites.select do |site, _|
48
+ Summary.matches?(Summary.path_of(site), globs)
49
+ end
50
+ end
51
+
52
+ # The line naming the operators ignored, eg "Path coverage
53
+ # ignores: &&="; none when none are.
54
+ def self.ignored_lines(ignored)
55
+ return [] if ignored.empty?
56
+
57
+ ["Path coverage ignores: #{ignored.join(', ')}"]
58
+ end
59
+
60
+ # A line per total, eg "Path coverage within methods: 1 / 4
61
+ # (25.00%)".
62
+ def self.totals_lines(totals)
63
+ totals.map do |name, pair|
64
+ "Path coverage within #{name}: #{figure(pair)}"
65
+ end
66
+ end
67
+
68
+ # [taken, possible] as "taken / possible (percent)", its
69
+ # percent to two places, or - when no path is possible.
70
+ def self.figure((taken, possible))
71
+ return "#{taken} / #{possible} (-)" if possible.zero?
72
+
73
+ percent = format('%.2f%%', 100.0 * taken / possible)
74
+ "#{taken} / #{possible} (#{percent})"
75
+ end
76
+ end
77
+ end
@@ -4,28 +4,44 @@ module PathCoverage
4
4
  # count stays in the operands and paths for reporters that
5
5
  # show them all.
6
6
  module Gaps
7
- # The gap lines of operands and paths, as Result gives
8
- # them, in manifest order; texts is { site => each leaf's
9
- # source text }.
10
- def self.lines(operands, paths: {}, texts: {})
7
+ # The gap lines of operands, paths and outside, as Result
8
+ # gives them, in manifest order; texts is { site => each
9
+ # leaf's source text }.
10
+ def self.lines(operands, paths: {}, texts: {}, outside: {})
11
11
  operands.flat_map do |site, counts|
12
12
  if counts.all? { |each| never_ran?(each) }
13
13
  next ["#{site} never reached"]
14
14
  end
15
15
 
16
- path_gaps(site, paths.fetch(site, []), texts[site])
16
+ path_gaps(site, paths.fetch(site, []), texts[site]) +
17
+ ruled_out(site, outside.fetch(site, {}), texts[site])
17
18
  end
18
19
  end
19
20
 
21
+ # One line per path site took that a treatment rules out,
22
+ # as Result#outside gives them, texts being each leaf's
23
+ # source text.
24
+ def self.ruled_out(site, taken, texts)
25
+ taken.keys.map do |path|
26
+ "#{site} path taken that a treatment rules out: " \
27
+ "#{steps(path, texts)}"
28
+ end
29
+ end
30
+
31
+ # The outcome of each leaf path reaches, each named by its
32
+ # text, eg "`u.name` nil".
33
+ def self.steps(path, texts)
34
+ path.zip(texts).filter_map do |outcome, text|
35
+ "#{code(text)} #{outcome}" unless outcome.nil?
36
+ end.join(', ')
37
+ end
38
+
20
39
  # One line per path site never took, as Result#paths gives
21
40
  # them, naming the outcome of each leaf the path reaches,
22
41
  # texts being each leaf's source text.
23
42
  def self.path_gaps(site, paths, texts)
24
43
  paths.select { |each| each[:count].zero? }.map do |each|
25
- steps = each[:path].zip(texts).filter_map do |outcome, text|
26
- "#{code(text)} #{outcome}" unless outcome.nil?
27
- end
28
- "#{site} path never taken: #{steps.join(', ')}"
44
+ "#{site} path never taken: #{steps(each[:path], texts)}"
29
45
  end
30
46
  end
31
47
 
@@ -13,21 +13,10 @@
13
13
  <body>
14
14
  <main class="page">
15
15
  <h1>Path coverage</h1>
16
+ {{tabs}}
16
17
  <div class="box">
17
- <table id="files">
18
- <thead>
19
- <tr>
20
- <th aria-sort="none"><button type="button" data-column="0" data-type="text">File</button></th>
21
- <th class="num" aria-sort="none"><button type="button" data-column="1" data-type="number">Sites</button></th>
22
- <th class="num" aria-sort="none"><button type="button" data-column="2" data-type="number">Outcomes seen</button></th>
23
- <th class="num" aria-sort="none"><button type="button" data-column="3" data-type="number">Missing</button></th>
24
- </tr>
25
- </thead>
26
- <tbody>
27
- {{rows}}
28
- </tbody>
29
- </table>
30
- </div>
18
+ {{tables}}
19
+ </div>{{ignored}}
31
20
  </main>
32
21
  <script>
33
22
  //<![CDATA[