Class: Mini::Test

Inherits:
Object show all
Defined in:
lib/mini/test.rb

Defined Under Namespace

Classes: TestCase

Constant Summary collapse

VERSION =
"1.2.0"
@@out =
$stdout

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTest

Returns a new instance of Test.



324
325
326
327
# File 'lib/mini/test.rb', line 324

def initialize
  @report = []
  @errors = @failures = 0
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



289
290
291
# File 'lib/mini/test.rb', line 289

def errors
  @errors
end

#failuresObject (readonly)

Returns the value of attribute failures.



289
290
291
# File 'lib/mini/test.rb', line 289

def failures
  @failures
end

#reportObject (readonly)

Returns the value of attribute report.



289
290
291
# File 'lib/mini/test.rb', line 289

def report
  @report
end

Class Method Details

.autorunObject



294
295
296
297
298
299
300
# File 'lib/mini/test.rb', line 294

def self.autorun
  at_exit {
    exit_code = Mini::Test.new.run(ARGV)
    exit exit_code if exit_code
  } unless @@installed_at_exit
  @@installed_at_exit = true
end

.output=(stream) ⇒ Object



302
303
304
# File 'lib/mini/test.rb', line 302

def self.output= stream
  @@out = stream
end

Instance Method Details

#puke(klass, meth, e) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/mini/test.rb', line 306

def puke klass, meth, e
  if Mini::Assertion === e then
    @failures += 1

    loc = e.backtrace.find { |s| s !~ /in .(assert|flunk|pass|fail|raise)/ }
    loc.sub!(/:in .*$/, '')

    @report << "Failure:\n#{meth}(#{klass}) [#{loc}]:\n#{e.message}\n"
    'F'
  else
    @errors += 1
    bt = Mini::filter_backtrace(e.backtrace).join("\n    ")
    e = "Error:\n#{meth}(#{klass}):\n#{e.class}: #{e.message}\n    #{bt}\n"
    @report << e
    'E'
  end
end

#run(args) ⇒ Object

Top level driver, controls all output and filtering.



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
# File 'lib/mini/test.rb', line 332

def run args
  filter = if args.first =~ /^(-n|--name)$/ then
             args.shift
             arg = args.shift
             arg =~ /\/(.*)\// ? Regexp.new($1) : arg
           else
             /./ # anything - ^test_ already filtered by #tests
           end

  @@out.puts "Loaded suite #{$0.sub(/\.rb$/, '')}\nStarted"

  start = Time.now
  run_test_suites filter

  @@out.puts
  @@out.puts "Finished in #{'%.6f' % (Time.now - start)} seconds."

  @report.each_with_index do |msg, i|
    @@out.puts "\n%3d) %s" % [i + 1, msg]
  end

  @@out.puts

  format = "%d tests, %d assertions, %d failures, %d errors"
  @@out.puts format % [@test_count, @assertion_count, failures, errors]

  return failures + errors if @test_count > 0 # or return nil...
end

#run_test_suites(filter = /^test/) ⇒ Object



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
# File 'lib/mini/test.rb', line 361

def run_test_suites filter = /^test/
  @test_count, @assertion_count = 0, 0
  old_sync, @@out.sync = @@out.sync, true if @@out.respond_to? :sync=
  TestCase.test_suites.each do |suite|
    suite.test_methods.grep(filter).each do |test|
      inst = suite.new test
      inst._assertions = 0
      @@out.puts "\n#{test}: " if $DEBUG
      result = '.'
      begin
        inst.setup
        inst.__send__ test
      rescue Exception => e
        result = puke(suite, test, e)
      ensure
        begin
          inst.teardown
        rescue Exception => e
          result = puke(suite, test, e)
        end
      end
      @@out.print result
      @@out.puts if $DEBUG
      @test_count += 1
      @assertion_count += inst._assertions
    end
  end
  @@out.sync = old_sync if @@out.respond_to? :sync=
  [@test_count, @assertion_count]
end