/
abstract.rb
547 lines (459 loc) · 13.8 KB
/
abstract.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
require 'ansi'
require 'abbrev'
module Tapout
##
# Namespace for Report Formats.
#
module Reporters
# Default reporter.
DEAFULT_REPORTER = 'dot'
# Returns a Hash of name to reporter class.
def self.index
@index ||= {}
end
# Returns a reporter class given it's name or a unique abbreviation of it.
# If `name` is `nil` then the default dot reporter is returned.
def self.factory(name)
list = index.keys.abbrev
rptr = index[list[name || DEAFULT_REPORTER]]
unless rptr
begin
require "tapout-#{name}"
list = index.keys.abbrev
rptr = index[list[name || DEAFULT_REPORTER]]
rescue LoadError
end
raise ArgumentError, "Unrecognized reporter -- #{name.inspect}" unless rptr
end
rptr
end
##
# The Abstract class serves as a base class for all reporters. Reporters
# must sublcass Abstract in order to be added the the Reporters Index.
#
class Abstract
# When Abstract is inherited it saves a reference to it in `Reporters.index`.
def self.inherited(subclass)
name = subclass.name.split('::').last.downcase
name = name.chomp('reporter')
Reporters.index[name] = subclass
end
#old_sync, @@out.sync = @@out.sync, true if io.respond_to? :sync=
# New reporter.
def initialize
@passed = []
@failed = []
@raised = []
@skipped = []
@omitted = []
#$stdin.sync = true
$stdout.sync = true
@case_stack = []
@source = {}
@exit_code = 0 # assume passing
end
# When all is said and done.
def finalize
@exit_code
end
# Handle header.
def start_suite(entry)
@start_time = Time.now
end
# At the start of a new test case.
def start_case(entry)
end
# Handle test. This is run before the status handlers.
def start_test(entry)
end
# Handle test with pass status.
def pass(entry)
@passed << entry
end
# Handle test with fail status.
def fail(entry)
@failed << entry
end
# Handle test with error status.
def error(entry)
@raised << entry
end
# Handle test with omit status.
def omit(entry)
@omitted << entry
end
# Handle test with skip or pending status.
def todo(entry)
@skipped << entry
end
# Same as todo.
alias_method :skip, :todo
# Handle an arbitray note.
def note(entry)
end
# Handle running tally.
def tally(entry)
end
# When a test unit is complete.
def finish_test(entry)
end
# When a test case is complete.
def finish_case(entry)
end
# Handle final entry.
def finish_suite(entry)
end
# -- H A N D L E R --
#
def <<(entry)
handle(entry)
end
# Handler method. This dispatches a given entry to the appropriate
# report methods.
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
# Get the exit code.
def exit_code
@exit_code
end
# Calculate the lapsed time, the rate of testing and average time per test.
#
# @return [Array<Float>] Lapsed time, rate and average.
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
# Return the total counts given a tally or final entry.
#
# @return [Array<Integer>] The total, fail, error, todo and omit counts.
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
# Generate a tally message given a tally or final entry.
#
# @return [String] tally message
def tally_message(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
# Give a test entry, returns a clean and filtered backtrace.
#
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
# Used to clean-up backtrace.
#
# TODO: Use Rubinius global system instead.
INTERNALS = /(lib|bin)#{Regexp.escape(File::SEPARATOR)}tapout/
# Clean the backtrace of any "boring" reference.
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 = trace[0, config.trace_depth]
trace
end
# Get s nicely formatted string of backtrace and source code, ready
# for output.
#
# @return [String] Formatted backtrace with source code.
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
# Returns an associative array of backtraces along with corresponding
# source code, if available.
#
# @return [Array<String,String>]
# Array of backtrace line and source code.
def backtrace_snippets_chain(test)
code = test['exception']['snippet']
file = test['exception']['file']
line = test['exception']['line']
chain = []
bts = backtrace(test)
if bts.empty?
if file && line
bts << "#{file}:#{line}"
end
end
bts.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
if chain.first && chain.first.last.nil?
chain[0][1] = code_snippet('snippet'=>code, 'line'=>line)
end
chain
end
# Parse a bactrace line into file and line number. Returns nil for both
# if parsing fails.
#
# @return [Array<String,Integer>] File and line number.
def parse_backtrace(bt)
if md = /(.+?):(\d+)/.match(bt)
return md[1], md[2].to_i
else
return nil, nil
end
end
# Returns a String of source code.
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 = config.lines # 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
#
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
# Cache source file text. This is only used if the TAP-Y stream
# doesn not provide a snippet and the test file is locatable.
#
# @return [String] File contents.
def source(file)
@source[file] ||= (
File.readlines(file)
)
end
# Parse source location from caller, caller[0] or an Exception object.
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
#
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
#
def captured_stdout(test)
stdout = test['stdout'].to_s.strip
return if stdout.empty?
if block_given?
yield(stdout)
else
stdout
end
end
#
def captured_stderr(test)
stderr = test['stderr'].to_s.strip
return if stderr.empty?
if block_given?
yield(stderr)
else
stderr
end
end
#
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
#
def captured_output?(test)
captured_stdout?(test) || captured_stderr?(test)
end
#
def captured_stdout?(test)
stderr = test['stdout'].to_s.strip
!stderr.empty?
end
#
def captured_stderr?(test)
stderr = test['stderr'].to_s.strip
!stderr.empty?
end
#
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
# Access to configurtion.
def config
Tapout.config
end
private
#def puts(*s)
# super(*s)
# $stdout.flush
#end
end#class Abstract
end#module Reporters
end