highline-sgonyea 1.6.12

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