Skip to content

Commit

Permalink
If not run any test... no color!
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosbrando committed Feb 19, 2011
1 parent 8f260a7 commit ee73dec
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
41 changes: 17 additions & 24 deletions lib/colorific.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,10 @@
require "minitest/autorun"
require 'progressbar'

module Colorific
COLORS = { :clear => 0, :red => 31, :green => 32, :yellow => 33 }
class MiniTest::Unit
ANSI_COLOR_CODES = { :clear => "\e[0m", :red => "\e[31m", :green => "\e[32m", :yellow => "\e[33m" }
TEST_COLORS = { "F" => :red, "E" => :red, "S" => :yellow, "." => :green }

def self.[](color_name)
"\e[#{COLORS[color_name.to_sym]}m"
end

def self.colored(status, msg)
color_name = TEST_COLORS[status[0,1]]
return msg unless color_name
Colorific[color_name] + msg + Colorific[:clear]
end
end

class MiniTest::Unit
alias :original_puke :puke
alias :original_run_suites :_run_suites
alias :original_status :status
Expand All @@ -27,7 +15,7 @@ def puke(klass, meth, e)

report = @report.pop
lines = report.split(/\n/)
lines[0] = Colorific.colored(r, lines[0])
lines[0] = tint(r, lines[0])
@report << lines.join("\n")
r
end
Expand All @@ -44,33 +32,38 @@ def status(io = self.output)
end

def print(*a)
case type = a.join
when '.'
increment
when 'S', 'F', 'E'
set_color(type)
if %w(. S F E).include?(a.join)
set_color(a.join)
increment
else
output.print(*a)
end
end

protected
def tint(status, msg)
color_name = TEST_COLORS[status[0,1]]
return msg unless color_name
ANSI_COLOR_CODES[color_name] + msg + ANSI_COLOR_CODES[:clear]
end

def set_color(type)
case type
when "F", "E"
@state = :red
when '.'
@state = :green unless @state == :yellow || @state == :red
when "S"
@state = :yellow unless @state == :red
when "F", "E"
@state = :red
end
end

def state
@state ||= :green
@state ||= :clear
end

def with_color
output.print Colorific[state]
output.print ANSI_COLOR_CODES[state]
yield
output.print "\e[0m"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/colorific/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Colorific
VERSION = "0.0.1"
VERSION = "0.0.2"
end
9 changes: 9 additions & 0 deletions test/test_nothing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require File.dirname(__FILE__) + '/../lib/colorific'

class ColorificPassTest < MiniTest::Unit::TestCase

def do_nothing

end

end

0 comments on commit ee73dec

Please sign in to comment.