path_coverage 0.2.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 (57) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +112 -113
  3. data/lib/path_coverage/calls.rb +4 -11
  4. data/lib/path_coverage/card.rb +14 -0
  5. data/lib/path_coverage/condition.rb +8 -1
  6. data/lib/path_coverage/configuration.rb +1 -17
  7. data/lib/path_coverage/configuration_paths.rb +4 -2
  8. data/lib/path_coverage/console_summary.rb +15 -17
  9. data/lib/path_coverage/gaps.rb +12 -9
  10. data/lib/path_coverage/html/file.html +5 -5
  11. data/lib/path_coverage/html/index.html +3 -1
  12. data/lib/path_coverage/html/settings.js +50 -0
  13. data/lib/path_coverage/html/side.js +341 -432
  14. data/lib/path_coverage/html/style.css +330 -225
  15. data/lib/path_coverage/html/theme.js +27 -0
  16. data/lib/path_coverage/html_branch.rb +30 -0
  17. data/lib/path_coverage/html_cards.rb +111 -0
  18. data/lib/path_coverage/html_cells.rb +66 -0
  19. data/lib/path_coverage/html_file.rb +82 -68
  20. data/lib/path_coverage/html_index.rb +28 -29
  21. data/lib/path_coverage/html_index_head.rb +10 -30
  22. data/lib/path_coverage/html_labels.rb +23 -0
  23. data/lib/path_coverage/html_methods.rb +66 -0
  24. data/lib/path_coverage/html_nav.rb +50 -50
  25. data/lib/path_coverage/html_report.rb +45 -49
  26. data/lib/path_coverage/html_statements.rb +166 -0
  27. data/lib/path_coverage/html_syntax.rb +156 -0
  28. data/lib/path_coverage/html_template.rb +6 -0
  29. data/lib/path_coverage/html_tree.rb +36 -72
  30. data/lib/path_coverage/json_reader.rb +21 -14
  31. data/lib/path_coverage/json_writer.rb +26 -13
  32. data/lib/path_coverage/kernel.rb +29 -49
  33. data/lib/path_coverage/kinds/block.rb +2 -1
  34. data/lib/path_coverage/kinds/case.rb +7 -4
  35. data/lib/path_coverage/kinds/contains.rb +6 -4
  36. data/lib/path_coverage/kinds/safe.rb +5 -3
  37. data/lib/path_coverage/manifest.rb +2 -1
  38. data/lib/path_coverage/manifest_blocks.rb +3 -4
  39. data/lib/path_coverage/manifest_case.rb +2 -3
  40. data/lib/path_coverage/manifest_choices.rb +4 -3
  41. data/lib/path_coverage/manifest_folding.rb +1 -1
  42. data/lib/path_coverage/merge.rb +3 -3
  43. data/lib/path_coverage/method_statements.rb +300 -0
  44. data/lib/path_coverage/recorder.rb +12 -31
  45. data/lib/path_coverage/render.rb +19 -22
  46. data/lib/path_coverage/rewriter.rb +36 -55
  47. data/lib/path_coverage/site_tree.rb +9 -0
  48. data/lib/path_coverage/statement_arms.rb +161 -0
  49. data/lib/path_coverage/summary.rb +11 -15
  50. data/lib/path_coverage/totals.rb +6 -12
  51. data/lib/path_coverage/version.rb +1 -1
  52. data/lib/path_coverage/within_methods.rb +92 -0
  53. metadata +16 -11
  54. data/lib/path_coverage/html/toggle.js +0 -12
  55. data/lib/path_coverage/html_paths.rb +0 -138
  56. data/lib/path_coverage/path_gaps.rb +0 -41
  57. data/lib/path_coverage/path_manifest.rb +0 -110
@@ -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
@@ -16,9 +16,8 @@ module PathCoverage
16
16
  def self.page(rows, total, groups = [], ignored: [])
17
17
  tables = [['All files', rows, total], *groups]
18
18
  names = tables.map(&:first)
19
- HtmlTemplate.fill(
20
- HtmlTemplate.asset('index.html'),
21
- 'style' => HtmlTemplate.asset('style.css'),
19
+ HtmlTemplate.page(
20
+ 'index.html',
22
21
  'script' => script(groups),
23
22
  'tabs' => groups.empty? ? '' : HtmlTabs.bar(names),
24
23
  'tables' => tables_of(tables),
@@ -43,18 +42,21 @@ module PathCoverage
43
42
  end.join("\n")
44
43
  end
45
44
 
46
- # The page's script: its column sorting, and, with groups,
47
- # its tab switching.
45
+ # The page's script: its column sorting, with groups its tab
46
+ # switching, then its settings and its theme button.
48
47
  def self.script(groups)
49
- sort = HtmlTemplate.asset('sort.js')
50
- groups.empty? ? sort : sort + HtmlTemplate.asset('tabs.js')
48
+ names = ['sort.js', *('tabs.js' unless groups.empty?),
49
+ 'settings.js', 'theme.js']
50
+ names.map { |name| HtmlTemplate.asset(name) }.join
51
51
  end
52
52
 
53
53
  # The index-th tab's table of rows, total as its footer; any
54
- # but the first starts hidden.
54
+ # but the first starts hidden. Only the first, All files,
55
+ # gives its rows ids, as the others repeat its rows.
55
56
  def self.table(index, rows, total)
56
- body = rows.map { |row| row(row) }.join("\n")
57
- head = HtmlIndexHead.of(total[:methods])
57
+ anchored = index.zero?
58
+ body = rows.map { |row| row(row, anchored:) }.join("\n")
59
+ head = HtmlIndexHead.row
58
60
  [
59
61
  open_tag(index),
60
62
  "<thead>\n#{head}\n</thead>",
@@ -73,11 +75,13 @@ module PathCoverage
73
75
  end
74
76
 
75
77
  # The table row of one Summary.files row; each cell has a
76
- # data-sort value for the page's column sorting.
77
- 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)
78
81
  path = CGI.escapeHTML(row[:path])
79
82
  link = %(<a href="files/#{path}.html">#{path}</a>)
80
- "<tr>#{cell(path, link)}#{counts(row).join}</tr>"
83
+ id = anchored ? %( id="row-#{path}") : ''
84
+ "<tr#{id}>#{cell(path, link)}#{counts(row).join}</tr>"
81
85
  end
82
86
 
83
87
  # The footer row: the sums over the table's files.
@@ -86,32 +90,27 @@ module PathCoverage
86
90
  end
87
91
 
88
92
  # The cells after the file's: its sites, then its total
89
- # within each decision, then, when methods are measured, its
90
- # total within each method.
93
+ # within each method.
91
94
  def self.counts(row)
92
- cells = [
93
- cell(row[:sites], row[:sites]),
94
- *paths_cells(row[:decisions], 'no decisions')
95
- ]
96
- return cells unless row[:methods]
97
-
98
- none = 'no method with two or more decisions'
99
- cells + paths_cells(row[:methods], none)
95
+ [cell(row[:sites], row[:sites]), *paths_cells(row[:methods])]
100
96
  end
101
97
 
98
+ # Why a total of no paths shows no percent.
99
+ NONE = 'no method with two or more decisions'.freeze
100
+
102
101
  # Three cells showing [taken, possible] paths, eg 3, / 6 and
103
102
  # 50%, each sorted by its number, so they read 3 / 6 50%.
104
103
  # With no paths to take, as in a group no file is in, or a
105
104
  # file with no method of two or more decisions, the percent
106
- # shows -, titled with none, saying why, with an empty sort
107
- # value, which sort.js keeps last whichever way the column
108
- # is sorted.
109
- def self.paths_cells(pair, none)
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)
110
109
  taken, possible = pair
111
110
  counts = [cell(taken, taken, css: 'taken'),
112
111
  cell(possible, "/ #{possible}", css: 'of')]
113
- none_cell = cell('', '-', none, css: 'percent')
114
- return [*counts, none_cell] if possible.zero?
112
+ none = cell('', '-', NONE, css: 'percent')
113
+ return [*counts, none] if possible.zero?
115
114
 
116
115
  percent = (100.0 * taken / possible).round
117
116
  [*counts, cell(percent, "#{percent}%", css: 'percent')]
@@ -1,41 +1,21 @@
1
1
  module PathCoverage
2
- # The two heading rows of the HTML index's files tables: File
3
- # and Sites, then each total's name over its taken, / of and %
4
- # columns, every column sortable.
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
5
  module HtmlIndexHead
6
6
  # The class and label of each of a total's three columns.
7
7
  PARTS = [%w[taken taken], ['of', '/ of'], %w[percent %]].freeze
8
8
 
9
- # The two heading rows; methods' total only when methods are
10
- # measured.
11
- def self.of(methods)
12
- names = ['Within each decision']
13
- names << 'Within each method' if methods
14
- rows = [top_row(names), part_row(names)]
15
- rows.map { |row| "<tr>\n#{row}\n</tr>" }.join("\n")
16
- end
17
-
18
- # The first heading row: File and Sites, each over both rows,
19
- # then each total's name over its three columns.
20
- def self.top_row(names)
9
+ # The heading row.
10
+ def self.row
21
11
  fixed = [
22
- heading(' rowspan="2"', 0, 'text', 'File'),
23
- heading(' class="num" rowspan="2"', 1, 'number', 'Sites')
12
+ heading('', 0, 'text', 'File'),
13
+ heading(' class="num"', 1, 'number', 'Sites')
24
14
  ]
25
- groups = names.map do |name|
26
- %(<th class="group" colspan="3">#{name}</th>)
15
+ parts = PARTS.each_with_index.map do |(css, label), i|
16
+ heading(%( class="#{css}"), 2 + i, 'number', label)
27
17
  end
28
- (fixed + groups).join("\n")
29
- end
30
-
31
- # The second heading row: each total's taken, / of and %.
32
- def self.part_row(names)
33
- names.each_index.flat_map do |group|
34
- PARTS.each_with_index.map do |(css, label), i|
35
- column = 2 + (3 * group) + i
36
- heading(%( class="#{css}"), column, 'number', label)
37
- end
38
- end.join("\n")
18
+ "<tr>\n#{(fixed + parts).join("\n")}\n</tr>"
39
19
  end
40
20
 
41
21
  # A sortable heading, attrs its th's, sorting the column-th
@@ -0,0 +1,23 @@
1
+ require 'cgi/escape'
2
+ require_relative 'condition'
3
+ require_relative 'html_syntax'
4
+ require_relative 'kinds'
5
+
6
+ module PathCoverage
7
+ # The labels of a site's tree on a file's page, coloured as the
8
+ # code they quote.
9
+ module HtmlLabels
10
+ # The label of an arm of a condition whose operator is
11
+ # choice, as its kind names it, as HTML: a then that names its
12
+ # arm, named, there being no then in the code, plain; a
13
+ # ternary's coloured as HtmlSyntax.ternary_arm has it; any
14
+ # other, eg else or when, coloured as the code it is.
15
+ def self.arm(arm, choice, named)
16
+ label = Kinds.of(choice).arm_label(arm, choice)
17
+ return CGI.escapeHTML(label) if named
18
+ return HtmlSyntax.ternary_arm(label) if choice == '?:'
19
+
20
+ HtmlSyntax.fragment(label)
21
+ end
22
+ end
23
+ end