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,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,86 +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. The files dir is emptied
19
- # first, so it holds no page for a file no longer covered.
20
- 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
21
25
  dir = config.output_dir
22
26
  FileUtils.rm_rf("#{dir}/files")
23
- index = index(files, config, paths, site_paths)
27
+ cards = WithinMethods.cards(files, config, site_paths, reached)
28
+ index = index(config, site_paths, cards)
24
29
  File.write("#{dir}/index.html", index)
30
+ write_pages(operands, files, config, recorded)
31
+ end
32
+
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)
25
36
  files.each do |full_path, parsed|
26
37
  path = config.relative(full_path)
27
- out = "#{dir}/files/#{path}.html"
28
- # simplecov:disable
29
- grids = paths ? path_grids(path, parsed, paths) : ''
30
- # simplecov:enable
31
- write_page(out, path, parsed, [operands, site_paths], grids)
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)
32
41
  end
33
42
  end
34
43
 
35
44
  # The index page: a row per file of its totals within each
36
- # decision, and, when paths are given, as they are when
37
- # methods are measured, within each method too;
38
- # then their Total row. With groups configured, each group,
39
- # and the files in none, get a tab of their own too. Any
40
- # operators config ignored are named after them.
41
- def self.index(files, config, paths, site_paths)
42
- methods = files.map do |full_path, parsed|
43
- path = config.relative(full_path)
44
- PathManifest.methods(parsed[:source], path)
45
- end
46
- all = methods.reduce({}, :merge)
47
- rows = Summary.files(site_paths, all, paths)
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)
48
53
  groups = config.groups
49
54
  tabs = groups.empty? ? [] : Summary.groups(rows, groups)
50
55
  ignored = config.ignored
51
56
  HtmlIndex.page(rows, Summary.total(rows), tabs, ignored:)
52
57
  end
53
58
 
54
- # simplecov:disable
55
- # SPIKE: the hidden path grids of one file's methods.
56
- def self.path_grids(path, parsed, paths)
57
- source = parsed[:source]
58
- methods = PathManifest.methods(source, path)
59
- HtmlPaths.grids(source, methods, paths)
60
- end
61
- # simplecov:enable
62
-
63
59
  # Writes one file's page to out, from its sites' operand
64
- # 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.
65
62
  def self.write_page(out, path, parsed,
66
- (operands, site_paths), grids)
63
+ (operands, site_paths), cards)
67
64
  source, sites = parsed.values_at(:source, :sites)
68
- lines = SourceLines.of(source, sites, operands)
69
- 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)
70
69
  FileUtils.mkdir_p(File.dirname(out))
71
- File.write(out, HtmlFile.page(path, lines, trees, grids))
70
+ File.write(out, page)
72
71
  end
73
72
 
74
- # { first line of a span => [SiteTree.of, ...] } for the
75
- # sites of one file, each under the span its line is in.
76
- def self.trees(source, sites, lines, site_paths)
77
- trees = Hash.new { |all, first| all[first] = [] }
78
- sites.each do |site, condition|
79
- first = lines[SourceLines.line_of(site) - 1][:span].begin
80
- tree = SiteTree.of(source, condition, site_paths.fetch(site))
81
- 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], []))
82
79
  end
83
- trees
84
80
  end
85
81
 
86
82
  # simplecov:disable
@@ -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
@@ -0,0 +1,156 @@
1
+ require 'cgi/escape'
2
+ require 'prism'
3
+
4
+ module PathCoverage
5
+ # The colours of a file's code on its page: each token Prism
6
+ # lexes, as Ruby itself does, gets the class of its kind, eg k
7
+ # for a keyword, so the page's CSS colours it.
8
+ module HtmlSyntax
9
+ # The class of each coloured token type that is not a keyword
10
+ # or a number: c comment, s string, t constant, v instance
11
+ # variable, o an operator that decides a path.
12
+ CLASSES = {
13
+ COMMENT: 'c', CONSTANT: 't', INSTANCE_VARIABLE: 'v',
14
+ **%i[
15
+ AMPERSAND_AMPERSAND PIPE_PIPE AMPERSAND_DOT QUESTION_MARK
16
+ COLON AMPERSAND_AMPERSAND_EQUAL PIPE_PIPE_EQUAL
17
+ ].to_h { |type| [type, 'o'] },
18
+ **%i[
19
+ STRING_BEGIN STRING_CONTENT STRING_END SYMBOL_BEGIN LABEL
20
+ PERCENT_LOWER_W WORDS_SEP REGEXP_BEGIN REGEXP_END
21
+ HEREDOC_START HEREDOC_END
22
+ ].to_h { |type| [type, 's'] }
23
+ }.freeze
24
+
25
+ # { line number => [[from, to, class], ...] } for the lines of
26
+ # source that have coloured tokens, columns counting
27
+ # characters, a token over many lines having a range on each,
28
+ # touching ranges of one class joined.
29
+ def self.of(source)
30
+ layout = Layout.new(source)
31
+ pairs = classed(source).flat_map do |from, to, css|
32
+ layout.ranges(from, to).map do |number, first, last|
33
+ [number, [first, last, css]]
34
+ end
35
+ end
36
+ pairs.group_by(&:first).transform_values do |line|
37
+ joined(line.map(&:last).sort)
38
+ end
39
+ end
40
+
41
+ # A source's lines, by the byte offset each starts at.
42
+ class Layout
43
+ # The lines of source, line 1 starting at byte 0.
44
+ def initialize(source)
45
+ @source = source
46
+ @starts = [0]
47
+ source.each_line do |line|
48
+ @starts << (@starts.last + line.bytesize)
49
+ end
50
+ end
51
+
52
+ # [[line number, first column, last column], ...] for each
53
+ # line the bytes from...to reach, columns counting characters
54
+ # and kept within the line: from the line holding from to the
55
+ # last starting before to. The last start is the source's
56
+ # size, so both searches always find a line.
57
+ def ranges(from, to)
58
+ first = @starts.bsearch_index { |start| start > from } - 1
59
+ last = @starts.bsearch_index { |start| start >= to }
60
+ reached = (first...last).map { |n| columns(n, from, to) }
61
+ reached.compact
62
+ end
63
+
64
+ # [line number, first column, last column] of the bytes
65
+ # from...to on the index-th line; nil when none are on it.
66
+ def columns(index, from, to)
67
+ start = @starts[index]
68
+ size = @starts[index + 1] - start
69
+ text = @source.byteslice(start, size).chomp
70
+ first = chars(text, from - start)
71
+ last = chars(text, to - start)
72
+ first < last ? [index + 1, first, last] : nil
73
+ end
74
+
75
+ # How many characters of text its first bytes bytes hold.
76
+ def chars(text, bytes)
77
+ text.byteslice(0, bytes.clamp(0, text.bytesize)).length
78
+ end
79
+ end
80
+
81
+ # [[from, to, class], ...], the byte range and class of each
82
+ # coloured token of source; a symbol's name is coloured as
83
+ # its colon is.
84
+ def self.classed(source)
85
+ previous = nil
86
+ Prism.lex(source).value.filter_map do |token, _|
87
+ css = class_of(token.type, previous)
88
+ previous = token.type
89
+ location = token.location
90
+ css && [location.start_offset, location.end_offset, css]
91
+ end
92
+ end
93
+
94
+ # The class of a token of type, after one of type previous;
95
+ # nil for a token left uncoloured.
96
+ def self.class_of(type, previous)
97
+ return 's' if previous == :SYMBOL_BEGIN
98
+
99
+ name = type.to_s
100
+ return 'k' if name.start_with?('KEYWORD_')
101
+ return 'n' if name.start_with?('INTEGER', 'FLOAT')
102
+
103
+ CLASSES[type]
104
+ end
105
+
106
+ # ranges, in order, each touching one of the same class joined
107
+ # to it.
108
+ def self.joined(ranges)
109
+ ranges.each_with_object([]) do |(from, to, css), out|
110
+ last = out.last
111
+ if last && last[1] == from && last[2] == css
112
+ last[1] = to
113
+ else
114
+ out << [from, to, css]
115
+ end
116
+ end
117
+ end
118
+
119
+ # The names of the operators that decide a path, as a site
120
+ # tree's labels write them. Alone, some lex as other tokens,
121
+ # eg ?: as a character and || as two pipes, so each is
122
+ # coloured as an operator by name.
123
+ DECIDING = %w[&& || &. ?: &&= ||=].freeze
124
+
125
+ # text, a fragment of code on one line, eg a site tree's
126
+ # operator or operand, escaped and coloured as the code it is
127
+ # part of; one of DECIDING as the operator it names.
128
+ def self.fragment(text)
129
+ if DECIDING.include?(text)
130
+ return %(<span class="o">#{CGI.escapeHTML(text)}</span>)
131
+ end
132
+
133
+ html(text, of(text).fetch(1, []))
134
+ end
135
+
136
+ # A ternary's arm's label, eg ? total > 100, as HTML: its ? or
137
+ # : coloured as an operator, the code after it as code.
138
+ def self.ternary_arm(label)
139
+ mark, code = label.split(' ', 2)
140
+ %(<span class="o">#{mark}</span> #{fragment(code.to_s)})
141
+ end
142
+
143
+ # text, escaped, each of tokens' ranges, [[from, to, class],
144
+ # ...], in a span of its class.
145
+ def self.html(text, tokens)
146
+ at = 0
147
+ parts = tokens.map do |from, to, css|
148
+ plain = CGI.escapeHTML(text[at...from])
149
+ at = to
150
+ token = CGI.escapeHTML(text[from...to])
151
+ %(#{plain}<span class="#{css}">#{token}</span>)
152
+ end
153
+ parts.join + CGI.escapeHTML(text[at..])
154
+ end
155
+ end
156
+ end
@@ -10,6 +10,12 @@ module PathCoverage
10
10
  File.read(File.join(DIR, name))
11
11
  end
12
12
 
13
+ # The page template named name, its {{style}} the report's
14
+ # style sheet, and each other {{name}} values[name].
15
+ def self.page(name, values)
16
+ fill(asset(name), values.merge('style' => asset('style.css')))
17
+ end
18
+
13
19
  # template with each {{name}} replaced by values[name].
14
20
  def self.fill(template, values)
15
21
  template.gsub(/\{\{(\w+)\}\}/) do