Created
March 12, 2009 18:56
-
-
Save joshuaclayton/78234 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
| require 'test/unit' | |
| require 'test/unit/ui/console/testrunner' | |
| # cute. | |
| 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 | |
| SILENT = Test::Unit::UI::SILENT | |
| PROGRESS_ONLY = Test::Unit::UI::PROGRESS_ONLY | |
| NORMAL = Test::Unit::UI::NORMAL | |
| VERBOSE = Test::Unit::UI::VERBOSE | |
| def initialize(suite, output_level=NORMAL, io=$stdout) | |
| super | |
| end | |
| def test_started(name) | |
| @cached_test_name = "#{name}: " | |
| end | |
| def add_fault(fault) | |
| @faults << fault | |
| color = :red | |
| case fault.single_character_display | |
| when "E" then output_single(colorize(@cached_test_name, color = :yellow), VERBOSE) | |
| when "F" then output_single(colorize(@cached_test_name, color = :red), VERBOSE) | |
| end | |
| output_single(colorize(fault.single_character_display, color), PROGRESS_ONLY) | |
| @already_outputted = true | |
| @cached_test_name = nil | |
| end | |
| def test_finished(name) | |
| unless @already_outputted | |
| output_single(colorize(@cached_test_name), VERBOSE) | |
| output_single(colorize("."), PROGRESS_ONLY) | |
| end | |
| nl(VERBOSE) | |
| @already_outputted = false | |
| @cached_test_name = nil | |
| end | |
| def colorize(text, color = :green) | |
| Color.send(color, text) | |
| 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')) | |
| end | |
| end | |
| class Test::Unit::Error | |
| alias :old_long_display :long_display | |
| def long_display | |
| old_long_display.sub('Error', Color.yellow('Error')) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment