• Download Gist
Prettier-turn-like MiniTests.rb
Ruby
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
require 'minitest/unit'
require 'minitest/spec'
require 'ansi'
 
class MiniTest::Unit
include ANSI::Code
 
PADDING_SIZE = 4
 
def run(args = [])
@verbose = true
 
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."
 
@@out.puts
 
@@out.print "%d tests, " % test_count
@@out.print "%d assertions, " % assertion_count
@@out.print red { "%d failures, " % failures }
@@out.print yellow { "%d errors, " % errors }
@@out.puts cyan { "%d skips" % skips}
 
return failures + errors if @test_count > 0 # or return nil...
end
 
# Overwrite #run_test_suites so that it prints out reports
# as errors are generated.
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|
test_cases = suite.test_methods.grep(filter)
if test_cases.size > 0
@@out.print "\n#{suite}:\n"
end
 
test_cases.each do |test|
inst = suite.new test
inst._assertions = 0
 
t = Time.now
 
@broken = nil
 
@@out.print(case inst.run(self)
when :pass
@broken = false
green { pad_with_size "PASS" }
when :error
@broken = true
yellow { pad_with_size "ERROR" }
when :fail
@broken = true
red { pad_with_size "FAIL" }
when :skip
@broken = false
cyan { pad_with_size "SKIP" }
end)
 
 
@@out.print " #{test}"
@@out.print " (%.2fs) " % (Time.now - t)
 
if @broken
@@out.puts
 
report = @report.last
@@out.puts pad(report[:message], 10)
trace = MiniTest::filter_backtrace(report[:exception].backtrace).first
@@out.print pad(trace, 10)
 
@@out.puts
end
 
@@out.puts
@test_count += 1
@assertion_count += inst._assertions
end
end
@@out.sync = old_sync if @@out.respond_to? :sync=
[@test_count, @assertion_count]
end
 
def pad(str, size=PADDING_SIZE)
" " * size + str
end
 
def pad_with_size(str)
pad("%5s" % str)
end
 
# Overwrite #puke method so that is stores a hash
# with :message and :exception keys.
def puke(klass, meth, e)
result = nil
msg = case e
when MiniTest::Skip
@skips += 1
result = :skip
e.message
when MiniTest::Assertion
@failures += 1
result = :fail
e.message
else
@errors += 1
result = :error
"#{e.class}: #{e.message}\n"
end
 
@report << {:message => msg, :exception => e}
result
end
 
 
class TestCase
# Overwrite #run method so that is uses symbols
# as return values rather than characters.
def run(runner)
result = :pass
begin
@passed = nil
self.setup
self.__send__ self.name
@passed = true
rescue Exception => e
@passed = false
result = runner.puke(self.class, self.name, e)
ensure
begin
self.teardown
rescue Exception => e
result = runner.puke(self.class, self.name, e)
end
end
result
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.