public
Rubygem
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/thoughtbot/shoulda.git
tsaleh (author)
Fri Apr 06 08:09:35 -0700 2007
commit  f77e17b6e4c4d899ed19d23f0502dda2aef4916f
tree    31e97c1802cfc2c2a28ea0ff61695fe5af40e372
parent  7910643c6f48fa7d502a2a2fb50bc331f565d4a4
shoulda / lib / color.rb
100644 78 lines (71 sloc) 2.397 kb
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
require 'test/unit/ui/console/testrunner'
 
# Completely stolen from redgreen gem
#
# Adds colored output to your tests. Specify <tt>color: true</tt> in
# your <tt>test/shoulda.conf</tt> file to enable.
#
# *Bug*: for some reason, this adds another line of output to the end of
# every rake task, as though there was another (empty) set of tests.
# A fix would be most welcome.
#
module ThoughtBot::Shoulda::Color
  COLORS = { :clear => 0, :red => 31, :green => 32, :yellow => 33 } # :nodoc:
  def self.method_missing(color_name, *args) # :nodoc:
    color(color_name) + args.first + color(:clear)
  end
  def self.color(color) # :nodoc:
    "\e[#{COLORS[color.to_sym]}m"
  end
end
 
module Test # :nodoc:
  module Unit # :nodoc:
    class TestResult # :nodoc:
      alias :old_to_s :to_s
      def to_s
        if old_to_s =~ /\d+ tests, \d+ assertions, (\d+) failures, (\d+) errors/
          ThoughtBot::Shoulda::Color.send($1.to_i != 0 || $2.to_i != 0 ? :red : :green, $&)
        end
      end
    end
 
    class AutoRunner # :nodoc:
      alias :old_initialize :initialize
      def initialize(standalone)
        old_initialize(standalone)
        @runner = proc do |r|
          Test::Unit::UI::Console::RedGreenTestRunner
        end
      end
    end
 
    class Failure # :nodoc:
      alias :old_long_display :long_display
      def long_display
        # old_long_display.sub('Failure', ThoughtBot::Shoulda::Color.red('Failure'))
        ThoughtBot::Shoulda::Color.red(old_long_display)
      end
    end
 
    class Error # :nodoc:
      alias :old_long_display :long_display
      def long_display
        # old_long_display.sub('Error', ThoughtBot::Shoulda::Color.yellow('Error'))
        ThoughtBot::Shoulda::Color.yellow(old_long_display)
      end
    end
 
    module UI # :nodoc:
      module Console # :nodoc:
        class RedGreenTestRunner < Test::Unit::UI::Console::TestRunner # :nodoc:
          def output_single(something, level=NORMAL)
            return unless (output?(level))
            something = case something
            when '.' then ThoughtBot::Shoulda::Color.green('.')
            when 'F' then ThoughtBot::Shoulda::Color.red("F")
            when 'E' then ThoughtBot::Shoulda::Color.yellow("E")
            else something
            end
            @io.write(something)
            @io.flush
          end
        end
      end
    end
  end
end