Skip to content
This repository
tag: 1.5.0.RC3
Fetching contributors…

Cannot retrieve contributors at this time

file 112 lines (96 sloc) 2.261 kb
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

$silentTests = false
$testnum=0
$ntest=0
$failed = []
$curtestOK=true
$saved_stdout = $stdout

module MiniRUnit
  class Failure
    def initialize(what, testnum, msg, where)
      @what, @testnum, @msg, @where = what, testnum, msg, where
    end

    def to_s
      sprintf("FAILED %s %d %s-- %s\n", @what, @testnum, @msg, @where)
    end
  end

  class Error
    def initialize(what, testnum, boom)
      @what, @testnum, @boom = what, testnum, boom
    end

    def to_s
      sprintf("EXCEPTION raised %s %d -- \n\tException: %s\n\t%s",
              @what, @testnum, @boom.to_s, @boom.backtrace.join("\n\t"))
    end
  end
end


def test_check(what)
  $saved_stdout.printf "%s : ", what unless $silentTests
  $what = what
  $testnum = 0
end

def test_ok(cond, msg="")
  $testnum+=1
  $ntest+=1
  if cond
    $saved_stdout.print "." unless $silentTests
  else
    where = caller.reject {|where| where =~ /minirunit/}[0]
    $failed.push(MiniRUnit::Failure.new($what, $testnum, msg, where))
    $saved_stdout.print "F" unless $silentTests
    $curtestOK=false
  end
end

def test_fail(msg="")
  test_ok(false, msg)
end

def test_equal(a,b)
 test_ok(a == b, "expected #{a.inspect}, found #{b.inspect}")
end

def test_no_exception(&proc)
  raised = false
  begin
    proc.call
  rescue Exception => x
    raised = x
  end
  test_ok(!raised, "unexpected exception #{raised}")
end

def test_exception(type=Exception, &proc)
  raised = false
  begin
    proc.call
  rescue type=>e
    raised = true
  end
  test_ok(raised, "#{type} expected")
  e
end

def test_get_last_failed
  if $failed.empty?
    return nil
  end
  return $failed.last
end

def test_print_report
  $saved_stdout.puts
  $saved_stdout.puts "-" * 80
  $failed.each { |error| $saved_stdout.puts error }
  $saved_stdout.puts "-" * 80
  $saved_stdout.puts "Tests: #$ntest. (Ok: #{$ntest - $failed.size}; Failed: #{$failed.size})"
end

def test_load(test)
  begin
$curtestOK=true
load(test)
  rescue Exception => boom
$saved_stdout.puts 'ERROR' unless $silentTests
$failed.push(MiniRUnit::Error.new($what, $testnum, boom))
  else
if $curtestOK
$saved_stdout.puts 'OK' unless $silentTests
else
$saved_stdout.puts 'FAILED' unless $silentTests
end
  end
end

at_exit { test_print_report }
Something went wrong with that request. Please try again.