Class: Tapout::Reporters::Abstract
- Inherits:
-
Object
- Object
- Tapout::Reporters::Abstract
- Defined in:
- lib/tapout/reporters/abstract.rb
Overview
The Abstract class serves as a base class for all reporters. Reporters must sublcass Abstract in order to be added the the Reporters Index.
Direct Known Subclasses
BreakdownReporter, DotReporter, Html, Markdown, Outline, PrettyReporter, ProgressReporter, RuntimeReporter, TapReporter, TurnReporter
Constant Summary collapse
- INTERNALS =
Used to clean-up backtrace.
TODO: Use Rubinius global system instead.
/(lib|bin)#{Regexp.escape(File::SEPARATOR)}tapout/
Class Method Summary collapse
-
.inherited(subclass) ⇒ Object
When Abstract is inherited it saves a reference to it in ‘Reporters.index`.
Instance Method Summary collapse
- #<<(entry) ⇒ Object
-
#backtrace(test) ⇒ Object
Give a test entry, returns a clean and filtered backtrace.
-
#backtrace_snippets(test) ⇒ String
Get s nicely formatted string of backtrace and source code, ready for output.
-
#backtrace_snippets_chain(test) ⇒ Array<String,String>
Returns an associative array of backtraces along with corresponding source code, if available.
- #captured_output(test) ⇒ Object
- #captured_output?(test) ⇒ Boolean
- #captured_stderr(test) ⇒ Object
- #captured_stderr?(test) ⇒ Boolean
- #captured_stdout(test) ⇒ Object
- #captured_stdout?(test) ⇒ Boolean
-
#clean_backtrace(backtrace) ⇒ Object
Clean the backtrace of any “boring” reference.
-
#code_snippet(entry) ⇒ Object
Returns a String of source code.
- #complete_cases(case_entry = nil) ⇒ Object
-
#config ⇒ Object
Access to configurtion.
-
#count_tally(entry) ⇒ Array<Integer>
Return the total counts given a tally or final entry.
- #duration(seconds, precision = 2) ⇒ Object
-
#error(entry) ⇒ Object
Handle test with error status.
-
#exit_code ⇒ Object
Get the exit code.
-
#fail(entry) ⇒ Object
Handle test with fail status.
-
#finalize ⇒ Object
When all is said and done.
-
#finish_case(entry) ⇒ Object
When a test case is complete.
-
#finish_suite(entry) ⇒ Object
Handle final entry.
-
#finish_test(entry) ⇒ Object
When a test unit is complete.
- #format_snippet_array(array, line) ⇒ Object
-
#handle(entry) ⇒ Object
Handler method.
-
#initialize ⇒ Abstract
constructor
New reporter.
-
#note(entry) ⇒ Object
Handle an arbitray note.
-
#omit(entry) ⇒ Object
Handle test with omit status.
-
#parse_backtrace(bt) ⇒ Array<String,Integer>
Parse a bactrace line into file and line number.
-
#parse_source_location(caller) ⇒ Object
Parse source location from caller, caller or an Exception object.
-
#pass(entry) ⇒ Object
Handle test with pass status.
-
#source(file) ⇒ String
Cache source file text.
-
#start_case(entry) ⇒ Object
At the start of a new test case.
-
#start_suite(entry) ⇒ Object
Handle header.
-
#start_test(entry) ⇒ Object
Handle test.
-
#tally(entry) ⇒ Object
Handle running tally.
-
#tally_message(entry) ⇒ String
Generate a tally message given a tally or final entry.
-
#time_tally(entry) ⇒ Array<Float>
Calculate the lapsed time, the rate of testing and average time per test.
-
#todo(entry) ⇒ Object
(also: #skip)
Handle test with skip or pending status.
Constructor Details
#initialize ⇒ Abstract
New reporter.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/tapout/reporters/abstract.rb', line 49 def initialize @passed = [] @failed = [] @raised = [] @skipped = [] @omitted = [] @case_stack = [] @source = {} @exit_code = 0 # assume passing end |
Class Method Details
.inherited(subclass) ⇒ Object
When Abstract is inherited it saves a reference to it in ‘Reporters.index`.
40 41 42 43 44 |
# File 'lib/tapout/reporters/abstract.rb', line 40 def self.inherited(subclass) name = subclass.name.split('::').last.downcase name = name.chomp('reporter') Reporters.index[name] = subclass end |
Instance Method Details
#<<(entry) ⇒ Object
130 131 132 |
# File 'lib/tapout/reporters/abstract.rb', line 130 def <<(entry) handle(entry) end |
#backtrace(test) ⇒ Object
Give a test entry, returns a clean and filtered backtrace.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/tapout/reporters/abstract.rb', line 247 def backtrace(test) exception = test['exception'] trace = exception['backtrace'] file = exception['file'] line = exception['line'] if trace trace = clean_backtrace(trace) else trace = [] trace << "#{file}:#{line}" if file && line end trace end |
#backtrace_snippets(test) ⇒ String
Get s nicely formatted string of backtrace and source code, ready for output.
292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/tapout/reporters/abstract.rb', line 292 def backtrace_snippets(test) string = [] backtrace_snippets_chain(test).each do |(stamp, snip)| string << stamp.ansi(*config.highlight) if snip if snip.index('=>') string << snip.sub(/(\=\>.*?)$/, '\1'.ansi(*config.highlight)) else string << snip end end end string.join("\n") end |
#backtrace_snippets_chain(test) ⇒ Array<String,String>
Returns an associative array of backtraces along with corresponding source code, if available.
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/tapout/reporters/abstract.rb', line 312 def backtrace_snippets_chain(test) code = test['exception']['snippet'] line = test['exception']['line'] chain = [] backtrace(test).each do |bt| if md = /(.+?):(\d+)/.match(bt) chain << [bt, code_snippet('file'=>md[1], 'line'=>md[2].to_i)] else chain << [bt, nil] end end # use the tap-y/j snippet if the first file was not found chain[0][1] = code_snippet('snippet'=>snippet, 'line'=>line) unless chain[0][1] chain end |
#captured_output(test) ⇒ Object
477 478 479 480 481 482 |
# File 'lib/tapout/reporters/abstract.rb', line 477 def captured_output(test) str = "" str += captured_stdout(test){ |c| "\nSTDOUT\n#{c.tabto(4)}\n" }.to_s str += captured_stderr(test){ |c| "\nSTDERR\n#{c.tabto(4)}\n" }.to_s str end |
#captured_output?(test) ⇒ Boolean
485 486 487 |
# File 'lib/tapout/reporters/abstract.rb', line 485 def captured_output?(test) captured_stdout?(test) || captured_stderr?(test) end |
#captured_stderr(test) ⇒ Object
466 467 468 469 470 471 472 473 474 |
# File 'lib/tapout/reporters/abstract.rb', line 466 def captured_stderr(test) stderr = test['stderr'].to_s.strip return if stderr.empty? if block_given? yield(stderr) else stderr end end |
#captured_stderr?(test) ⇒ Boolean
496 497 498 499 |
# File 'lib/tapout/reporters/abstract.rb', line 496 def captured_stderr?(test) stderr = test['stderr'].to_s.strip !stderr.empty? end |
#captured_stdout(test) ⇒ Object
455 456 457 458 459 460 461 462 463 |
# File 'lib/tapout/reporters/abstract.rb', line 455 def captured_stdout(test) stdout = test['stdout'].to_s.strip return if stdout.empty? if block_given? yield(stdout) else stdout end end |
#captured_stdout?(test) ⇒ Boolean
490 491 492 493 |
# File 'lib/tapout/reporters/abstract.rb', line 490 def captured_stdout?(test) stderr = test['stdout'].to_s.strip !stderr.empty? end |
#clean_backtrace(backtrace) ⇒ Object
Clean the backtrace of any “boring” reference.
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/tapout/reporters/abstract.rb', line 270 def clean_backtrace(backtrace) if ENV['debug'] trace = backtrace else trace = backtrace.reject{ |bt| bt =~ INTERNALS } end trace = trace.map do |bt| if i = bt.index(':in') bt[0...i] else bt end end trace = backtrace if trace.empty? trace = trace.map{ |bt| bt.sub(Dir.pwd+File::SEPARATOR,'') } trace end |
#code_snippet(entry) ⇒ Object
Returns a String of source code.
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/tapout/reporters/abstract.rb', line 342 def code_snippet(entry) file = entry['file'] line = entry['line'] snippet = entry['snippet'] s = [] case snippet when String lines = snippet.lines.to_a index = line - ((lines.size - 1) / 2) lines.each do |line| s << [index, line] index += 1 end when Array snippet.each do |h| s << [h.keys.first, h.values.first] end else ##backtrace = exception.backtrace.reject{ |bt| bt =~ INTERNALS } ##backtrace.first =~ /(.+?):(\d+(?=:|\z))/ or return "" #caller =~ /(.+?):(\d+(?=:|\z))/ or return "" #source_file, source_line = $1, $2.to_i if file && File.file?(file) source = source(file) radius = 3 # number of surrounding lines to show region = [line - radius, 1].max .. [line + radius, source.length].min #len = region.last.to_s.length s = region.map do |n| #format % [n, source[n-1].chomp] [n, source[n-1].chomp] end end end format_snippet_array(s, line) # len = s.map{ |(n,t)| n }.max.to_s.length # # # ensure proper alignment by zero-padding line numbers # format = " %5s %0#{len}d %s" # # #s = s.map{|n,t|[n,t]}.sort{|a,b|a[0]<=>b[0]} # # pretty = s.map do |(n,t)| # format % [('=>' if n == line), n, t.rstrip] # end #.unshift "[#{region.inspect}] in #{source_file}" # # return pretty end |
#complete_cases(case_entry = nil) ⇒ Object
442 443 444 445 446 447 448 449 450 451 452 |
# File 'lib/tapout/reporters/abstract.rb', line 442 def complete_cases(case_entry=nil) if case_entry while @case_stack.last and @case_stack.last['level'].to_i >= case_entry['level'].to_i finish_case(@case_stack.pop) end else while @case_stack.last finish_case(@case_stack.pop) end end end |
#config ⇒ Object
Access to configurtion.
512 513 514 |
# File 'lib/tapout/reporters/abstract.rb', line 512 def config Tapout.config end |
#count_tally(entry) ⇒ Array<Integer>
Return the total counts given a tally or final entry.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/tapout/reporters/abstract.rb', line 193 def count_tally(entry) total = @passed.size + @failed.size + @raised.size + @skipped.size + @omitted.size total = entry['counts']['total'] || total if counts = entry['counts'] pass = counts['pass'] || @passed.size fail = counts['fail'] || @failed.size error = counts['error'] || @raised.size todo = counts['todo'] || @skipped.size omit = counts['omit'] || @omitted.size else pass, fail, error, todo, omit = *[@passed, @failed, @raised, @skipped, @omitted].map{ |e| e.size } end return total, pass, fail, error, todo, omit end |
#duration(seconds, precision = 2) ⇒ Object
502 503 504 505 506 507 508 509 |
# File 'lib/tapout/reporters/abstract.rb', line 502 def duration(seconds, precision=2) p = precision.to_i s = seconds.to_i f = seconds - s h, s = s.divmod(60) m, s = s.divmod(60) "%02d:%02d:%02d.%0#{p}d" % [h, m, s, f * 10**p] end |
#error(entry) ⇒ Object
Handle test with error status.
90 91 92 |
# File 'lib/tapout/reporters/abstract.rb', line 90 def error(entry) @raised << entry end |
#exit_code ⇒ Object
Get the exit code.
172 173 174 |
# File 'lib/tapout/reporters/abstract.rb', line 172 def exit_code @exit_code end |
#fail(entry) ⇒ Object
Handle test with fail status.
85 86 87 |
# File 'lib/tapout/reporters/abstract.rb', line 85 def fail(entry) @failed << entry end |
#finalize ⇒ Object
When all is said and done.
62 63 64 |
# File 'lib/tapout/reporters/abstract.rb', line 62 def finalize @exit_code end |
#finish_case(entry) ⇒ Object
When a test case is complete.
120 121 |
# File 'lib/tapout/reporters/abstract.rb', line 120 def finish_case(entry) end |
#finish_suite(entry) ⇒ Object
Handle final entry.
124 125 |
# File 'lib/tapout/reporters/abstract.rb', line 124 def finish_suite(entry) end |
#finish_test(entry) ⇒ Object
When a test unit is complete.
116 117 |
# File 'lib/tapout/reporters/abstract.rb', line 116 def finish_test(entry) end |
#format_snippet_array(array, line) ⇒ Object
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/tapout/reporters/abstract.rb', line 400 def format_snippet_array(array, line) s = array len = s.map{ |(n,t)| n }.max.to_s.length # ensure proper alignment by zero-padding line numbers format = " %5s %0#{len}d %s" #s = s.map{|n,t|[n,t]}.sort{|a,b|a[0]<=>b[0]} pretty = s.map do |(n,t)| format % [('=>' if n == line), n, t.rstrip] end #.unshift "[#{region.inspect}] in #{source_file}" pretty.join("\n") end |
#handle(entry) ⇒ Object
Handler method. This dispatches a given entry to the appropriate report methods.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/tapout/reporters/abstract.rb', line 136 def handle(entry) case entry['type'] when 'suite' start_suite(entry) when 'case' complete_cases(entry) @case_stack << entry start_case(entry) when 'note' note(entry) when 'test' start_test(entry) case entry['status'] when 'pass' pass(entry) when 'fail' @exit_code = -1 fail(entry) when 'error' @exit_code = -1 error(entry) when 'omit' omit(entry) when 'todo', 'skip', 'pending' todo(entry) end finish_test(entry) when 'tally' tally(entry) when 'final' complete_cases finish_suite(entry) end end |
#note(entry) ⇒ Object
Handle an arbitray note.
108 109 |
# File 'lib/tapout/reporters/abstract.rb', line 108 def note(entry) end |
#omit(entry) ⇒ Object
Handle test with omit status.
95 96 97 |
# File 'lib/tapout/reporters/abstract.rb', line 95 def omit(entry) @omitted << entry end |
#parse_backtrace(bt) ⇒ Array<String,Integer>
Parse a bactrace line into file and line number. Returns nil for both if parsing fails.
333 334 335 336 337 338 339 |
# File 'lib/tapout/reporters/abstract.rb', line 333 def parse_backtrace(bt) if md = /(.+?):(\d+)/.match(bt) return md[1], md[2].to_i else return nil, nil end end |
#parse_source_location(caller) ⇒ Object
Parse source location from caller, caller or an Exception object.
428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/tapout/reporters/abstract.rb', line 428 def parse_source_location(caller) case caller when Exception trace = caller.backtrace.reject{ |bt| bt =~ INTERNALS } caller = trace.first when Array caller = caller.first end caller =~ /(.+?):(\d+(?=:|\z))/ or return "" source_file, source_line = $1, $2.to_i returnf source_file, source_line end |
#pass(entry) ⇒ Object
Handle test with pass status.
80 81 82 |
# File 'lib/tapout/reporters/abstract.rb', line 80 def pass(entry) @passed << entry end |
#source(file) ⇒ String
Cache source file text. This is only used if the TAP-Y stream doesn not provide a snippet and the test file is locatable.
421 422 423 424 425 |
# File 'lib/tapout/reporters/abstract.rb', line 421 def source(file) @source[file] ||= ( File.readlines(file) ) end |
#start_case(entry) ⇒ Object
At the start of a new test case.
72 73 |
# File 'lib/tapout/reporters/abstract.rb', line 72 def start_case(entry) end |
#start_suite(entry) ⇒ Object
Handle header.
67 68 69 |
# File 'lib/tapout/reporters/abstract.rb', line 67 def start_suite(entry) @start_time = Time.now end |
#start_test(entry) ⇒ Object
Handle test. This is run before the status handlers.
76 77 |
# File 'lib/tapout/reporters/abstract.rb', line 76 def start_test(entry) end |
#tally(entry) ⇒ Object
Handle running tally.
112 113 |
# File 'lib/tapout/reporters/abstract.rb', line 112 def tally(entry) end |
#tally_message(entry) ⇒ String
Generate a tally message given a tally or final entry.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/tapout/reporters/abstract.rb', line 213 def (entry) sums = count_tally(entry) total, pass, fail, error, todo, omit = *sums # TODO: Assertion counts isn't TAP-Y/J spec, is it a good idea to add ? if entry['counts'] && entry['counts']['assertions'] assertions = entry['counts']['assertions']['pass'] failures = entry['counts']['assertions']['fail'] else assertions = nil failures = nil end text = [] text << "%s pass".ansi(*config.pass) text << "%s fail".ansi(*config.fail) text << "%s errs".ansi(*config.error) text << "%s todo".ansi(*config.todo) text << "%s omit".ansi(*config.omit) text = "%s tests: " + text.join(", ") if assertions text << " (%s/%s assertions)" text = text % (sums + [assertions - failures, assertions]) else text = text % sums end text end |
#time_tally(entry) ⇒ Array<Float>
Calculate the lapsed time, the rate of testing and average time per test.
179 180 181 182 183 184 185 186 187 188 |
# File 'lib/tapout/reporters/abstract.rb', line 179 def time_tally(entry) total = @passed.size + @failed.size + @raised.size + @skipped.size + @omitted.size total = entry['counts']['total'] || total time = (entry['time'] || (Time.now - @start_time)).to_f rate = total / time avg = time / total return time, rate, avg end |
#todo(entry) ⇒ Object Also known as: skip
Handle test with skip or pending status.
100 101 102 |
# File 'lib/tapout/reporters/abstract.rb', line 100 def todo(entry) @skipped << entry end |