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,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
@@ -0,0 +1,66 @@
1
+ module PathCoverage
2
+ # What each line of a file's page shows of the methods around
3
+ # it, from its cards, as Card gives them: a method's lines
4
+ # marked for CSS to draw its stripe and band, its def's number
5
+ # marked to open its card, and its body's rows folded under its
6
+ # def line until then. A top card, of sites outside every def,
7
+ # is no method's.
8
+ module HtmlMethods
9
+ # The cards of cards that are methods'.
10
+ def self.of(cards)
11
+ cards.reject(&:top)
12
+ end
13
+
14
+ # Example: line 3, f's def at column 2, gives
15
+ # class="src in-method def-line" data-method-status="covered"
16
+ # style="--def-col: 2; --def-end: 15".
17
+ # The attributes of the code of line number, its text text:
18
+ # in a method, its innermost one's status and def's column,
19
+ # marked as its def line, with its text's length, or as its
20
+ # last line when it is; plain in none.
21
+ def self.src_attributes(methods, number, text)
22
+ holders = methods.select { |card| card.holds?(number) }
23
+ return 'class="src"' if holders.empty?
24
+
25
+ inner = holders.max_by(&:first)
26
+ css = ['src', 'in-method', *marks(methods, number)].join(' ')
27
+ style = "--def-col: #{inner.column}"
28
+ style += "; --def-end: #{text.length}" if inner.first == number
29
+ %(class="#{css}" data-method-status="#{inner.status}") +
30
+ %( style="#{style}")
31
+ end
32
+
33
+ # The marks of a line that is some method's first or last.
34
+ def self.marks(methods, number)
35
+ firsts = methods.map(&:first)
36
+ lasts = methods.map(&:last)
37
+ [('def-line' if firsts.include?(number)),
38
+ ('method-end' if lasts.include?(number))].compact
39
+ end
40
+
41
+ # The attributes a def's number adds, opening its method's
42
+ # card, which starts closed: none on any other line.
43
+ def self.number_attributes(methods, number)
44
+ card = methods.find { |each| each.first == number }
45
+ return 'class="lineno"' unless card
46
+
47
+ closed = 'aria-expanded="false"'
48
+ %(class="lineno mdef" data-status="#{card.status}" #{closed})
49
+ end
50
+
51
+ # True when line number's row starts folded: in a method's
52
+ # body, after its def line. A row in a method is one line, so
53
+ # a def line's row is never folded under its own def.
54
+ def self.folded?(methods, number)
55
+ methods.any? do |card|
56
+ ((card.first + 1)..card.last).cover?(number)
57
+ end
58
+ end
59
+
60
+ # True when line number is in no method, so its site's button
61
+ # opens its lines' top card.
62
+ def self.loose?(methods, number)
63
+ methods.none? { |card| card.holds?(number) }
64
+ end
65
+ end
66
+ end
@@ -1,67 +1,67 @@
1
1
  module PathCoverage
2
- # The ways around a file's page: the header's lists of links to
3
- # each span, and the arrows between the spans with gaps.
2
+ # The ways around a file's page, from its cards, as Card gives
3
+ # them: the header's row of jumps to each card with gaps, and
4
+ # the arrows in the gutter between the cards of each status.
4
5
  module HtmlNav
5
- JUMP_LISTS = { 'covered' => 'Covered', 'gaps' => 'Gaps' }.freeze
6
+ # Each status's arrows, gaps first, and how a label names it.
7
+ KINDS = { 'gaps' => 'with gaps', 'covered' => 'covered' }.freeze
6
8
 
7
- # The header's lists of links to the first line of each span:
8
- # those covered, then those with gaps.
9
- def self.jumps(lines)
10
- firsts = lines.select { |line| first_of_span?(line) }
11
- lists = JUMP_LISTS.map do |status, label|
12
- spans = firsts.select { |line| line[:status].to_s == status }
13
- jump_list(status, label, spans.map { |line| line[:number] })
14
- end
15
- %(<nav class="jumps" aria-label="Sites">#{lists.join}</nav>)
16
- end
17
-
18
- # True for the line a span starts on.
19
- def self.first_of_span?(line)
20
- line[:span]&.begin == line[:number]
9
+ # The header's row of jumps, labelled Gaps, to the first line
10
+ # of each card with gaps, in line order; marked empty when
11
+ # there are none, so the header keeps its height.
12
+ def self.jumps(cards)
13
+ firsts = firsts(cards, 'gaps')
14
+ links = firsts.map { |first| jump(first) }
15
+ empty = links.empty? ? ' data-empty=""' : ''
16
+ name = '<span class="jump-label">Gaps</span>'
17
+ list = %(<div class="jump-list"#{empty}>#{name}#{links.join})
18
+ %(<nav class="jumps" aria-label="Cards">#{list}</div></nav>)
21
19
  end
22
20
 
23
- # One labelled list of links to lines.
24
- def self.jump_list(status, label, numbers)
25
- links = numbers.map { |number| jump(status, number) }
26
- name = %(<span class="jump-label">#{label}</span>)
27
- list = %(<div class="jump-list" data-jumps="#{status}">)
28
- "#{list}#{name}#{links.join}</div>"
21
+ # One jump to a card with gaps at line first.
22
+ def self.jump(first)
23
+ attributes = %(data-status="gaps" href="#L#{first}")
24
+ %(<a class="jump" #{attributes}>#{first}</a>)
29
25
  end
30
26
 
31
- # One link to a line.
32
- def self.jump(status, number)
33
- attributes = %(data-status="#{status}" href="#L#{number}")
34
- %(<a class="jump" #{attributes}>#{number}</a>)
27
+ # The first lines of the cards of status, in line order.
28
+ def self.firsts(cards, status)
29
+ cards.select { |card| card.status == status }.map(&:first).sort
35
30
  end
36
31
 
37
- # The first line of each span whose sites have gaps, given the
38
- # listing's blocks.
39
- def self.gap_lines(blocks)
40
- firsts = blocks.map(&:first)
41
- firsts.select { |line| line[:status] == :gaps }.map do |line|
42
- line[:number]
32
+ # The gutter of the row of numbers: by each card's first line,
33
+ # an arrow to the card of its status before it, and one to the
34
+ # one after, wrapping round at either end; empty on every
35
+ # other row, so the numbers stay aligned.
36
+ def self.cell(numbers, cards)
37
+ links = KINDS.keys.flat_map do |status|
38
+ firsts = firsts(cards, status)
39
+ (firsts & numbers).flat_map do |first|
40
+ arrows(firsts, first, status)
41
+ end
43
42
  end
43
+ %(<span class="nav" data-nav="">#{links.join}</span>)
44
44
  end
45
45
 
46
- # The cell left of a row's number: at a span with gaps, links
47
- # to the previous and next such span, wrapping round at the
48
- # ends; empty on every other row, so the numbers stay aligned.
49
- def self.cell(number, gaps)
50
- index = gaps.index(number)
51
- links = index ? arrows(gaps, index) : ''
52
- %(<span class="nav" data-nav="">#{links}</span>)
46
+ # The arrows by the card of status at line first to those
47
+ # either side of it in firsts, the first lines of the cards of
48
+ # its status.
49
+ def self.arrows(firsts, first, status)
50
+ index = firsts.index(first)
51
+ following = firsts[(index + 1) % firsts.size]
52
+ [arrow('prev', firsts[index - 1], status),
53
+ arrow('next', following, status)]
53
54
  end
54
55
 
55
- # The links to the spans with gaps either side of gaps[index].
56
- def self.arrows(gaps, index)
57
- previous = gaps[index - 1]
58
- following = gaps[(index + 1) % gaps.size]
59
- [
60
- %(<a data-prev="" href="#L#{previous}"),
61
- %( aria-label="Previous gap, line #{previous}">&#9650;</a>),
62
- %(<a data-next="" href="#L#{following}"),
63
- %( aria-label="Next gap, line #{following}">&#9660;</a>)
64
- ].join
56
+ # Each arrow's way and how its label names it.
57
+ WAYS = { 'prev' => 'Previous', 'next' => 'Next' }.freeze
58
+
59
+ # One arrow, way prev or next, to the card of status at line;
60
+ # CSS draws its glyph, in its status's colour.
61
+ def self.arrow(way, line, status)
62
+ label = "#{WAYS[way]} card #{KINDS[status]}, line #{line}"
63
+ %(<a data-#{way}="" data-status="#{status}" href="#L#{line}") +
64
+ %( aria-label="#{label}"></a>)
65
65
  end
66
66
  end
67
67
  end
@@ -1,79 +1,82 @@
1
1
  require 'fileutils'
2
+ require_relative 'html_cards'
2
3
  require_relative 'html_file'
3
4
  require_relative 'html_index'
4
- require_relative 'html_paths'
5
- require_relative 'path_manifest'
5
+ require_relative 'html_syntax'
6
6
  require_relative 'site_tree'
7
7
  require_relative 'source_lines'
8
8
  require_relative 'summary'
9
+ require_relative 'within_methods'
9
10
 
10
11
  module PathCoverage
11
12
  # The :html reporter: index.html, and files/<path>.html for
12
13
  # each covered file, written to the configuration's
13
14
  # output_dir.
14
15
  module HtmlReport
15
- # Writes every page. operands and site_paths are as Result
16
- # gives them, site_paths holding every site in files; files
17
- # is { full path => {source:, sites:} } for each covered
18
- # file, as the kernel parsed it.
19
- def self.write(operands, files, config, paths = nil, site_paths:)
16
+ # Writes every page. recorded is [site_paths, reached]:
17
+ # site_paths, like operands, as Result gives them, holding
18
+ # every site in files; reached as Recorder#reached gives it,
19
+ # nil when not known. files is { full path => {source:,
20
+ # sites:} } for each covered file, as the kernel parsed it.
21
+ # The files dir is emptied first, so it holds no page for a
22
+ # file no longer covered.
23
+ def self.write(operands, files, config, recorded)
24
+ site_paths, reached = recorded
20
25
  dir = config.output_dir
21
- index = index(files, config, paths, site_paths)
26
+ FileUtils.rm_rf("#{dir}/files")
27
+ cards = WithinMethods.cards(files, config, site_paths, reached)
28
+ index = index(config, site_paths, cards)
22
29
  File.write("#{dir}/index.html", index)
23
- files.each do |full_path, parsed|
24
- path = config.relative(full_path)
25
- out = "#{dir}/files/#{path}.html"
26
- # simplecov:disable
27
- grids = paths ? path_grids(path, parsed, paths) : ''
28
- # simplecov:enable
29
- write_page(out, path, parsed, [operands, site_paths], grids)
30
- end
30
+ write_pages(operands, files, config, recorded)
31
31
  end
32
32
 
33
- # The index page: a row per file of its totals within each
34
- # decision, and, when paths are given, as they are when
35
- # methods are measured, across each method's decisions too;
36
- # then their Total row.
37
- def self.index(files, config, paths, site_paths)
38
- methods = files.map do |full_path, parsed|
33
+ # Writes each covered file's page, recorded as write has it,
34
+ # with its methods' cards.
35
+ def self.write_pages(operands, files, config, recorded)
36
+ files.each do |full_path, parsed|
39
37
  path = config.relative(full_path)
40
- PathManifest.methods(parsed[:source], path)
38
+ cards = HtmlCards.methods(path, parsed, recorded)
39
+ out = "#{config.output_dir}/files/#{path}.html"
40
+ write_page(out, path, parsed, [operands, recorded[0]], cards)
41
41
  end
42
- all = methods.reduce({}, :merge)
43
- rows = Summary.files(site_paths, all, paths)
44
- HtmlIndex.page(rows, Summary.total(rows))
45
42
  end
46
43
 
47
- # simplecov:disable
48
- # SPIKE: the hidden path grids of one file's methods.
49
- def self.path_grids(path, parsed, paths)
50
- source = parsed[:source]
51
- methods = PathManifest.methods(source, path)
52
- HtmlPaths.grids(source, methods, paths)
44
+ # The index page: a row per file of its totals within each
45
+ # decision, and, when cards are given, as they are when
46
+ # methods are measured, within each method too, its defs'
47
+ # cards added up; then their Total row. With groups
48
+ # configured, each group, and the files in none, get a tab
49
+ # of their own too. Any operators config ignored are named
50
+ # after them.
51
+ def self.index(config, site_paths, cards)
52
+ rows = Summary.files(site_paths, cards)
53
+ groups = config.groups
54
+ tabs = groups.empty? ? [] : Summary.groups(rows, groups)
55
+ ignored = config.ignored
56
+ HtmlIndex.page(rows, Summary.total(rows), tabs, ignored:)
53
57
  end
54
- # simplecov:enable
55
58
 
56
59
  # Writes one file's page to out, from its sites' operand
57
- # counts and paths.
60
+ # counts and paths, with cards, its methods', then a card for
61
+ # each span of its sites at the top level.
58
62
  def self.write_page(out, path, parsed,
59
- (operands, site_paths), grids)
63
+ (operands, site_paths), cards)
60
64
  source, sites = parsed.values_at(:source, :sites)
61
- lines = SourceLines.of(source, sites, operands)
62
- trees = trees(source, sites, lines, site_paths)
65
+ plain = SourceLines.of(source, sites, operands)
66
+ lines = coloured(source, plain)
67
+ tops = HtmlCards.tops(path, parsed, lines, site_paths)
68
+ page = HtmlFile.page(path, lines, cards + tops)
63
69
  FileUtils.mkdir_p(File.dirname(out))
64
- File.write(out, HtmlFile.page(path, lines, trees, grids))
70
+ File.write(out, page)
65
71
  end
66
72
 
67
- # { first line of a span => [SiteTree.of, ...] } for the
68
- # sites of one file, each under the span its line is in.
69
- def self.trees(source, sites, lines, site_paths)
70
- trees = Hash.new { |all, first| all[first] = [] }
71
- sites.each do |site, condition|
72
- first = lines[SourceLines.line_of(site) - 1][:span].begin
73
- tree = SiteTree.of(source, condition, site_paths.fetch(site))
74
- trees[first] << located(source, condition, tree)
73
+ # lines, as SourceLines.of gives them for source, each with
74
+ # its coloured tokens, as HtmlSyntax.of gives them.
75
+ def self.coloured(source, lines)
76
+ tokens = HtmlSyntax.of(source)
77
+ lines.map do |line|
78
+ line.merge(tokens: tokens.fetch(line[:number], []))
75
79
  end
76
- trees
77
80
  end
78
81
 
79
82
  # simplecov:disable
@@ -81,24 +84,34 @@ module PathCoverage
81
84
  # on it and on each of its operands: a leaf's from its byte
82
85
  # range, a condition's and an arm's from its extent, the
83
86
  # whole of it as written. Columns count characters, not
84
- # bytes.
87
+ # bytes. A node past the condition's operands, eg owner at
88
+ # the end of user&.account&.owner, is kept as it is.
85
89
  def self.located(source, condition, tree)
86
90
  pairs = condition.operands.zip(tree[:operands])
87
91
  operands = pairs.map do |operand, node|
88
- case operand
89
- when Condition then located(source, operand, node)
90
- when Arm then located_arm(source, operand, node)
91
- else node.merge(at: at(source, operand))
92
- end
92
+ located_operand(source, operand, node)
93
+ end
94
+ rest = tree[:operands].drop(pairs.size)
95
+ tree.merge(at: at(source, condition.extent),
96
+ operands: operands + rest)
97
+ end
98
+
99
+ # SPIKE: the node of operand with its at:, as located gives
100
+ # a condition's, an arm's or a leaf's.
101
+ def self.located_operand(source, operand, node)
102
+ case operand
103
+ when Condition then located(source, operand, node)
104
+ when Arm then located_arm(source, operand, node)
105
+ else node.merge(at: at(source, operand))
93
106
  end
94
- tree.merge(at: at(source, condition.extent), operands:)
95
107
  end
96
108
 
97
109
  # An arm's node with its at:, nil for an arm with no
98
110
  # statements, and its body's tree located.
99
111
  def self.located_arm(source, arm, node)
100
- body = arm.body && located(source, arm.body, node[:body])
101
- node.merge(at: arm.extent && at(source, arm.extent), body:)
112
+ body = arm.body ? located(source, arm.body, node[:body]) : nil
113
+ at = arm.extent ? at(source, arm.extent) : nil
114
+ node.merge(at:, body:)
102
115
  end
103
116
 
104
117
  # [line, column, line, column] of the byte range in source.
@@ -0,0 +1,166 @@
1
+ require 'cgi/escape'
2
+ require_relative 'html_syntax'
3
+ require_relative 'method_statements'
4
+
5
+ module PathCoverage
6
+ # One method's card in method mode, hidden in its file's page
7
+ # until side.js moves it into the rail: a title row of the
8
+ # method's name, its complexity, its size and the paths
9
+ # taken, a row titling the columns, then a row per statement,
10
+ # as MethodStatements gives them.
11
+ module HtmlStatements
12
+ # Example: ship, an if then a value, gives its title row,
13
+ # the column titles, then an if row and a value row.
14
+ # The card of the def named name at site, its rows' taken as
15
+ # MethodStatements.taken gives them, last its def's last
16
+ # line. tree, when given, gives a row's site's tree rows,
17
+ # folded under its row.
18
+ def self.card(site, name, rows, taken, last, &)
19
+ first, column = site.split(':').last(2)
20
+ attrs = [
21
+ %(id="paths-#{first}" data-first="#{first}"),
22
+ %(data-col="#{column}" data-last="#{last}"),
23
+ %(data-status="#{status(rows, taken)}" hidden="hidden")
24
+ ].join(' ')
25
+ head = title_row(first, name, rows, taken) + columns_row
26
+ %(<div class="#{GRID}" #{attrs}>) +
27
+ %(#{head}#{body(rows, taken, &)}</div>)
28
+ end
29
+
30
+ # The classes of every card.
31
+ GRID = 'pathgrid statements'.freeze
32
+
33
+ # Example: FEE = (a ? 1 : 0) + (b ? 2 : 0) on line 1, scope
34
+ # file, gives a card titled file, a ?: row each.
35
+ # The card of the sites at the top level whose lines start at
36
+ # site, as card gives it, last its lines' last; titled by
37
+ # scope, eg file or its module, as a method's card is by its
38
+ # method, and marked top-level, as it is no method's: a class
39
+ # no other part of the page has, so no style of another's, eg
40
+ # the header's top line's display, shows it when it is
41
+ # hidden.
42
+ def self.top_card(site, scope, rows, taken, last, &)
43
+ card(site, scope, rows, taken, last, &)
44
+ .sub(%(class="#{GRID}"), %(class="#{GRID} top-level"))
45
+ end
46
+
47
+ # Each statement's row, a site's with the rows tree gives
48
+ # for that site folded under it.
49
+ def self.body(rows, taken, &tree)
50
+ rows.zip(taken).map do |row, count|
51
+ site = row.site
52
+ folded = tree&.call(site) if site
53
+ row_of(row, count, folds: !folded.nil?) + fold(row, folded)
54
+ end.join
55
+ end
56
+
57
+ # covered when every row's taken is known and is all of its
58
+ # paths; gaps otherwise, as a row not known may have one.
59
+ def self.status(rows, taken)
60
+ done = rows.zip(taken).all? { |row, count| count == row.paths }
61
+ done ? 'covered' : 'gaps'
62
+ end
63
+
64
+ # The row level with the def: its name, *complexity, then
65
+ # how many of its paths were not taken, those of rows not
66
+ # known among them, and how many were.
67
+ def self.title_row(first, name, rows, taken)
68
+ seen = taken.compact.sum
69
+ cells = [
70
+ "*#{MethodStatements.complexity(rows)}",
71
+ (MethodStatements.size(rows) - seen).to_s, seen.to_s
72
+ ]
73
+ attrs = %(class="prow calls" data-line="#{first}")
74
+ row(attrs, CGI.escapeHTML(name.to_s), cells)
75
+ end
76
+
77
+ # The row naming the not taken and taken columns.
78
+ def self.columns_row
79
+ cells = ['', 'not taken', 'taken']
80
+ row('class="prow columns"', 'line:col', cells)
81
+ end
82
+
83
+ # One statement's row: its line:column, the column two
84
+ # digits so they line up, its top, how many of its paths
85
+ # were not taken, and how many were, ? for both when not
86
+ # known. A site's row is marked as one, so a click on it can
87
+ # open its tree; when it folds a tree, its arrow, left of its
88
+ # line:column, shows whether the fold is open.
89
+ def self.row_of(statement, count, folds: false)
90
+ counts = count ? [statement.paths - count, count] : %w[? ?]
91
+ cells = [top(statement), *counts.map(&:to_s)]
92
+ css = statement.site ? 'prow site' : 'prow'
93
+ attrs = %(class="#{css}" data-line="#{statement.line}")
94
+ lead = folds ? arrow(statement) : ''
95
+ row(attrs, label(statement), cells, lead)
96
+ end
97
+
98
+ # Example: if, a site's top, as <span class="k">if</span>.
99
+ # statement's top, coloured as the code pane colours the
100
+ # code it quotes, eg if, def and return as keywords; but
101
+ # value and (), which name kinds of statement and quote no
102
+ # code, escaped and plain.
103
+ def self.top(statement)
104
+ text = statement.top
105
+ return CGI.escapeHTML(text) if KINDS.include?(text)
106
+
107
+ HtmlSyntax.fragment(text)
108
+ end
109
+
110
+ # The tops that name a kind of statement, quoting no code.
111
+ KINDS = %w[value ()].freeze
112
+
113
+ # The arrow left of a row's line:column, its glyph, as
114
+ # style.css draws it, pointing down while its fold is shut
115
+ # and up while it is open.
116
+ def self.arrow(statement)
117
+ [
118
+ '<button type="button" class="fold-arrow"',
119
+ ' aria-expanded="false"',
120
+ %( aria-label="Paths of #{label(statement)}"></button>)
121
+ ].join
122
+ end
123
+
124
+ # Example: 19:04, a statement on line 19 at column 4.
125
+ # statement's line:column, the column two digits so they
126
+ # line up.
127
+ def self.label(statement)
128
+ format('%<line>d:%<column>02d',
129
+ line: statement.line, column: statement.column)
130
+ end
131
+
132
+ # The fold under statement's row, hidden until the row is
133
+ # clicked: tree, its site's rows as its tree draws them.
134
+ # Empty for a row with no tree.
135
+ def self.fold(statement, tree)
136
+ return '' unless tree
137
+
138
+ [
139
+ %(<div class="fold" data-site="#{statement.site}"),
140
+ ' hidden="hidden"><div class="site" data-site="">',
141
+ tree, '</div></div>'
142
+ ].join
143
+ end
144
+
145
+ # The class each of a row's cells adds after pcell: none for
146
+ # its top, then those colouring not taken and taken.
147
+ CELLS = ['', ' not-taken', ' was-taken'].freeze
148
+
149
+ # The class marking a not taken cell of 0, no gap, coloured
150
+ # blue as taken is; none for any other cell, as a count of
151
+ # paths not taken, or one not known, ?, is a gap.
152
+ def self.none(cell, css)
153
+ css == ' not-taken' && cell == '0' ? ' none' : ''
154
+ end
155
+
156
+ # A row div with attrs, lead, then its label, then a span
157
+ # per cell.
158
+ def self.row(attrs, label, cells, lead = '')
159
+ spans = cells.zip(CELLS).map do |cell, css|
160
+ %(<span class="pcell#{css}#{none(cell, css)}">#{cell}</span>)
161
+ end
162
+ %(<div #{attrs}>#{lead}<span class="plabel">#{label}</span>) +
163
+ %(#{spans.join}</div>)
164
+ end
165
+ end
166
+ end