Added test colorization

git-svn-id: https://svn.thoughtbot.com/plugins/tb_test_helpers/trunk@48 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
This commit is contained in:
tsaleh 2007-03-15 18:58:15 +00:00
parent 99cb931dd7
commit cfb496b13f
4 changed files with 94 additions and 0 deletions

61
lib/color.rb Normal file
View File

@ -0,0 +1,61 @@
require 'test/unit/ui/console/testrunner'
# Completely stolen from redgreen gem
module Color
COLORS = { :clear => 0, :red => 31, :green => 32, :yellow => 33 }
def self.method_missing(color_name, *args)
color(color_name) + args.first + color(:clear)
end
def self.color(color)
"\e[#{COLORS[color.to_sym]}m"
end
end
class Test::Unit::UI::Console::RedGreenTestRunner < Test::Unit::UI::Console::TestRunner
def output_single(something, level=NORMAL)
return unless (output?(level))
something = case something
when '.' then Color.green('.')
when 'F' then Color.red("F")
when 'E' then Color.yellow("E")
else something
end
@io.write(something)
@io.flush
end
end
class Test::Unit::AutoRunner
alias :old_initialize :initialize
def initialize(standalone)
old_initialize(standalone)
@runner = proc do |r|
Test::Unit::UI::Console::RedGreenTestRunner
end
end
end
class Test::Unit::TestResult
alias :old_to_s :to_s
def to_s
if old_to_s =~ /\d+ tests, \d+ assertions, (\d+) failures, (\d+) errors/
Color.send($1.to_i != 0 || $2.to_i != 0 ? :red : :green, $&)
end
end
end
class Test::Unit::Failure
alias :old_long_display :long_display
def long_display
# old_long_display.sub('Failure', Color.red('Failure'))
Color.red(old_long_display)
end
end
class Test::Unit::Error
alias :old_long_display :long_display
def long_display
# old_long_display.sub('Error', Color.yellow('Error'))
Color.yellow(old_long_display)
end
end

View File

@ -1,5 +1,12 @@
require 'active_record_helpers'
require 'should'
require 'yaml'
config_file = "tb_test_helpers.conf"
config_file = defined?(RAILS_ROOT) ? File.join(RAILS_ROOT, "test", "tb_test_helpers.conf") : File.join("test", "tb_test_helpers.conf")
tb_test_options = (YAML.load_file(config_file) rescue {}).symbolize_keys
require 'color' if tb_test_options[:color]
module Test # :nodoc:
module Unit # :nodoc:

View File

@ -12,6 +12,7 @@ class ContextTest < Test::Unit::TestCase # :nodoc:
end
should "have name set right" do
raise RuntimeError, "Whops!"
assert_match(/^test context with setup block/, self.to_s)
end

25
test/helpers_test.rb Normal file
View File

@ -0,0 +1,25 @@
require File.join(File.dirname(__FILE__), 'test_helper')
class Val
@@val = 0
def self.val; @@val; end
def self.inc(i=1); @@val += i; end
end
class ContextTest < Test::Unit::TestCase # :nodoc:
context "assert_difference" do
should "pass when incrementing by one" do
assert_difference(Val, :val, 1) do
Val.inc
end
end
should "pass when incrementing by two" do
assert_difference(Val, :val, 2) do
Val.inc(2)
end
end
end
end