summaryrefslogtreecommitdiff
path: root/test/test_menu.rb
blob: bf4bcb5df4e3b190460350d85b1b22780e473c56 (plain)
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
#!/usr/bin/env ruby
# 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 "test_helper"

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_text_override_index_selects_name
    @input << "1\n"
    @input.rewind

    selected = @terminal.choose do |menu|
      menu.choice("Sample1", nil, "Sample2")
      menu.choice("Sample2", nil, "Sample1")
    end
    assert_equal(selected, "Sample1")
    assert_equal("1. Sample2\n" +
                 "2. Sample1\n" +
                 "?  ", @output.string)
  end

  def test_text_override_selections_matches_name
    @input << "Sample2\n"
    @input.rewind

    selected = @terminal.choose do |menu|
      menu.choice("Sample1", nil, "Sample2")
      menu.choice("Sample2", nil, "Sample1")
    end
    assert_equal(selected, "Sample2")
    assert_equal("1. Sample2\n" +
                 "2. Sample1\n" +
                 "?  ", @output.string)
  end

  def test_menu_add_item_index_selects_name
    @input << "1\n"
    @input.rewind

    selected = @terminal.choose do |menu|
      menu.add_item(HighLine::Menu::MenuItem.new("Sample1", text: "Sample2"))
      menu.add_item(HighLine::Menu::MenuItem.new("Sample2", text: "Sample1"))
    end
    assert_equal(selected, "Sample1")
    assert_equal("1. Sample2\n" +
                 "2. Sample1\n" +
                 "?  ", @output.string)
  end

  def test_menu_add_item_selections_matches_name
    @input << "Sample2\n"
    @input.rewind

    selected = @terminal.choose do |menu|
      menu.add_item(HighLine::Menu::MenuItem.new("Sample1", text: "Sample2"))
      menu.add_item(HighLine::Menu::MenuItem.new("Sample2", text: "Sample1"))
    end
    assert_equal(selected, "Sample2")
    assert_equal("1. Sample2\n" +
                 "2. Sample1\n" +
                 "?  ", @output.string)
  end

  def test_menu_build_item
    @input << "Sample2\n"
    @input.rewind

    selected = @terminal.choose do |menu|
      menu.add_item(menu.build_item("Sample1", text: "Sample2"))
      menu.add_item(menu.build_item("Sample2", text: "Sample1"))
    end
    assert_equal(selected, "Sample2")
    assert_equal("1. Sample2\n" +
                 "2. Sample1\n" +
                 "?  ", @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_index_with_color
    index_color = :rgb_77bbff

    @input << "Sample1\n"
    @input.rewind

    @terminal.choose do |menu|
      # Default:  menu.index = :number
      menu.index_color = index_color

      menu.choice "Sample1"
      menu.choice "Sample2"
      menu.choice "Sample3"
    end

    assert_equal(
      HighLine.color("1. ", index_color) + "Sample1\n" +
      HighLine.color("2. ", index_color) + "Sample2\n" +
      HighLine.color("3. ", index_color) + "Sample3\n" +
      "?  ",
      @output.string
    )

    @output.truncate(@output.rewind)
    @input.rewind

    @terminal.choose do |menu|
      menu.index        = :letter
      menu.index_suffix = ") "
      menu.index_color = index_color

      menu.choice "Sample1"
      menu.choice "Sample2"
      menu.choice "Sample3"
    end

    assert_equal(
      HighLine.color("a) ", index_color) + "Sample1\n" +
      HighLine.color("b) ", index_color) + "Sample2\n" +
      HighLine.color("c) ", index_color) + "Sample3\n?  ",
      @output.string
    )

    @output.truncate(@output.rewind)
    @input.rewind

    @terminal.choose do |menu|
      menu.index = :none
      menu.index_color = index_color

      menu.choice "Sample1"
      menu.choice "Sample2"
      menu.choice "Sample3"
    end

    assert_equal(
      HighLine.color("Sample1", index_color) + "\n" +
      HighLine.color("Sample2", index_color) + "\n" +
      HighLine.color("Sample3", index_color) + "\n?  ",
      @output.string
    )

    @output.truncate(@output.rewind)
    @input.rewind

    @terminal.choose do |menu|
      menu.index = "*"
      menu.index_color = index_color

      menu.choice "Sample1"
      menu.choice "Sample2"
      menu.choice "Sample3"
    end

    colored_asterix = HighLine.color("* ", index_color)
    assert_equal(
      "#{colored_asterix}Sample1\n" +
      "#{colored_asterix}Sample2\n" +
      "#{colored_asterix}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

  def test_autocomplete_prompt
    @input << "lisp\nRuby\n"
    @input.rewind

    answer = @terminal.choose do |menu|
      menu.choice(:Perl)
      menu.choice(:Python)
      menu.choice(:Ruby)
      menu.prompt = "What is your favorite programming language?  "
    end
    languages = [:Perl, :Python, :Ruby]
    assert_equal("1. Perl\n" +
                 "2. Python\n" +
                 "3. Ruby\n" +
                 "What is your favorite programming language?  " +
                  "You must choose one of [1, 2, 3, Perl, Python, Ruby].\n" +
                  "?  ", @output.string )
  end

  # Issue #180 - https://github.com/JEG2/highline/issues/180
  def test_menu_prompt
    @input << "2\n1\n"
    @input.rewind

    selected = @terminal.choose do |menu|
      menu.responses[:ask_on_error] = "> "
      menu.prompt = "> "
      menu.choice :exit, "Exit cube editor"
    end

    prompt = "> "
    first_asking = "1. exit\n"
    error_message = "You must choose one of [1, exit].\n"
    complete_interaction = first_asking + prompt + error_message + prompt # Same prompt when repeating question

    assert_equal complete_interaction, @output.string
  end

  def test_menu_gather_integer
    @input << "Sample1\nlast\n"
    @input.rewind

    selected = @terminal.choose do |menu|
      menu.gather = 2
      menu.choice "Sample1"
      menu.choice "Sample2"
      menu.choice "last"
    end
    assert_equal(["Sample1", "last"], selected)

    assert_equal("1. Sample1\n" +
                 "2. Sample2\n" +
                 "3. last\n" +
                 "?  1. Sample1\n" +
                 "2. Sample2\n" +
                 "3. last\n" +
                 "?  ", @output.string)
  end

  def test_menu_gather_string
    @input << "Sample1\nlast\n"
    @input.rewind

    selected = @terminal.choose do |menu|
      menu.gather = :last
      menu.choice "Sample1"
      menu.choice "Sample2"
      menu.choice :last
    end
    assert_equal(["Sample1"], selected)

    assert_equal("1. Sample1\n" +
                 "2. Sample2\n" +
                 "3. last\n" +
                 "?  1. Sample1\n" +
                 "2. Sample2\n" +
                 "3. last\n" +
                 "?  ", @output.string)
  end

  def test_menu_gather_symbol
    @input << "Sample1\nlast\n"
    @input.rewind

    selected = @terminal.choose do |menu|
      menu.gather = "last"
      menu.choice "Sample1"
      menu.choice "Sample2"
      menu.choice "last"
    end
    assert_equal(["Sample1"], selected)

    assert_equal("1. Sample1\n" +
                 "2. Sample2\n" +
                 "3. last\n" +
                 "?  1. Sample1\n" +
                 "2. Sample2\n" +
                 "3. last\n" +
                 "?  ", @output.string)
  end

  def test_menu_gather_regexp
    @input << "Sample1\nlast\n"
    @input.rewind

    selected = @terminal.choose do |menu|
      menu.gather = /la/
      menu.choice "Sample1"
      menu.choice "Sample2"
      menu.choice "last"
    end
    assert_equal(["Sample1"], selected)

    assert_equal("1. Sample1\n" +
                 "2. Sample2\n" +
                 "3. last\n" +
                 "?  1. Sample1\n" +
                 "2. Sample2\n" +
                 "3. last\n" +
                 "?  ", @output.string)
  end

  def test_menu_gather_hash
    @input << "Sample1\n3\n"
    @input.rewind

    selected = @terminal.choose do |menu|
      menu.gather = { "First" => true, second: true }
      menu.choice "Sample1"
      menu.choice "Sample2"
      menu.choice "last"
    end
    assert_equal({ "First" => "Sample1", second: "last" }, selected)

    assert_equal("1. Sample1\n" +
                 "2. Sample2\n" +
                 "3. last\n" +
                 "?  1. Sample1\n" +
                 "2. Sample2\n" +
                 "3. last\n" +
                 "?  ", @output.string)
  end
end