benry-cmdapp 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/help_test.rb ADDED
@@ -0,0 +1,755 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'oktest'
4
+
5
+ require 'benry/cmdapp'
6
+ require_relative './shared'
7
+
8
+
9
+ Oktest.scope do
10
+
11
+
12
+ topic Benry::CmdApp::HelpBuilder do
13
+
14
+
15
+ before do
16
+ @builder = Benry::CmdApp::HelpBuilder.new()
17
+ end
18
+
19
+
20
+ topic '#build_section()' do
21
+
22
+ spec "[!cfijh] includes section title and content if specified by config." do
23
+ msg = @builder.build_section("Example", " $ echo 'Hello, world!'")
24
+ ok {msg} == <<"END"
25
+ \e[34mExample:\e[0m
26
+ $ echo 'Hello, world!'
27
+ END
28
+ end
29
+
30
+ spec "[!09jzn] third argument can be nil." do
31
+ msg = @builder.build_section("Example", " $ echo 'Hello, world!'", "(see https://...)")
32
+ ok {msg} == <<"END"
33
+ \e[34mExample:\e[0m (see https://...)
34
+ $ echo 'Hello, world!'
35
+ END
36
+ end
37
+
38
+ end
39
+
40
+
41
+ end
42
+
43
+
44
+ topic Benry::CmdApp::ActionHelpBuilder do
45
+ include ActionMetadataTestingHelper
46
+
47
+
48
+ topic '#build_help_message()' do
49
+
50
+ spec "[!pqoup] adds detail text into help if specified." do
51
+ expected = <<END
52
+ testapp halo1 -- greeting
53
+
54
+ See: https://example.com/doc.html
55
+
56
+ Usage:
57
+ $ testapp halo1 [<options>] [<user>]
58
+
59
+ Options:
60
+ -l, --lang=<en|fr|it> : language
61
+ END
62
+ detail = "See: https://example.com/doc.html"
63
+ [detail, detail+"\n"].each do |detail_|
64
+ metadata = new_metadata(new_schema(), detail: detail)
65
+ msg = metadata.help_message("testapp")
66
+ msg = uncolorize(msg)
67
+ ok {msg} == expected
68
+ end
69
+ end
70
+
71
+ spec "[!zbc4y] adds '[<options>]' into 'Usage:' section only when any options exist." do
72
+ schema = new_schema(lang: false)
73
+ msg = new_metadata(schema).help_message("testapp")
74
+ msg = uncolorize(msg)
75
+ ok {msg}.include?("Usage:\n" +
76
+ " $ testapp halo1 [<user>]\n")
77
+ #
78
+ schema = new_schema(lang: true)
79
+ msg = new_metadata(schema).help_message("testapp")
80
+ msg = uncolorize(msg)
81
+ ok {msg}.include?("Usage:\n" +
82
+ " $ testapp halo1 [<options>] [<user>]\n")
83
+ end
84
+
85
+ spec "[!8b02e] ignores '[<options>]' in 'Usage:' when only hidden options speicified." do
86
+ schema = new_schema(lang: false)
87
+ schema.add(:_lang, "-l, --lang=<en|fr|it>", "language")
88
+ msg = new_metadata(schema).help_message("testapp")
89
+ msg = uncolorize(msg)
90
+ ok {msg} =~ /^ \$ testapp halo1 \[<user>\]\n/
91
+ ok {msg} =~ /^Usage:\n \$ testapp halo1 \[<user>\]$/
92
+ end
93
+
94
+ spec "[!ou3md] not add extra whiespace when no arguments of command." do
95
+ schema = new_schema(lang: true)
96
+ msg = new_metadata(schema, :halo3).help_message("testapp")
97
+ msg = uncolorize(msg)
98
+ ok {msg} =~ /^ \$ testapp halo3 \[<options>\]\n/
99
+ ok {msg} =~ /^Usage:\n \$ testapp halo3 \[<options>\]$/
100
+ end
101
+
102
+ spec "[!g2ju5] adds 'Options:' section." do
103
+ schema = new_schema(lang: true)
104
+ msg = new_metadata(schema).help_message("testapp")
105
+ msg = uncolorize(msg)
106
+ ok {msg}.include?("Options:\n" +
107
+ " -l, --lang=<en|fr|it> : language\n")
108
+ end
109
+
110
+ spec "[!pvu56] ignores 'Options:' section when no options exist." do
111
+ schema = new_schema(lang: false)
112
+ msg = new_metadata(schema).help_message("testapp")
113
+ msg = uncolorize(msg)
114
+ ok {msg}.NOT.include?("Options:\n")
115
+ end
116
+
117
+ spec "[!hghuj] ignores 'Options:' section when only hidden options speicified." do
118
+ schema = new_schema(lang: false)
119
+ schema.add(:_lang, "-l, --lang=<en|fr|it>", "language") # hidden option
120
+ msg = new_metadata(schema).help_message("testapp")
121
+ msg = uncolorize(msg)
122
+ ok {msg}.NOT.include?("Options:\n")
123
+ end
124
+
125
+ spec "[!vqqq1] hidden option should be shown in weak format." do
126
+ schema = new_schema(lang: false)
127
+ schema.add(:file , "-f, --file=<file>", "filename")
128
+ schema.add(:_lang, "-l, --lang=<lang>", "language") # hidden option
129
+ msg = new_metadata(schema).help_message("testapp", true)
130
+ ok {msg}.end_with?(<<"END")
131
+ \e[34mOptions:\e[0m
132
+ \e[1m-f, --file=<file> \e[0m : filename
133
+ \e[1m\e[2m-l, --lang=<lang>\e[0m \e[0m : language
134
+ END
135
+ end
136
+
137
+ spec "[!dukm7] includes detailed description of option." do
138
+ schema = new_schema(lang: false)
139
+ schema.add(:lang, "-l, --lang=<lang>", "language",
140
+ detail: "detailed description1\ndetailed description2")
141
+ msg = new_metadata(schema).help_message("testapp")
142
+ msg = uncolorize(msg)
143
+ ok {msg}.end_with?(<<"END")
144
+ Options:
145
+ -l, --lang=<lang> : language
146
+ detailed description1
147
+ detailed description2
148
+ END
149
+ end
150
+
151
+ spec "[!0p2gt] adds postamble text if specified." do
152
+ postamble = "Tips: `testapp -h <action>` print help message of action."
153
+ schema = new_schema(lang: false)
154
+ ameta = new_metadata(schema, postamble: postamble)
155
+ msg = ameta.help_message("testapp")
156
+ msg = uncolorize(msg)
157
+ ok {msg} == <<END
158
+ testapp halo1 -- greeting
159
+
160
+ Usage:
161
+ $ testapp halo1 [<user>]
162
+
163
+ Tips: `testapp -h <action>` print help message of action.
164
+ END
165
+ end
166
+
167
+ spec "[!v5567] adds '\n' at end of preamble text if it doesn't end with '\n'." do
168
+ postamble = "END"
169
+ schema = new_schema(lang: false)
170
+ ameta = new_metadata(schema, postamble: postamble)
171
+ msg = ameta.help_message("testapp")
172
+ ok {msg}.end_with?("\nEND\n")
173
+ end
174
+
175
+ spec "[!x0z89] required arg is represented as '<arg>'." do
176
+ schema = new_schema(lang: false)
177
+ metadata = Benry::CmdApp::ActionMetadata.new("args1", MetadataTestAction, :args1, "", schema)
178
+ msg = metadata.help_message("testapp")
179
+ msg = uncolorize(msg)
180
+ ok {msg} =~ /^ \$ testapp args1 <aa> <bb>$/
181
+ end
182
+
183
+ spec "[!md7ly] optional arg is represented as '[<arg>]'." do
184
+ schema = new_schema(lang: false)
185
+ metadata = Benry::CmdApp::ActionMetadata.new("args2", MetadataTestAction, :args2, "", schema)
186
+ msg = without_tty { metadata.help_message("testapp") }
187
+ msg = uncolorize(msg)
188
+ ok {msg} =~ /^ \$ testapp args2 <aa> \[<bb> \[<cc>\]\]$/
189
+ end
190
+
191
+ spec "[!xugkz] variable args are represented as '[<arg>...]'." do
192
+ schema = new_schema(lang: false)
193
+ metadata = Benry::CmdApp::ActionMetadata.new("args3", MetadataTestAction, :args3, "", schema)
194
+ msg = metadata.help_message("testapp")
195
+ msg = uncolorize(msg)
196
+ ok {msg} =~ /^ \$ testapp args3 <aa> \[<bb> \[<cc> \[<dd>...\]\]\]$/
197
+ end
198
+
199
+ spec "[!eou4h] converts arg name 'xx_or_yy_or_zz' into 'xx|yy|zz'." do
200
+ schema = new_schema(lang: false)
201
+ metadata = Benry::CmdApp::ActionMetadata.new("args4", MetadataTestAction, :args4, "", schema)
202
+ msg = metadata.help_message("testapp")
203
+ msg = uncolorize(msg)
204
+ ok {msg} =~ /^ \$ testapp args4 <xx\|yy\|zz>$/
205
+ end
206
+
207
+ spec "[!naoft] converts arg name '_xx_yy_zz' into '_xx-yy-zz'." do
208
+ schema = new_schema(lang: false)
209
+ metadata = Benry::CmdApp::ActionMetadata.new("args5", MetadataTestAction, :args5, "", schema)
210
+ msg = metadata.help_message("testapp")
211
+ msg = uncolorize(msg)
212
+ ok {msg} =~ /^ \$ testapp args5 <_xx-yy-zz>$/
213
+ end
214
+
215
+ end
216
+
217
+
218
+ end
219
+
220
+
221
+ topic Benry::CmdApp::AppHelpBuilder do
222
+ include CommonTestingHelper
223
+
224
+ before do
225
+ @config = Benry::CmdApp::Config.new("test app", "1.0.0").tap do |config|
226
+ config.app_name = "TestApp"
227
+ config.app_command = "testapp"
228
+ config.option_all = true
229
+ config.option_debug = true
230
+ config.default_action = nil
231
+ end
232
+ @schema = Benry::CmdApp::AppOptionSchema.new(@config)
233
+ @builder = Benry::CmdApp::AppHelpBuilder.new(@config, @schema)
234
+ end
235
+
236
+ topic '#build_help_message()' do
237
+
238
+ class HelpMessageTest < Benry::CmdApp::ActionScope
239
+ @action.("greeting #1")
240
+ def yo_yo()
241
+ end
242
+ @action.("greeting #2")
243
+ def ya__ya()
244
+ end
245
+ @action.("greeting #3")
246
+ def _aha()
247
+ end
248
+ @action.("greeting #4")
249
+ def ya___mada()
250
+ end
251
+ end
252
+
253
+ Benry::CmdApp.action_alias("yes", "yo-yo")
254
+
255
+ before do
256
+ clear_index_except(HelpMessageTest)
257
+ end
258
+
259
+ after do
260
+ restore_index()
261
+ end
262
+
263
+ expected_color = <<"END"
264
+ \e[1mTestApp\e[0m (1.0.0) -- test app
265
+
266
+ \e[34mUsage:\e[0m
267
+ $ \e[1mtestapp\e[0m [<options>] [<action> [<arguments>...]]
268
+
269
+ \e[34mOptions:\e[0m
270
+ \e[1m-h, --help \e[0m : print help message
271
+ \e[1m-V, --version \e[0m : print version
272
+ \e[1m-a, --all \e[0m : list all actions including private (hidden) ones
273
+ \e[1m-D, --debug \e[0m : debug mode (set $DEBUG_MODE to true)
274
+
275
+ \e[34mActions:\e[0m
276
+ \e[1mya:ya \e[0m : greeting #2
277
+ \e[1myes \e[0m : alias of 'yo-yo' action
278
+ \e[1myo-yo \e[0m : greeting #1
279
+ END
280
+
281
+ expected_mono = <<'END'
282
+ TestApp (1.0.0) -- test app
283
+
284
+ Usage:
285
+ $ testapp [<options>] [<action> [<arguments>...]]
286
+
287
+ Options:
288
+ -h, --help : print help message
289
+ -V, --version : print version
290
+ -a, --all : list all actions including private (hidden) ones
291
+ -D, --debug : debug mode (set $DEBUG_MODE to true)
292
+
293
+ Actions:
294
+ ya:ya : greeting #2
295
+ yes : alias of 'yo-yo' action
296
+ yo-yo : greeting #1
297
+ END
298
+
299
+ spec "[!rvpdb] returns help message." do
300
+ msg = @builder.build_help_message()
301
+ msg_color = msg
302
+ msg_mono = uncolorize(msg)
303
+ ok {msg_mono} == expected_mono
304
+ ok {msg_color} == expected_color
305
+ end
306
+
307
+ def _with_color_mode(val, &b)
308
+ bkup = $COLOR_MODE
309
+ $COLOR_MODE = val
310
+ yield
311
+ ensure
312
+ $COLOR_MODE = bkup
313
+ end
314
+
315
+ spec "[!34y8e] includes application name specified by config." do
316
+ @config.app_name = "MyGreatApp"
317
+ msg = @builder.build_help_message()
318
+ msg = uncolorize(msg)
319
+ ok {msg} =~ /^MyGreatApp \(1\.0\.0\) -- test app$/
320
+ end
321
+
322
+ spec "[!744lx] includes application description specified by config." do
323
+ @config.app_desc = "my great app"
324
+ msg = @builder.build_help_message()
325
+ msg = uncolorize(msg)
326
+ ok {msg} =~ /^TestApp \(1\.0\.0\) -- my great app$/
327
+ end
328
+
329
+ spec "[!d1xz4] includes version number if specified by config." do
330
+ @config.app_version = "1.2.3"
331
+ msg = @builder.build_help_message()
332
+ msg = uncolorize(msg)
333
+ ok {msg} =~ /^TestApp \(1\.2\.3\) -- test app$/
334
+ #
335
+ @config.app_version = nil
336
+ msg = @builder.build_help_message()
337
+ msg = uncolorize(msg)
338
+ ok {msg} =~ /^TestApp -- test app$/
339
+ end
340
+
341
+ spec "[!775jb] includes detail text if specified by config." do
342
+ @config.app_detail = "See https://example.com/doc.html"
343
+ msg = @builder.build_help_message()
344
+ msg = uncolorize(msg)
345
+ ok {msg}.start_with?(<<END)
346
+ TestApp (1.0.0) -- test app
347
+
348
+ See https://example.com/doc.html
349
+
350
+ Usage:
351
+ END
352
+ #
353
+ @config.app_detail = nil
354
+ msg = @builder.build_help_message()
355
+ msg = uncolorize(msg)
356
+ ok {msg}.start_with?(<<END)
357
+ TestApp (1.0.0) -- test app
358
+
359
+ Usage:
360
+ END
361
+ end
362
+
363
+ spec "[!t3tbi] adds '\\n' before detail text only when app desc specified." do
364
+ @config.app_desc = nil
365
+ @config.app_detail = "See https://..."
366
+ msg = @builder.build_help_message()
367
+ msg = uncolorize(msg)
368
+ ok {msg}.start_with?(<<END)
369
+ See https://...
370
+
371
+ Usage:
372
+ $ testapp [<options>] [<action> [<arguments>...]]
373
+
374
+ END
375
+ end
376
+
377
+ spec "[!rvhzd] no preamble when neigher app desc nor detail specified." do
378
+ @config.app_desc = nil
379
+ @config.app_detail = nil
380
+ msg = @builder.build_help_message()
381
+ msg = uncolorize(msg)
382
+ ok {msg}.start_with?(<<END)
383
+ Usage:
384
+ $ testapp [<options>] [<action> [<arguments>...]]
385
+
386
+ END
387
+ end
388
+
389
+ spec "[!o176w] includes command name specified by config." do
390
+ @config.app_name = "GreatCommand"
391
+ @config.app_command = "greatcmd"
392
+ msg = @builder.build_help_message()
393
+ msg = uncolorize(msg)
394
+ ok {msg}.start_with?(<<END)
395
+ GreatCommand (1.0.0) -- test app
396
+
397
+ Usage:
398
+ $ greatcmd [<options>] [<action> [<arguments>...]]
399
+
400
+ Options:
401
+ END
402
+ end
403
+
404
+ spec "[!proa4] includes description of global options." do
405
+ @config.app_version = "1.0.0"
406
+ @config.option_debug = true
407
+ app = Benry::CmdApp::Application.new(@config)
408
+ msg = without_tty { app.help_message() }
409
+ msg = uncolorize(msg)
410
+ ok {msg}.include?(<<END)
411
+ Options:
412
+ -h, --help : print help message
413
+ -V, --version : print version
414
+ -a, --all : list all actions including private (hidden) ones
415
+ -D, --debug : debug mode (set $DEBUG_MODE to true)
416
+
417
+ Actions:
418
+ END
419
+ #
420
+ @config.app_version = nil
421
+ @config.option_debug = false
422
+ app = Benry::CmdApp::Application.new(@config)
423
+ msg = without_tty { app.help_message() }
424
+ msg = uncolorize(msg)
425
+ ok {msg}.include?(<<END)
426
+ Options:
427
+ -h, --help : print help message
428
+ -a, --all : list all actions including private (hidden) ones
429
+
430
+ Actions:
431
+ END
432
+ end
433
+
434
+ spec "[!in3kf] ignores private (hidden) options." do
435
+ @config.app_version = nil
436
+ @config.option_debug = false
437
+ app = Benry::CmdApp::Application.new(@config)
438
+ schema = app.instance_variable_get('@schema')
439
+ schema.add(:_log, "-L", "private option")
440
+ msg = app.help_message()
441
+ msg = uncolorize(msg)
442
+ ok {msg} !~ /^ -L /
443
+ ok {msg}.include?(<<END)
444
+ Options:
445
+ -h, --help : print help message
446
+ -a, --all : list all actions including private (hidden) ones
447
+
448
+ Actions:
449
+ END
450
+ end
451
+
452
+ spec "[!ywarr] not ignore private (hidden) options if 'all' flag is true." do
453
+ @config.app_version = nil
454
+ @config.option_debug = false
455
+ app = Benry::CmdApp::Application.new(@config)
456
+ schema = app.instance_variable_get('@schema')
457
+ schema.add(:_log, "-L", "private option")
458
+ msg = app.help_message(true)
459
+ msg = uncolorize(msg)
460
+ ok {msg} =~ /^ -L /
461
+ ok {msg}.include?(<<END)
462
+ Options:
463
+ -h, --help : print help message
464
+ -a, --all : list all actions including private (hidden) ones
465
+ -L : private option
466
+
467
+ Actions:
468
+ END
469
+ end
470
+
471
+ fixture :app3 do
472
+ config = Benry::CmdApp::Config.new(nil)
473
+ schema = Benry::CmdApp::AppOptionSchema.new(nil)
474
+ schema.add(:help, "-h, --help", "print help message")
475
+ app = Benry::CmdApp::Application.new(config, schema)
476
+ end
477
+
478
+ spec "[!p1tu9] prints option in weak format if option is hidden." do
479
+ |app3|
480
+ app3.schema.add(:_log, "-L", "private option") # !!!
481
+ msg = app3.help_message(true)
482
+ ok {msg}.include?(<<END)
483
+ \e[34mOptions:\e[0m
484
+ \e[1m-h, --help \e[0m : print help message
485
+ \e[1m\e[2m-L\e[0m \e[0m : private option
486
+
487
+ END
488
+ end
489
+
490
+ spec "[!bm71g] ignores 'Options:' section if no options exist." do
491
+ @config.option_help = false
492
+ @config.option_all = false
493
+ @config.app_version = nil
494
+ @config.option_debug = false
495
+ app = Benry::CmdApp::Application.new(@config)
496
+ schema = app.instance_variable_get('@schema')
497
+ schema.add(:_log, "-L", "private option")
498
+ msg = app.help_message()
499
+ msg = uncolorize(msg)
500
+ ok {msg} !~ /^Options:$/
501
+ end
502
+
503
+ spec "[!jat15] includes action names ordered by name." do
504
+ msg = @builder.build_help_message()
505
+ msg = uncolorize(msg)
506
+ ok {msg}.end_with?(<<'END')
507
+ Actions:
508
+ ya:ya : greeting #2
509
+ yes : alias of 'yo-yo' action
510
+ yo-yo : greeting #1
511
+ END
512
+ end
513
+
514
+ spec "[!df13s] includes default action name if specified by config." do
515
+ @config.default_action = nil
516
+ msg = @builder.build_help_message()
517
+ msg = uncolorize(msg)
518
+ ok {msg} =~ /^Actions:$/
519
+ #
520
+ @config.default_action = "yo-yo"
521
+ msg = @builder.build_help_message()
522
+ msg = uncolorize(msg)
523
+ ok {msg} =~ /^Actions: \(default: yo-yo\)$/
524
+ end
525
+
526
+ spec "[!b3l3m] not show private (hidden) action names in default." do
527
+ msg = @builder.build_help_message()
528
+ msg = uncolorize(msg)
529
+ ok {msg} !~ /^ _aha /
530
+ ok {msg} !~ /^ ya:_mada /
531
+ ok {msg}.end_with?(<<END)
532
+ Actions:
533
+ ya:ya : greeting #2
534
+ yes : alias of 'yo-yo' action
535
+ yo-yo : greeting #1
536
+ END
537
+ end
538
+
539
+ spec "[!yigf3] shows private (hidden) action names if 'all' flag is true." do
540
+ msg = @builder.build_help_message(true)
541
+ msg = uncolorize(msg)
542
+ ok {msg} =~ /^ _aha /
543
+ ok {msg} =~ /^ ya:_mada /
544
+ ok {msg}.end_with?(<<END)
545
+ Actions:
546
+ _aha : greeting #3
547
+ ya:_mada : greeting #4
548
+ ya:ya : greeting #2
549
+ yes : alias of 'yo-yo' action
550
+ yo-yo : greeting #1
551
+ END
552
+ end
553
+
554
+ spec "[!5d9mc] shows hidden action in weak format." do
555
+ HelpMessageTest.class_eval { private :yo_yo }
556
+ #
557
+ begin
558
+ msg = @builder.build_help_message(true)
559
+ ok {msg}.end_with?(<<END)
560
+ \e[34mActions:\e[0m
561
+ \e[1m_aha \e[0m : greeting #3
562
+ \e[1mya:_mada \e[0m : greeting #4
563
+ \e[1mya:ya \e[0m : greeting #2
564
+ \e[1m\e[2myes\e[0m \e[0m : alias of 'yo-yo' action
565
+ \e[1m\e[2myo-yo\e[0m \e[0m : greeting #1
566
+ END
567
+ ensure
568
+ HelpMessageTest.class_eval { public :yo_yo }
569
+ end
570
+ end
571
+
572
+ spec "[!awk3l] shows important action in strong format." do
573
+ with_important("yo-yo"=>true) do
574
+ msg = @builder.build_help_message()
575
+ ok {msg}.end_with?(<<END)
576
+ \e[34mActions:\e[0m
577
+ \e[1mya:ya \e[0m : greeting #2
578
+ \e[1m\e[4myes\e[0m \e[0m : alias of 'yo-yo' action
579
+ \e[1m\e[4myo-yo\e[0m \e[0m : greeting #1
580
+ END
581
+ end
582
+ end
583
+
584
+ spec "[!9k4dv] shows unimportant action in weak fomrat." do
585
+ with_important("yo-yo"=>false) do
586
+ msg = @builder.build_help_message()
587
+ ok {msg}.end_with?(<<END)
588
+ \e[34mActions:\e[0m
589
+ \e[1mya:ya \e[0m : greeting #2
590
+ \e[1m\e[2myes\e[0m \e[0m : alias of 'yo-yo' action
591
+ \e[1m\e[2myo-yo\e[0m \e[0m : greeting #1
592
+ END
593
+ end
594
+ end
595
+
596
+ spec "[!i04hh] includes postamble text if specified by config." do
597
+ @config.help_postamble = "Home:\n https://example.com/\n"
598
+ msg = @builder.build_help_message()
599
+ ok {msg}.end_with?(<<"END")
600
+ Home:
601
+ https://example.com/
602
+ END
603
+ end
604
+
605
+ spec "[!ckagw] adds '\\n' at end of postamble text if it doesn't end with '\\n'." do
606
+ @config.help_postamble = "END"
607
+ msg = @builder.build_help_message()
608
+ ok {msg}.end_with?("\nEND\n")
609
+ end
610
+
611
+ spec "[!oxpda] prints 'Aliases:' section only when 'config.help_aliases' is true." do
612
+ app = Benry::CmdApp::Application.new(@config)
613
+ #
614
+ @config.help_aliases = true
615
+ msg = app.help_message()
616
+ msg = Benry::CmdApp::Util.del_escape_seq(msg)
617
+ ok {msg}.end_with?(<<'END')
618
+ Actions:
619
+ ya:ya : greeting #2
620
+ yo-yo : greeting #1
621
+
622
+ Aliases:
623
+ yes : alias of 'yo-yo' action
624
+ END
625
+ #
626
+ @config.help_aliases = false
627
+ msg = app.help_message()
628
+ msg = Benry::CmdApp::Util.del_escape_seq(msg)
629
+ ok {msg}.end_with?(<<'END')
630
+ Actions:
631
+ ya:ya : greeting #2
632
+ yes : alias of 'yo-yo' action
633
+ yo-yo : greeting #1
634
+ END
635
+ end
636
+
637
+ spec "[!kqnxl] array of section may have two or three elements." do
638
+ @config.help_sections = [
639
+ ["Example", " $ echo foobar", "(see https://...)"],
640
+ ["Tips", " * foobar"],
641
+ ]
642
+ app = Benry::CmdApp::Application.new(@config)
643
+ msg = app.help_message()
644
+ ok {msg}.end_with?(<<"END")
645
+
646
+ \e[34mExample:\e[0m (see https://...)
647
+ $ echo foobar
648
+
649
+ \e[34mTips:\e[0m
650
+ * foobar
651
+ END
652
+ end
653
+
654
+ end
655
+
656
+
657
+ topic '#build_aliases()' do
658
+
659
+ class BuildAliasTest < Benry::CmdApp::ActionScope
660
+ prefix "help25"
661
+ @action.("test #1")
662
+ def test1; end
663
+ @action.("test #2")
664
+ def test2; end
665
+ @action.("test #3")
666
+ def test3; end
667
+ @action.("test #4")
668
+ def test4; end
669
+ end
670
+
671
+ Benry::CmdApp.action_alias "h25t3", "help25:test3"
672
+ Benry::CmdApp.action_alias "h25t1", "help25:test1"
673
+ Benry::CmdApp.action_alias "_h25t4", "help25:test4"
674
+
675
+ before do
676
+ clear_index_except(BuildAliasTest)
677
+ end
678
+
679
+ after do
680
+ restore_index()
681
+ end
682
+
683
+ def new_help_builder(**kws)
684
+ config = Benry::CmdApp::Config.new("test app", "1.2.3", **kws)
685
+ schema = Benry::CmdApp::AppOptionSchema.new(config)
686
+ builder = Benry::CmdApp::AppHelpBuilder.new(config, schema)
687
+ return builder
688
+ end
689
+
690
+ spec "[!tri8x] includes alias names in order of registration." do
691
+ hb = new_help_builder(help_aliases: true)
692
+ msg = hb.__send__(:build_aliases)
693
+ ok {msg} == <<"END"
694
+ \e[34mAliases:\e[0m
695
+ \e[1mh25t3 \e[0m : alias of 'help25:test3' action
696
+ \e[1mh25t1 \e[0m : alias of 'help25:test1' action
697
+ END
698
+ end
699
+
700
+ spec "[!5g72a] not show hidden alias names in default." do
701
+ hb = new_help_builder(help_aliases: true)
702
+ msg = hb.__send__(:build_aliases)
703
+ ok {msg} !~ /_h25t4/
704
+ end
705
+
706
+ spec "[!ekuqm] shows all alias names including private ones if 'all' flag is true." do
707
+ hb = new_help_builder(help_aliases: true)
708
+ msg = hb.__send__(:build_aliases, true)
709
+ ok {msg} =~ /_h25t4/
710
+ ok {msg} == <<"END"
711
+ \e[34mAliases:\e[0m
712
+ \e[1mh25t3 \e[0m : alias of 'help25:test3' action
713
+ \e[1mh25t1 \e[0m : alias of 'help25:test1' action
714
+ \e[1m_h25t4 \e[0m : alias of 'help25:test4' action
715
+ END
716
+ end
717
+
718
+ spec "[!aey2k] shows alias in strong or weak format according to action." do
719
+ with_important("help25:test1"=>true, "help25:test3"=>false) do
720
+ hb = new_help_builder(help_aliases: true)
721
+ msg = hb.__send__(:build_aliases, true)
722
+ ok {msg} == <<"END"
723
+ \e[34mAliases:\e[0m
724
+ \e[1m\e[2mh25t3\e[0m \e[0m : alias of 'help25:test3' action
725
+ \e[1m\e[4mh25t1\e[0m \e[0m : alias of 'help25:test1' action
726
+ \e[1m_h25t4 \e[0m : alias of 'help25:test4' action
727
+ END
728
+ end
729
+ end
730
+
731
+ spec "[!p3oh6] now show 'Aliases:' section if no aliases defined." do
732
+ Benry::CmdApp::INDEX.instance_variable_get('@aliases').clear()
733
+ hb = new_help_builder(help_aliases: true)
734
+ msg = hb.__send__(:build_aliases)
735
+ ok {msg} == nil
736
+ end
737
+
738
+ spec "[!we1l8] shows 'Aliases:' section if any aliases defined." do
739
+ hb = new_help_builder(help_aliases: true)
740
+ msg = hb.__send__(:build_aliases)
741
+ ok {msg} != nil
742
+ ok {msg} == <<"END"
743
+ \e[34mAliases:\e[0m
744
+ \e[1mh25t3 \e[0m : alias of 'help25:test3' action
745
+ \e[1mh25t1 \e[0m : alias of 'help25:test1' action
746
+ END
747
+ end
748
+
749
+ end
750
+
751
+
752
+ end
753
+
754
+
755
+ end