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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
# coding: utf-8
# tc_menu.rb
#
# Created by Gregory Thomas Brown on 2005-05-10.
# Copyright 2005. All rights reserved.
#
# This is Free Software. See LICENSE and COPYING for details.
require "minitest/autorun"
require "highline"
require "stringio"
class TestMenu < Minitest::Test
def setup
HighLine.reset
@input = StringIO.new
@output = StringIO.new
@terminal = HighLine.new(@input, @output)
end
def test_choices
@input << "2\n"
@input.rewind
output = @terminal.choose do |menu|
menu.choices("Sample1", "Sample2", "Sample3")
end
assert_equal("Sample2", output)
end
def test_flow
@input << "Sample1\n"
@input.rewind
@terminal.choose do |menu|
# Default: menu.flow = :rows
menu.choice "Sample1"
menu.choice "Sample2"
menu.choice "Sample3"
end
assert_equal("1. Sample1\n2. Sample2\n3. Sample3\n? ", @output.string)
@output.truncate(@output.rewind)
@input.rewind
@terminal.choose do |menu|
menu.flow = :columns_across
menu.choice "Sample1"
menu.choice "Sample2"
menu.choice "Sample3"
end
assert_equal("1. Sample1 2. Sample2 3. Sample3\n? ", @output.string)
@output.truncate(@output.rewind)
@input.rewind
@terminal.choose do |menu|
menu.flow = :inline
menu.index = :none
menu.choice "Sample1"
menu.choice "Sample2"
menu.choice "Sample3"
end
assert_equal("Sample1, Sample2 or Sample3? ", @output.string)
end
def test_unicode_flow
@input << "1\n"
@input.rewind
@terminal.choose do |menu|
# Default: menu.flow = :rows
menu.choice "Unicode right single quotation mark: ’"
end
assert_equal("1. Unicode right single quotation mark: ’\n? ".encode(@output.external_encoding, { :undef => :replace }), @output.string)
end
def test_help
@input << "help\nhelp load\nhelp rules\nhelp missing\n"
@input.rewind
4.times do
@terminal.choose do |menu|
menu.shell = true
menu.choice(:load, "Load a file.")
menu.choice(:save, "Save data in file.")
menu.choice(:quit, "Exit program.")
menu.help("rules", "The rules of this system are as follows...")
end
end
assert_equal( "1. load\n2. save\n3. quit\n4. help\n? " +
"This command will display helpful messages about " +
"functionality, like this one. To see the help for a " +
"specific topic enter:\n" +
"\thelp [TOPIC]\n" +
"Try asking for help on any of the following:\n" +
"\nload quit rules save \n" +
"1. load\n2. save\n3. quit\n4. help\n? " +
"= load\n\n" +
"Load a file.\n" +
"1. load\n2. save\n3. quit\n4. help\n? " +
"= rules\n\n" +
"The rules of this system are as follows...\n" +
"1. load\n2. save\n3. quit\n4. help\n? " +
"= missing\n\n" +
"There's no help for that topic.\n", @output.string )
end
def test_index
@input << "Sample1\n"
@input.rewind
@terminal.choose do |menu|
# Default: menu.index = :number
menu.choice "Sample1"
menu.choice "Sample2"
menu.choice "Sample3"
end
assert_equal("1. Sample1\n2. Sample2\n3. Sample3\n? ", @output.string)
@output.truncate(@output.rewind)
@input.rewind
@terminal.choose do |menu|
menu.index = :letter
menu.index_suffix = ") "
menu.choice "Sample1"
menu.choice "Sample2"
menu.choice "Sample3"
end
assert_equal("a) Sample1\nb) Sample2\nc) Sample3\n? ", @output.string)
@output.truncate(@output.rewind)
@input.rewind
@terminal.choose do |menu|
menu.index = :none
menu.choice "Sample1"
menu.choice "Sample2"
menu.choice "Sample3"
end
assert_equal("Sample1\nSample2\nSample3\n? ", @output.string)
@output.truncate(@output.rewind)
@input.rewind
@terminal.choose do |menu|
menu.index = "*"
menu.choice "Sample1"
menu.choice "Sample2"
menu.choice "Sample3"
end
assert_equal("* Sample1\n* Sample2\n* Sample3\n? ", @output.string)
end
def test_layouts
@input << "save\n"
@input.rewind
@terminal.choose(:load, :save, :quit) # Default: layout = :list
assert_equal("1. load\n2. save\n3. quit\n? ", @output.string)
@input.rewind
@output.truncate(@output.rewind)
@terminal.choose(:load, :save, :quit) do |menu|
menu.header = "File Menu"
end
assert_equal( "File Menu:\n" +
"1. load\n2. save\n3. quit\n? ", @output.string )
@input.rewind
@output.truncate(@output.rewind)
@terminal.choose(:load, :save, :quit) do |menu|
menu.layout = :one_line
menu.header = "File Menu"
menu.prompt = "Operation? "
end
assert_equal( "File Menu: Operation? " +
"(load, save or quit) ", @output.string )
@input.rewind
@output.truncate(@output.rewind)
@terminal.choose(:load, :save, :quit) do |menu|
menu.layout = :menu_only
end
assert_equal("load, save or quit? ", @output.string)
@input.rewind
@output.truncate(@output.rewind)
@terminal.choose(:load, :save, :quit) do |menu|
menu.layout = '<%= list(@menu) %>File Menu: '
end
assert_equal("1. load\n2. save\n3. quit\nFile Menu: ", @output.string)
end
def test_list_option
@input << "l\n"
@input.rewind
@terminal.choose(:load, :save, :quit) do |menu|
menu.layout = :menu_only
menu.list_option = ", or "
end
assert_equal("load, save, or quit? ", @output.string)
end
def test_nil_on_handled
@input << "3\n3\n2\n"
@input.rewind
# Shows that by default proc results are returned.
output = @terminal.choose do |menu|
menu.choice "Sample1" do "output1" end
menu.choice "Sample2" do "output2" end
menu.choice "Sample3" do "output3" end
end
assert_equal("output3", output)
#
# Shows that they can be replaced with +nil+ by setting
# _nil_on_handled to +true+.
#
output = @terminal.choose do |menu|
menu.nil_on_handled = true
menu.choice "Sample1" do "output1" end
menu.choice "Sample2" do "output2" end
menu.choice "Sample3" do "output3" end
end
assert_equal(nil, output)
# Shows that a menu item without a proc will be returned no matter what.
output = @terminal.choose do |menu|
menu.choice "Sample1"
menu.choice "Sample2"
menu.choice "Sample3"
end
assert_equal("Sample2", output)
end
def test_passed_command
@input << "q\n"
@input.rewind
selected = nil
@terminal.choose do |menu|
menu.choices(:load, :save, :quit) { |command| selected = command }
end
assert_equal(:quit, selected)
end
def test_question_options
@input << "save\n"
@input.rewind
answer = @terminal.choose(:Load, :Save, :Quit) do |menu|
menu.case = :capitalize
end
assert_equal(:Save, answer)
@input.rewind
answer = @terminal.choose(:Load, :Save, :Quit) do |menu|
menu.case = :capitalize
menu.character = :getc
end
assert_equal(:Save, answer)
assert_equal(?a, @input.getc)
end
def test_select_by
@input << "Sample1\n2\n"
@input.rewind
selected = @terminal.choose do |menu|
menu.choice "Sample1"
menu.choice "Sample2"
menu.choice "Sample3"
end
assert_equal("Sample1", selected)
@input.rewind
selected = @terminal.choose do |menu|
menu.select_by = :index
menu.choice "Sample1"
menu.choice "Sample2"
menu.choice "Sample3"
end
assert_equal("Sample2", selected)
@input.rewind
selected = @terminal.choose do |menu|
menu.select_by = :name
menu.choice "Sample1"
menu.choice "Sample2"
menu.choice "Sample3"
end
assert_equal("Sample1", selected)
end
def test_hidden
@input << "Hidden\n4\n"
@input.rewind
selected = @terminal.choose do |menu|
menu.choice "Sample1"
menu.choice "Sample2"
menu.choice "Sample3"
menu.hidden "Hidden!"
end
assert_equal("Hidden!", selected)
assert_equal("1. Sample1\n2. Sample2\n3. Sample3\n? ", @output.string)
@input.rewind
selected = @terminal.choose do |menu|
menu.select_by = :index
menu.choice "Sample1"
menu.choice "Sample2"
menu.choice "Sample3"
menu.hidden "Hidden!"
end
assert_equal("Hidden!", selected)
@input.rewind
selected = @terminal.choose do |menu|
menu.select_by = :name
menu.choice "Sample1"
menu.choice "Sample2"
menu.choice "Sample3"
menu.hidden "Hidden!"
end
assert_equal("Hidden!", selected)
@input.rewind
end
def test_select_by_letter
@input << "b\n"
@input.rewind
selected = @terminal.choose do |menu|
menu.index = :letter
menu.choice :save
menu.choice :load
menu.choice :quit
end
assert_equal(:load, selected)
end
def test_shell
@input << "save --some-option my_file.txt\n"
@input.rewind
selected = nil
options = nil
answer = @terminal.choose do |menu|
menu.choices(:load, :quit)
menu.choice(:save) do |command, details|
selected = command
options = details
"Saved!"
end
menu.shell = true
end
assert_equal("Saved!", answer)
assert_equal(:save, selected)
assert_equal("--some-option my_file.txt", options)
end
def test_simple_menu_shortcut
@input << "3\n"
@input.rewind
selected = @terminal.choose(:save, :load, :quit)
assert_equal(:quit, selected)
end
def test_symbols
@input << "3\n"
@input.rewind
selected = @terminal.choose do |menu|
menu.choices(:save, :load, :quit)
end
assert_equal(:quit, selected)
end
def test_paged_print_infinite_loop_bug
@terminal.page_at = 5
# Will page twice, so start with two new lines
@input << "\n\n3\n"
@input.rewind
# Sadly this goes into an infinite loop without the fix to page_print
selected = @terminal.choose(* 1..10)
assert_equal(selected, 3)
end
def test_cancel_paging
# Tests that paging can be cancelled halfway through
@terminal.page_at = 5
# Will page twice, so stop after first page and make choice 3
@input << "q3\n"
@input.rewind
selected = @terminal.choose(* 1..10)
assert_equal(selected, 3)
# Make sure paging message appeared
assert( @output.string.index('press enter/return to continue or q to stop'),
"Paging message did not appear." )
# Make sure it only appeared once
assert( @output.string !~ /q to stop.*q to stop/m,
"Paging message appeared more than once." )
end
end
|