highline 2.0.0.pre.develop.9 → 2.0.0.pre.develop.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +59 -5
  3. data/.travis.yml +9 -4
  4. data/Changelog.md +11 -0
  5. data/Gemfile +12 -19
  6. data/Rakefile +5 -11
  7. data/examples/ansi_colors.rb +6 -11
  8. data/examples/asking_for_arrays.rb +4 -3
  9. data/examples/basic_usage.rb +29 -22
  10. data/examples/color_scheme.rb +11 -10
  11. data/examples/get_character.rb +6 -5
  12. data/examples/limit.rb +2 -1
  13. data/examples/menus.rb +11 -11
  14. data/examples/overwrite.rb +7 -6
  15. data/examples/page_and_wrap.rb +5 -4
  16. data/examples/password.rb +2 -1
  17. data/examples/repeat_entry.rb +7 -5
  18. data/examples/trapping_eof.rb +2 -1
  19. data/examples/using_readline.rb +2 -1
  20. data/highline.gemspec +25 -25
  21. data/lib/highline.rb +103 -111
  22. data/lib/highline/builtin_styles.rb +45 -41
  23. data/lib/highline/color_scheme.rb +32 -28
  24. data/lib/highline/compatibility.rb +3 -3
  25. data/lib/highline/custom_errors.rb +2 -1
  26. data/lib/highline/import.rb +8 -11
  27. data/lib/highline/list.rb +4 -8
  28. data/lib/highline/list_renderer.rb +207 -201
  29. data/lib/highline/menu.rb +75 -63
  30. data/lib/highline/menu/item.rb +2 -0
  31. data/lib/highline/paginator.rb +5 -6
  32. data/lib/highline/question.rb +38 -36
  33. data/lib/highline/question/answer_converter.rb +2 -2
  34. data/lib/highline/question_asker.rb +15 -17
  35. data/lib/highline/simulate.rb +11 -13
  36. data/lib/highline/statement.rb +12 -10
  37. data/lib/highline/string.rb +9 -8
  38. data/lib/highline/string_extensions.rb +30 -14
  39. data/lib/highline/style.rb +68 -45
  40. data/lib/highline/template_renderer.rb +5 -5
  41. data/lib/highline/terminal.rb +24 -31
  42. data/lib/highline/terminal/io_console.rb +2 -2
  43. data/lib/highline/terminal/ncurses.rb +4 -3
  44. data/lib/highline/terminal/unix_stty.rb +12 -9
  45. data/lib/highline/version.rb +1 -1
  46. data/lib/highline/wrapper.rb +12 -11
  47. metadata +34 -43
  48. data/test/acceptance/acceptance.rb +0 -62
  49. data/test/acceptance/acceptance_test.rb +0 -69
  50. data/test/acceptance/at_color_output_using_erb_templates.rb +0 -17
  51. data/test/acceptance/at_echo_false.rb +0 -23
  52. data/test/acceptance/at_readline.rb +0 -37
  53. data/test/io_console_compatible.rb +0 -37
  54. data/test/string_methods.rb +0 -35
  55. data/test/test_answer_converter.rb +0 -26
  56. data/test/test_color_scheme.rb +0 -94
  57. data/test/test_helper.rb +0 -22
  58. data/test/test_highline.rb +0 -1627
  59. data/test/test_import.rb +0 -55
  60. data/test/test_list.rb +0 -60
  61. data/test/test_menu.rb +0 -749
  62. data/test/test_paginator.rb +0 -73
  63. data/test/test_question_asker.rb +0 -20
  64. data/test/test_simulator.rb +0 -24
  65. data/test/test_string_extension.rb +0 -72
  66. data/test/test_string_highline.rb +0 -42
  67. data/test/test_style.rb +0 -613
  68. data/test/test_wrapper.rb +0 -188
data/test/test_import.rb DELETED
@@ -1,55 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # coding: utf-8
3
-
4
- # tc_import.rb
5
- #
6
- # Created by James Edward Gray II on 2005-04-26.
7
- # Copyright 2005 Gray Productions. All rights reserved.
8
- #
9
- # This is Free Software. See LICENSE and COPYING for details.
10
-
11
- require "test_helper"
12
-
13
- require "highline/import"
14
- require "stringio"
15
-
16
- class TestImport < Minitest::Test
17
- def test_import
18
- assert_respond_to(self, :agree)
19
- assert_respond_to(self, :ask)
20
- assert_respond_to(self, :choose)
21
- assert_respond_to(self, :say)
22
- end
23
-
24
- def test_or_ask
25
- old_terminal = $terminal
26
-
27
- input = StringIO.new
28
- output = StringIO.new
29
- $terminal = HighLine.new(input, output)
30
-
31
- input << "10\n"
32
- input.rewind
33
-
34
- assert_equal(10, nil.or_ask("How much? ", Integer))
35
-
36
- input.rewind
37
-
38
- assert_equal(20, "20".or_ask("How much? ", Integer))
39
- assert_equal(20, 20.or_ask("How much? ", Integer))
40
-
41
- assert_equal(10, 20.or_ask("How much? ", Integer) { |q| q.in = 1..10 })
42
- ensure
43
- $terminal = old_terminal
44
- end
45
-
46
- def test_redirection
47
- old_terminal = $terminal
48
-
49
- $terminal = HighLine.new(nil, (output = StringIO.new))
50
- say("Testing...")
51
- assert_equal("Testing...\n", output.string)
52
- ensure
53
- $terminal = old_terminal
54
- end
55
- end
data/test/test_list.rb DELETED
@@ -1,60 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # coding: utf-8
3
-
4
- require "test_helper"
5
-
6
- require "highline/list"
7
-
8
- class TestHighLineList < Minitest::Test
9
- def setup
10
- @items = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" ]
11
- end
12
-
13
- def test_in_2_cols
14
- list_in_two_cols =
15
- [ [ "a", "b" ],
16
- [ "c", "d" ],
17
- [ "e", "f" ],
18
- [ "g", "h" ],
19
- [ "i", "j" ] ]
20
-
21
- highline_list = HighLine::List.new(@items, cols: 2)
22
-
23
- assert_equal list_in_two_cols, highline_list.list
24
- end
25
-
26
- def test_in_2_cols_col_down
27
- col_down_list =
28
- [ [ "a", "f"],
29
- [ "b", "g"],
30
- [ "c", "h"],
31
- [ "d", "i"],
32
- [ "e", "j"] ]
33
-
34
- highline_list = HighLine::List.new(@items, cols: 2, col_down: true)
35
-
36
- assert_equal col_down_list, highline_list.list
37
- end
38
-
39
- def test_in_2_cols_transposed
40
- transposed_list =
41
- [ [ "a", "c", "e", "g", "i" ],
42
- [ "b", "d", "f", "h", "j" ] ]
43
-
44
- highline_list = HighLine::List.new(@items, cols: 2, transpose: true)
45
-
46
- assert_equal transposed_list, highline_list.list
47
- end
48
-
49
- def test_in_3_cols
50
- list_in_three_cols =
51
- [ [ "a", "b", "c" ],
52
- [ "d", "e", "f" ],
53
- [ "g", "h", "i" ],
54
- [ "j" ] ]
55
-
56
- highline_list = HighLine::List.new(@items, cols: 3)
57
-
58
- assert_equal list_in_three_cols, highline_list.list
59
- end
60
- end
data/test/test_menu.rb DELETED
@@ -1,749 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # coding: utf-8
3
-
4
- # tc_menu.rb
5
- #
6
- # Created by Gregory Thomas Brown on 2005-05-10.
7
- # Copyright 2005. All rights reserved.
8
- #
9
- # This is Free Software. See LICENSE and COPYING for details.
10
-
11
- require "test_helper"
12
-
13
- require "highline"
14
- require "stringio"
15
-
16
- class TestMenu < Minitest::Test
17
- def setup
18
- HighLine.reset
19
- @input = StringIO.new
20
- @output = StringIO.new
21
- @terminal = HighLine.new(@input, @output)
22
- end
23
-
24
- def test_choices
25
- @input << "2\n"
26
- @input.rewind
27
-
28
- output = @terminal.choose do |menu|
29
- menu.choices("Sample1", "Sample2", "Sample3")
30
- end
31
- assert_equal("Sample2", output)
32
- end
33
-
34
- def test_default
35
- @input << "\n"
36
- @input.rewind
37
-
38
- output = @terminal.choose do |menu|
39
- menu.choices("Sample1", "Sample2", "Sample3")
40
- menu.default = "Sample1"
41
- end
42
-
43
- assert_equal("Sample1", output)
44
- end
45
-
46
- def test_flow
47
- @input << "Sample1\n"
48
- @input.rewind
49
-
50
- @terminal.choose do |menu|
51
- # Default: menu.flow = :rows
52
-
53
- menu.choice "Sample1"
54
- menu.choice "Sample2"
55
- menu.choice "Sample3"
56
- end
57
- assert_equal("1. Sample1\n2. Sample2\n3. Sample3\n? ", @output.string)
58
-
59
- @output.truncate(@output.rewind)
60
- @input.rewind
61
-
62
- @terminal.choose do |menu|
63
- menu.flow = :columns_across
64
-
65
- menu.choice "Sample1"
66
- menu.choice "Sample2"
67
- menu.choice "Sample3"
68
- end
69
- assert_equal("1. Sample1 2. Sample2 3. Sample3\n? ", @output.string)
70
-
71
- @output.truncate(@output.rewind)
72
- @input.rewind
73
-
74
- @terminal.choose do |menu|
75
- menu.flow = :inline
76
- menu.index = :none
77
-
78
- menu.choice "Sample1"
79
- menu.choice "Sample2"
80
- menu.choice "Sample3"
81
- end
82
- assert_equal("Sample1, Sample2 or Sample3? ", @output.string)
83
- end
84
-
85
- def test_unicode_flow
86
- @input << "1\n"
87
- @input.rewind
88
-
89
- @terminal.choose do |menu|
90
- # Default: menu.flow = :rows
91
- menu.choice "Unicode right single quotation mark: ’"
92
- end
93
- assert_equal("1. Unicode right single quotation mark: ’\n? ".encode(@output.external_encoding, { :undef => :replace }), @output.string)
94
- end
95
-
96
- def test_text_override_index_selects_name
97
- @input << "1\n"
98
- @input.rewind
99
-
100
- selected = @terminal.choose do |menu|
101
- menu.choice("Sample1", nil, "Sample2")
102
- menu.choice("Sample2", nil, "Sample1")
103
- end
104
- assert_equal(selected, "Sample1")
105
- assert_equal("1. Sample2\n" +
106
- "2. Sample1\n" +
107
- "? ", @output.string)
108
- end
109
-
110
- def test_text_override_selections_matches_name
111
- @input << "Sample2\n"
112
- @input.rewind
113
-
114
- selected = @terminal.choose do |menu|
115
- menu.choice("Sample1", nil, "Sample2")
116
- menu.choice("Sample2", nil, "Sample1")
117
- end
118
- assert_equal(selected, "Sample2")
119
- assert_equal("1. Sample2\n" +
120
- "2. Sample1\n" +
121
- "? ", @output.string)
122
- end
123
-
124
- def test_menu_add_item_index_selects_name
125
- @input << "1\n"
126
- @input.rewind
127
-
128
- selected = @terminal.choose do |menu|
129
- menu.add_item(HighLine::Menu::Item.new("Sample1", text: "Sample2"))
130
- menu.add_item(HighLine::Menu::Item.new("Sample2", text: "Sample1"))
131
- end
132
- assert_equal(selected, "Sample1")
133
- assert_equal("1. Sample2\n" +
134
- "2. Sample1\n" +
135
- "? ", @output.string)
136
- end
137
-
138
- def test_menu_add_item_selections_matches_name
139
- @input << "Sample2\n"
140
- @input.rewind
141
-
142
- selected = @terminal.choose do |menu|
143
- menu.add_item(HighLine::Menu::Item.new("Sample1", text: "Sample2"))
144
- menu.add_item(HighLine::Menu::Item.new("Sample2", text: "Sample1"))
145
- end
146
- assert_equal(selected, "Sample2")
147
- assert_equal("1. Sample2\n" +
148
- "2. Sample1\n" +
149
- "? ", @output.string)
150
- end
151
-
152
- def test_menu_build_item
153
- @input << "Sample2\n"
154
- @input.rewind
155
-
156
- selected = @terminal.choose do |menu|
157
- menu.add_item(menu.build_item("Sample1", text: "Sample2"))
158
- menu.add_item(menu.build_item("Sample2", text: "Sample1"))
159
- end
160
- assert_equal(selected, "Sample2")
161
- assert_equal("1. Sample2\n" +
162
- "2. Sample1\n" +
163
- "? ", @output.string)
164
- end
165
-
166
- def test_help
167
- @input << "help\nhelp load\nhelp rules\nhelp missing\n"
168
- @input.rewind
169
-
170
- 4.times do
171
- @terminal.choose do |menu|
172
- menu.shell = true
173
-
174
- menu.choice(:load, "Load a file.")
175
- menu.choice(:save, "Save data in file.")
176
- menu.choice(:quit, "Exit program.")
177
-
178
- menu.help("rules", "The rules of this system are as follows...")
179
- end
180
- end
181
- assert_equal( "1. load\n2. save\n3. quit\n4. help\n? " +
182
- "This command will display helpful messages about " +
183
- "functionality, like this one. To see the help for a " +
184
- "specific topic enter:\n" +
185
- "\thelp [TOPIC]\n" +
186
- "Try asking for help on any of the following:\n" +
187
- "\nload quit rules save \n" +
188
- "1. load\n2. save\n3. quit\n4. help\n? " +
189
- "= load\n\n" +
190
- "Load a file.\n" +
191
- "1. load\n2. save\n3. quit\n4. help\n? " +
192
- "= rules\n\n" +
193
- "The rules of this system are as follows...\n" +
194
- "1. load\n2. save\n3. quit\n4. help\n? " +
195
- "= missing\n\n" +
196
- "There's no help for that topic.\n", @output.string )
197
- end
198
-
199
- def test_index
200
- @input << "Sample1\n"
201
- @input.rewind
202
-
203
- @terminal.choose do |menu|
204
- # Default: menu.index = :number
205
-
206
- menu.choice "Sample1"
207
- menu.choice "Sample2"
208
- menu.choice "Sample3"
209
- end
210
- assert_equal("1. Sample1\n2. Sample2\n3. Sample3\n? ", @output.string)
211
-
212
- @output.truncate(@output.rewind)
213
- @input.rewind
214
-
215
- @terminal.choose do |menu|
216
- menu.index = :letter
217
- menu.index_suffix = ") "
218
-
219
- menu.choice "Sample1"
220
- menu.choice "Sample2"
221
- menu.choice "Sample3"
222
- end
223
- assert_equal("a) Sample1\nb) Sample2\nc) Sample3\n? ", @output.string)
224
-
225
- @output.truncate(@output.rewind)
226
- @input.rewind
227
-
228
- @terminal.choose do |menu|
229
- menu.index = :none
230
-
231
- menu.choice "Sample1"
232
- menu.choice "Sample2"
233
- menu.choice "Sample3"
234
- end
235
- assert_equal("Sample1\nSample2\nSample3\n? ", @output.string)
236
-
237
- @output.truncate(@output.rewind)
238
- @input.rewind
239
-
240
- @terminal.choose do |menu|
241
- menu.index = "*"
242
-
243
- menu.choice "Sample1"
244
- menu.choice "Sample2"
245
- menu.choice "Sample3"
246
- end
247
- assert_equal("* Sample1\n* Sample2\n* Sample3\n? ", @output.string)
248
- end
249
-
250
- def test_index_with_color
251
- index_color = :rgb_77bbff
252
-
253
- @input << "Sample1\n"
254
- @input.rewind
255
-
256
- @terminal.choose do |menu|
257
- # Default: menu.index = :number
258
- menu.index_color = index_color
259
-
260
- menu.choice "Sample1"
261
- menu.choice "Sample2"
262
- menu.choice "Sample3"
263
- end
264
-
265
- assert_equal(
266
- HighLine.color("1. ", index_color) + "Sample1\n" +
267
- HighLine.color("2. ", index_color) + "Sample2\n" +
268
- HighLine.color("3. ", index_color) + "Sample3\n" +
269
- "? ",
270
- @output.string
271
- )
272
-
273
- @output.truncate(@output.rewind)
274
- @input.rewind
275
-
276
- @terminal.choose do |menu|
277
- menu.index = :letter
278
- menu.index_suffix = ") "
279
- menu.index_color = index_color
280
-
281
- menu.choice "Sample1"
282
- menu.choice "Sample2"
283
- menu.choice "Sample3"
284
- end
285
-
286
- assert_equal(
287
- HighLine.color("a) ", index_color) + "Sample1\n" +
288
- HighLine.color("b) ", index_color) + "Sample2\n" +
289
- HighLine.color("c) ", index_color) + "Sample3\n? ",
290
- @output.string
291
- )
292
-
293
- @output.truncate(@output.rewind)
294
- @input.rewind
295
-
296
- @terminal.choose do |menu|
297
- menu.index = :none
298
- menu.index_color = index_color
299
-
300
- menu.choice "Sample1"
301
- menu.choice "Sample2"
302
- menu.choice "Sample3"
303
- end
304
-
305
- assert_equal(
306
- HighLine.color("Sample1", index_color) + "\n" +
307
- HighLine.color("Sample2", index_color) + "\n" +
308
- HighLine.color("Sample3", index_color) + "\n? ",
309
- @output.string
310
- )
311
-
312
- @output.truncate(@output.rewind)
313
- @input.rewind
314
-
315
- @terminal.choose do |menu|
316
- menu.index = "*"
317
- menu.index_color = index_color
318
-
319
- menu.choice "Sample1"
320
- menu.choice "Sample2"
321
- menu.choice "Sample3"
322
- end
323
-
324
- colored_asterix = HighLine.color("* ", index_color)
325
- assert_equal(
326
- "#{colored_asterix}Sample1\n" +
327
- "#{colored_asterix}Sample2\n" +
328
- "#{colored_asterix}Sample3\n? ",
329
- @output.string
330
- )
331
- end
332
-
333
- def test_layouts
334
- @input << "save\n"
335
- @input.rewind
336
-
337
- @terminal.choose(:load, :save, :quit) # Default: layout = :list
338
- assert_equal("1. load\n2. save\n3. quit\n? ", @output.string)
339
-
340
- @input.rewind
341
- @output.truncate(@output.rewind)
342
-
343
- @terminal.choose(:load, :save, :quit) do |menu|
344
- menu.header = "File Menu"
345
- end
346
- assert_equal( "File Menu:\n" +
347
- "1. load\n2. save\n3. quit\n? ", @output.string )
348
-
349
- @input.rewind
350
- @output.truncate(@output.rewind)
351
-
352
- @terminal.choose(:load, :save, :quit) do |menu|
353
- menu.layout = :one_line
354
- menu.header = "File Menu"
355
- menu.prompt = "Operation? "
356
- end
357
- assert_equal( "File Menu: Operation? " +
358
- "(load, save or quit) ", @output.string )
359
-
360
- @input.rewind
361
- @output.truncate(@output.rewind)
362
-
363
- @terminal.choose(:load, :save, :quit) do |menu|
364
- menu.layout = :menu_only
365
- end
366
- assert_equal("load, save or quit? ", @output.string)
367
-
368
- @input.rewind
369
- @output.truncate(@output.rewind)
370
-
371
- @terminal.choose(:load, :save, :quit) do |menu|
372
- menu.layout = '<%= list(menu) %>File Menu: '
373
- end
374
- assert_equal("1. load\n2. save\n3. quit\nFile Menu: ", @output.string)
375
- end
376
-
377
- def test_list_option
378
- @input << "l\n"
379
- @input.rewind
380
-
381
- @terminal.choose(:load, :save, :quit) do |menu|
382
- menu.layout = :menu_only
383
- menu.list_option = ", or "
384
- end
385
- assert_equal("load, save, or quit? ", @output.string)
386
- end
387
-
388
- def test_nil_on_handled
389
- @input << "3\n3\n2\n"
390
- @input.rewind
391
-
392
- # Shows that by default proc results are returned.
393
- output = @terminal.choose do |menu|
394
- menu.choice "Sample1" do "output1" end
395
- menu.choice "Sample2" do "output2" end
396
- menu.choice "Sample3" do "output3" end
397
- end
398
- assert_equal("output3", output)
399
-
400
- #
401
- # Shows that they can be replaced with +nil+ by setting
402
- # _nil_on_handled to +true+.
403
- #
404
- output = @terminal.choose do |menu|
405
- menu.nil_on_handled = true
406
- menu.choice "Sample1" do "output1" end
407
- menu.choice "Sample2" do "output2" end
408
- menu.choice "Sample3" do "output3" end
409
- end
410
- assert_equal(nil, output)
411
-
412
- # Shows that a menu item without a proc will be returned no matter what.
413
- output = @terminal.choose do |menu|
414
- menu.choice "Sample1"
415
- menu.choice "Sample2"
416
- menu.choice "Sample3"
417
- end
418
- assert_equal("Sample2", output)
419
- end
420
-
421
- def test_passed_command
422
- @input << "q\n"
423
- @input.rewind
424
-
425
- selected = nil
426
- @terminal.choose do |menu|
427
- menu.choices(:load, :save, :quit) { |command| selected = command }
428
- end
429
- assert_equal(:quit, selected)
430
- end
431
-
432
- def test_question_options
433
- @input << "save\n"
434
- @input.rewind
435
-
436
- answer = @terminal.choose(:Load, :Save, :Quit) do |menu|
437
- menu.case = :capitalize
438
- end
439
- assert_equal(:Save, answer)
440
-
441
- @input.rewind
442
-
443
- answer = @terminal.choose(:Load, :Save, :Quit) do |menu|
444
- menu.case = :capitalize
445
- menu.character = :getc
446
- end
447
- assert_equal(:Save, answer)
448
- assert_equal(?a, @input.getc)
449
- end
450
-
451
- def test_select_by
452
- @input << "Sample1\n2\n"
453
- @input.rewind
454
-
455
- selected = @terminal.choose do |menu|
456
- menu.choice "Sample1"
457
- menu.choice "Sample2"
458
- menu.choice "Sample3"
459
- end
460
- assert_equal("Sample1", selected)
461
-
462
- @input.rewind
463
-
464
- selected = @terminal.choose do |menu|
465
- menu.select_by = :index
466
-
467
- menu.choice "Sample1"
468
- menu.choice "Sample2"
469
- menu.choice "Sample3"
470
- end
471
- assert_equal("Sample2", selected)
472
-
473
- @input.rewind
474
-
475
- selected = @terminal.choose do |menu|
476
- menu.select_by = :name
477
-
478
- menu.choice "Sample1"
479
- menu.choice "Sample2"
480
- menu.choice "Sample3"
481
- end
482
- assert_equal("Sample1", selected)
483
- end
484
-
485
- def test_hidden
486
- @input << "Hidden\n4\n"
487
- @input.rewind
488
-
489
- selected = @terminal.choose do |menu|
490
- menu.choice "Sample1"
491
- menu.choice "Sample2"
492
- menu.choice "Sample3"
493
- menu.hidden "Hidden!"
494
- end
495
- assert_equal("Hidden!", selected)
496
- assert_equal("1. Sample1\n2. Sample2\n3. Sample3\n? ", @output.string)
497
-
498
- @input.rewind
499
-
500
- selected = @terminal.choose do |menu|
501
- menu.select_by = :index
502
-
503
- menu.choice "Sample1"
504
- menu.choice "Sample2"
505
- menu.choice "Sample3"
506
- menu.hidden "Hidden!"
507
- end
508
- assert_equal("Hidden!", selected)
509
-
510
- @input.rewind
511
-
512
- selected = @terminal.choose do |menu|
513
- menu.select_by = :name
514
-
515
- menu.choice "Sample1"
516
- menu.choice "Sample2"
517
- menu.choice "Sample3"
518
- menu.hidden "Hidden!"
519
- end
520
- assert_equal("Hidden!", selected)
521
-
522
- @input.rewind
523
- end
524
-
525
- def test_select_by_letter
526
- @input << "b\n"
527
- @input.rewind
528
-
529
- selected = @terminal.choose do |menu|
530
- menu.index = :letter
531
- menu.choice :save
532
- menu.choice :load
533
- menu.choice :quit
534
- end
535
- assert_equal(:load, selected)
536
- end
537
-
538
- def test_shell
539
- @input << "save --some-option my_file.txt\n"
540
- @input.rewind
541
-
542
- selected = nil
543
- options = nil
544
- answer = @terminal.choose do |menu|
545
- menu.choices(:load, :quit)
546
- menu.choice(:save) do |command, details|
547
- selected = command
548
- options = details
549
-
550
- "Saved!"
551
- end
552
- menu.shell = true
553
- end
554
- assert_equal("Saved!", answer)
555
- assert_equal(:save, selected)
556
- assert_equal("--some-option my_file.txt", options)
557
- end
558
-
559
- def test_simple_menu_shortcut
560
- @input << "3\n"
561
- @input.rewind
562
-
563
- selected = @terminal.choose(:save, :load, :quit)
564
- assert_equal(:quit, selected)
565
- end
566
-
567
- def test_symbols
568
- @input << "3\n"
569
- @input.rewind
570
-
571
- selected = @terminal.choose do |menu|
572
- menu.choices(:save, :load, :quit)
573
- end
574
- assert_equal(:quit, selected)
575
- end
576
-
577
- def test_paged_print_infinite_loop_bug
578
- @terminal.page_at = 5
579
- # Will page twice, so start with two new lines
580
- @input << "\n\n3\n"
581
- @input.rewind
582
-
583
- # Sadly this goes into an infinite loop without the fix to page_print
584
- selected = @terminal.choose(* 1..10)
585
- assert_equal(selected, 3)
586
- end
587
-
588
- def test_cancel_paging
589
- # Tests that paging can be cancelled halfway through
590
- @terminal.page_at = 5
591
- # Will page twice, so stop after first page and make choice 3
592
- @input << "q3\n"
593
- @input.rewind
594
-
595
- selected = @terminal.choose(* 1..10)
596
- assert_equal(selected, 3)
597
-
598
- # Make sure paging message appeared
599
- assert( @output.string.index('press enter/return to continue or q to stop'),
600
- "Paging message did not appear." )
601
-
602
- # Make sure it only appeared once
603
- assert( @output.string !~ /q to stop.*q to stop/m,
604
- "Paging message appeared more than once." )
605
- end
606
-
607
- def test_autocomplete_prompt
608
- @input << "lisp\nRuby\n"
609
- @input.rewind
610
-
611
- answer = @terminal.choose do |menu|
612
- menu.choice(:Perl)
613
- menu.choice(:Python)
614
- menu.choice(:Ruby)
615
- menu.prompt = "What is your favorite programming language? "
616
- end
617
- languages = [:Perl, :Python, :Ruby]
618
- assert_equal("1. Perl\n" +
619
- "2. Python\n" +
620
- "3. Ruby\n" +
621
- "What is your favorite programming language? " +
622
- "You must choose one of [1, 2, 3, Perl, Python, Ruby].\n" +
623
- "? ", @output.string )
624
- end
625
-
626
- # Issue #180 - https://github.com/JEG2/highline/issues/180
627
- def test_menu_prompt
628
- @input << "2\n1\n"
629
- @input.rewind
630
-
631
- selected = @terminal.choose do |menu|
632
- menu.responses[:ask_on_error] = "> "
633
- menu.prompt = "> "
634
- menu.choice :exit, "Exit cube editor"
635
- end
636
-
637
- prompt = "> "
638
- first_asking = "1. exit\n"
639
- error_message = "You must choose one of [1, exit].\n"
640
- complete_interaction = first_asking + prompt + error_message + prompt # Same prompt when repeating question
641
-
642
- assert_equal complete_interaction, @output.string
643
- end
644
-
645
- def test_menu_gather_integer
646
- @input << "Sample1\nlast\n"
647
- @input.rewind
648
-
649
- selected = @terminal.choose do |menu|
650
- menu.gather = 2
651
- menu.choice "Sample1"
652
- menu.choice "Sample2"
653
- menu.choice "last"
654
- end
655
- assert_equal(["Sample1", "last"], selected)
656
-
657
- assert_equal("1. Sample1\n" +
658
- "2. Sample2\n" +
659
- "3. last\n" +
660
- "? 1. Sample1\n" +
661
- "2. Sample2\n" +
662
- "3. last\n" +
663
- "? ", @output.string)
664
- end
665
-
666
- def test_menu_gather_string
667
- @input << "Sample1\nlast\n"
668
- @input.rewind
669
-
670
- selected = @terminal.choose do |menu|
671
- menu.gather = :last
672
- menu.choice "Sample1"
673
- menu.choice "Sample2"
674
- menu.choice :last
675
- end
676
- assert_equal(["Sample1"], selected)
677
-
678
- assert_equal("1. Sample1\n" +
679
- "2. Sample2\n" +
680
- "3. last\n" +
681
- "? 1. Sample1\n" +
682
- "2. Sample2\n" +
683
- "3. last\n" +
684
- "? ", @output.string)
685
- end
686
-
687
- def test_menu_gather_symbol
688
- @input << "Sample1\nlast\n"
689
- @input.rewind
690
-
691
- selected = @terminal.choose do |menu|
692
- menu.gather = "last"
693
- menu.choice "Sample1"
694
- menu.choice "Sample2"
695
- menu.choice "last"
696
- end
697
- assert_equal(["Sample1"], selected)
698
-
699
- assert_equal("1. Sample1\n" +
700
- "2. Sample2\n" +
701
- "3. last\n" +
702
- "? 1. Sample1\n" +
703
- "2. Sample2\n" +
704
- "3. last\n" +
705
- "? ", @output.string)
706
- end
707
-
708
- def test_menu_gather_regexp
709
- @input << "Sample1\nlast\n"
710
- @input.rewind
711
-
712
- selected = @terminal.choose do |menu|
713
- menu.gather = /la/
714
- menu.choice "Sample1"
715
- menu.choice "Sample2"
716
- menu.choice "last"
717
- end
718
- assert_equal(["Sample1"], selected)
719
-
720
- assert_equal("1. Sample1\n" +
721
- "2. Sample2\n" +
722
- "3. last\n" +
723
- "? 1. Sample1\n" +
724
- "2. Sample2\n" +
725
- "3. last\n" +
726
- "? ", @output.string)
727
- end
728
-
729
- def test_menu_gather_hash
730
- @input << "Sample1\n3\n"
731
- @input.rewind
732
-
733
- selected = @terminal.choose do |menu|
734
- menu.gather = { "First" => true, second: true }
735
- menu.choice "Sample1"
736
- menu.choice "Sample2"
737
- menu.choice "last"
738
- end
739
- assert_equal({ "First" => "Sample1", second: "last" }, selected)
740
-
741
- assert_equal("1. Sample1\n" +
742
- "2. Sample2\n" +
743
- "3. last\n" +
744
- "? 1. Sample1\n" +
745
- "2. Sample2\n" +
746
- "3. last\n" +
747
- "? ", @output.string)
748
- end
749
- end