path_coverage 0.1.0 → 0.3.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 (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +315 -107
  3. data/lib/path_coverage/calls.rb +55 -23
  4. data/lib/path_coverage/card.rb +14 -0
  5. data/lib/path_coverage/condition.rb +33 -3
  6. data/lib/path_coverage/configuration.rb +43 -22
  7. data/lib/path_coverage/configuration_paths.rb +58 -0
  8. data/lib/path_coverage/console_summary.rb +75 -0
  9. data/lib/path_coverage/gaps.rb +32 -13
  10. data/lib/path_coverage/html/file.html +5 -5
  11. data/lib/path_coverage/html/index.html +6 -18
  12. data/lib/path_coverage/html/settings.js +50 -0
  13. data/lib/path_coverage/html/side.js +341 -413
  14. data/lib/path_coverage/html/sort.js +24 -21
  15. data/lib/path_coverage/html/style.css +339 -207
  16. data/lib/path_coverage/html/tabs.js +15 -0
  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 +111 -0
  20. data/lib/path_coverage/html_cells.rb +66 -0
  21. data/lib/path_coverage/html_file.rb +82 -68
  22. data/lib/path_coverage/html_index.rb +95 -41
  23. data/lib/path_coverage/html_index_head.rb +29 -0
  24. data/lib/path_coverage/html_labels.rb +23 -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 +69 -56
  28. data/lib/path_coverage/html_statements.rb +166 -0
  29. data/lib/path_coverage/html_syntax.rb +156 -0
  30. data/lib/path_coverage/html_tabs.rb +26 -0
  31. data/lib/path_coverage/html_template.rb +6 -0
  32. data/lib/path_coverage/html_tree.rb +59 -74
  33. data/lib/path_coverage/json_reader.rb +101 -0
  34. data/lib/path_coverage/json_writer.rb +52 -7
  35. data/lib/path_coverage/kernel.rb +55 -53
  36. data/lib/path_coverage/kinds/base.rb +71 -0
  37. data/lib/path_coverage/kinds/block.rb +137 -0
  38. data/lib/path_coverage/kinds/case.rb +92 -0
  39. data/lib/path_coverage/kinds/chain.rb +45 -0
  40. data/lib/path_coverage/kinds/choice.rb +68 -0
  41. data/lib/path_coverage/kinds/contains.rb +113 -0
  42. data/lib/path_coverage/kinds/safe.rb +153 -0
  43. data/lib/path_coverage/kinds/write.rb +54 -0
  44. data/lib/path_coverage/kinds.rb +37 -0
  45. data/lib/path_coverage/manifest.rb +60 -20
  46. data/lib/path_coverage/manifest_blocks.rb +48 -10
  47. data/lib/path_coverage/manifest_case.rb +2 -3
  48. data/lib/path_coverage/manifest_chains.rb +76 -0
  49. data/lib/path_coverage/manifest_choices.rb +32 -18
  50. data/lib/path_coverage/manifest_folding.rb +91 -0
  51. data/lib/path_coverage/manifest_ignoring.rb +35 -0
  52. data/lib/path_coverage/manifest_writes.rb +67 -0
  53. data/lib/path_coverage/merge.rb +19 -3
  54. data/lib/path_coverage/method_statements.rb +300 -0
  55. data/lib/path_coverage/post.rb +8 -8
  56. data/lib/path_coverage/recorder.rb +20 -79
  57. data/lib/path_coverage/recorder_blocks.rb +68 -0
  58. data/lib/path_coverage/recorder_cases.rb +23 -0
  59. data/lib/path_coverage/recorder_chains.rb +40 -0
  60. data/lib/path_coverage/recorder_writes.rb +26 -0
  61. data/lib/path_coverage/render.rb +56 -47
  62. data/lib/path_coverage/result.rb +19 -0
  63. data/lib/path_coverage/rewriter.rb +69 -107
  64. data/lib/path_coverage/rewriter_nested.rb +62 -0
  65. data/lib/path_coverage/site_paths.rb +9 -58
  66. data/lib/path_coverage/site_tree.rb +21 -57
  67. data/lib/path_coverage/statement_arms.rb +161 -0
  68. data/lib/path_coverage/summary.rb +41 -17
  69. data/lib/path_coverage/totals.rb +6 -12
  70. data/lib/path_coverage/version.rb +1 -1
  71. data/lib/path_coverage/within_methods.rb +92 -0
  72. metadata +40 -11
  73. data/lib/path_coverage/html/toggle.js +0 -12
  74. data/lib/path_coverage/html_paths.rb +0 -138
  75. data/lib/path_coverage/path_gaps.rb +0 -41
  76. data/lib/path_coverage/path_manifest.rb +0 -110
@@ -0,0 +1,15 @@
1
+ // Switches the index page's tabs: a click on a tab selects it and
2
+ // shows the table it controls, hiding every other tab's table.
3
+ (function () {
4
+ var tabs = Array.from(document.querySelectorAll('[role="tab"]'));
5
+ tabs.forEach(function (tab) {
6
+ tab.addEventListener('click', function () {
7
+ tabs.forEach(function (other) {
8
+ var selected = other === tab;
9
+ other.setAttribute('aria-selected', selected ? 'true' : 'false');
10
+ var table = document.getElementById(other.getAttribute('aria-controls'));
11
+ table.hidden = !selected;
12
+ });
13
+ });
14
+ });
15
+ })();
@@ -0,0 +1,27 @@
1
+ // Switches the page between light and dark as the title bar's theme
2
+ // button is pressed: the page's html element names the theme, and
3
+ // the page's CSS draws it. The theme is a setting, carried from page
4
+ // to page as settings.js has it; a page with none opens dark.
5
+ (function () {
6
+ var settings = window.pathCoverageSettings;
7
+ var button = document.querySelector('button.theme');
8
+ if (!button) { return; }
9
+
10
+ // Names theme on the html element, labels the button with the
11
+ // theme it switches to, and makes every link carry theme.
12
+ function show(theme) {
13
+ document.documentElement.setAttribute('data-theme', theme);
14
+ var other = theme === 'dark' ? 'light' : 'dark';
15
+ button.textContent = theme === 'dark' ? '☀' : '☾';
16
+ button.setAttribute('aria-label', 'Switch to ' + other);
17
+ button.title = 'Switch to ' + other;
18
+ settings.carry('theme', theme);
19
+ }
20
+
21
+ show(settings.given('theme', ['light', 'dark']) || 'dark');
22
+ button.addEventListener('click', function () {
23
+ var theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'light' : 'dark';
24
+ show(theme);
25
+ settings.keep('theme', theme);
26
+ });
27
+ })();
@@ -0,0 +1,30 @@
1
+ module PathCoverage
2
+ # The branches of a site'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. CSS draws each kind as lines a whole row
6
+ # tall, so a branch joins the rows above and below it.
7
+ module HtmlBranch
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
+
18
+ # Example: ths gives a tee, a dash and a space cell.
19
+ # The cell drawing branch: a cell of each of its kinds,
20
+ # hidden from screen readers, as the rows' order already
21
+ # says how they nest.
22
+ def self.cell(branch)
23
+ cells = branch.chars.map do |kind|
24
+ %(<span class="bx" data-bx="#{kind}"></span>)
25
+ end
26
+ attrs = 'class="branch" data-branch="" aria-hidden="true"'
27
+ %(<span #{attrs}>#{cells.join}</span>)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,111 @@
1
+ require_relative 'card'
2
+ require_relative 'html_statements'
3
+ require_relative 'html_tree'
4
+ require_relative 'method_statements'
5
+ require_relative 'site_tree'
6
+
7
+ module PathCoverage
8
+ # The cards of one file's page, as Card gives them, in the rail
9
+ # beside its code: a card for each of its methods, and one for
10
+ # each span of its lines holding sites at the top level; each
11
+ # site's row with its tree folded under it. It relies on
12
+ # HtmlReport.located to place each tree's rows in the source.
13
+ module HtmlCards
14
+ # The cards of each method of the file at path, parsed as
15
+ # Manifest.parse gives it; site_paths as Result#paths gives
16
+ # them, reached as Recorder#reached does.
17
+ def self.methods(path, parsed, (site_paths, reached))
18
+ source, sites = parsed.values_at(:source, :sites)
19
+ names = MethodStatements.names(source, path, sites)
20
+ lasts = MethodStatements.lasts(source, path, sites)
21
+ tree = ->(at) { fold(source, sites, site_paths, at) }
22
+ MethodStatements.of(source, path, sites).map do |site, rows|
23
+ taken = MethodStatements.taken(rows, site_paths, reached)
24
+ named = [names[site], lasts[site]]
25
+ method_card(site, named, rows, taken, &tree)
26
+ end
27
+ end
28
+
29
+ # The Card of the def at site named name, ending on line last,
30
+ # its rows' taken as taken gives them.
31
+ def self.method_card(site, (name, last), rows, taken, &)
32
+ html = HtmlStatements.card(site, name, rows, taken, last, &)
33
+ first, column = site.split(':').last(2).map { Integer(_1) }
34
+ status = HtmlStatements.status(rows, taken)
35
+ Card.new(first:, last:, column:, status:, top: false,
36
+ sites: nil, html:)
37
+ end
38
+
39
+ # A top card for each span of lines holding sites at the top
40
+ # level of the file at path, outside every def, as each
41
+ # span's lines share one button: a row per site in it. lines
42
+ # are as SourceLines.of gives them.
43
+ def self.tops(path, parsed, lines, site_paths)
44
+ source, sites = parsed.values_at(:source, :sites)
45
+ tree = ->(at) { fold(source, sites, site_paths, at) }
46
+ scopes = scopes(source)
47
+ spans(path, parsed, lines).map do |span, rows|
48
+ top(path, span, rows, site_paths, scopes[span.begin], &tree)
49
+ end
50
+ end
51
+
52
+ # The top card of the span of lines of the file at path,
53
+ # holding rows, titled scope.
54
+ def self.top(path, span, rows, site_paths, scope, &)
55
+ taken = MethodStatements.taken(rows, site_paths)
56
+ site = "#{path}:#{span.begin}:0"
57
+ html = HtmlStatements.top_card(site, scope, rows, taken,
58
+ span.end, &)
59
+ Card.new(first: span.begin, last: span.end, column: 0,
60
+ status: HtmlStatements.status(rows, taken), top: true,
61
+ sites: rows.size, html:)
62
+ end
63
+
64
+ # A lambda giving the scope of a line of source, as scope
65
+ # gives it, its modules and classes found once.
66
+ def self.scopes(source)
67
+ holders = holders_in(Prism.parse(source).value)
68
+ ->(line) { scope(holders, line) }
69
+ end
70
+
71
+ # Example: line 2, inside module Shop on lines 1 to 9, gives
72
+ # Shop.
73
+ # The name of the innermost of holders, modules and classes,
74
+ # whose lines hold line, a top card's scope, as a def is a
75
+ # method card's; file for a line in none.
76
+ def self.scope(holders, line)
77
+ holding = holders.select do |node|
78
+ location = node.location
79
+ (location.start_line..location.end_line).cover?(line)
80
+ end
81
+ holding.empty? ? 'file' : holding.last.name.to_s
82
+ end
83
+
84
+ # The kinds of node that are a top card's scope.
85
+ HOLDERS = [Prism::ModuleNode, Prism::ClassNode].freeze
86
+
87
+ # Every module and class node in the tree at node, each
88
+ # before those nested in it.
89
+ def self.holders_in(node)
90
+ own = HOLDERS.include?(node.class) ? [node] : []
91
+ own + node.compact_child_nodes.flat_map { holders_in(_1) }
92
+ end
93
+
94
+ # { span => its top-level rows } of the file at path, each
95
+ # row under the span its line is in.
96
+ def self.spans(path, parsed, lines)
97
+ source, sites = parsed.values_at(:source, :sites)
98
+ rows = MethodStatements.top_rows(source, path, sites)
99
+ rows.group_by { lines[_1.line - 1][:span] }
100
+ end
101
+
102
+ # The rows of site's tree, as a file's page draws them, for
103
+ # its row's fold in a card.
104
+ def self.fold(source, sites, site_paths, site)
105
+ condition = sites.fetch(site)
106
+ tree = SiteTree.of(source, condition, site_paths.fetch(site))
107
+ located = HtmlReport.located(source, condition, tree)
108
+ HtmlTree.site_rows(located).join
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,66 @@
1
+ require_relative 'kinds'
2
+
3
+ module PathCoverage
4
+ # The cells of a row of a site's tree on a file's page, one per
5
+ # path. A cell says what its path did: its text a letter, eg T
6
+ # or F, or empty with a mark CSS draws, eg a tick; with text to
7
+ # show on hover when its mark alone may not say enough.
8
+ module HtmlCells
9
+ # [text, mark, hover] of a leaf's or a condition's cell, by
10
+ # the value it took on the path: a letter for each value
11
+ # but a &. receiver's, which is a mark, present or absent,
12
+ # hovering as not nil or nil.
13
+ OUTCOMES = {
14
+ true => ['T'], false => ['F'], nil => [''],
15
+ ITEMS[0] => ['0'], ITEMS[1] => ['1'], ITEMS[2] => ['*'],
16
+ ANSWERS[0] => ['T'], ANSWERS[1] => ['F'], ANSWERS[2] => ['TF'],
17
+ PRESENCE[0] => ['', 'present', PRESENCE[0]],
18
+ PRESENCE[1] => ['', 'absent', PRESENCE[1]]
19
+ }.freeze
20
+
21
+ # [text, mark] of an arm's cell, or of one only called: taken
22
+ # on a path that took it or called it, else blank.
23
+ TAKEN = { true => ['', 'taken'], false => [''] }.freeze
24
+
25
+ # The cells of values, each named as names gives it.
26
+ def self.of(values, names = OUTCOMES)
27
+ values.map { |value| cell(*names[value]) }.join
28
+ end
29
+
30
+ # One cell: text, and its mark and its hover, when it has
31
+ # them.
32
+ def self.cell(text, mark = nil, hover = nil)
33
+ attrs = +'class="cell" data-cell=""'
34
+ attrs << %( data-mark="#{mark}") if mark
35
+ attrs << %( data-hover="#{hover}") if hover
36
+ %(<span #{attrs}>#{text}</span>)
37
+ end
38
+
39
+ # Example: counts 0, 2 and 1284 give 0, * and *.
40
+ # One cell per path: 0 for a path never taken, marked as a
41
+ # gap, * for one taken any number of times, as whether it
42
+ # was taken is what counts; how many times shows on hover,
43
+ # so a count in the thousands never widens the cell.
44
+ def self.counts(counts)
45
+ counts.map do |count|
46
+ css, mark = MARKS[count.zero?]
47
+ hover = times(count)
48
+ %(<span class="#{css}" data-count="" data-hover="#{hover}">) +
49
+ %(#{mark}</span>)
50
+ end.join
51
+ end
52
+
53
+ # [class, mark] of a path's count cell, by whether its count
54
+ # is zero.
55
+ MARKS = { true => ['count never', '0'],
56
+ false => ['count', '*'] }.freeze
57
+
58
+ # Example: 1 gives 1 time, 1284 gives 1,284 times.
59
+ # How many times a path was taken, as its mark's hover reads
60
+ # it, its thousands apart, so a big count still reads.
61
+ def self.times(count)
62
+ digits = count.to_s.reverse.scan(/\d{1,3}/).join(',').reverse
63
+ "#{digits} #{count == 1 ? 'time' : 'times'}"
64
+ end
65
+ end
66
+ end
@@ -1,109 +1,123 @@
1
1
  require 'cgi/escape'
2
+ require_relative 'card'
3
+ require_relative 'html_methods'
2
4
  require_relative 'html_nav'
5
+ require_relative 'html_syntax'
3
6
  require_relative 'html_template'
4
- require_relative 'html_tree'
5
7
 
6
8
  module PathCoverage
7
- # One covered file's HTML page: its source, with the numbers
8
- # of the lines holding a condition as the one button that
9
- # opens the condition's tree.
9
+ # One covered file's HTML page: its source beside the rail of
10
+ # its cards, as Card gives them, one open at a time. A method's
11
+ # def number opens its card; a site outside every method has
12
+ # one button over its lines' numbers that opens their top card.
10
13
  module HtmlFile
11
14
  # The whole page for the file at path (relative to root),
12
15
  # written at files/<path>.html, so it links back to the
13
16
  # index one folder up for each folder in that.
14
- def self.page(path, lines, trees, grids = '')
15
- HtmlTemplate.fill(
16
- HtmlTemplate.asset('file.html'),
17
+ def self.page(path, lines, cards = [])
18
+ HtmlTemplate.page(
19
+ 'file.html',
17
20
  'path' => CGI.escapeHTML(path),
18
21
  'index' => "#{'../' * (path.count('/') + 1)}index.html",
19
- 'style' => HtmlTemplate.asset('style.css'),
20
22
  'script' => script,
21
- 'grids' => grids,
22
- 'jumps' => HtmlNav.jumps(lines),
23
- 'listing' => listing(lines, trees)
23
+ 'rail' => cards.map(&:html).join("\n"),
24
+ 'jumps' => HtmlNav.jumps(cards),
25
+ 'listing' => listing(lines, cards)
24
26
  )
25
27
  end
26
28
 
27
- # The page's script: its tree toggles, then its side rails.
29
+ # The page's script: its rail's behaviour, then its settings
30
+ # and its theme button.
28
31
  def self.script
29
- HtmlTemplate.asset('toggle.js') + HtmlTemplate.asset('side.js')
32
+ %w[side.js settings.js theme.js].map do |name|
33
+ HtmlTemplate.asset(name)
34
+ end.join
30
35
  end
31
36
 
32
- # The source listing of lines, as SourceLines.of gives them:
33
- # each run of lines sharing a span is one block, followed
34
- # by its hidden tree. trees is { span's first line =>
35
- # [SiteTree.of, ...] }.
36
- def self.listing(lines, trees = {})
37
- blocks = lines.chunk_while do |line, following|
38
- line[:span] && line[:span] == following[:span]
37
+ # The source listing of lines, as SourceLines.of gives them,
38
+ # marked with the methods of cards: a row per line, but one
39
+ # row over all the lines of a site outside every method.
40
+ def self.listing(lines, cards = [])
41
+ methods = HtmlMethods.of(cards)
42
+ tops = cards.select(&:top).to_h { |card| [card.first, card] }
43
+ rows = lines.chunk { |line| row_key(line, methods) }
44
+ body = rows.map do |key, row|
45
+ next line(row[0], cards, methods) unless key.is_a?(Range)
46
+
47
+ loose_block(row, cards, tops[key.begin])
39
48
  end
40
- gaps = HtmlNav.gap_lines(blocks)
41
- body = blocks.map { |block| block(block, trees, gaps) }
42
49
  %(<div class="code">\n#{body.join("\n")}\n</div>)
43
50
  end
44
51
 
45
- # A plain line, or the lines of one span behind one button
46
- # with the span's tree straight after them; either way after
47
- # its nav cell.
48
- def self.block(lines, trees, gaps)
49
- first = lines[0]
50
- number = first[:number]
51
- cell = HtmlNav.cell(number, gaps)
52
- return plain_line(first, cell) if first[:span].nil?
52
+ # The span of line's site when it is outside every method, so
53
+ # its lines share one row; else line's own number.
54
+ def self.row_key(line, methods)
55
+ span = line[:span]
56
+ loose = span && HtmlMethods.loose?(methods, span.begin)
57
+ loose ? span : line[:number]
58
+ end
53
59
 
54
- site_trees = trees.fetch(number, [])
55
- open = first[:status] == :gaps
56
- span_block(lines, site_trees.size, cell) +
57
- HtmlTree.of(number, site_trees, open)
60
+ # A line of its own, after its gutter, folded when it is in a
61
+ # method's body.
62
+ def self.line(line, cards, methods)
63
+ number = line[:number]
64
+ cell = HtmlNav.cell([number], cards)
65
+ folded = HtmlMethods.folded?(methods, number)
66
+ css = folded ? 'line method-folded' : 'line'
67
+ parts = number(number, methods) + code(line, methods)
68
+ %(<div class="#{css}">#{cell}#{parts}</div>)
58
69
  end
59
70
 
60
- # The lines of one span after their nav cell: one button over
61
- # all their numbers, and how many sites it opens when that is
62
- # two or more, beside their code.
63
- def self.span_block(lines, sites, cell)
64
- numbers = lines.map { |line| number(line[:number]) }.join
65
- codes = lines.map { |line| code(line) }.join
66
- srcs = %(<div class="srcs">#{codes}</div>)
67
- content = site_count(sites) + numbers
68
- button = button(lines[0], content)
69
- %(<div class="block">#{cell}#{button}#{srcs}</div>)
71
+ # The lines of a site outside every method after their gutter:
72
+ # one button over all their numbers, opening their top card,
73
+ # top, and how many sites it opens when that is two or more,
74
+ # beside their code.
75
+ def self.loose_block(lines, cards, top)
76
+ numbers = lines.map { |line| line[:number] }
77
+ cell = HtmlNav.cell(numbers, cards)
78
+ spans = numbers.map { |number| number(number, []) }.join
79
+ button = button(lines[0], top, site_count(top) + spans)
80
+ srcs = lines.map { |line| code(line, []) }.join
81
+ %(<div class="block">#{cell}#{button}) +
82
+ %(<div class="srcs">#{srcs}</div></div>)
70
83
  end
71
84
 
72
- # The count of a button's sites, shown only for two or more.
73
- def self.site_count(sites)
85
+ # The count of a top card's sites, shown only for two or more.
86
+ def self.site_count(top)
87
+ sites = top ? top.sites : 0
74
88
  return '' if sites < 2
75
89
 
76
90
  %(<span class="site-count" data-site-count="">#{sites}</span>)
77
91
  end
78
92
 
79
- # A line with no condition: its nav cell, number and code.
80
- def self.plain_line(line, cell)
81
- parts = cell + number(line[:number]) + code(line)
82
- %(<div class="line">#{parts}</div>)
83
- end
84
-
85
- # The button over a span's numbers, toggling the tree named
86
- # after its first line; expanded when that tree starts open.
87
- def self.button(first, numbers)
88
- expanded = first[:status] == :gaps
93
+ # The button over a loose site's numbers, wearing its top
94
+ # card's status, else its first line's, closed until its card
95
+ # opens.
96
+ def self.button(first, top, numbers)
97
+ status = top ? top.status : first[:status]
89
98
  [
90
- %(<button type="button" class="lnum"),
91
- %( data-status="#{first[:status]}"),
92
- %( aria-expanded="#{expanded}"),
93
- %( aria-controls="tree-#{first[:number]}">#{numbers}</button>)
99
+ %(<button type="button" class="lnum loose"),
100
+ %( data-status="#{status}" aria-expanded="false"),
101
+ %( aria-controls="paths-#{first[:number]}">),
102
+ numbers, '</button>'
94
103
  ].join
95
104
  end
96
105
 
97
- # One line's number, the target of a link to #L<number>.
98
- def self.number(number)
99
- attributes = %(id="L#{number}" data-line="#{number}")
100
- %(<span class="lineno" #{attributes}>#{number}</span>)
106
+ # One line's number, the target of a link to #L<number>; a
107
+ # def's marked to open its method's card.
108
+ def self.number(number, methods)
109
+ css = HtmlMethods.number_attributes(methods, number)
110
+ attributes = %(#{css} id="L#{number}" data-line="#{number}")
111
+ %(<span #{attributes}>#{number}</span>)
101
112
  end
102
113
 
103
- # One line's code, escaped.
104
- def self.code(line)
105
- text = CGI.escapeHTML(line[:text])
106
- %(<span class="src" data-src="">#{text}</span>)
114
+ # One line's code, escaped, each of its tokens, if any, in a
115
+ # span of its colour's class, marked with its methods.
116
+ def self.code(line, methods)
117
+ number, plain = line.values_at(:number, :text)
118
+ text = HtmlSyntax.html(plain, line.fetch(:tokens, []))
119
+ css = HtmlMethods.src_attributes(methods, number, plain)
120
+ %(<span #{css} data-src="">#{text}</span>)
107
121
  end
108
122
  end
109
123
  end
@@ -1,74 +1,128 @@
1
1
  require 'cgi/escape'
2
+ require_relative 'html_index_head'
3
+ require_relative 'html_tabs'
2
4
  require_relative 'html_template'
3
5
 
4
6
  module PathCoverage
5
7
  # The HTML index page: one sortable row per covered file,
6
- # each linking to that file's own page, then a Total row.
8
+ # each linking to that file's own page, then a Total row; with
9
+ # groups, one such table per tab.
7
10
  module HtmlIndex
8
- # The whole index page: the files table, sortable by any
9
- # column, with one row per Summary.files row, and total, as
10
- # Summary.total gives it, as its footer.
11
- def self.page(rows, total)
12
- HtmlTemplate.fill(
13
- HtmlTemplate.asset('index.html'),
14
- 'style' => HtmlTemplate.asset('style.css'),
15
- 'script' => HtmlTemplate.asset('sort.js'),
16
- 'rows' => rows.map { |row| row(row) }.join("\n"),
17
- 'total' => total_row(total),
18
- 'across' => total[:methods] ? ACROSS : ''
11
+ # The whole index page. rows are Summary.files rows and total
12
+ # their Summary.total. groups are [name, rows, total] for
13
+ # each group, each given a tab after All files; with none
14
+ # there is no tab bar, just the one table. Any operators
15
+ # ignored are named after the tables.
16
+ def self.page(rows, total, groups = [], ignored: [])
17
+ tables = [['All files', rows, total], *groups]
18
+ names = tables.map(&:first)
19
+ HtmlTemplate.page(
20
+ 'index.html',
21
+ 'script' => script(groups),
22
+ 'tabs' => groups.empty? ? '' : HtmlTabs.bar(names),
23
+ 'tables' => tables_of(tables),
24
+ 'ignored' => ignored_line(ignored)
19
25
  )
20
26
  end
21
27
 
22
- # The heading of the across column, there only when methods
23
- # are measured.
24
- ACROSS = '<th class="num" aria-sort="none"><button ' \
25
- 'type="button" data-column="3" data-type="number">' \
26
- "Across a method's decisions</button></th>".freeze
28
+ # The paragraph naming the operators ignored, eg
29
+ # "Ignored: &&="; empty when none are.
30
+ def self.ignored_line(ignored)
31
+ return '' if ignored.empty?
32
+
33
+ text = CGI.escapeHTML("Ignored: #{ignored.join(', ')}")
34
+ %(<p class="ignored" data-ignored="">#{text}</p>)
35
+ end
36
+
37
+ # Each of tables, [name, rows, total], as the table of its
38
+ # tab.
39
+ def self.tables_of(tables)
40
+ tables.each_with_index.map do |(_, rows, total), index|
41
+ table(index, rows, total)
42
+ end.join("\n")
43
+ end
44
+
45
+ # The page's script: its column sorting, with groups its tab
46
+ # switching, then its settings and its theme button.
47
+ def self.script(groups)
48
+ names = ['sort.js', *('tabs.js' unless groups.empty?),
49
+ 'settings.js', 'theme.js']
50
+ names.map { |name| HtmlTemplate.asset(name) }.join
51
+ end
52
+
53
+ # The index-th tab's table of rows, total as its footer; any
54
+ # but the first starts hidden. Only the first, All files,
55
+ # gives its rows ids, as the others repeat its rows.
56
+ def self.table(index, rows, total)
57
+ anchored = index.zero?
58
+ body = rows.map { |row| row(row, anchored:) }.join("\n")
59
+ head = HtmlIndexHead.row
60
+ [
61
+ open_tag(index),
62
+ "<thead>\n#{head}\n</thead>",
63
+ "<tbody>\n#{body}\n</tbody>",
64
+ "<tfoot>\n#{total_row(total)}\n</tfoot>",
65
+ '</table>'
66
+ ].join("\n")
67
+ end
68
+
69
+ # The opening tag of the index-th tab's table, which the tab
70
+ # of the same index controls; any but the first is hidden.
71
+ def self.open_tag(index)
72
+ hidden = index.zero? ? '' : ' hidden="hidden"'
73
+ id = %(id="files-#{index}")
74
+ %(<table class="files" #{id} role="tabpanel"#{hidden}>)
75
+ end
27
76
 
28
77
  # The table row of one Summary.files row; each cell has a
29
- # data-sort value for the page's column sorting.
30
- def self.row(row)
78
+ # data-sort value for the page's column sorting. An anchored
79
+ # row has the id row-<path>, which the file's page links to.
80
+ def self.row(row, anchored: false)
31
81
  path = CGI.escapeHTML(row[:path])
32
82
  link = %(<a href="files/#{path}.html">#{path}</a>)
33
- "<tr>#{cell(path, link)}#{counts(row).join}</tr>"
83
+ id = anchored ? %( id="row-#{path}") : ''
84
+ "<tr#{id}>#{cell(path, link)}#{counts(row).join}</tr>"
34
85
  end
35
86
 
36
- # The footer row: the sums over every file.
87
+ # The footer row: the sums over the table's files.
37
88
  def self.total_row(total)
38
89
  "<tr>#{cell('Total', 'Total')}#{counts(total).join}</tr>"
39
90
  end
40
91
 
41
92
  # The cells after the file's: its sites, then its total
42
- # within each decision, then, when methods are measured, its
43
- # total across a method's decisions.
93
+ # within each method.
44
94
  def self.counts(row)
45
- cells = [
46
- cell(row[:sites], row[:sites]),
47
- paths_cell(row[:decisions])
48
- ]
49
- row[:methods] ? cells + [paths_cell(row[:methods])] : cells
95
+ [cell(row[:sites], row[:sites]), *paths_cells(row[:methods])]
50
96
  end
51
97
 
52
- # A cell showing [taken, possible] paths, eg 3 / 6 (50%),
53
- # sorted by its percent. With no paths to take, as in a file
54
- # with no method of two or more decisions, it shows -,
55
- # titled with why, with an empty sort value, which sort.js
56
- # keeps last whichever way the column is sorted.
57
- def self.paths_cell(pair)
98
+ # Why a total of no paths shows no percent.
99
+ NONE = 'no method with two or more decisions'.freeze
100
+
101
+ # Three cells showing [taken, possible] paths, eg 3, / 6 and
102
+ # 50%, each sorted by its number, so they read 3 / 6 50%.
103
+ # With no paths to take, as in a group no file is in, or a
104
+ # file with no method of two or more decisions, the percent
105
+ # shows -, titled with NONE, saying why, with an empty sort
106
+ # value, which sort.js keeps last whichever way the column is
107
+ # sorted.
108
+ def self.paths_cells(pair)
58
109
  taken, possible = pair
59
- if possible.zero?
60
- return cell('', '-', 'no method with two or more decisions')
61
- end
110
+ counts = [cell(taken, taken, css: 'taken'),
111
+ cell(possible, "/ #{possible}", css: 'of')]
112
+ none = cell('', '-', NONE, css: 'percent')
113
+ return [*counts, none] if possible.zero?
62
114
 
63
115
  percent = (100.0 * taken / possible).round
64
- cell(percent, "#{taken} / #{possible} (#{percent}%)")
116
+ [*counts, cell(percent, "#{percent}%", css: 'percent')]
65
117
  end
66
118
 
67
119
  # One table cell, sorted by sort_value, showing content,
68
- # with title as its hover text when there is one.
69
- def self.cell(sort_value, content, title = nil)
120
+ # with title as its hover text and css as its class, each
121
+ # when there is one.
122
+ def self.cell(sort_value, content, title = nil, css: nil)
70
123
  hover = title ? %( title="#{title}") : ''
71
- %(<td data-sort="#{sort_value}"#{hover}>#{content}</td>)
124
+ klass = css ? %( class="#{css}") : ''
125
+ %(<td#{klass} data-sort="#{sort_value}"#{hover}>#{content}</td>)
72
126
  end
73
127
  end
74
128
  end
@@ -0,0 +1,29 @@
1
+ module PathCoverage
2
+ # The heading row of the HTML index's files tables: File and
3
+ # Sites, then the taken, / of and % columns of each file's total
4
+ # within each method, every column sortable.
5
+ module HtmlIndexHead
6
+ # The class and label of each of a total's three columns.
7
+ PARTS = [%w[taken taken], ['of', '/ of'], %w[percent %]].freeze
8
+
9
+ # The heading row.
10
+ def self.row
11
+ fixed = [
12
+ heading('', 0, 'text', 'File'),
13
+ heading(' class="num"', 1, 'number', 'Sites')
14
+ ]
15
+ parts = PARTS.each_with_index.map do |(css, label), i|
16
+ heading(%( class="#{css}"), 2 + i, 'number', label)
17
+ end
18
+ "<tr>\n#{(fixed + parts).join("\n")}\n</tr>"
19
+ end
20
+
21
+ # A sortable heading, attrs its th's, sorting the column-th
22
+ # column as type, its button showing label.
23
+ def self.heading(attrs, column, type, label)
24
+ data = %(data-column="#{column}" data-type="#{type}")
25
+ button = %(<button type="button" #{data}>#{label}</button>)
26
+ %(<th#{attrs} aria-sort="none">#{button}</th>)
27
+ end
28
+ end
29
+ end