Commit 93f859662e9f6f4036c20e8e6a9c3ac81a7f8090
1 parent
10bed1b8
Exists in
master
and in
22 other branches
Coloring test output
Taken from a comment by Edvard Majakari (http://majakari.net/) at http://errtheblog.com/posts/34-a-ruby-rainbow Changed the coloring to use the dark versions of the colores instead of the light ones
Showing
2 changed files
with
59 additions
and
0 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,57 @@ |
| 1 | +require 'test/unit' | |
| 2 | +require 'test/unit/ui/console/testrunner' | |
| 3 | + | |
| 4 | +module Color | |
| 5 | + COLORS = { :clear => 0, :red => 31, :green => 32, :yellow => 33 } | |
| 6 | + def self.method_missing(color_name, *args) | |
| 7 | + colname = color_name.to_s | |
| 8 | + ansi_color = (colname =~ /^light/ ? | |
| 9 | + light_color(colname.gsub(/light/, '')) : | |
| 10 | + color(colname)) | |
| 11 | + ansi_color + args.first + color(:clear) | |
| 12 | + end | |
| 13 | + def self.color(color) | |
| 14 | + "\e[#{COLORS[color.to_sym]}m" | |
| 15 | + end | |
| 16 | + | |
| 17 | + def self.light_color(color) | |
| 18 | + "\e[1;#{COLORS[color.to_sym]}m" | |
| 19 | + end | |
| 20 | +end | |
| 21 | + | |
| 22 | +class Test::Unit::UI::Console::TestRunner | |
| 23 | + def output_single(something, level=NORMAL) | |
| 24 | + return unless (output?(level)) | |
| 25 | + something = case something | |
| 26 | + when '.' then Color.green('.') | |
| 27 | + when 'F' then Color.red("F") | |
| 28 | + when 'E' then Color.yellow("E") | |
| 29 | + else something | |
| 30 | + end | |
| 31 | + @io.write(something) | |
| 32 | + @io.flush | |
| 33 | + end | |
| 34 | +end | |
| 35 | + | |
| 36 | +class Test::Unit::TestResult | |
| 37 | + alias :old_to_s :to_s | |
| 38 | + def to_s | |
| 39 | + if old_to_s =~ /\d+ tests, \d+ assertions, (\d+) failures, (\d+) errors/ | |
| 40 | + Color.send($1.to_i != 0 || $2.to_i != 0 ? :red : :green, $&) | |
| 41 | + end | |
| 42 | + end | |
| 43 | +end | |
| 44 | + | |
| 45 | +class Test::Unit::Failure | |
| 46 | + alias :old_long_display :long_display | |
| 47 | + def long_display | |
| 48 | + old_long_display.sub('Failure', Color.red('Failure')) | |
| 49 | + end | |
| 50 | +end | |
| 51 | + | |
| 52 | +class Test::Unit::Error | |
| 53 | + alias :old_long_display :long_display | |
| 54 | + def long_display | |
| 55 | + old_long_display.sub('Error', Color.yellow('Error')) | |
| 56 | + end | |
| 57 | +end | ... | ... |