highline 1.6.2 → 1.6.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # tc_string_extension.rb
4
+ #
5
+ # Created by Richard LeBer 2011-06-27
6
+ #
7
+ # This is Free Software. See LICENSE and COPYING for details.
8
+
9
+ require "test/unit"
10
+
11
+ require "highline"
12
+ require "stringio"
13
+ require "string_methods"
14
+
15
+ class TestStringExtension < Test::Unit::TestCase
16
+ def setup
17
+ HighLine.colorize_strings
18
+ @string = "string"
19
+ end
20
+
21
+ include StringMethods
22
+ end
@@ -0,0 +1,40 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # tc_highline_string.rb
4
+ #
5
+ # Created by Richard LeBer 2011-06-27
6
+ #
7
+ # This is Free Software. See LICENSE and COPYING for details.
8
+
9
+ require "test/unit"
10
+
11
+ require "highline"
12
+ require "stringio"
13
+ require "string_methods"
14
+
15
+ class TestHighLineString < Test::Unit::TestCase
16
+ def setup
17
+ @string = HighLine::String.new("string")
18
+ end
19
+
20
+ def test_string_class
21
+ # Basic constructor
22
+ assert_equal HighLine::String, @string.class
23
+ assert_equal "string", @string
24
+
25
+ # Alternative constructor method
26
+ new_string = HighLine::String("string")
27
+ assert_equal HighLine::String, new_string.class
28
+ assert_equal @string, new_string
29
+
30
+ # String methods work
31
+ assert_equal 6, @string.size
32
+ assert_equal "STRING", @string.upcase
33
+ end
34
+
35
+ include StringMethods
36
+
37
+ def test_string_class_is_unchanged
38
+ assert_raise(::NoMethodError) { "string".color(:blue) }
39
+ end
40
+ end
@@ -0,0 +1,569 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # tc_style.rb
4
+ #
5
+ # Created by Richard LeBer on 2011-06-11.
6
+ #
7
+ # This is Free Software. See LICENSE and COPYING for details.
8
+
9
+ require "test/unit"
10
+
11
+ require "highline"
12
+ require "stringio"
13
+
14
+ class TestStyle < Test::Unit::TestCase
15
+
16
+ def setup
17
+ @input = StringIO.new
18
+ @output = StringIO.new
19
+ @terminal = HighLine.new(@input, @output)
20
+ @style1 = HighLine::Style.new(:name=>:foo, :code=>"\e[99m", :rgb=>[1,2,3])
21
+ @style2 = HighLine::Style.new(:name=>:lando, :code=>"\e[98m")
22
+ @style3 = HighLine::Style.new(:name=>[:foo, :lando], :list=>[:foo, :lando])
23
+ @style4 = HighLine::Style(:rgb_654321)
24
+ end
25
+
26
+ def teardown
27
+ # HighLine::Style.clear_index
28
+ end
29
+
30
+ def test_style_method
31
+ # Retrieve a style from an existing Style (no new Style created)
32
+ new_style = @style1.dup # This will replace @style1 in the indexes
33
+ s = HighLine.Style(@style1)
34
+ assert_instance_of HighLine::Style, s
35
+ assert_same new_style, s # i.e. s===the latest style created, but not the one searched for
36
+
37
+ # Retrieve a style from a new Style (no new Style created)
38
+ s2 = HighLine::Style.new(:name=>:bar, :code=>"\e[97m")
39
+ s = HighLine.Style(s2)
40
+ assert_instance_of HighLine::Style, s
41
+ assert_same s2, s
42
+
43
+ # Create a builtin style from an existing ANSI escape string
44
+ s = HighLine.Style("\e[1m")
45
+ assert_instance_of HighLine::Style, s
46
+ assert_nil s.list
47
+ assert_equal "\e[1m", s.code
48
+ assert_equal :bold, s.name
49
+
50
+ # Create a builtin style from a new ANSI escape string
51
+ s = HighLine.Style("\e[96m")
52
+ assert_instance_of HighLine::Style, s
53
+ assert_nil s.list
54
+ assert_equal "\e[96m", s.code
55
+
56
+ # Create a builtin style from a symbol
57
+ s = HighLine.Style(:red)
58
+ assert_instance_of HighLine::Style, s
59
+ assert_nil s.list
60
+ assert_equal :red, s.name
61
+
62
+ # Retrieve an existing style by name (no new Style created)
63
+ s = HighLine.Style(@style2.name)
64
+ assert_instance_of HighLine::Style, s
65
+ assert_same @style2, s
66
+
67
+ # See below for color scheme tests
68
+
69
+ # Create style from a Hash
70
+ s = HighLine.Style(:name=>:han, :code=>"blah", :rgb=>'phooey')
71
+ assert_instance_of HighLine::Style, s
72
+ assert_equal :han, s.name
73
+ assert_equal "blah", s.code
74
+ assert_equal "phooey", s.rgb
75
+
76
+ # Create style from an RGB foreground color code
77
+ s = HighLine.Style(:rgb_1f2e3d)
78
+ assert_instance_of HighLine::Style, s
79
+ assert_equal :rgb_1f2e3d, s.name
80
+ assert_equal "\e[38;5;23m", s.code # Trust me; more testing below
81
+ assert_equal [31,46,61], s.rgb # 0x1f==31, 0x2e==46, 0x3d=61
82
+
83
+ # Create style from an RGB background color code
84
+ s = HighLine.Style(:on_rgb_1f2e3d)
85
+ assert_instance_of HighLine::Style, s
86
+ assert_equal :on_rgb_1f2e3d, s.name
87
+ assert_equal "\e[48;5;23m", s.code # Trust me; more testing below
88
+ assert_equal [31,46,61], s.rgb # 0x1f==31, 0x2e==46, 0x3d=61
89
+
90
+ # Create a style list
91
+ s1 = HighLine.Style(:bold, :red)
92
+ assert_instance_of HighLine::Style, s1
93
+ assert_equal [:bold, :red], s1.list
94
+
95
+ # Find an existing style list
96
+ s2 = HighLine.Style(:bold, :red)
97
+ assert_instance_of HighLine::Style, s2
98
+ assert_same s1, s2
99
+
100
+ # Create a style list with nils
101
+ s1 = HighLine.Style(:underline, nil, :blue)
102
+ assert_instance_of HighLine::Style, s1
103
+ assert_equal [:underline, :blue], s1.list
104
+
105
+ # Raise an error for an undefined style
106
+ assert_raise(::NameError) { HighLine.Style(:fubar) }
107
+ end
108
+
109
+ def test_no_color_scheme
110
+ HighLine.color_scheme = nil
111
+ assert_raise(::NameError) { HighLine.Style(:critical) }
112
+ end
113
+
114
+ def test_with_color_scheme
115
+ HighLine.color_scheme = HighLine::SampleColorScheme.new
116
+ s = HighLine.Style(:critical)
117
+ assert_instance_of HighLine::Style, s
118
+ assert_equal :critical, s.name
119
+ assert_equal [:yellow, :on_red], s.list
120
+ end
121
+
122
+ def test_builtin_foreground_colors_defined
123
+ HighLine::COLORS.each do |color|
124
+ style = HighLine.const_get(color+'_STYLE')
125
+ assert_instance_of HighLine::Style, style
126
+ assert_equal color.downcase.to_sym, style.name
127
+ assert style.builtin
128
+ code = HighLine.const_get(color)
129
+ assert_instance_of String, code, "Bad code for #{color}"
130
+ end
131
+ end
132
+
133
+ def test_builtin_background_colors_defined
134
+ HighLine::COLORS.each do |color|
135
+ style = HighLine.const_get('ON_' + color+'_STYLE')
136
+ assert_instance_of HighLine::Style, style
137
+ assert_equal ('ON_'+color).downcase.to_sym, style.name
138
+ assert style.builtin
139
+ code = HighLine.const_get('ON_' + color)
140
+ assert_instance_of String, code, "Bad code for ON_#{color}"
141
+ end
142
+ end
143
+
144
+ def test_builtin_styles_defined
145
+ HighLine::STYLES.each do |style_constant|
146
+ style = HighLine.const_get(style_constant+'_STYLE')
147
+ assert_instance_of HighLine::Style, style
148
+ assert_equal style_constant.downcase.to_sym, style.name
149
+ assert style.builtin
150
+ code = HighLine.const_get(style_constant)
151
+ assert_instance_of String, code, "Bad code for #{style_constant}"
152
+ end
153
+ end
154
+
155
+ def test_index
156
+ # Add a Style with a new name and code
157
+ assert_nil HighLine::Style.list[:s1]
158
+ assert_nil HighLine::Style.code_index['foo']
159
+ s1 = HighLine::Style.new(:name=>:s1, :code=>'foo')
160
+ assert_not_nil HighLine::Style.list[:s1]
161
+ assert_same s1, HighLine::Style.list[:s1]
162
+ assert_equal :s1, HighLine::Style.list[:s1].name
163
+ assert_equal 'foo', HighLine::Style.list[:s1].code
164
+ styles = HighLine::Style.list.size
165
+ codes = HighLine::Style.code_index.size
166
+ assert_instance_of Array, HighLine::Style.code_index['foo']
167
+ assert_equal 1, HighLine::Style.code_index['foo'].size
168
+ assert_same s1, HighLine::Style.code_index['foo'].last
169
+ assert_equal :s1, HighLine::Style.code_index['foo'].last.name
170
+ assert_equal 'foo', HighLine::Style.code_index['foo'].last.code
171
+
172
+ # Add another Style with a new name and code
173
+ assert_nil HighLine::Style.list[:s2]
174
+ assert_nil HighLine::Style.code_index['bar']
175
+ s2 = HighLine::Style.new(:name=>:s2, :code=>'bar')
176
+ assert_equal styles+1, HighLine::Style.list.size
177
+ assert_equal codes+1, HighLine::Style.code_index.size
178
+ assert_not_nil HighLine::Style.list[:s2]
179
+ assert_same s2, HighLine::Style.list[:s2]
180
+ assert_equal :s2, HighLine::Style.list[:s2].name
181
+ assert_equal 'bar', HighLine::Style.list[:s2].code
182
+ assert_instance_of Array, HighLine::Style.code_index['bar']
183
+ assert_equal 1, HighLine::Style.code_index['bar'].size
184
+ assert_same s2, HighLine::Style.code_index['bar'].last
185
+ assert_equal :s2, HighLine::Style.code_index['bar'].last.name
186
+ assert_equal 'bar', HighLine::Style.code_index['bar'].last.code
187
+
188
+ # Add a Style with an existing name
189
+ s3_before = HighLine::Style.list[:s2]
190
+ assert_not_nil HighLine::Style.list[:s2]
191
+ assert_nil HighLine::Style.code_index['baz']
192
+ s3 = HighLine::Style.new(:name=>:s2, :code=>'baz')
193
+ assert_not_same s2, s3
194
+ assert_not_same s3_before, s3
195
+ assert_equal styles+1, HighLine::Style.list.size
196
+ assert_equal codes+2, HighLine::Style.code_index.size
197
+ assert_not_nil HighLine::Style.list[:s2]
198
+ assert_same s3, HighLine::Style.list[:s2]
199
+ assert_not_same s2, HighLine::Style.list[:s2]
200
+ assert_equal :s2, HighLine::Style.list[:s2].name
201
+ assert_equal 'baz', HighLine::Style.list[:s2].code
202
+ assert_instance_of Array, HighLine::Style.code_index['baz']
203
+ assert_equal 1, HighLine::Style.code_index['baz'].size
204
+ assert_same s3, HighLine::Style.code_index['baz'].last
205
+ assert_equal :s2, HighLine::Style.code_index['baz'].last.name
206
+ assert_equal 'baz', HighLine::Style.code_index['baz'].last.code
207
+
208
+ # Add a Style with an existing code
209
+ assert_equal 1, HighLine::Style.code_index['baz'].size
210
+ s4 = HighLine::Style.new(:name=>:s4, :code=>'baz')
211
+ assert_equal styles+2, HighLine::Style.list.size
212
+ assert_equal codes+2, HighLine::Style.code_index.size
213
+ assert_not_nil HighLine::Style.list[:s4]
214
+ assert_same s4, HighLine::Style.list[:s4]
215
+ assert_equal :s4, HighLine::Style.list[:s4].name
216
+ assert_equal 'baz', HighLine::Style.list[:s4].code
217
+ assert_equal 2, HighLine::Style.code_index['baz'].size
218
+ assert_same s3, HighLine::Style.code_index['baz'].first # Unchanged from last time
219
+ assert_equal :s2, HighLine::Style.code_index['baz'].first.name # Unchanged from last time
220
+ assert_equal 'baz', HighLine::Style.code_index['baz'].first.code # Unchanged from last time
221
+ assert_same s4, HighLine::Style.code_index['baz'].last
222
+ assert_equal :s4, HighLine::Style.code_index['baz'].last.name
223
+ assert_equal 'baz', HighLine::Style.code_index['baz'].last.code
224
+ end
225
+
226
+ def test_rgb_hex
227
+ assert_equal "abcdef", HighLine::Style.rgb_hex("abcdef")
228
+ assert_equal "ABCDEF", HighLine::Style.rgb_hex("AB","CD","EF")
229
+ assert_equal "010203", HighLine::Style.rgb_hex(1,2,3)
230
+ assert_equal "123456", HighLine::Style.rgb_hex(18,52,86)
231
+ end
232
+
233
+ def test_rgb_parts
234
+ assert_equal [1,2,3], HighLine::Style.rgb_parts("010203")
235
+ assert_equal [18,52,86], HighLine::Style.rgb_parts("123456")
236
+ end
237
+
238
+ def test_rgb
239
+ s = HighLine::Style.rgb(1, 2, 3)
240
+ assert_instance_of HighLine::Style, s
241
+ assert_equal :rgb_010203, s.name
242
+ assert_equal [1,2,3], s.rgb
243
+ assert_equal "\e[38;5;16m", s.code
244
+
245
+ s = HighLine::Style.rgb("12", "34","56")
246
+ assert_instance_of HighLine::Style, s
247
+ assert_equal :rgb_123456, s.name
248
+ assert_equal [0x12, 0x34, 0x56], s.rgb
249
+ assert_equal "\e[38;5;24m", s.code
250
+
251
+ s = HighLine::Style.rgb("abcdef")
252
+ assert_instance_of HighLine::Style, s
253
+ assert_equal :rgb_abcdef, s.name
254
+ assert_equal [0xab, 0xcd, 0xef], s.rgb
255
+ assert_equal "\e[38;5;189m", s.code
256
+ end
257
+
258
+ def test_rgb_number
259
+ # ANSI RGB coding splits 0..255 into equal sixths, and then the
260
+ # red green and blue are encoded in base 6, plus 16, i.e.
261
+ # 16 + 36*(red_level) + 6*(green_level) + blue_level,
262
+ # where each of red_level, green_level, and blue_level are in
263
+ # the range 0..5
264
+
265
+ # This test logic works because 42 is just below 1/6 of 255,
266
+ # and 43 is just above
267
+
268
+ assert_equal 16 + 0*36 + 0*6 + 0, HighLine::Style.rgb_number( 0, 0, 0)
269
+ assert_equal 16 + 0*36 + 0*6 + 0, HighLine::Style.rgb_number( 0, 0, 42)
270
+ assert_equal 16 + 0*36 + 0*6 + 1, HighLine::Style.rgb_number( 0, 0, 43)
271
+
272
+ assert_equal 16 + 0*36 + 0*6 + 0, HighLine::Style.rgb_number( 0, 42, 0)
273
+ assert_equal 16 + 0*36 + 0*6 + 0, HighLine::Style.rgb_number( 0, 42, 42)
274
+ assert_equal 16 + 0*36 + 0*6 + 1, HighLine::Style.rgb_number( 0, 42, 43)
275
+
276
+ assert_equal 16 + 0*36 + 1*6 + 0, HighLine::Style.rgb_number( 0, 43, 0)
277
+ assert_equal 16 + 0*36 + 1*6 + 0, HighLine::Style.rgb_number( 0, 43, 42)
278
+ assert_equal 16 + 0*36 + 1*6 + 1, HighLine::Style.rgb_number( 0, 43, 43)
279
+
280
+ assert_equal 16 + 0*36 + 0*6 + 0, HighLine::Style.rgb_number( 42, 0, 0)
281
+ assert_equal 16 + 0*36 + 0*6 + 0, HighLine::Style.rgb_number( 42, 0, 42)
282
+ assert_equal 16 + 0*36 + 0*6 + 1, HighLine::Style.rgb_number( 42, 0, 43)
283
+
284
+ assert_equal 16 + 0*36 + 0*6 + 0, HighLine::Style.rgb_number( 42, 42, 0)
285
+ assert_equal 16 + 0*36 + 0*6 + 0, HighLine::Style.rgb_number( 42, 42, 42)
286
+ assert_equal 16 + 0*36 + 0*6 + 1, HighLine::Style.rgb_number( 42, 42, 43)
287
+
288
+ assert_equal 16 + 0*36 + 1*6 + 0, HighLine::Style.rgb_number( 42, 43, 0)
289
+ assert_equal 16 + 0*36 + 1*6 + 0, HighLine::Style.rgb_number( 42, 43, 42)
290
+ assert_equal 16 + 0*36 + 1*6 + 1, HighLine::Style.rgb_number( 42, 43, 43)
291
+
292
+ assert_equal 16 + 1*36 + 0*6 + 0, HighLine::Style.rgb_number( 43, 0, 0)
293
+ assert_equal 16 + 1*36 + 0*6 + 0, HighLine::Style.rgb_number( 43, 0, 42)
294
+ assert_equal 16 + 1*36 + 0*6 + 1, HighLine::Style.rgb_number( 43, 0, 43)
295
+
296
+ assert_equal 16 + 1*36 + 0*6 + 0, HighLine::Style.rgb_number( 43, 42, 0)
297
+ assert_equal 16 + 1*36 + 0*6 + 0, HighLine::Style.rgb_number( 43, 42, 42)
298
+ assert_equal 16 + 1*36 + 0*6 + 1, HighLine::Style.rgb_number( 43, 42, 43)
299
+
300
+ assert_equal 16 + 1*36 + 1*6 + 0, HighLine::Style.rgb_number( 43, 43, 0)
301
+ assert_equal 16 + 1*36 + 1*6 + 0, HighLine::Style.rgb_number( 43, 43, 42)
302
+ assert_equal 16 + 1*36 + 1*6 + 1, HighLine::Style.rgb_number( 43, 43, 43)
303
+
304
+ assert_equal 16 + 5*36 + 5*6 + 5, HighLine::Style.rgb_number(255,255,255)
305
+ end
306
+
307
+ def test_ansi_rgb_to_hex
308
+ assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0)
309
+ assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0)
310
+ assert_equal "00002b", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 1)
311
+
312
+ assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0)
313
+ assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0)
314
+ assert_equal "00002b", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 1)
315
+
316
+ assert_equal "002b00", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 1*6 + 0)
317
+ assert_equal "002b00", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 1*6 + 0)
318
+ assert_equal "002b2b", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 1*6 + 1)
319
+
320
+ assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0)
321
+ assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0)
322
+ assert_equal "00002b", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 1)
323
+
324
+ assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0)
325
+ assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0)
326
+ assert_equal "00002b", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 1)
327
+
328
+ assert_equal "002b00", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 1*6 + 0)
329
+ assert_equal "002b00", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 1*6 + 0)
330
+ assert_equal "002b2b", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 1*6 + 1)
331
+
332
+ assert_equal "2b0000", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 0*6 + 0)
333
+ assert_equal "2b0000", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 0*6 + 0)
334
+ assert_equal "2b002b", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 0*6 + 1)
335
+
336
+ assert_equal "2b0000", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 0*6 + 0)
337
+ assert_equal "2b0000", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 0*6 + 0)
338
+ assert_equal "2b002b", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 0*6 + 1)
339
+
340
+ assert_equal "2b2b00", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 1*6 + 0)
341
+ assert_equal "2b2b00", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 1*6 + 0)
342
+ assert_equal "2b2b2b", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 1*6 + 1)
343
+
344
+ # 0xd5 is the smallest number where n/255.0*6.0 > 5
345
+ assert_equal "d5d5d5", HighLine::Style.ansi_rgb_to_hex(16 + 5*36 + 5*6 + 5)
346
+ end
347
+
348
+ def test_list
349
+ list_size = HighLine::Style.list.size
350
+ # Add a Style with a new name and code
351
+ assert_nil HighLine::Style.list[:s5]
352
+ s5 = HighLine::Style.new(:name=>:s5, :code=>'foo')
353
+ assert_not_nil HighLine::Style.list[:s5]
354
+ assert_equal list_size+1, HighLine::Style.list.size
355
+ assert_not_nil HighLine::Style.list[:s5]
356
+ assert_same s5, HighLine::Style.list[:s5]
357
+ assert_equal :s5, HighLine::Style.list[:s5].name
358
+ assert_equal 'foo', HighLine::Style.list[:s5].code
359
+
360
+ # Add another Style with a new name and code
361
+ assert_nil HighLine::Style.list[:s6]
362
+ s6 = HighLine::Style.new(:name=>:s6, :code=>'bar')
363
+ assert_equal list_size+2, HighLine::Style.list.size
364
+ assert_not_nil HighLine::Style.list[:s6]
365
+ assert_same s6, HighLine::Style.list[:s6]
366
+ assert_equal :s6, HighLine::Style.list[:s6].name
367
+ assert_equal 'bar', HighLine::Style.list[:s6].code
368
+
369
+ # Add a Style with an existing name
370
+ s7 = HighLine::Style.new(:name=>:s6, :code=>'baz')
371
+ assert_equal list_size+2, HighLine::Style.list.size # No net addition to list
372
+ assert_not_nil HighLine::Style.list[:s6]
373
+ assert_same s7, HighLine::Style.list[:s6] # New one replaces old one
374
+ assert_not_same s6, HighLine::Style.list[:s6]
375
+ assert_equal :s6, HighLine::Style.list[:s6].name
376
+ assert_equal 'baz', HighLine::Style.list[:s6].code
377
+ end
378
+
379
+ def test_code_index
380
+ list_size = HighLine::Style.code_index.size
381
+
382
+ # Add a Style with a new name and code
383
+ assert_nil HighLine::Style.code_index['chewie']
384
+ s8 = HighLine::Style.new(:name=>:s8, :code=>'chewie')
385
+ assert_equal list_size+1, HighLine::Style.code_index.size
386
+ assert_instance_of Array, HighLine::Style.code_index['chewie']
387
+ assert_equal 1, HighLine::Style.code_index['chewie'].size
388
+ assert_equal :s8, HighLine::Style.code_index['chewie'].last.name
389
+ assert_equal 'chewie', HighLine::Style.code_index['chewie'].last.code
390
+
391
+ # Add another Style with a new name and code
392
+ assert_nil HighLine::Style.code_index['c3po']
393
+ s9 = HighLine::Style.new(:name=>:s9, :code=>'c3po')
394
+ assert_equal list_size+2, HighLine::Style.code_index.size
395
+ assert_instance_of Array, HighLine::Style.code_index['c3po']
396
+ assert_equal 1, HighLine::Style.code_index['c3po'].size
397
+ assert_equal :s9, HighLine::Style.code_index['c3po'].last.name
398
+ assert_equal 'c3po', HighLine::Style.code_index['c3po'].last.code
399
+
400
+ # Add a Style with an existing code
401
+ assert_equal 1, HighLine::Style.code_index['c3po'].size
402
+ s10 = HighLine::Style.new(:name=>:s10, :code=>'c3po')
403
+ assert_equal list_size+2, HighLine::Style.code_index.size
404
+ assert_equal 2, HighLine::Style.code_index['c3po'].size
405
+ assert_equal :s10, HighLine::Style.code_index['c3po'].last.name
406
+ assert_equal 'c3po', HighLine::Style.code_index['c3po'].last.code
407
+ end
408
+
409
+ def test_uncolor
410
+ # Normal color
411
+ assert_equal "This should be reverse underlined magenta!\n",
412
+ HighLine::Style.uncolor("This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n" )
413
+
414
+ # RGB color
415
+ assert_equal "This should be rgb_906030!\n",
416
+ HighLine::Style.uncolor("This should be \e[38;5;137mrgb_906030\e[0m!\n" )
417
+ end
418
+
419
+ def test_color
420
+ assert_equal "\e[99mstring\e[0m", @style1.color("string") # simple style
421
+ assert_equal "\e[99m\e[98mstring\e[0m", @style3.color("string") # Style list
422
+ end
423
+
424
+ def test_code
425
+ assert_equal "\e[99m", @style1.code # simple style
426
+ assert_equal "\e[99m\e[98m", @style3.code # Style list
427
+ end
428
+
429
+ def test_red
430
+ assert_equal 0x65, @style4.red
431
+ assert_equal 0, HighLine::Style(:none).red # Probably reliable
432
+ assert_equal 0, HighLine::Style(:black).red # Probably reliable
433
+ assert_equal 255, HighLine::Style(:bright_magenta).red # Seems to be reliable
434
+ assert_equal 255, HighLine::Style(:on_none).red # Probably reliable
435
+ end
436
+
437
+ def test_green
438
+ assert_equal 0x43, @style4.green
439
+ assert_equal 0, HighLine::Style(:none).green # Probably reliable
440
+ assert_equal 0, HighLine::Style(:black).green # Probably reliable
441
+ assert 240 <= HighLine::Style(:bright_cyan).green # Probably reliable
442
+ assert_equal 255, HighLine::Style(:on_none).green # Probably reliable
443
+ end
444
+
445
+ def test_blue
446
+ assert_equal 0x21, @style4.blue
447
+ assert_equal 0, HighLine::Style(:none).blue # Probably reliable
448
+ assert_equal 0, HighLine::Style(:black).blue # Probably reliable
449
+ assert_equal 255, HighLine::Style(:bright_blue).blue # Probably reliable
450
+ assert_equal 255, HighLine::Style(:on_none).blue # Probably reliable
451
+ end
452
+
453
+ def test_builtin
454
+ assert HighLine::Style(:red).builtin
455
+ assert !@style1.builtin
456
+ end
457
+
458
+ def test_variant
459
+ style1_name = @style1.name
460
+ style1_code = @style1.code
461
+ style1_rgb = @style1.rgb
462
+
463
+ s1 = @style1.variant(:new_foo1, :code=>'abracadabra')
464
+ assert_instance_of HighLine::Style, s1
465
+ assert_not_same @style1, s1 # This is a copy
466
+ assert_equal :new_foo1, s1.name # Changed
467
+ assert_equal 'abracadabra', s1.code # Changed
468
+ assert_equal [1,2,3], s1.rgb # Unchanged
469
+
470
+ s2 = @style1.variant(:new_foo2, :increment=>-15)
471
+ assert_instance_of HighLine::Style, s2
472
+ assert_not_same @style1, s2 # This is a copy
473
+ assert_equal :new_foo2, s2.name # Changed
474
+ assert_equal "\e[84m", s2.code # 99 (original code) - 15
475
+ assert_equal [1,2,3], s2.rgb # Unchanged
476
+
477
+ s3 = @style1.variant(:new_foo3, :code=>"\e[55m", :increment=>15)
478
+ assert_instance_of HighLine::Style, s3
479
+ assert_not_same @style1, s3 # This is a copy
480
+ assert_equal :new_foo3, s3.name # Changed
481
+ assert_equal "\e[70m", s3.code # 99 (new code) + 15
482
+ assert_equal [1,2,3], s3.rgb # Unchanged
483
+
484
+ s4 = @style1.variant(:new_foo4, :code=>"\e[55m", :increment=>15, :rgb=>"blah")
485
+ assert_instance_of HighLine::Style, s4
486
+ assert_not_same @style1, s4 # This is a copy
487
+ assert_equal :new_foo4, s4.name # Changed
488
+ assert_equal "\e[70m", s4.code # 99 (new code) + 15
489
+ assert_equal 'blah', s4.rgb # Changed
490
+
491
+ s5 = @style1.variant(:new_foo5)
492
+ assert_instance_of HighLine::Style, s5
493
+ assert_not_same @style1, s5 # This is a copy
494
+ assert_equal :new_foo5, s5.name # Changed
495
+ assert_equal "\e[99m", s5.code # Unchanged
496
+ assert_equal [1,2,3], s5.rgb # Unchanged
497
+
498
+ # No @style1's have been harmed in the running of this test
499
+ assert_equal style1_name, @style1.name
500
+ assert_equal style1_code, @style1.code
501
+ assert_equal style1_rgb, @style1.rgb
502
+
503
+ assert_raise(::RuntimeError) { @style3.variant(:new_foo6) } # Can't create a variant of a list style
504
+ end
505
+
506
+ def test_on
507
+ style1_name = @style1.name
508
+ style1_code = @style1.code
509
+ style1_rgb = @style1.rgb
510
+
511
+ s1 = @style1.on
512
+ assert_instance_of HighLine::Style, s1
513
+ assert_not_same @style1, s1 # This is a copy
514
+ assert_equal :on_foo, s1.name # Changed
515
+ assert_equal "\e[109m", s1.code # Changed
516
+ assert_equal [1,2,3], s1.rgb # Unchanged
517
+
518
+ # No @style1's have been harmed in the running of this test
519
+ assert_equal style1_name, @style1.name
520
+ assert_equal style1_code, @style1.code
521
+ assert_equal style1_rgb, @style1.rgb
522
+
523
+ assert_raise(::RuntimeError) { @style3.on } # Can't create a variant of a list style
524
+ end
525
+
526
+ def test_bright
527
+ style1_name = @style1.name
528
+ style1_code = @style1.code
529
+ style1_rgb = @style1.rgb
530
+
531
+ s1 = @style1.bright
532
+ assert_instance_of HighLine::Style, s1
533
+ assert_not_same @style1, s1 # This is a copy
534
+ assert_equal :bright_foo, s1.name # Changed
535
+ assert_equal "\e[159m", s1.code # Changed
536
+ assert_equal [129,130,131], s1.rgb # Changed
537
+
538
+ # No @style1's have been harmed in the running of this test
539
+ assert_equal style1_name, @style1.name
540
+ assert_equal style1_code, @style1.code
541
+ assert_equal style1_rgb, @style1.rgb
542
+
543
+ s2_base = HighLine::Style.new(:name=>:leia, :code=>"\e[92m", :rgb=>[0,0,14])
544
+ s2 = s2_base.bright
545
+ assert_instance_of HighLine::Style, s2
546
+ assert_not_same s2_base, s2 # This is a copy
547
+ assert_equal :bright_leia, s2.name # Changed
548
+ assert_equal "\e[152m", s2.code # Changed
549
+ assert_equal [0,0,142], s2.rgb # Changed
550
+
551
+ s3_base = HighLine::Style.new(:name=>:luke, :code=>"\e[93m", :rgb=>[20,21,0])
552
+ s3 = s3_base.bright
553
+ assert_instance_of HighLine::Style, s3
554
+ assert_not_same s3_base, s3 # This is a copy
555
+ assert_equal :bright_luke, s3.name # Changed
556
+ assert_equal "\e[153m", s3.code # Changed
557
+ assert_equal [148,149,0], s3.rgb # Changed
558
+
559
+ s4_base = HighLine::Style.new(:name=>:r2d2, :code=>"\e[94m", :rgb=>[0,0,0])
560
+ s4 = s4_base.bright
561
+ assert_instance_of HighLine::Style, s4
562
+ assert_not_same s4_base, s4 # This is a copy
563
+ assert_equal :bright_r2d2, s4.name # Changed
564
+ assert_equal "\e[154m", s4.code # Changed
565
+ assert_equal [128,128,128], s4.rgb # Changed; special case
566
+
567
+ assert_raise(::RuntimeError) { @style3.bright } # Can't create a variant of a list style
568
+ end
569
+ end