Class: Test::Reporters::Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/test/reporters/abstract.rb

Overview

Test Reporter Base Class

Direct Known Subclasses

AbstractHash, Dotprogress, Html, Outline, Progress, Summary, Tap

Constant Summary collapse

TITLES =

TODO: lump skipped and omitted into one group ?

{
  :pass  => 'passing',
  :fail  => 'failures',
  :error => 'errors',
  :todo  => 'pending',
  :omit  => 'omissions',
  :skip  => 'skipped'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner) ⇒ Abstract

Returns a new instance of Abstract.



31
32
33
34
35
36
37
# File 'lib/test/reporters/abstract.rb', line 31

def initialize(runner)
  @runner = runner
  #@source = {}

  # in case start_suite is overridden
  @start_time = Time.now
end

Instance Attribute Details

#runnerObject (readonly)

Returns the value of attribute runner.



40
41
42
# File 'lib/test/reporters/abstract.rb', line 40

def runner
  @runner
end

Class Method Details

.inherited(base) ⇒ Object



21
22
23
# File 'lib/test/reporters/abstract.rb', line 21

def self.inherited(base)
  registry << base
end

.registryObject



26
27
28
# File 'lib/test/reporters/abstract.rb', line 26

def self.registry
  @registry ||= []
end

Instance Method Details

#begin_case(test_case) ⇒ Object



48
49
# File 'lib/test/reporters/abstract.rb', line 48

def begin_case(test_case)
end

#begin_suite(test_suite) ⇒ Object



43
44
45
# File 'lib/test/reporters/abstract.rb', line 43

def begin_suite(test_suite)
  @start_time = Time.now
end

#begin_test(test) ⇒ Object



52
53
# File 'lib/test/reporters/abstract.rb', line 52

def begin_test(test)
end

#clean_backtrace(exception) ⇒ Array (protected)

Remove reference to lemon library from backtrace.

Parameters:

  • exception (Exception)

    The error that was rasied.

Returns:

  • (Array)

    filtered backtrace



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/test/reporters/abstract.rb', line 196

def clean_backtrace(exception)
  trace = (Exception === exception ? exception.backtrace : exception)
  return trace if $DEBUG
  trace = trace.reject{ |t| RUBY_IGNORE_CALLERS.any?{ |r| r =~ t }}
  trace = trace.map do |t|
    i = t.index(':in')
    i ? t[0...i] : t
  end
  #if trace.empty?
  #  exception
  #else
  #  exception.set_backtrace(trace) if Exception === exception
  #  exception
  #end
  trace.uniq
end

#code(source, line = nil) ⇒ CodeSnippet (protected)

That an exception, backtrace or source code text and line number and return a CodeSnippet object.

Returns:



217
218
219
220
221
222
223
224
# File 'lib/test/reporters/abstract.rb', line 217

def code(source, line=nil)
  case source
  when Exception, Array
    CodeSnippet.from_backtrace(clean_backtrace(source))
  else
    CodeSnippet.new(source, line)
  end
end

#end_case(test_case) ⇒ Object



92
93
# File 'lib/test/reporters/abstract.rb', line 92

def end_case(test_case)
end

#end_suite(test_suite) ⇒ Object



96
97
# File 'lib/test/reporters/abstract.rb', line 96

def end_suite(test_suite)
end

#end_test(test) ⇒ Object



88
89
# File 'lib/test/reporters/abstract.rb', line 88

def end_test(test)
end

#error(test, exception) ⇒ Object



76
77
# File 'lib/test/reporters/abstract.rb', line 76

def error(test, exception)
end

#fail(test, exception) ⇒ Object



72
73
# File 'lib/test/reporters/abstract.rb', line 72

def fail(test, exception)
end

#file(exception) ⇒ Object (protected)



255
256
257
# File 'lib/test/reporters/abstract.rb', line 255

def file(exception)
  file_and_line_array(exception).first
end

#file_and_line(exception) ⇒ Object (protected)



231
232
233
234
235
236
237
# File 'lib/test/reporters/abstract.rb', line 231

def file_and_line(exception)
  line = clean_backtrace(exception)[0]
  return "" unless line
  i = line.rindex(':in')
  line = i ? line[0...i] : line
  File.basename(line)
end

#file_and_line_array(exception) ⇒ Object (protected)



240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/test/reporters/abstract.rb', line 240

def file_and_line_array(exception)
  case exception
  when Exception
    line = exception.backtrace[0]
  else
    line = exception[0] # backtrace
  end
  return ["", 0] unless line
  i = line.rindex(':in')
  line = i ? line[0...i] : line
  f, l = File.basename(line).split(':')
  return [f, l.to_i]
end

#line(exception) ⇒ Object (protected)



260
261
262
# File 'lib/test/reporters/abstract.rb', line 260

def line(exception)
  file_and_line_array(exception).last
end

#omit(test, exception) ⇒ Object

Report an omitted test.



84
85
# File 'lib/test/reporters/abstract.rb', line 84

def omit(test, exception)
end

#pass(test) ⇒ Object



68
69
# File 'lib/test/reporters/abstract.rb', line 68

def pass(test)
end

#recordObject (protected)



101
102
103
# File 'lib/test/reporters/abstract.rb', line 101

def record
  runner.recorder
end

#skip_case(test_case) ⇒ Object



56
57
# File 'lib/test/reporters/abstract.rb', line 56

def skip_case(test_case)
end

#skip_test(test) ⇒ Object



60
61
# File 'lib/test/reporters/abstract.rb', line 60

def skip_test(test)
end

#subtotalObject (protected)



136
137
138
139
140
# File 'lib/test/reporters/abstract.rb', line 136

def subtotal
  [:todo, :pass, :fail, :error, :omit, :skip].inject(0) do |s,r|
    s += record[r.to_sym].size; s
  end
end

#tallyString (protected)

Common tally stamp any reporter can use.

Returns:



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/test/reporters/abstract.rb', line 158

def tally
  sizes  = {}
  names  = %w{error fail todo omit skip}.map{ |n| n.to_sym }
  names.each do |r|
    sizes[r] = record[r].size
  end

  #names.unshift(:tests)
  #sizes[:tests] = total

  s = []
  names.each do |n|
    next unless sizes[n] > 0
    s << tally_item(n, sizes)
  end

  'Executed ' + "#{total}".ansi(:bold) + ' tests with ' + s.join(', ')
end

#tally_item(name, sizes) ⇒ Object (protected)



178
179
180
181
182
183
184
# File 'lib/test/reporters/abstract.rb', line 178

def tally_item(name, sizes)
  x = []
  x << "%s" % sizes[name].to_s.ansi(:bold)
  x << " %s" % TITLES[name].downcase
  x << " (%.1f%%)" % ((sizes[name].to_f/total*100)) if runner.verbose?
  x.join('')
end

#timestampObject (protected)

Common timestamp any reporter can use.



124
125
126
127
128
# File 'lib/test/reporters/abstract.rb', line 124

def timestamp
  seconds = Time.now - @start_time

  "Finished in %.5fs, %.2f tests/s." % [seconds, total/seconds]
end

#todo(test, exception) ⇒ Object

Report a pending test.



80
81
# File 'lib/test/reporters/abstract.rb', line 80

def todo(test, exception)
end

#totalObject (protected)



131
132
133
# File 'lib/test/reporters/abstract.rb', line 131

def total
  @total ||= subtotal
end

#total_count(suite) ⇒ Object (protected)

Count up the total number of tests.



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/test/reporters/abstract.rb', line 111

def total_count(suite)
  c = 0
  suite.each do |tc|
    if tc.respond_to?(:each)
      c += total_count(tc)
    else
      c += 1
    end
  end
  return c
end