Module: Chukan::Test

Defined in:
lib/chukan.rb

Defined Under Namespace

Modules: Color

Constant Summary collapse

SEPARATOR =
"\n"
@@start =
nil
@@cases =
Hash.new {|hash,key| hash[key] = [0,0,0] }
@@count =
0
@@data =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



440
441
442
443
444
445
# File 'lib/chukan.rb', line 440

def self.included(mod)
  unless @@start
    ObjectSpace.define_finalizer(mod, report)
    @@start = Time.now
  end
end

.reportObject



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/chukan.rb', line 423

def self.report
  proc {
    finish = Time.now
    puts "\n1..#{@@count}"
    $stderr.puts "Finished in #{finish - @@start} seconds."
    $stderr.puts ""
    succes  = @@cases.to_a.inject(0) {|r,(n,c)| r + c[0] }
    failure = @@cases.to_a.inject(0) {|r,(n,c)| r + c[1] }
    error   = @@cases.to_a.inject(0) {|r,(n,c)| r + c[2] }
    $stderr.puts "#{@@cases.size} tests, " +
      "#{succes+failure+error} assertions, " +
      "#{Color::FAIL}#{failure} failures, " +
      "#{Color::ERROR}#{error} errors" +
      "#{Color::NORMAL}"
  }
end

Instance Method Details

#dataObject



464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/chukan.rb', line 464

def data
  require 'yaml'
  @@data ||= YAML.load_stream(DATA.read.gsub(/(^\t+)/) {
    '  ' * $+.length
  }).documents.map {|obj|
    obj.each_pair {|k,v|
      (class<<obj; self; end).instance_eval do
        define_method(k) { v }
      end rescue nil
    } rescue obj
  }
end

#run(&block) ⇒ Object



477
478
479
480
481
# File 'lib/chukan.rb', line 477

def run(&block)
  data.each {|d|
    yield d
  }
end

#test(name = nil, directive = nil, &block) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/chukan.rb', line 447

def test(name = nil, directive = nil, &block)
  if yield
    tap(Color::SUCCESS, "ok", @@count+=1, name, directive)
    @@cases[name][0] += 1
  else
    tap(Color::FAIL, "not ok", @@count+=1, name, directive)
    print_backtrace(caller, "test failed")
    @@cases[name][1] += 1
  end
rescue Exception
  tap(Color::ERROR, "not ok", @@count+=1, name, directive)
  print_backtrace($!.backtrace, "#{$!} (#{$!.class})")
  @@cases[name][2] += 1
ensure
  print Color::NORMAL
end