1# encoding: utf-8
2######################################################################
3# This file is imported from the minitest project.
4# DO NOT make modifications in this repo. They _will_ be reverted!
5# File a patch instead and assign it to Ryan Davis.
6######################################################################
7
8require "minitest/unit"
9
10##
11# Show your testing pride!
12
13class PrideIO
14
15  # Start an escape sequence
16  ESC = "\e["
17
18  # End the escape sequence
19  NND = "#{ESC}0m"
20
21  # The IO we're going to pipe through.
22  attr_reader :io
23
24  def initialize io # :nodoc:
25    @io = io
26    # stolen from /System/Library/Perl/5.10.0/Term/ANSIColor.pm
27    # also reference http://en.wikipedia.org/wiki/ANSI_escape_code
28    @colors ||= (31..36).to_a
29    @size   = @colors.size
30    @index  = 0
31    # io.sync = true
32  end
33
34  ##
35  # Wrap print to colorize the output.
36
37  def print o
38    case o
39    when "." then
40      io.print pride o
41    when "E", "F" then
42      io.print "#{ESC}41m#{ESC}37m#{o}#{NND}"
43    else
44      io.print o
45    end
46  end
47
48  def puts(*o) # :nodoc:
49    o.map! { |s|
50      s.to_s.sub(/Finished tests/) {
51        @index = 0
52        'Fabulous tests'.split(//).map { |c|
53          pride(c)
54        }.join
55      }
56    }
57
58    super
59  end
60
61  ##
62  # Color a string.
63
64  def pride string
65    string = "*" if string == "."
66    c = @colors[@index % @size]
67    @index += 1
68    "#{ESC}#{c}m#{string}#{NND}"
69  end
70
71  def method_missing msg, *args # :nodoc:
72    io.send(msg, *args)
73  end
74end
75
76##
77# If you thought the PrideIO was colorful...
78#
79# (Inspired by lolcat, but with clean math)
80
81class PrideLOL < PrideIO
82  PI_3 = Math::PI / 3 # :nodoc:
83
84  def initialize io # :nodoc:
85    # walk red, green, and blue around a circle separated by equal thirds.
86    #
87    # To visualize, type this into wolfram-alpha:
88    #
89    #   plot (3*sin(x)+3), (3*sin(x+2*pi/3)+3), (3*sin(x+4*pi/3)+3)
90
91    # 6 has wide pretty gradients. 3 == lolcat, about half the width
92    @colors = (0...(6 * 7)).map { |n|
93      n *= 1.0 / 6
94      r  = (3 * Math.sin(n           ) + 3).to_i
95      g  = (3 * Math.sin(n + 2 * PI_3) + 3).to_i
96      b  = (3 * Math.sin(n + 4 * PI_3) + 3).to_i
97
98      # Then we take rgb and encode them in a single number using base 6.
99      # For some mysterious reason, we add 16... to clear the bottom 4 bits?
100      # Yes... they're ugly.
101
102      36 * r + 6 * g + b + 16
103    }
104
105    super
106  end
107
108  ##
109  # Make the string even more colorful. Damnit.
110
111  def pride string
112    c = @colors[@index % @size]
113    @index += 1
114    "#{ESC}38;5;#{c}m#{string}#{NND}"
115  end
116end
117
118klass = ENV['TERM'] =~ /^xterm|-256color$/ ? PrideLOL : PrideIO
119MiniTest::Unit.output = klass.new(MiniTest::Unit.output)
120