forked from ku1ik/rainbow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rainbow_test.rb
265 lines (202 loc) · 6.02 KB
/
rainbow_test.rb
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
require 'test/unit'
require File.expand_path(File.dirname(__FILE__) + '/../lib/rainbow')
class RainbowTest < Test::Unit::TestCase #:nodoc:
def test_color_by_name
assert_equal "\e[31mhello\e[0m", "hello".color(:red)
end
def test_color_by_rgb
assert_equal "\e[38;5;196mhello\e[0m", "hello".color(255, 0, 0)
end
def test_foreground_alias
assert_equal "hello".color(:red), "hello".foreground(:red)
end
def test_colour_alias
assert_equal "hello".color(:red), "hello".colour(:red)
end
def test_background_by_name
assert_equal "\e[42mhello\e[0m", "hello".background(:green)
end
def test_background_by_rgb
assert_equal "\e[48;5;46mhello\e[0m", "hello".background(0, 255, 0)
end
def test_color_and_background
assert_equal "\e[31m\e[42mhello\e[0m", "hello".color(:red).background(:green)
end
def test_hex_color
assert_equal "\e[48;5;46mhello\e[0m", "hello".background("#00FF00")
assert_equal "\e[48;5;46mhello\e[0m", "hello".background("00FF00")
assert_equal "\e[48;5;46mhello\e[0m", "hello".background("00ff00")
end
def test_bad_color_name
assert_raises ArgumentError do
"hello".background(:baaaad)
end
end
def test_rgb_color_with_2_args
assert_raises ArgumentError do
"hello".background(1, 2)
end
end
def test_rgb_color_with_4_args
assert_raises ArgumentError do
"hello".background(1, 2, 3, 4)
end
end
def test_rgb_color_with_values_below_zero
assert_raises ArgumentError do
"hello".background(-3, 2, 3)
end
end
def test_rgb_color_with_values_above_255
assert_raises ArgumentError do
"hello".background(256, 2, 3)
end
end
def test_bright
assert_equal "\e[1mhello\e[0m", "hello".bright
end
def test_bright_and_color
assert_equal "\e[1m\e[31mhello\e[0m", "hello".bright.color(:red)
end
def test_bright_and_background
assert_equal "\e[1m\e[44mhello\e[0m", "hello".bright.background(:blue)
end
def test_color_override
assert_equal "\e[31m\e[34m\e[33mhello\e[0m", "hello".color(:red).color(:blue).color(:yellow)
end
def test_reset
assert_equal "\e[0mhello\e[0m", "hello".reset
end
def test_italic
assert_equal "\e[3mhello\e[0m", "hello".italic
end
def test_underline
assert_equal "\e[4mhello\e[0m", "hello".underline
end
def test_blink
assert_equal "\e[5mhello\e[0m", "hello".blink
end
def test_inverse
assert_equal "\e[7mhello\e[0m", "hello".inverse
end
def test_hide
assert_equal "\e[8mhello\e[0m", "hello".hide
end
class MyString < String
end
def test_inheritance
my_string = MyString.new "hello"
assert_equal "\e[31mhello\e[0m", my_string.color(:red)
end
def test_disabled
Sickill::Rainbow.enabled = false
assert_equal "hello", "hello".color(:red)
Sickill::Rainbow.enabled = true
end
end
class AnsiColorTest < Test::Unit::TestCase
include Sickill::Rainbow
### Foreground
def test_bad_foreground_name
assert_raises ArgumentError do
AnsiColor.new(:foreground, :azerty).code
end
end
def test_by_name_black_foreground
assert_equal 30, AnsiColor.new(:foreground, :black).code
end
def test_by_name_red_foreground
assert_equal 31, AnsiColor.new(:foreground, :red).code
end
def test_by_name_green_foreground
assert_equal 32, AnsiColor.new(:foreground, :green).code
end
def test_by_name_yellow_foreground
assert_equal 33, AnsiColor.new(:foreground, :yellow).code
end
def test_by_name_blue_foreground
assert_equal 34, AnsiColor.new(:foreground, :blue).code
end
def test_by_name_magenta_foreground
assert_equal 35, AnsiColor.new(:foreground, :magenta).code
end
def test_by_name_cyan_foreground
assert_equal 36, AnsiColor.new(:foreground, :cyan).code
end
def test_by_name_white_foreground
assert_equal 37, AnsiColor.new(:foreground, :white).code
end
### Background
def test_bad_background_name
assert_raises ArgumentError do
AnsiColor.new(:background, :azerty).code
end
end
def test_by_name_black_background
assert_equal 40, AnsiColor.new(:background, :black).code
end
def test_by_name_red_background
assert_equal 41, AnsiColor.new(:background, :red).code
end
def test_by_name_green_background
assert_equal 42, AnsiColor.new(:background, :green).code
end
def test_by_name_yellow_background
assert_equal 43, AnsiColor.new(:background, :yellow).code
end
def test_by_name_blue_background
assert_equal 44, AnsiColor.new(:background, :blue).code
end
def test_by_name_magenta_background
assert_equal 45, AnsiColor.new(:background, :magenta).code
end
def test_by_name_cyan_background
assert_equal 46, AnsiColor.new(:background, :cyan).code
end
def test_by_name_white_background
assert_equal 47, AnsiColor.new(:background, :white).code
end
### Hex color
def test_by_hex_maj
assert_equal "38;5;46", AnsiColor.new(:foreground, "00FF00").code
end
def test_by_hex_min
assert_equal "38;5;46", AnsiColor.new(:foreground, "00ff00").code
end
def test_by_hex_maj_with_sharp
assert_equal "38;5;46", AnsiColor.new(:foreground, "#00FF00").code
end
def test_by_hex_min_with_sharp
assert_equal "38;5;46", AnsiColor.new(:foreground, "#00ff00").code
end
### RGB color
def test_too_few_colors
assert_raises ArgumentError do
AnsiColor.new(:foreground, 255, 0).code
end
end
def test_too_much_colors
assert_raises ArgumentError do
AnsiColor.new(:foreground, 255, 0, 0, 0).code
end
end
def test_by_rgb_red_foreground
assert_equal "38;5;196", AnsiColor.new(:foreground, 255, 0, 0).code
end
end
class AnsiRgbTest < Test::Unit::TestCase
include Sickill::Rainbow
def test_red
assert_equal "38;5;196", AnsiRgb.new(:foreground, [255, 0, 0]).code
end
def test_rgb_color_with_values_below_zero
assert_raises ArgumentError do
AnsiRgb.new(:foreground, [-1, 0, 0])
end
end
def test_rgb_color_with_values_above_255
assert_raises ArgumentError do
AnsiRgb.new(:foreground, [256, 0, 0])
end
end
end