path_coverage 0.4.0 → 0.6.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 (108) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +260 -300
  3. data/exe/path_coverage_report +4 -0
  4. data/lib/path_coverage/call_trace.rb +109 -0
  5. data/lib/path_coverage/calls.rb +73 -43
  6. data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +35 -33
  7. data/lib/path_coverage/condition.rb +14 -14
  8. data/lib/path_coverage/configuration.rb +7 -4
  9. data/lib/path_coverage/configuration_limits.rb +52 -0
  10. data/lib/path_coverage/configuration_paths.rb +7 -7
  11. data/lib/path_coverage/console_summary.rb +28 -26
  12. data/lib/path_coverage/{site.rb → decision.rb} +2 -2
  13. data/lib/path_coverage/decision_locations.rb +76 -0
  14. data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +13 -11
  15. data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
  16. data/lib/path_coverage/fold_branches.rb +18 -0
  17. data/lib/path_coverage/fold_cells.rb +42 -0
  18. data/lib/path_coverage/{html_tree.rb → fold_rows.rb} +43 -72
  19. data/lib/path_coverage/gaps.rb +33 -15
  20. data/lib/path_coverage/json_reader.rb +54 -19
  21. data/lib/path_coverage/json_reports.rb +109 -0
  22. data/lib/path_coverage/json_writer.rb +29 -14
  23. data/lib/path_coverage/kernel.rb +77 -58
  24. data/lib/path_coverage/kinds/base.rb +16 -16
  25. data/lib/path_coverage/kinds/block.rb +16 -15
  26. data/lib/path_coverage/kinds/case.rb +14 -12
  27. data/lib/path_coverage/kinds/chain.rb +40 -11
  28. data/lib/path_coverage/kinds/choice.rb +4 -3
  29. data/lib/path_coverage/kinds/contains.rb +40 -32
  30. data/lib/path_coverage/kinds/safe.rb +38 -26
  31. data/lib/path_coverage/kinds/write.rb +6 -6
  32. data/lib/path_coverage/kinds.rb +2 -2
  33. data/lib/path_coverage/leaving.rb +52 -0
  34. data/lib/path_coverage/loop_forks.rb +57 -0
  35. data/lib/path_coverage/loop_nodes.rb +91 -0
  36. data/lib/path_coverage/manifest.rb +92 -58
  37. data/lib/path_coverage/manifest_blocks.rb +5 -26
  38. data/lib/path_coverage/manifest_case.rb +2 -2
  39. data/lib/path_coverage/manifest_chains.rb +8 -8
  40. data/lib/path_coverage/manifest_choices.rb +23 -20
  41. data/lib/path_coverage/manifest_disabled.rb +97 -0
  42. data/lib/path_coverage/manifest_folding.rb +43 -40
  43. data/lib/path_coverage/manifest_ignoring.rb +6 -6
  44. data/lib/path_coverage/manifest_writes.rb +11 -10
  45. data/lib/path_coverage/measured_by.rb +20 -0
  46. data/lib/path_coverage/merge.rb +7 -7
  47. data/lib/path_coverage/method_loops.rb +172 -0
  48. data/lib/path_coverage/method_statements.rb +150 -134
  49. data/lib/path_coverage/method_ways.rb +170 -0
  50. data/lib/path_coverage/paths_context.rb +104 -0
  51. data/lib/path_coverage/paths_fitting.rb +31 -0
  52. data/lib/path_coverage/paths_forks.rb +72 -0
  53. data/lib/path_coverage/paths_gaps.rb +137 -0
  54. data/lib/path_coverage/paths_leaving.rb +40 -0
  55. data/lib/path_coverage/paths_report.rb +16 -16
  56. data/lib/path_coverage/paths_rescues.rb +146 -0
  57. data/lib/path_coverage/paths_scopes.rb +37 -0
  58. data/lib/path_coverage/paths_tree.rb +203 -0
  59. data/lib/path_coverage/paths_way_lists.rb +72 -0
  60. data/lib/path_coverage/paths_ways.rb +193 -0
  61. data/lib/path_coverage/recorder.rb +71 -36
  62. data/lib/path_coverage/recorder_blocks.rb +17 -17
  63. data/lib/path_coverage/recorder_cases.rb +12 -11
  64. data/lib/path_coverage/recorder_chains.rb +12 -12
  65. data/lib/path_coverage/recorder_writes.rb +9 -9
  66. data/lib/path_coverage/render.rb +58 -51
  67. data/lib/path_coverage/rescue_forks.rb +174 -0
  68. data/lib/path_coverage/result.rb +32 -31
  69. data/lib/path_coverage/rewriter.rb +116 -35
  70. data/lib/path_coverage/rewriter_nested.rb +13 -13
  71. data/lib/path_coverage/runs_report.rb +84 -0
  72. data/lib/path_coverage/source_lines.rb +16 -16
  73. data/lib/path_coverage/source_report.rb +100 -0
  74. data/lib/path_coverage/stable_leaves.rb +162 -0
  75. data/lib/path_coverage/statement_arms.rb +53 -38
  76. data/lib/path_coverage/summary.rb +4 -62
  77. data/lib/path_coverage/totals.rb +12 -10
  78. data/lib/path_coverage/version.rb +1 -1
  79. data/lib/path_coverage/walk.rb +106 -0
  80. data/lib/path_coverage/way_algebras.rb +127 -0
  81. data/lib/path_coverage/ways_report.rb +91 -0
  82. data/lib/path_coverage/ways_totals.rb +46 -0
  83. data/lib/path_coverage/within_methods.rb +152 -97
  84. metadata +39 -30
  85. data/lib/path_coverage/card.rb +0 -14
  86. data/lib/path_coverage/card_tree.rb +0 -186
  87. data/lib/path_coverage/card_ways.rb +0 -140
  88. data/lib/path_coverage/html/file.html +0 -33
  89. data/lib/path_coverage/html/index.html +0 -29
  90. data/lib/path_coverage/html/settings.js +0 -50
  91. data/lib/path_coverage/html/side.js +0 -599
  92. data/lib/path_coverage/html/sort.js +0 -34
  93. data/lib/path_coverage/html/style.css +0 -484
  94. data/lib/path_coverage/html/tabs.js +0 -15
  95. data/lib/path_coverage/html/theme.js +0 -27
  96. data/lib/path_coverage/html_branch.rb +0 -30
  97. data/lib/path_coverage/html_cards.rb +0 -88
  98. data/lib/path_coverage/html_cells.rb +0 -71
  99. data/lib/path_coverage/html_file.rb +0 -123
  100. data/lib/path_coverage/html_index.rb +0 -128
  101. data/lib/path_coverage/html_index_head.rb +0 -29
  102. data/lib/path_coverage/html_labels.rb +0 -16
  103. data/lib/path_coverage/html_methods.rb +0 -66
  104. data/lib/path_coverage/html_nav.rb +0 -67
  105. data/lib/path_coverage/html_report.rb +0 -131
  106. data/lib/path_coverage/html_statements.rb +0 -186
  107. data/lib/path_coverage/html_tabs.rb +0 -26
  108. data/lib/path_coverage/html_template.rb +0 -26
@@ -7,27 +7,106 @@ module PathCoverage
7
7
  # outcome when the code runs. It inserts text at the byte
8
8
  # ranges the manifest found, and never parses.
9
9
  module Rewriter
10
- # Returns source with each operand in sites wrapped in a
11
- # record call, each site as a whole in a leave call that
12
- # enters it first, and each statement in reaches, { name =>
13
- # offset }, after a reach call that counts its runs. Only
14
- # text is inserted, never a newline, so backtrace line
15
- # numbers still match the file on disk.
16
- def self.rewrite(source, sites, reaches = {})
17
- inserts = sites.flat_map do |site, condition|
18
- site_inserts(site, condition)
10
+ # Returns source with each operand in decisions wrapped in a
11
+ # record call, each decision as a whole in a leave call that
12
+ # enters it first, then the inserts of marks, as mark_inserts
13
+ # gives them. Only text is inserted, never a newline, so
14
+ # backtrace line numbers still match the file on disk.
15
+ def self.rewrite(source, decisions, marks = {})
16
+ inserts = decisions.flat_map do |decision, condition|
17
+ decision_inserts(decision, condition)
18
+ end
19
+ apply(source.dup, inserts + mark_inserts(marks))
20
+ end
21
+
22
+ # The inserts of marks, a hash. Each key is optional, and a
23
+ # missing key marks nothing. reaches is { name => offset }:
24
+ # each statement there gets a reach call before it, counting
25
+ # its runs. passes is { name => offset }: each loop body there
26
+ # gets a pass call first. entries is { name => offset }: each
27
+ # method's entry is reached there, outermost, before any
28
+ # statement's reach at the same place. completions is { rescue
29
+ # fork's name => where }: each body completing is noted there.
30
+ # rescues is { [fork's name, number] => offset }: each rescue
31
+ # running is noted there. values is { fork's name => byte
32
+ # range }: each rescue modifier's rescue running is noted
33
+ # there.
34
+ def self.mark_inserts(marks)
35
+ reach_inserts(marks.fetch(:reaches, {})) +
36
+ pass_inserts(marks.fetch(:passes, {})) +
37
+ reach_inserts(marks.fetch(:entries, {}), :entry) +
38
+ completed_inserts(marks.fetch(:completions, {})) +
39
+ rescued_inserts(marks.fetch(:rescues, {})) +
40
+ ran_inserts(marks.fetch(:values, {}))
41
+ end
42
+
43
+ # Example: { "k.rb:3:18" => 25...26 } wraps bytes 25...26.
44
+ # The inserts that wrap the rescue of each rescue modifier in
45
+ # values, { fork's name => its byte range }, in a ran call. The
46
+ # call keeps the rescue's value, and notes that its rescue 1
47
+ # ran.
48
+ def self.ran_inserts(values)
49
+ values.flat_map do |name, range|
50
+ rank = [range.size, LAYERS.index(:completed)]
51
+ call = "PathCoverage.ran(#{name.inspect}, 1, ("
52
+ [[range.begin, call, rank], [range.end, '))', rank, :close]]
53
+ end
54
+ end
55
+
56
+ # Example: { ["k.rb:4:2", 1] => 52 } puts a rescued call at 52.
57
+ # The inserts that put a rescued call after the header of each
58
+ # rescue in rescues, { [fork's name, its number] => offset }.
59
+ # The call is a statement of its own, the first its rescue
60
+ # runs.
61
+ def self.rescued_inserts(rescues)
62
+ rank = [Float::INFINITY, LAYERS.index(:reach)]
63
+ rescues.map do |(name, number), at|
64
+ call = "; PathCoverage.rescued(#{name.inspect}, #{number})"
65
+ [at, call, rank]
66
+ end
67
+ end
68
+
69
+ # Example: { "k.rb:4:2" => 30...43 } wraps bytes 30...43.
70
+ # The inserts that note each body of completions completing.
71
+ # completions is { rescue fork's name => where }. A byte range
72
+ # is a last statement, wrapped in a completed call that keeps
73
+ # its value, inside any reach before it. A byte offset is an
74
+ # empty body. It gets a completed call of nil, a statement of
75
+ # its own, because the body's value is nil either way.
76
+ def self.completed_inserts(completions)
77
+ completions.flat_map do |name, at|
78
+ call = "PathCoverage.completed(#{name.inspect}, "
79
+ next [[at, "#{call}nil); ", own_rank]] unless at.is_a?(Range)
80
+
81
+ rank = [at.size, LAYERS.index(:completed)]
82
+ [[at.begin, "#{call}(", rank], [at.end, '))', rank, :close]]
83
+ end
84
+ end
85
+
86
+ # The rank of a completed call that is a statement of its own,
87
+ # for an empty body. It is inside any reach at the same place.
88
+ def self.own_rank = [Float::INFINITY, LAYERS.index(:completed)]
89
+
90
+ # Example: { "k.rb:4:16" => 60 } puts a pass call at 60.
91
+ # The inserts that put a pass call, a statement of its own,
92
+ # first in each loop body in passes, { name => offset }. It
93
+ # comes before the reach of a row at the same place, so the
94
+ # row is a step of the pass.
95
+ def self.pass_inserts(passes)
96
+ rank = [Float::INFINITY, LAYERS.index(:pass)]
97
+ passes.map do |name, at|
98
+ [at, "PathCoverage.pass(#{name.inspect}); ", rank]
19
99
  end
20
- apply(source.dup, inserts + reach_inserts(reaches))
21
100
  end
22
101
 
23
102
  # Example: { "k.rb:2:2" => 18 } puts a reach call at 18.
24
103
  # The inserts that put a reach call, a statement of its
25
104
  # own, before each statement in reaches, outside all else
26
- # there. A statement reached by
105
+ # there, in layer. A statement reached by
27
106
  # its byte range, eg an endless def's body, is wrapped in
28
107
  # parentheses with its call, being one expression.
29
- def self.reach_inserts(reaches)
30
- rank = [Float::INFINITY, LAYERS.index(:reach)]
108
+ def self.reach_inserts(reaches, layer = :reach)
109
+ rank = [Float::INFINITY, LAYERS.index(layer)]
31
110
  reaches.flat_map do |name, at|
32
111
  call = "PathCoverage.reach(#{name.inspect}); "
33
112
  next [[at, call, rank]] unless at.is_a?(Range)
@@ -36,58 +115,60 @@ module PathCoverage
36
115
  end
37
116
  end
38
117
 
39
- # The inserts for one site, as its kind gives them, eg its
118
+ # The inserts for one decision, as its kind gives them, eg its
40
119
  # leaves' records and its leave; a block method's call or a
41
120
  # write is noted as if nested at leaf 0.
42
- def self.site_inserts(site, condition)
43
- Kinds.of(condition.operator).site_inserts(site, condition)
121
+ def self.decision_inserts(decision, condition)
122
+ Kinds.of(condition.operator).decision_inserts(decision,
123
+ condition)
44
124
  end
45
125
 
46
- # The inserts that wrap each of site's leaves in a record
47
- # call, innermost of all; a leaf of a nested site that needs
126
+ # The inserts that wrap each of decision's leaves in a record
127
+ # call, innermost of all; a leaf of a nested decision that needs
48
128
  # a call of its own, eg a &. chain's, in the call
49
- # RewriterNested gives it, or in none, and that site's own
129
+ # RewriterNested gives it, or in none, and that decision's own
50
130
  # inserts too.
51
- def self.record_inserts(site, condition)
52
- nested = RewriterNested.calls(site, condition)
131
+ def self.record_inserts(decision, condition)
132
+ nested = RewriterNested.calls(decision, condition)
53
133
  leaves = condition.leaves
54
134
  records = leaves.each_with_index.flat_map do |range, i|
55
- call = nested.fetch(range) { record_call(site, i) }
135
+ call = nested.fetch(range) { record_call(decision, i) }
56
136
  next [] unless call
57
137
 
58
138
  wrap(call, range, condition.commands.include?(range), :leaf)
59
139
  end
60
- records + RewriterNested.inserts(site, condition)
140
+ records + RewriterNested.inserts(decision, condition)
61
141
  end
62
142
 
63
143
  WORD_OPERATORS = %w[and or].freeze
64
144
 
65
- # The inserts that wrap site as a whole in a leave call,
145
+ # The inserts that wrap decision as a whole in a leave call,
66
146
  # outside its leaves' record calls, whose first argument
67
- # enters the site before any leaf runs, so each evaluation
147
+ # enters the decision before any leaf runs, so each evaluation
68
148
  # fills in a trail of its own. An and or or is held in
69
149
  # parentheses, being, like a command call, no bare argument.
70
- def self.trail_inserts(site, condition)
71
- enter = "PathCoverage.enter(#{site.inspect}, " \
150
+ def self.trail_inserts(decision, condition)
151
+ enter = "PathCoverage.enter(#{decision.inspect}, " \
72
152
  "#{condition.leaves.size})"
73
153
  call = "PathCoverage.leave(#{enter}, "
74
154
  wrapped = Kinds.of(condition.operator).trailed(condition)
75
155
  nested = wrapped.is_a?(Condition)
76
156
  word = nested && WORD_OPERATORS.include?(wrapped.operator)
77
- wrap(call, nested ? wrapped.extent : wrapped, word, :site)
157
+ wrap(call, nested ? wrapped.extent : wrapped, word, :decision)
78
158
  end
79
159
 
80
160
  # The opening of the record call around operand index of
81
- # the site.
82
- def self.record_call(site, index)
83
- "PathCoverage.record(#{site.inspect}, #{index}, "
161
+ # the decision.
162
+ def self.record_call(decision, index)
163
+ "PathCoverage.record(#{decision.inspect}, #{index}, "
84
164
  end
85
165
 
86
166
  # The layers of inserts, innermost first, that break a tie
87
167
  # between inserts wrapping the same range: a leaf's call, a
88
- # nested kind's call around its whole, eg end_items, a site's
89
- # leave, and a statement's reach.
90
- LAYERS = %i[leaf kind site reach].freeze
168
+ # nested kind's call around its whole, eg end_items, a decision's
169
+ # leave, a rescue body's last statement's completed, a
170
+ # statement's reach, a loop's pass, and a method's entry.
171
+ LAYERS = %i[leaf kind decision completed reach pass entry].freeze
91
172
 
92
173
  # The two inserts that wrap the code at range in call. Their
93
174
  # order, [size, rank], nests them among inserts at the same
@@ -112,7 +193,7 @@ module PathCoverage
112
193
  # opening inserts go in lowest order first, leaving the
113
194
  # highest outermost, then closing ones highest order first,
114
195
  # so the innermost call closes first. Eg a ternary whose
115
- # predicate is a leaf: the site's leave, wrapping the whole
196
+ # predicate is a leaf: the decision's leave, wrapping the whole
116
197
  # ternary, opens outside the record wrapping its predicate.
117
198
  def self.apply(source, inserts)
118
199
  sorted = inserts.sort_by do |offset, _, order, close|
@@ -2,38 +2,38 @@ require_relative 'condition'
2
2
  require_relative 'kinds'
3
3
 
4
4
  module PathCoverage
5
- # The part of the rewriter that instruments the sites nested in
6
- # another, eg the &. chain in ok && user&.admin?. The site
7
- # around them enters and leaves; each nested site's leaves are
8
- # noted as that site's, at their place among its leaves, by the
5
+ # The part of the rewriter that instruments the decisions nested in
6
+ # another, eg the &. chain in ok && user&.admin?. The decision
7
+ # around them enters and leaves; each nested decision's leaves are
8
+ # noted as that decision's, at their place among its leaves, by the
9
9
  # calls its kind gives in place of plain records.
10
10
  module RewriterNested
11
11
  # { leaf range => the opening of its call, nil for none } for
12
- # the leaves of every site nested in condition whose kind has
12
+ # the leaves of every decision nested in condition whose kind has
13
13
  # calls of its own, condition itself included.
14
- def self.calls(site, condition)
14
+ def self.calls(decision, condition)
15
15
  leaves = condition.leaves
16
16
  rooted(condition).reduce({}) do |calls, inner|
17
17
  kind = Kinds.of(inner.operator)
18
- calls.merge(kind.calls(site, inner, leaves))
18
+ calls.merge(kind.calls(decision, inner, leaves))
19
19
  end
20
20
  end
21
21
 
22
- # The inserts, besides its leaves' calls, for every site
22
+ # The inserts, besides its leaves' calls, for every decision
23
23
  # nested in condition whose kind has calls of its own,
24
24
  # condition itself included, as its kind gives them.
25
- def self.inserts(site, condition)
25
+ def self.inserts(decision, condition)
26
26
  leaves = condition.leaves
27
27
  rooted(condition).flat_map do |inner|
28
28
  at = leaves.index(inner.operands.first)
29
29
  kind = Kinds.of(inner.operator)
30
- kind.inserts(site, inner, at, condition.commands)
30
+ kind.inserts(decision, inner, at, condition.commands)
31
31
  end
32
32
  end
33
33
 
34
34
  # condition alone when its kind has calls of its own, eg a
35
- # block method's call that is a site of its own, its leaves
36
- # then noted at their places from 0; else the sites nested in
35
+ # block method's call that is a decision of its own, its leaves
36
+ # then noted at their places from 0; else the decisions nested in
37
37
  # it.
38
38
  def self.rooted(condition)
39
39
  return [condition] if own_calls?(condition)
@@ -41,7 +41,7 @@ module PathCoverage
41
41
  nested(condition)
42
42
  end
43
43
 
44
- # The sites nested in condition whose kind has calls of its
44
+ # The decisions nested in condition whose kind has calls of its
45
45
  # own, in its operands and in its arms' conditions.
46
46
  def self.nested(condition)
47
47
  condition.operands.flat_map do |operand|
@@ -0,0 +1,84 @@
1
+ require 'json'
2
+ require_relative 'configuration'
3
+ require_relative 'render'
4
+ require_relative 'ways_totals'
5
+
6
+ module PathCoverage
7
+ # The path_coverage_report command: reports saved runs' JSON
8
+ # reports as one, each loop a decision only when asked.
9
+ module RunsReport
10
+ HELP = <<~HELP.freeze
11
+ Usage: path_coverage_report [--loops] [--max-paths N] SOURCE_ROOT GLOBS REPORT_DIR RUN_JSON...
12
+
13
+ Reports the saved runs RUN_JSON... as one, writing the report into
14
+ REPORT_DIR, then prints each method's untested paths and the totals.
15
+ Exits 0 when every path is taken, and 1 when any is not.
16
+
17
+ SOURCE_ROOT the directory holding the covered source
18
+ GLOBS comma-separated globs of the covered files, relative to SOURCE_ROOT
19
+ REPORT_DIR the directory it writes its report into
20
+ RUN_JSON... one or more saved runs' JSON files, space-separated
21
+
22
+ Options:
23
+ -h Show this help
24
+ --loops Count each loop as a decision: 0 items or 1+ items
25
+ --max-paths N Fail each method with more than N paths
26
+ (default 4096, at most 65536)
27
+
28
+ Example:
29
+ path_coverage_report --max-paths 8 /app 'lib/**/*.rb' report runs/*.json
30
+ HELP
31
+
32
+ # Reports the runs argv names, [--loops] [--max-paths N]
33
+ # SOURCE_ROOT GLOBS REPORT_DIR RUN_JSON..., as HELP says,
34
+ # printing the untested paths, then the totals, read from the
35
+ # JSON it wrote, so a run with no untested path still prints
36
+ # them; -h prints HELP. Returns the exit status: 1 when the
37
+ # report fails, as WaysTotals.failed? says, else 0.
38
+ def self.main(argv)
39
+ return help if argv.first == '-h'
40
+
41
+ options, (source_root, globs, report_dir, *runs) = options(argv)
42
+ config = configured(source_root, globs, report_dir, options)
43
+ Render.from_json(config, runs)
44
+ written = JSON.parse(File.read(config.json_path))
45
+ puts WaysTotals.lines(written)
46
+ WaysTotals.failed?(written) ? 1 : 0
47
+ end
48
+
49
+ # Prints HELP; returns 0.
50
+ def self.help
51
+ print HELP
52
+ 0
53
+ end
54
+
55
+ # [{ loops:, max_paths: }, the rest of argv] of argv's leading
56
+ # options; loops false and max_paths the default when not
57
+ # given.
58
+ def self.options(argv)
59
+ options = { loops: false, max_paths: Configuration::MAX_PATHS }
60
+ rest = argv.dup
61
+ while %w[--loops --max-paths].include?(rest.first)
62
+ flag = rest.shift
63
+ next options[:loops] = true if flag == '--loops'
64
+
65
+ options[:max_paths] = Integer(rest.shift)
66
+ end
67
+ [options, rest]
68
+ end
69
+
70
+ # The configuration reporting the files globs, comma-separated,
71
+ # match under source_root into report_dir, with the :ways
72
+ # reporter and options' loops and max_paths.
73
+ def self.configured(source_root, globs, report_dir, options)
74
+ Configuration.new do
75
+ root source_root
76
+ globs.split(',').each { cover _1 }
77
+ coverage_dir report_dir
78
+ report :ways
79
+ max_paths options[:max_paths]
80
+ loops if options[:loops]
81
+ end
82
+ end
83
+ end
84
+ end
@@ -5,11 +5,11 @@ module PathCoverage
5
5
  # conditions on it.
6
6
  module SourceLines
7
7
  # [{number:, text:, status:, span:}] for each line of
8
- # source. A line in a site's span shares one button with
8
+ # source. A line in a decision's span shares one button with
9
9
  # the span's other lines; status and span are nil on a line
10
10
  # with no condition.
11
- def self.of(source, sites, operands)
12
- marks = marks(source, sites, operands)
11
+ def self.of(source, decisions, operands)
12
+ marks = marks(source, decisions, operands)
13
13
  source.lines.each_with_index.map do |text, index|
14
14
  number = index + 1
15
15
  mark = marks.fetch(number, {})
@@ -20,28 +20,28 @@ module PathCoverage
20
20
  end
21
21
  end
22
22
 
23
- # { line number => {status:, span:} } for each line a site
23
+ # { line number => {status:, span:} } for each line a decision
24
24
  # covers, every line of a merged span sharing that span.
25
- def self.marks(source, sites, operands)
26
- blocks = merged(spans_of(source, sites, operands))
25
+ def self.marks(source, decisions, operands)
26
+ blocks = merged(spans_of(source, decisions, operands))
27
27
  blocks.each_with_object({}) do |(span, status), marks|
28
28
  span.each { |number| marks[number] = { status:, span: } }
29
29
  end
30
30
  end
31
31
 
32
- # [span, status] for each site: from the line it starts on
32
+ # [span, status] for each decision: from the line it starts on
33
33
  # to the line its last operand ends on.
34
- def self.spans_of(source, sites, operands)
35
- sites.map do |site, condition|
34
+ def self.spans_of(source, decisions, operands)
35
+ decisions.map do |decision, condition|
36
36
  last = condition.leaves.last
37
- span = line_of(site)..end_line(source, last)
38
- [span, status_of(operands.fetch(site))]
37
+ span = line_of(decision)..end_line(source, last)
38
+ [span, status_of(operands.fetch(decision))]
39
39
  end
40
40
  end
41
41
 
42
42
  # The spans merged wherever they overlap, in line order. A
43
- # merged span has :gaps if any of its sites has, so a
44
- # covered site never hides another's gaps.
43
+ # merged span has :gaps if any of its decisions has, so a
44
+ # covered decision never hides another's gaps.
45
45
  def self.merged(spans)
46
46
  sorted = spans.sort_by { |span, _| span.begin }
47
47
  sorted.each_with_object([]) do |(span, status), blocks|
@@ -66,9 +66,9 @@ module PathCoverage
66
66
  gaps.empty? ? :covered : :gaps
67
67
  end
68
68
 
69
- # The line a site starts on, from its path:line:column name.
70
- def self.line_of(site)
71
- site[/:(\d+):\d+\z/, 1].to_i
69
+ # The line a decision starts on, from its path:line:column name.
70
+ def self.line_of(decision)
71
+ decision[/:(\d+):\d+\z/, 1].to_i
72
72
  end
73
73
 
74
74
  # The line of source on which byte range ends.
@@ -0,0 +1,100 @@
1
+ require_relative 'code_tokens'
2
+ require_relative 'fold_rows'
3
+ require_relative 'decision_locations'
4
+ require_relative 'decision_tree'
5
+ require_relative 'source_lines'
6
+ require_relative 'summary'
7
+
8
+ module PathCoverage
9
+ # What a renderer needs of each covered file beyond the JSON
10
+ # report's counts, worked out from its source, so it can draw
11
+ # the file's page without parsing the source itself.
12
+ module SourceReport
13
+ # The version of the source report's shape.
14
+ SCHEMA = 1
15
+
16
+ # The URL of the schema the source report follows.
17
+ SCHEMA_URL = 'https://path-coverage.github.io/schemas/v2/' \
18
+ 'path_coverage.source.schema.json'.freeze
19
+
20
+ # The source report of a run, headed by its schema's URL and
21
+ # version: config's ignored operators, the
22
+ # covered files of each of config's groups, each top of the
23
+ # rows of flows, { def decision => flow }, coloured, and each of
24
+ # files', { full path => {source:, decisions:} }, lines and
25
+ # decisions, each decision's paths as decision_paths has
26
+ # them, its fold headed by its row's top.
27
+ def self.of(files, config, decision_paths, flows)
28
+ rows = flows.values.flat_map { _1['rows'] }
29
+ tops = rows.to_h { [_1['decision'], _1['top']] }
30
+ shown = files.to_h do |full_path, parsed|
31
+ [config.relative(full_path),
32
+ file(parsed, decision_paths, tops)]
33
+ end
34
+ { '$schema': SCHEMA_URL, schema: SCHEMA,
35
+ ignored: config.ignored, groups: groups(config, shown.keys),
36
+ labels: labels(rows), files: shown }
37
+ end
38
+
39
+ # { top => it coloured } of each distinct top of rows, a
40
+ # decision's or a statement's, eg return.
41
+ def self.labels(rows)
42
+ rows.map { _1['top'] }.uniq.to_h do |top|
43
+ [top, CodeTokens.label_code(top)]
44
+ end
45
+ end
46
+
47
+ # { name => the paths its globs match } of each of config's
48
+ # groups.
49
+ def self.groups(config, paths)
50
+ config.groups.transform_values do |globs|
51
+ paths.select { Summary.matches?(_1, globs) }
52
+ end
53
+ end
54
+
55
+ # One file's lines and decisions, parsed being its {source:,
56
+ # decisions:}.
57
+ def self.file(parsed, decision_paths, tops)
58
+ source, decisions = parsed.values_at(:source, :decisions)
59
+ shown = decisions.keys.to_h do |name|
60
+ paths = decision_paths.fetch(name)
61
+ [name,
62
+ decision(source, decisions, name, paths, tops.fetch(name))]
63
+ end
64
+ { lines: lines(source), decisions: shown }
65
+ end
66
+
67
+ # Example: "ok?(1)\n" gives [{ text: 'ok?(1)', tokens:
68
+ # [[4, 5, 'n']] }].
69
+ # Each line of source: its text and its coloured tokens.
70
+ def self.lines(source)
71
+ tokens = CodeTokens.of(source)
72
+ source.lines.each_with_index.map do |text, index|
73
+ { text: text.chomp, tokens: tokens.fetch(index + 1, []) }
74
+ end
75
+ end
76
+
77
+ # Example: ok?(id) && new?(id) on line 2 gives span [2, 2].
78
+ # The decision named name, of decisions, { name => Condition }, in
79
+ # source: the [first, last] lines it spans, and its fold's
80
+ # rows, headed by top, its row's top, a column for each of
81
+ # paths, [{path:, count:}], in their order, with no counts.
82
+ def self.decision(source, decisions, name, paths, top)
83
+ condition = decisions.fetch(name)
84
+ last = SourceLines.end_line(source, condition.leaves.last)
85
+ span = [SourceLines.line_of(name), last]
86
+ { span:, fold: fold(source, condition, paths, top) }
87
+ end
88
+
89
+ # The rows of condition's fold, headed by top, its decision row
90
+ # with no counts.
91
+ def self.fold(source, condition, paths, top)
92
+ outcomes = paths.map { _1[:path] }
93
+ tree = DecisionTree.built(source, condition, outcomes)
94
+ located = DecisionLocations.located(source, condition, tree)
95
+ else_at = DecisionLocations.else_at(source, condition)
96
+ own, *rest = FoldRows.rows(located.merge(else_at:, label: top))
97
+ [own.except(:counts), *rest]
98
+ end
99
+ end
100
+ end