class MiniTest::Unit

def self.autorun

def self.autorun
  at_exit {
    next if $! # don't run if there was an exception
    exit_code = MiniTest::Unit.new.run(ARGV)
    exit false if exit_code && exit_code != 0
  } unless @@installed_at_exit
  @@installed_at_exit = true
end

def self.output= stream

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

def initialize # :nodoc:

:nodoc:
def initialize # :nodoc:
  @report = []
  @errors = @failures = @skips = 0
  @verbose = false
end

def location e # :nodoc:

:nodoc:
def location e # :nodoc:
  last_before_assertion = ""
  e.backtrace.reverse_each do |s|
    break if s =~ /in .(assert|refute|flunk|pass|fail|raise|must|wont)/
    last_before_assertion = s
  end
  last_before_assertion.sub(/:in .*$/, '')
end

def process_args args = []

def process_args args = []
  options = {}
  OptionParser.new do |opts|
    opts.banner  = 'minitest options:'
    opts.version = MiniTest::Unit::VERSION
    opts.on '-h', '--help', 'Display this help.' do
      puts opts
      exit
    end
    opts.on '-s', '--seed SEED', Integer, "Sets random seed" do |m|
      options[:seed] = m.to_i
    end
    opts.on '-v', '--verbose', "Verbose. Show progress processing files." do
      options[:verbose] = true
    end
    opts.on '-n', '--name PATTERN', "Filter test names on pattern." do |a|
      options[:filter] = a
    end
    opts.parse args
  end
  options
end

def puke klass, meth, e

def puke klass, meth, e
  e = case e
      when MiniTest::Skip then
        @skips += 1
        "Skipped:\n#{meth}(#{klass}) [#{location e}]:\n#{e.message}\n"
      when MiniTest::Assertion then
        @failures += 1
        "Failure:\n#{meth}(#{klass}) [#{location e}]:\n#{e.message}\n"
      else
        @errors += 1
        bt = MiniTest::filter_backtrace(e.backtrace).join("\n    ")
        "Error:\n#{meth}(#{klass}):\n#{e.class}: #{e.message}\n    #{bt}\n"
      end
  @report << e
  e[0, 1]
end

def run args = []

def run args = []
  options = process_args args
  @verbose = options[:verbose]
  filter = options[:filter] || '/./'
  filter = Regexp.new $1 if filter and filter =~ /\/(.*)\//
  seed = options[:seed]
  unless seed then
    srand
    seed = srand % 0xFFFF
  end
  srand seed
  help = ["--seed", seed]
  help.push "--verbose" if @verbose
  help.push("--name", options[:filter].inspect) if options[:filter]
  @@out.puts "Test run options: #{help.join(" ")}"
  @@out.puts
  @@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
  status
  @@out.puts
  @@out.puts "Test run options: #{help.join(" ")}"
  return failures + errors if @test_count > 0 # or return nil...
rescue Interrupt
  abort 'Interrupted'
end

def run_test_suites filter = /./

def run_test_suites filter = /./
  @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.print "#{suite}##{test}: " if @verbose
      @start_time = Time.now
      result = inst.run(self)
      @@out.print "%.2f s: " % (Time.now - @start_time) if @verbose
      @@out.print result
      @@out.puts if @verbose
      @test_count += 1
      @assertion_count += inst._assertions
    end
  end
  @@out.sync = old_sync if @@out.respond_to? :sync=
  [@test_count, @assertion_count]
end

def status io = @@out

def status io = @@out
  format = "%d tests, %d assertions, %d failures, %d errors, %d skips"
  io.puts format % [test_count, assertion_count, failures, errors, skips]
end