/
reporter.rb
126 lines (102 loc) · 3.42 KB
/
reporter.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
module Turn
require 'turn/colorize'
require 'turn/core_ext'
# There are two distinct way in which a report may be utilized
# by a Runner: per-call or per-file. The method #pass, #fail
# and #error are generic, and will be used in either case.
# A per-call runner will use all the methods of a Reporter,
# while a per-file runner will use start_case per file,
# and will not use the start_test and finish_test methods,
# since those are beyond it's grainularity.
#
class Reporter
include Colorize
# Where to send report, defaults to `$stdout`.
attr :io
def initialize(io, opts={})
@io = io || $stdout
@trace = opts[:trace]
@natural = opts[:natural]
end
# These methods are called in the process of running the tests.
# At the very start, before any testcases are run, this is called.
def start_suite(test_suite)
end
# Invoked before a testcase is run.
def start_case(test_case)
end
# Invoked before a test is run.
def start_test(test)
end
# Invoked when a test passes.
def pass(message=nil)
end
# Invoked when a test raises an assertion.
def fail(assertion, message=nil)
end
# Invoked when a test raises an exception.
def error(exception, message=nil)
end
# Invoked when a test is skipped.
def skip(exception, message=nil)
end
# Invoked after a test has been run.
def finish_test(test)
end
# Invoked after all tests in a testcase have ben run.
def finish_case(test_case)
end
# After all tests are run, this is the last observable action.
def finish_suite(test_suite)
end
private
# Apply filter_backtrace and limit_backtrace in one go.
def clean_backtrace(backtrace)
limit_backtrace(filter_backtrace(backtrace))
end
$RUBY_IGNORE_CALLERS ||= []
$RUBY_IGNORE_CALLERS.concat([
/\/lib\/turn.*\.rb/,
/\/bin\/turn/,
/\/lib\/minitest.*\.rb/,
/\/test\/unit(?!(\/.*\_test.rb)|.*\/test_.*).*\.rb.*/
])
# Filter backtrace of unimportant entries, and applies count limit if set in
# configuration. Setting $DEBUG to true will deactivate filter, or if the filter
# happens to remove all backtrace entries it will revert to the full backtrace,
# as that probably means there was an issue with the test harness itself.
def filter_backtrace(backtrace)
return [] unless backtrace
bt = backtrace.dup
bt = bt.reject{ |line| $RUBY_IGNORE_CALLERS.any?{ |re| re =~ line } } unless $DEBUG
#bt.reject!{ |line| line.rindex('minitest') }
#bt.reject!{ |line| line.rindex('test/unit') }
#bt.reject!{ |line| line.rindex('lib/turn') }
#bt.reject!{ |line| line.rindex('bin/turn') }
bt = backtrace if bt.empty? # if empty just dump the whole thing
bt.map{ |line| line.sub(Dir.pwd+'/', '') }
end
# Limit backtrace to number of lines if `trace` configuration option is set.
def limit_backtrace(backtrace)
return [] unless backtrace
@trace ? backtrace[0, @trace.to_i] : backtrace
end
#
def naturalized_name(test)
if @natural
" #{test.name.gsub("test_", "").gsub(/_/, " ")}"
else
" #{test.name}"
end
end
#
def ticktock
t = Time.now - @time
h, t = t.divmod(60)
m, t = t.divmod(60)
s = t.truncate
f = ((t - s) * 1000).to_i
"%01d:%02d:%02d.%03d" % [h,m,s,f]
end
end
end