Created
September 19, 2011 13:39
-
-
Save jm/1226519 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: UTF-8 | |
module UniTest | |
class <<self | |
attr_accessor :test_cases | |
end | |
def self.run_tests | |
tc_instances = (test_cases || []).map {|tc| tc.new} | |
tc_instances.map do |test_case| | |
puts "Running tests in #{test_case.class.name}:" | |
test_case.test_methods.each do |test_method| | |
begin | |
test_case.setup | |
begin | |
test_case.send(test_method) | |
rescue Exception => e | |
test_case.error(e) | |
end | |
ensure | |
test_case.teardown | |
end | |
end | |
puts | |
end | |
total_passes, total_errors, total_failures = [0]*3 | |
errors = [] | |
failures = [] | |
tc_instances.each do |tc| | |
total_passes += tc.pass_count | |
total_errors += tc.error_count | |
total_failures += tc.failure_count | |
errors += tc.errors | |
failures += tc.failures | |
end | |
unless failures.empty? | |
puts | |
puts "★★★ FAILURES ★★★" | |
failures.each do |message, fail| | |
puts "☆ #{message}" | |
puts fail | |
puts | |
end | |
end | |
unless errors.empty? | |
puts | |
puts "⚑⚑⚑ ERRORS ⚑⚑⚑" | |
errors.each do |error| | |
puts "⚐ #{error.message}" | |
puts error.backtrace | |
puts | |
end | |
end | |
puts "#{total_passes} pass, #{total_failures} fail, #{total_errors} error" | |
end | |
class TestCase | |
attr_accessor :passes, :failures, :errors, :pass_count, :failure_count, :error_count | |
def initialize | |
@pass_count = 0 | |
@failure_count = 0 | |
@error_count = 0 | |
@failures = [] | |
@errors = [] | |
end | |
def self.inherited(klass) | |
UniTest.test_cases ||= [] | |
UniTest.test_cases << klass | |
end | |
def test_methods | |
(self.methods - Object.methods).select {|method_name| method_name =~ /^test_/ } | |
end | |
def pass | |
print "." | |
@pass_count += 1 | |
end | |
def fail(message, the_caller) | |
print "F" | |
@failure_count += 1 | |
@failures << [message, the_caller] | |
end | |
def error(e) | |
print "E" | |
@error_count += 1 | |
@errors << e | |
end | |
def ☺(value, message = "No message") | |
(value || (block_given? && yield)) ? pass : fail(message, caller) | |
end | |
alias_method :assert, :☺ | |
alias_method :☑, :☺ | |
def ☹(value, message = "No message") | |
!(value || (block_given? && yield)) ? pass : fail(message, caller) | |
end | |
alias_method :assert_not, :☹ | |
alias_method :☒, :☹ | |
# Probably make more sense to actually call "debugger" here rather than fail | |
# but oh well. | |
def ⌦(message = "Breakpoint / fail") | |
☺ false, message | |
end | |
def setup; end | |
def teardown; end | |
end | |
end | |
class TestThing < UniTest::TestCase | |
def test_win | |
☺ true | |
end | |
def test_fail | |
☹ true | |
end | |
def test_break | |
⌦ "GO WAI" | |
end | |
end | |
unless ENV['NOAUTORUN'] | |
at_exit do | |
UniTest.run_tests | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment