benry-cmdapp 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/help_test.rb CHANGED
@@ -1,215 +1,301 @@
1
1
  # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
2
3
 
3
- require 'oktest'
4
4
 
5
- require 'benry/cmdapp'
6
- require_relative './shared'
5
+ require_relative 'shared'
7
6
 
8
7
 
9
- Oktest.scope do
8
+ class HelpTestAction < Benry::CmdApp::Action
10
9
 
10
+ @action.("preamble and postamble",
11
+ detail: "See https://....",
12
+ postamble: [{"Examples:"=>" $ echo\n"}, "(Tips: blabla)"])
13
+ def prepostamble()
14
+ end
11
15
 
12
- topic Benry::CmdApp::HelpBuilder do
16
+ @action.("no options")
17
+ def noopt(aa, bb=nil)
18
+ puts "aa=#{aa.inspect}, bb=#{bb.inspect}"
19
+ end
13
20
 
21
+ @action.("usage sample", usage: "input.txt > output.txt")
22
+ def usagesample1(aa, bb, cc=nil)
23
+ end
14
24
 
15
- before do
16
- @builder = Benry::CmdApp::HelpBuilder.new()
25
+ @action.("usage sample", usage: ["input.txt | less", "input.txt > output.txt"])
26
+ @option.(:file, "-f <file>", "filename")
27
+ def usagesample2(aa, bb, cc=nil, file: nil)
28
+ end
29
+
30
+ @action.("arguments sample")
31
+ @option.(:xx, "--xx", "XX")
32
+ @option.(:yy, "--yy", "YY")
33
+ def argsample(aa, bb, cc=nil, dd=nil, *rest, xx: nil, yy: nil)
34
+ puts "aa=#{aa.inspect}, bb=#{bb.inspect}, cc=#{cc.inspect}, dd=#{dd.inspect}, rest=#{rest.inspect}, xx=#{xx.inspect}, yy=#{yy.inspect}"
35
+ end
36
+
37
+ category "secret:" do
38
+ @action.("secret action", hidden: true)
39
+ def crypt()
17
40
  end
41
+ end
18
42
 
43
+ category "descdemo:", "category description demo" do
44
+ @action.("demo #1")
45
+ def demo1()
46
+ end
47
+ @action.("demo #2")
48
+ def demo2()
49
+ end
50
+ end
19
51
 
20
- topic '#build_section()' do
52
+ ## alias test
53
+ @action.("sample action")
54
+ @option.(:aa, "-a, --aa", "option A")
55
+ @option.(:bb, " --bb", "option B")
56
+ def alitest1(xx, yy=0, *zz, aa: nil, bb: nil)
57
+ end
58
+ define_alias "alitest1x", "alitest1"
59
+ define_alias "alitest1y", ["alitest1", "--aa"]
60
+ Benry::CmdApp.define_alias! "alitest1z", ["alitest1y", "foobar"] # error
21
61
 
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
62
+ end
29
63
 
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
64
 
38
- end
65
+ class HelpTestBuilder < Benry::CmdApp::BaseHelpBuilder
66
+ HEADER_ACTIONS = "<ACTIONS>"
67
+ end
68
+
69
+
70
+ Oktest.scope do
39
71
 
40
72
 
73
+ def with_dummy_registry(registry=nil, &b)
74
+ registry ||= Benry::CmdApp::Registry.new()
75
+ bkup = nil
76
+ Benry::CmdApp.module_eval {
77
+ bkup = const_get :REGISTRY
78
+ remove_const :REGISTRY
79
+ const_set :REGISTRY, registry
80
+ }
81
+ yield registry
82
+ return registry
83
+ ensure
84
+ Benry::CmdApp.module_eval {
85
+ remove_const :REGISTRY
86
+ const_set :REGISTRY, bkup
87
+ }
41
88
  end
42
89
 
90
+ def new_registry_with_filter(*categories)
91
+ idx = Benry::CmdApp::Registry.new()
92
+ Benry::CmdApp::REGISTRY.metadata_each do |md|
93
+ if md.name.start_with?(*categories)
94
+ idx.metadata_add(md)
95
+ end
96
+ end
97
+ return idx
98
+ end
43
99
 
44
- topic Benry::CmdApp::ActionHelpBuilder do
45
- include ActionMetadataTestingHelper
100
+
101
+ topic Benry::CmdApp::BaseHelpBuilder do
102
+
103
+
104
+ before do
105
+ @config = Benry::CmdApp::Config.new("test app", "1.2.3", app_command: "testapp")
106
+ @builder = Benry::CmdApp::BaseHelpBuilder.new(@config)
107
+ end
46
108
 
47
109
 
48
110
  topic '#build_help_message()' do
49
111
 
50
- spec "[!pqoup] adds detail text into help if specified." do
51
- expected = <<END
52
- testapp halo1 -- greeting
112
+ spec "[!0hy81] this is an abstract method." do
113
+ pr = proc { @builder.build_help_message(nil) }
114
+ ok {pr}.raise?(NotImplementedError,
115
+ "Benry::CmdApp::BaseHelpBuilder#build_help_message(): not implemented yet.")
116
+ end
53
117
 
54
- See: https://example.com/doc.html
118
+ end
55
119
 
56
- Usage:
57
- $ testapp halo1 [<options>] [<user>]
58
120
 
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
121
+ topic '#render_section()' do
122
+
123
+ spec "[!61psk] returns section string with decorating header." do
124
+ s = @builder.__send__(:render_section, "Document:", "http://example.com/doc/\n")
125
+ ok {s} == ("\e[1;34mDocument:\e[0m\n" \
126
+ "http://example.com/doc/\n")
69
127
  end
70
128
 
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")
129
+ spec "[!0o8w4] appends '\n' to content if it doesn't end with '\n'." do
130
+ s = @builder.__send__(:render_section, "Document:", "http://...")
131
+ ok {s} == ("\e[1;34mDocument:\e[0m\n" \
132
+ "http://...\n")
133
+ ok {s}.end_with?("\n")
83
134
  end
84
135
 
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", hidden: true)
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>\]$/
136
+ end
137
+
138
+
139
+ topic '#render_sections()' do
140
+
141
+ spec "[!tqau1] returns nil if value is nil or empty." do
142
+ ok {@builder.__send__(:render_sections, nil, 'config.app_postamble')} == nil
143
+ ok {@builder.__send__(:render_sections, [] , 'config.app_postamble')} == nil
144
+ end
145
+
146
+ spec "[!ezb0d] returns value unchanged if value is a string." do
147
+ ok {@builder.__send__(:render_sections, "ABC\n", 'xxx')} == "ABC\n"
92
148
  end
93
149
 
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>\]$/
150
+ spec "[!gipxn] builds sections of help message if value is a hash object." do
151
+ ok {@builder.__send__(:render_sections, {"Doc:"=>"ABC\n"}, 'xxx')} == <<"END"
152
+ \e[1;34mDoc:\e[0m
153
+ ABC
154
+ END
100
155
  end
101
156
 
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")
157
+ spec "[!944rt] raises ActionError if unexpected value found in value." do
158
+ pr = proc { @builder.__send__(:render_sections, 123, 'xxx') }
159
+ ok {pr}.raise?(Benry::CmdApp::ActionError,
160
+ "123: Unexpected value found in `xxx`.")
108
161
  end
109
162
 
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")
163
+ end
164
+
165
+
166
+ topic '#render_option_help()' do
167
+
168
+ before do
169
+ @schema = Benry::CmdApp::ACTION_OPTION_SCHEMA_CLASS.new()
170
+ #@schema.add(:help , "-h, --help" , "help message")
171
+ @schema.add(:silent, "-s, --silent", "silent mode")
172
+ @schema.add(:file , "-f <file>" , "filename")
173
+ @schema.add(:debug , " --debug" , "debug mode", hidden: true)
174
+ config = Benry::CmdApp::Config.new("test app", "1.2.3")
175
+ @format = config.format_option
115
176
  end
116
177
 
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: true) # hidden option
120
- msg = new_metadata(schema).help_message("testapp")
121
- msg = uncolorize(msg)
122
- ok {msg}.NOT.include?("Options:\n")
178
+ spec "[!muhem] returns option part of help message." do
179
+ x = @builder.__send__(:render_option_help, @schema, @format)
180
+ ok {x} == <<"END"
181
+ -s, --silent : silent mode
182
+ -f <file> : filename
183
+ END
123
184
  end
124
185
 
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: true) # 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
186
+ spec "[!4z70n] includes hidden options when `all: true` passed." do
187
+ x = @builder.__send__(:render_option_help, @schema, @format, all: true)
188
+ ok {x} == <<"END"
189
+ -s, --silent : silent mode
190
+ -f <file> : filename
191
+ \e[2m --debug : debug mode\e[0m
134
192
  END
135
193
  end
136
194
 
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
195
+ spec "[!hxy1f] includes `detail:` kwarg value with indentation." do
196
+ @schema.add(:mode, "-m <mode>", "output mode", detail: <<END)
197
+ - v, verbose: print many output
198
+ - q, quiet: print litte output
199
+ - c, compact: print summary output
200
+ END
201
+ #
202
+ x = @builder.__send__(:render_option_help, @schema, @format)
203
+ ok {x} == <<"END"
204
+ -s, --silent : silent mode
205
+ -f <file> : filename
206
+ -m <mode> : output mode
207
+ - v, verbose: print many output
208
+ - q, quiet: print litte output
209
+ - c, compact: print summary output
210
+ END
211
+ #
212
+ x = @builder.__send__(:render_option_help, @schema, " %-15s # %s")
213
+ ok {x} == <<"END"
214
+ -s, --silent # silent mode
215
+ -f <file> # filename
216
+ -m <mode> # output mode
217
+ - v, verbose: print many output
218
+ - q, quiet: print litte output
219
+ - c, compact: print summary output
148
220
  END
149
221
  end
150
222
 
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
223
+ spec "[!jcqdf] returns nil if no options." do
224
+ schema = Benry::CmdApp::ACTION_OPTION_SCHEMA_CLASS.new()
225
+ x = @builder.__send__(:render_option_help, schema, @format)
226
+ ok {x} == nil
227
+ end
159
228
 
160
- Usage:
161
- $ testapp halo1 [<user>]
229
+ end
162
230
 
163
- Tips: `testapp -h <action>` print help message of action.
164
- END
231
+
232
+ topic '#decorate_command()' do
233
+
234
+ spec "[!zffx5] decorates command string." do
235
+ x = @builder.__send__(:decorate_command, "cmdname")
236
+ ok {x} == "\e[1mcmdname\e[0m"
237
+ end
238
+
239
+ end
240
+
241
+
242
+ topic '#decorate_header()' do
243
+
244
+ spec "[!4ufhw] decorates header string." do
245
+ x = @builder.__send__(:decorate_header, "Header:")
246
+ ok {x} == "\e[1;34mHeader:\e[0m"
247
+ end
248
+
249
+ end
250
+
251
+
252
+ topic '#decorate_extra()' do
253
+
254
+ spec "[!9nch4] decorates extra string." do
255
+ x = @builder.__send__(:decorate_extra, "(default: 'git')")
256
+ ok {x} == "\e[2m(default: 'git')\e[0m"
165
257
  end
166
258
 
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")
259
+ end
260
+
261
+
262
+ topic '#decorate_str()' do
263
+
264
+ spec "[!9qesd] decorates string if `hidden` is true." do
265
+ b = @builder
266
+ ok {b.__send__(:decorate_str, "FOOBAR", true, nil)} == "\e[2mFOOBAR\e[0m"
267
+ ok {b.__send__(:decorate_str, "FOOBAR", false, nil)} == "FOOBAR"
268
+ ok {b.__send__(:decorate_str, "FOOBAR", nil, nil)} == "FOOBAR"
173
269
  end
174
270
 
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>$/
271
+ spec "[!uql2d] decorates string if `important` is true." do
272
+ b = @builder
273
+ ok {b.__send__(:decorate_str, "FOOBAR", nil, true)} == "\e[1mFOOBAR\e[0m"
181
274
  end
182
275
 
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>\]\]$/
276
+ spec "[!mdhhr] decorates string if `important` is false." do
277
+ b = @builder
278
+ ok {b.__send__(:decorate_str, "FOOBAR", nil, false)} == "\e[2mFOOBAR\e[0m"
189
279
  end
190
280
 
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>...\]\]\]$/
281
+ spec "[!6uzbi] not decorates string if `hidden` is falthy and `important` is nil." do
282
+ b = @builder
283
+ ok {b.__send__(:decorate_str, "FOOBAR", nil, nil)} == "FOOBAR"
197
284
  end
198
285
 
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>$/
286
+ end
287
+
288
+
289
+ topic '#header()' do
290
+
291
+ spec "[!ep064] returns constant value defined in the class." do
292
+ b = Benry::CmdApp::BaseHelpBuilder.new(nil)
293
+ ok {b.__send__(:header, :HEADER_ACTIONS)} == "Actions:"
205
294
  end
206
295
 
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>$/
296
+ spec "[!viwtn] constant value defined in child class is prior to one defined in parent class." do
297
+ b = HelpTestBuilder.new(nil)
298
+ ok {b.__send__(:header, :HEADER_ACTIONS)} == "<ACTIONS>"
213
299
  end
214
300
 
215
301
  end
@@ -218,534 +304,949 @@ END
218
304
  end
219
305
 
220
306
 
221
- topic Benry::CmdApp::AppHelpBuilder do
222
- include CommonTestingHelper
307
+ topic Benry::CmdApp::ApplicationHelpBuilder do
223
308
 
224
309
  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)
310
+ @config = Benry::CmdApp::Config.new("test app", "1.2.3",
311
+ app_name: "TestApp", app_command: "testapp",
312
+ help_description: "This is a description of application.",
313
+ option_verbose: true, option_quiet: true,
314
+ option_color: true, #option_debug: true,
315
+ option_trace: true)
316
+ @builder = new_builder()
234
317
  end
235
318
 
319
+ def new_builder(config=nil)
320
+ return Benry::CmdApp::ApplicationHelpBuilder.new(config || @config)
321
+ end
322
+
323
+
236
324
  topic '#build_help_message()' do
237
325
 
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
326
+ spec "[!ezcs4] returns help message string of application." do
327
+ gschema = Benry::CmdApp::GLOBAL_OPTION_SCHEMA_CLASS.new(@config)
328
+ actual = @builder.build_help_message(gschema)
329
+ expected = <<"END"
330
+ \e[1mTestApp\e[0m \e[2m(1.2.3)\e[0m --- test app
331
+
332
+ \e[1;34mUsage:\e[0m
333
+ $ \e[1mtestapp\e[0m [<options>] <action> [<arguments>...]
334
+
335
+ \e[1;34mDescription:\e[0m
336
+ This is a description of application.
337
+
338
+ \e[1;34mOptions:\e[0m
339
+ -h, --help : print help message (of action if specified)
340
+ -V, --version : print version
341
+ -l, --list : list actions and aliases
342
+ -a, --all : list hidden actions/options, too
343
+ -v, --verbose : verbose mode
344
+ -q, --quiet : quiet mode
345
+ --color[=<on|off>] : color mode
346
+ -T, --trace : trace mode
347
+
348
+ \e[1;34mActions:\e[0m
349
+ END
350
+ ok {actual}.start_with?(expected)
351
+ ok {actual} !~ /^ *--debug/
251
352
  end
252
353
 
253
- Benry::CmdApp.action_alias("yes", "yo-yo")
354
+ spec "[!ntj2y] includes hidden actions and options if `all: true` passed." do
355
+ @config.option_debug = true
356
+ gschema = Benry::CmdApp::GLOBAL_OPTION_SCHEMA_CLASS.new(@config)
357
+ actual = @builder.build_help_message(gschema)
358
+ expected = <<"END"
359
+ \e[1mTestApp\e[0m \e[2m(1.2.3)\e[0m --- test app
254
360
 
255
- before do
256
- clear_index_except(HelpMessageTest)
361
+ \e[1;34mUsage:\e[0m
362
+ $ \e[1mtestapp\e[0m [<options>] <action> [<arguments>...]
363
+
364
+ \e[1;34mDescription:\e[0m
365
+ This is a description of application.
366
+
367
+ \e[1;34mOptions:\e[0m
368
+ -h, --help : print help message (of action if specified)
369
+ -V, --version : print version
370
+ -l, --list : list actions and aliases
371
+ -a, --all : list hidden actions/options, too
372
+ -v, --verbose : verbose mode
373
+ -q, --quiet : quiet mode
374
+ --color[=<on|off>] : color mode
375
+ --debug : debug mode
376
+ -T, --trace : trace mode
377
+
378
+ \e[1;34mActions:\e[0m
379
+ END
380
+ ok {actual}.start_with?(expected)
381
+ ok {actual} =~ /^ +--debug +: debug mode$/
257
382
  end
258
383
 
259
- after do
260
- restore_index()
384
+ end
385
+
386
+
387
+ topic '#section_preamble()' do
388
+
389
+ spec "[!51v42] returns preamble part of application help message." do
390
+ x = @builder.__send__(:section_preamble)
391
+ ok {x} == "\e[1mTestApp\e[0m \e[2m(1.2.3)\e[0m --- test app\n"
261
392
  end
262
393
 
263
- expected_color = <<"END"
264
- \e[1mTestApp\e[0m (1.0.0) -- test app
394
+ spec "[!bmh17] includes `config.app_name` or `config.app_command` into preamble." do
395
+ @config.app_name = "TestApp"
396
+ @config.app_command = "testapp"
397
+ x = @builder.__send__(:section_preamble)
398
+ ok {x} == "\e[1mTestApp\e[0m \e[2m(1.2.3)\e[0m --- test app\n"
399
+ #
400
+ @config.app_name = nil
401
+ x = @builder.__send__(:section_preamble)
402
+ ok {x} == "\e[1mtestapp\e[0m \e[2m(1.2.3)\e[0m --- test app\n"
403
+ end
265
404
 
266
- \e[34mUsage:\e[0m
267
- $ \e[1mtestapp\e[0m [<options>] [<action> [<arguments>...]]
405
+ spec "[!opii8] includes `config.app_versoin` into preamble if it is set." do
406
+ @config.app_version = "3.4.5"
407
+ x = @builder.__send__(:section_preamble)
408
+ ok {x} == "\e[1mTestApp\e[0m \e[2m(3.4.5)\e[0m --- test app\n"
409
+ #
410
+ @config.app_version = nil
411
+ x = @builder.__send__(:section_preamble)
412
+ ok {x} == "\e[1mTestApp\e[0m --- test app\n"
413
+ end
268
414
 
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)
415
+ spec "[!3h380] includes `config.app_detail` into preamble if it is set." do
416
+ @config.app_detail = "https://www.example.com/"
417
+ x = @builder.__send__(:section_preamble)
418
+ ok {x} == <<"END"
419
+ \e[1mTestApp\e[0m \e[2m(1.2.3)\e[0m --- test app
274
420
 
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
421
+ https://www.example.com/
279
422
  END
423
+ end
280
424
 
281
- expected_mono = <<'END'
282
- TestApp (1.0.0) -- test app
425
+ end
283
426
 
284
- Usage:
285
- $ testapp [<options>] [<action> [<arguments>...]]
286
427
 
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)
428
+ topic '#section_postamble()' do
292
429
 
293
- Actions:
294
- ya:ya : greeting #2
295
- yes : alias of 'yo-yo' action
296
- yo-yo : greeting #1
430
+ spec "[!64hj1] returns postamble of application help message." do
431
+ @config.help_postamble = [
432
+ {"Examples:" => " $ echo yes\n yes\n"},
433
+ "(Tips: blablabla)",
434
+ ]
435
+ x = @builder.__send__(:section_postamble)
436
+ ok {x} == <<"END"
437
+ \e[1;34mExamples:\e[0m
438
+ $ echo yes
439
+ yes
440
+
441
+ (Tips: blablabla)
297
442
  END
443
+ end
298
444
 
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
445
+ spec "[!z5k2w] returns nil if postamble not set." do
446
+ x = @builder.__send__(:section_postamble)
447
+ ok {x} == nil
305
448
  end
306
449
 
307
- def _with_color_mode(val, &b)
308
- bkup = $COLOR_MODE
309
- $COLOR_MODE = val
310
- yield
311
- ensure
312
- $COLOR_MODE = bkup
450
+ end
451
+
452
+
453
+ topic '#section_usage()' do
454
+
455
+ spec "[!h98me] returns 'Usage:' section of application help message." do
456
+ x = @builder.__send__(:section_usage)
457
+ ok {x} == <<"END"
458
+ \e[1;34mUsage:\e[0m
459
+ $ \e[1mtestapp\e[0m [<options>] <action> [<arguments>...]
460
+ END
313
461
  end
314
462
 
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$/
463
+ spec "[!i9d4r] includes `config.app_usage` into help message if it is set." do
464
+ @config.app_usage = "<command> [<args>...]"
465
+ x = @builder.__send__(:section_usage)
466
+ ok {x} == <<"END"
467
+ \e[1;34mUsage:\e[0m
468
+ $ \e[1mtestapp\e[0m [<options>] <command> [<args>...]
469
+ END
320
470
  end
321
471
 
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$/
472
+ end
473
+
474
+
475
+ topic '#section_description()' do
476
+
477
+ spec "[!qarrk] returns 'Description:' section if `config.help_description` is set." do
478
+ @config.help_description = "This is a description of application."
479
+ str = @builder.__send__(:section_description)
480
+ ok {str} == <<~"END"
481
+ \e[1;34mDescription:\e[0m
482
+ This is a description of application.
483
+ END
327
484
  end
328
485
 
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$/
486
+ spec "[!ealol] returns nil if `config.help_description` is nil." do
487
+ @config.help_description = nil
488
+ str = @builder.__send__(:section_description)
489
+ ok {str} == nil
339
490
  end
340
491
 
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
492
+ end
493
+
347
494
 
348
- See https://example.com/doc.html
495
+ topic '#section_options()' do
349
496
 
350
- Usage:
497
+ spec "[!f2n70] returns 'Options:' section of application help message." do
498
+ gschema = Benry::CmdApp::GLOBAL_OPTION_SCHEMA_CLASS.new(@config)
499
+ x = @builder.__send__(:section_options, gschema)
500
+ ok {x} == <<"END"
501
+ \e[1;34mOptions:\e[0m
502
+ -h, --help : print help message (of action if specified)
503
+ -V, --version : print version
504
+ -l, --list : list actions and aliases
505
+ -a, --all : list hidden actions/options, too
506
+ -v, --verbose : verbose mode
507
+ -q, --quiet : quiet mode
508
+ --color[=<on|off>] : color mode
509
+ -T, --trace : trace mode
351
510
  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
511
+ ok {x} !~ /--debug/
512
+ end
358
513
 
359
- Usage:
514
+ spec "[!0bboq] includes hidden options into help message if `all: true` passed." do
515
+ gschema = Benry::CmdApp::GLOBAL_OPTION_SCHEMA_CLASS.new(@config)
516
+ x = @builder.__send__(:section_options, gschema, all: true)
517
+ ok {x} == <<"END"
518
+ \e[1;34mOptions:\e[0m
519
+ -h, --help : print help message (of action if specified)
520
+ -V, --version : print version
521
+ -l, --list : list actions and aliases
522
+ \e[2m -L <topic> : topic list (actions|aliases|categories|abbrevs)\e[0m
523
+ -a, --all : list hidden actions/options, too
524
+ -v, --verbose : verbose mode
525
+ -q, --quiet : quiet mode
526
+ --color[=<on|off>] : color mode
527
+ \e[2m --debug : debug mode\e[0m
528
+ -T, --trace : trace mode
360
529
  END
530
+ ok {x} =~ /--debug/
361
531
  end
362
532
 
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://...
533
+ spec "[!fjhow] returns nil if no options." do
534
+ gschema = Benry::CmdApp::ACTION_OPTION_SCHEMA_CLASS.new
535
+ x = @builder.__send__(:section_options, gschema)
536
+ ok {x} == nil
537
+ end
370
538
 
371
- Usage:
372
- $ testapp [<options>] [<action> [<arguments>...]]
539
+ end
373
540
 
374
- END
541
+
542
+ topic '#section_actions()' do
543
+
544
+ spec "[!typ67] returns 'Actions:' section of help message." do
545
+ x = @builder.__send__(:section_actions)
546
+ ok {x} =~ /\A\e\[1;34mActions:\e\[0m$/
375
547
  end
376
548
 
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>...]]
549
+ spec "[!yn8ea] includes hidden actions into help message if `all: true` passed." do
550
+ x = @builder.__send__(:section_actions, all: true)
551
+ ok {x} =~ /debuginfo/
552
+ ok {x} =~ /^\e\[2m debuginfo : hidden action\e\[0m$/
553
+ #
554
+ x = @builder.__send__(:section_actions)
555
+ ok {x} !~ /debuginfo/
556
+ end
557
+
558
+ spec "[!10qp0] includes aliases if the 1st argument is true." do
559
+ x = @builder.__send__(:section_actions, true, all: true)
560
+ ok {x} =~ /\(alias: /
561
+ #
562
+ x = @builder.__send__(:section_actions, false, all: true)
563
+ ok {x} !~ /\(alias: /
564
+ end
385
565
 
566
+ spec "[!24by5] returns nil if no actions defined." do
567
+ debuginfo_md = Benry::CmdApp::REGISTRY.metadata_get("debuginfo")
568
+ hello_md = Benry::CmdApp::REGISTRY.metadata_get("hello")
569
+ registry = Benry::CmdApp::Registry.new()
570
+ with_dummy_registry(registry) do
571
+ builder = new_builder()
572
+ #
573
+ x = builder.__send__(:section_actions)
574
+ ok {x} == nil
575
+ #
576
+ registry.metadata_add(debuginfo_md)
577
+ x = builder.__send__(:section_actions)
578
+ ok {x} == nil
579
+ x = builder.__send__(:section_actions, all: true)
580
+ ok {x} == <<"END"
581
+ \e[1;34mActions:\e[0m
582
+ \e[2m debuginfo : hidden action\e[0m
386
583
  END
584
+ #
585
+ registry.metadata_add(hello_md)
586
+ x = builder.__send__(:section_actions)
587
+ ok {x} == <<"END"
588
+ \e[1;34mActions:\e[0m
589
+ hello : greeting message
590
+ END
591
+ x = builder.__send__(:section_actions, all: true)
592
+ ok {x} == <<"END"
593
+ \e[1;34mActions:\e[0m
594
+ \e[2m debuginfo : hidden action\e[0m
595
+ hello : greeting message
596
+ END
597
+ end
598
+ end
599
+
600
+ spec "[!8qz6a] adds default action name after header if it is set." do
601
+ @config.default_action = "help"
602
+ x = @builder.__send__(:section_actions)
603
+ ok {x} =~ /\A\e\[1;34mActions:\e\[0m \e\[2m\(default: help\)\e\[0m$/
604
+ #
605
+ @config.default_action = "hello"
606
+ x = @builder.__send__(:section_actions)
607
+ ok {x} =~ /\A\e\[1;34mActions:\e\[0m \e\[2m\(default: hello\)\e\[0m$/
608
+ end
609
+
610
+ end
611
+
612
+
613
+ topic '#_render_metadata_list()' do
614
+
615
+ spec "[!iokkp] builds list of actions or aliases." do
616
+ format = "%s : %s"
617
+ output = @builder.__send__(:_render_metadata_list, format) { true }
618
+ ok {output} =~ /^hello : greeting message$/
619
+ end
620
+
621
+ spec "[!grwkj] filters by block." do
622
+ format = "%s : %s"
623
+ output = @builder.__send__(:_render_metadata_list, format) {|md| md.alias? }
624
+ ok {output} !~ /^hello : greeting message$/
625
+ ok {output} =~ /alias for/
626
+ output = @builder.__send__(:_render_metadata_list, format) {|md| ! md.alias? }
627
+ ok {output} =~ /^hello : greeting message$/
628
+ ok {output} !~ /alias for/
387
629
  end
388
630
 
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
631
+ spec "[!hv7or] if action has any aliases, print them below of the action." do
632
+ format = " %-18s : %s"
633
+ include_aliases = true
634
+ output = @builder.instance_eval{
635
+ _render_metadata_list(format, include_aliases) {|md| ! md.alias? }
636
+ }
637
+ ok {output} =~ /^\e\[2m +\(alias: .*\)\e\[0m$/
638
+ end
639
+
640
+ end
641
+
642
+
643
+ topic '#section_availables()' do
644
+
645
+ spec "[!pz0cu] includes 'Actions:' and 'Aliases:' sections." do
646
+ output = @builder.section_availables()
647
+ ok {output} =~ /\A\e\[1;34mActions:\e\[0m\n/
648
+ ok {output} =~ /\n\n\e\[1;34mAliases:\e\[0m\n/
649
+ end
650
+
651
+ end
396
652
 
397
- Usage:
398
- $ greatcmd [<options>] [<action> [<arguments>...]]
399
653
 
400
- Options:
654
+ topic '#section_candidates()' do
655
+
656
+ spec "[!3c3f1] returns list of actions which name starts with prefix specified." do
657
+ x = @builder.section_candidates("git:")
658
+ ok {x} == <<"END"
659
+ \e[1;34mActions:\e[0m
660
+ git:stage : same as `git add -p`
661
+ git:staged : same as `git diff --cached`
662
+ git:unstage : same as `git reset HEAD`
401
663
  END
402
664
  end
403
665
 
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)
666
+ spec "[!idm2h] includes hidden actions when `all: true` passed." do
667
+ x = @builder.section_candidates("git:", all: true)
668
+ ok {x} == <<"END"
669
+ \e[1;34mActions:\e[0m
670
+ \e[2m git:correct : same as `git commit --amend`\e[0m
671
+ git:stage : same as `git add -p`
672
+ git:staged : same as `git diff --cached`
673
+ git:unstage : same as `git reset HEAD`
674
+ END
675
+ end
416
676
 
417
- Actions:
677
+ spec "[!duhyd] includes actions which name is same as prefix." do
678
+ HelpTestAction.class_eval do
679
+ category "p8572:", action: "aaa" do
680
+ @action.("AAA")
681
+ def aaa()
682
+ end
683
+ @action.("BBB")
684
+ def bbb()
685
+ end
686
+ end
687
+ @action.("sample")
688
+ def p8572x()
689
+ end
690
+ end
691
+ x = @builder.section_candidates("p8572:")
692
+ ok {x} == <<"END"
693
+ \e[1;34mActions:\e[0m
694
+ p8572 : AAA
695
+ p8572:bbb : BBB
418
696
  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
697
+ end
429
698
 
430
- Actions:
699
+ spec "[!nwwrd] if prefix is 'xxx:' and alias name is 'xxx' and action name of alias matches to 'xxx:', skip it because it will be shown in 'Aliases:' section." do
700
+ Benry::CmdApp.define_alias("git", "git:stage")
701
+ at_end { Benry::CmdApp.undef_alias("git") }
702
+ x = @builder.section_candidates("git:")
703
+ ok {x} == <<"END"
704
+ \e[1;34mActions:\e[0m
705
+ git:stage : same as `git add -p`
706
+ \e[2m (alias: git)\e[0m
707
+ git:staged : same as `git diff --cached`
708
+ git:unstage : same as `git reset HEAD`
709
+
710
+ \e[1;34mAliases:\e[0m
711
+ git : alias for 'git:stage'
431
712
  END
432
713
  end
433
714
 
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", hidden: true)
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
715
+ spec "[!otvbt] includes name of alias which corresponds to action starting with prefix." do
716
+ Benry::CmdApp.define_alias("add", "git:stage")
717
+ at_end { Benry::CmdApp.undef_alias("add") }
718
+ x = @builder.section_candidates("git:")
719
+ ok {x} == <<"END"
720
+ \e[1;34mActions:\e[0m
721
+ git:stage : same as `git add -p`
722
+ \e[2m (alias: add)\e[0m
723
+ git:staged : same as `git diff --cached`
724
+ git:unstage : same as `git reset HEAD`
725
+
726
+ \e[1;34mAliases:\e[0m
727
+ add : alias for 'git:stage'
728
+ END
729
+ end
447
730
 
448
- Actions:
731
+ spec "[!h5ek7] includes hidden aliases when `all: true` passed." do
732
+ Benry::CmdApp.define_alias("add", "git:stage", hidden: true)
733
+ at_end { Benry::CmdApp.undef_alias("add") }
734
+ x = @builder.section_candidates("git:", all: true)
735
+ ok {x} == <<"END"
736
+ \e[1;34mActions:\e[0m
737
+ \e[2m git:correct : same as `git commit --amend`\e[0m
738
+ git:stage : same as `git add -p`
739
+ \e[2m (alias: add)\e[0m
740
+ git:staged : same as `git diff --cached`
741
+ git:unstage : same as `git reset HEAD`
742
+
743
+ \e[1;34mAliases:\e[0m
744
+ \e[2m add : alias for 'git:stage'\e[0m
745
+ END
746
+ #
747
+ x = @builder.section_candidates("git:")
748
+ ok {x} == <<"END"
749
+ \e[1;34mActions:\e[0m
750
+ git:stage : same as `git add -p`
751
+ \e[2m (alias: add)\e[0m
752
+ git:staged : same as `git diff --cached`
753
+ git:unstage : same as `git reset HEAD`
449
754
  END
450
755
  end
451
756
 
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", hidden: true)
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
757
+ spec "[!9lnn2] alias names in candidate list are sorted by action name." do
758
+ HelpTestAction.class_eval do
759
+ category "c7784:" do
760
+ @action.("#5")
761
+ def a7784_5(); end
762
+ @action.("#3")
763
+ def a7784_3(); end
764
+ @action.("#1")
765
+ def a7784_1(); end
766
+ @action.("#2")
767
+ def a7784_2(); end
768
+ @action.("#4")
769
+ def a7784_4(); end
770
+ end
771
+ define_alias("a7784e", "c7784:a7784-5")
772
+ define_alias("a7784d", "c7784:a7784-3")
773
+ define_alias("a7784c", "c7784:a7784-1")
774
+ define_alias("a7784b", "c7784:a7784-2")
775
+ define_alias("a7784a", "c7784:a7784-4")
776
+ define_alias("a7784y", "c7784:a7784-4")
777
+ define_alias("a7784x", "c7784:a7784-4")
778
+ end
779
+ #
780
+ output = @builder.section_candidates("c7784:")
781
+ alias_section = output.split(/(?=\e\[1;34mAliases:\e\[0m\n)/, 2)[1]
782
+ ok {alias_section} == <<~"END"
783
+ \e[1;34mAliases:\e[0m
784
+ a7784c : alias for 'c7784:a7784-1'
785
+ a7784b : alias for 'c7784:a7784-2'
786
+ a7784d : alias for 'c7784:a7784-3'
787
+ a7784a : alias for 'c7784:a7784-4'
788
+ a7784x : alias for 'c7784:a7784-4'
789
+ a7784y : alias for 'c7784:a7784-4'
790
+ a7784e : alias for 'c7784:a7784-5'
791
+ END
792
+ end
466
793
 
467
- Actions:
794
+ spec "[!80t51] alias names are displayed in separated section from actions." do
795
+ Benry::CmdApp.define_alias("add", "git:stage")
796
+ at_end { Benry::CmdApp.undef_alias("add") }
797
+ x = @builder.section_candidates("git:")
798
+ ok {x} =~ /^\e\[1;34mAliases:\e\[0m$/
799
+ end
800
+
801
+ spec "[!rqx7w] returns header string if both no actions nor aliases found with names starting with prefix." do
802
+ x = @builder.section_candidates("blabla:")
803
+ ok {x} == "\e[1;34mActions:\e[0m\n\n"
804
+ end
805
+
806
+ end
807
+
808
+
809
+ topic '#section_aliases()' do
810
+
811
+ spec "[!496qq] renders alias list." do
812
+ Benry::CmdApp.define_alias("a9208", "hello")
813
+ x = @builder.section_aliases()
814
+ ok {x} =~ /\A\e\[1;34mAliases:\e\[0m\n/
815
+ ok {x} =~ /^ a9208 +: alias for 'hello'$/
816
+ end
817
+
818
+ spec "[!fj1c7] returns nil if no aliases found." do
819
+ registry = Benry::CmdApp::Registry.new()
820
+ @builder.instance_variable_set(:@_registry, registry)
821
+ x = @builder.section_aliases()
822
+ ok {x} == nil
823
+ registry.metadata_add(Benry::CmdApp::REGISTRY.metadata_get("hello"))
824
+ registry.metadata_add(Benry::CmdApp::AliasMetadata.new("h1", "hello", []))
825
+ x = @builder.section_aliases()
826
+ ok {x} != nil
827
+ ok {x} == <<"END"
828
+ \e[1;34mAliases:\e[0m
829
+ h1 : alias for 'hello'
468
830
  END
469
831
  end
470
832
 
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)
833
+ spec "[!d7vee] ignores hidden aliases in default." do
834
+ Benry::CmdApp.define_alias("a4904", "hello", hidden: true)
835
+ x = @builder.section_aliases()
836
+ ok {x} =~ /\A\e\[1;34mAliases:\e\[0m\n/
837
+ ok {x} !~ /a4904/
476
838
  end
477
839
 
478
- spec "[!p1tu9] prints option in weak format if option is hidden." do
479
- |app3|
480
- app3.schema.add(:log, "-L", "private option", hidden: true) # !!!
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
840
+ spec "[!4vvrs] include hidden aliases if `all: true` specifieid." do
841
+ Benry::CmdApp.define_alias("a4612", "hello", hidden: true)
842
+ x = @builder.section_aliases(all: true)
843
+ ok {x} =~ /\A\e\[1;34mAliases:\e\[0m\n/
844
+ ok {x} =~ /^\e\[2m a4612 +: alias for 'hello'\e\[0m$/
845
+ end
486
846
 
847
+ spec "[!v211d] sorts aliases by action names." do
848
+ old_registry = Benry::CmdApp::REGISTRY
849
+ names = ["hello", "debuginfo", "testerr1", "git:stage", "git:staged", "git:unstage"]
850
+ output = nil
851
+ with_dummy_registry do |new_registry|
852
+ names.each {|name| new_registry.metadata_add(old_registry.metadata_get(name)) }
853
+ Benry::CmdApp.define_alias("a1", "git:unstage")
854
+ Benry::CmdApp.define_alias("a2", "git:stage")
855
+ Benry::CmdApp.define_alias("a3", "git:staged")
856
+ Benry::CmdApp.define_alias("a6", "git:staged")
857
+ Benry::CmdApp.define_alias("a5", "git:staged")
858
+ Benry::CmdApp.define_alias("a4", "git:staged")
859
+ Benry::CmdApp.define_alias("a7", "testerr1")
860
+ Benry::CmdApp.define_alias("a8", "debuginfo")
861
+ Benry::CmdApp.define_alias("a9", "hello")
862
+ builder = new_builder()
863
+ output = builder.section_aliases()
864
+ end
865
+ ok {output} == <<"END"
866
+ \e[1;34mAliases:\e[0m
867
+ a8 : alias for 'debuginfo'
868
+ a2 : alias for 'git:stage'
869
+ a3 : alias for 'git:staged'
870
+ a4 : alias for 'git:staged'
871
+ a5 : alias for 'git:staged'
872
+ a6 : alias for 'git:staged'
873
+ a1 : alias for 'git:unstage'
874
+ a9 : alias for 'hello'
875
+ a7 : alias for 'testerr1'
487
876
  END
488
877
  end
489
878
 
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", hidden: true)
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 }
879
+ end
880
+
881
+
882
+ topic '#section_abbrevs()' do
883
+
884
+ spec "[!00ice] returns abbrev list string." do
885
+ Benry::CmdApp.define_abbrev("g25:", "git:")
886
+ x = @builder.section_abbrevs()
887
+ ok {x} =~ /\A\e\[1;34mAbbreviations:\e\[0m\n/
888
+ ok {x} =~ /^ g25: +=> +git:\n/
889
+ end
890
+
891
+ spec "[!dnt12] returns nil if no abbrevs found." do
892
+ registry = Benry::CmdApp::Registry.new()
893
+ @builder.instance_variable_set(:@_registry, registry)
894
+ ok {@builder.section_abbrevs()} == nil
895
+ registry.abbrev_add("g26:", "git:")
896
+ ok {@builder.section_abbrevs()} == <<END
897
+ \e[1;34mAbbreviations:\e[0m
898
+ g26: => git:
899
+ END
900
+ end
901
+
902
+ end
903
+
904
+
905
+ topic '#section_categories()' do
906
+
907
+ spec "[!crbav] returns top prefix list." do
908
+ x = @builder.section_categories(1)
909
+ ok {x} =~ /\A\e\[1;34mCategories:\e\[0m \e\[2m\(depth=\d+\)\e\[0m\n/
910
+ ok {x} =~ /^ git: \(\d+\)\n/
911
+ end
912
+
913
+ spec "[!alteh] includes prefix of hidden actions if `all: true` passed." do
914
+ x = @builder.section_categories(1, all: true)
915
+ ok {x} =~ /^ secret:/
916
+ x = @builder.section_categories(1)
917
+ ok {x} !~ /^ secret:/
918
+ end
919
+
920
+ spec "[!p4j1o] returns nil if no prefix found." do
921
+ registry = Benry::CmdApp::Registry.new
922
+ ["hello", "secret:crypt"].each do |action|
923
+ registry.metadata_add(Benry::CmdApp::REGISTRY.metadata_get(action))
924
+ end
556
925
  #
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 }
926
+ with_dummy_registry(registry) do
927
+ builder = new_builder()
928
+ x = builder.section_categories()
929
+ ok {x} == nil
930
+ x = builder.section_categories(all: true)
931
+ ok {x} != nil
569
932
  end
570
933
  end
571
934
 
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
935
+ spec "[!30l2j] includes number of actions per prefix." do
936
+ x = @builder.section_categories(all: true)
937
+ ok {x} =~ /^ git: \(\d+\)\n/
938
+ ok {x} =~ /^ secret: \(\d+\)\n/
939
+ end
940
+
941
+ spec "[!qxoja] includes category description if registered." do
942
+ x = @builder.section_categories(all: true)
943
+ ok {x} =~ /^ descdemo: \(2\) : category description demo$/
944
+ end
945
+
946
+ spec "[!k3y6q] uses `config.format_category` or `config.format_action`." do
947
+ @config.format_category = " %-15s # %s"
948
+ x = @builder.section_categories(all: true)
949
+ ok {x} =~ /^ descdemo: \(2\) # category description demo\n/
950
+ #
951
+ @config.format_category = nil
952
+ @config.format_category = " %-15s -- %s"
953
+ x = @builder.section_categories(all: true)
954
+ ok {x} =~ /^ descdemo: \(2\) -- category description demo$/
955
+ end
956
+
957
+ end
958
+
959
+
960
+ end
961
+
962
+
963
+ topic Benry::CmdApp::ActionHelpBuilder do
964
+
965
+ before do
966
+ @config = Benry::CmdApp::Config.new("test app", "1.2.3",
967
+ app_name: "TestApp", app_command: "testapp",
968
+ option_verbose: true, option_quiet: true,
969
+ option_color: true, #option_debug: true,
970
+ option_trace: true)
971
+ @builder = Benry::CmdApp::ActionHelpBuilder.new(@config)
972
+ @registry = Benry::CmdApp::REGISTRY
973
+ end
974
+
975
+ topic '#build_help_message()' do
976
+
977
+ spec "[!f3436] returns help message of an action." do
978
+ metadata = @registry.metadata_get("hello")
979
+ bkup = nil
980
+ metadata.instance_eval {
981
+ bkup = @description; @description = "Example of description."
982
+ }
983
+ at_end { metadata.instance_eval { @description = bkup } }
984
+ #
985
+ x = @builder.build_help_message(metadata)
986
+ ok {x}.start_with?(<<"END")
987
+ \e[1mtestapp hello\e[0m --- greeting message
988
+
989
+ \e[1;34mUsage:\e[0m
990
+ $ \e[1mtestapp hello\e[0m [<options>] [<name>]
991
+
992
+ \e[1;34mDescription:\e[0m
993
+ Example of description.
994
+
995
+ \e[1;34mOptions:\e[0m
996
+ -l, --lang=<lang> : language name (en/fr/it)
580
997
  END
581
- end
582
998
  end
583
999
 
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
1000
+ spec "[!8acs1] includes hidden options if `all: true` passed." do
1001
+ metadata = @registry.metadata_get("debuginfo")
1002
+ x = @builder.build_help_message(metadata, all: true)
1003
+ ok {x} == <<"END"
1004
+ \e[1mtestapp debuginfo\e[0m --- hidden action
1005
+
1006
+ \e[1;34mUsage:\e[0m
1007
+ $ \e[1mtestapp debuginfo\e[0m [<options>]
1008
+
1009
+ \e[1;34mOptions:\e[0m
1010
+ \e[2m -h, --help : print help message\e[0m
1011
+ \e[2m --val=<val> : something value\e[0m
592
1012
  END
593
- end
594
1013
  end
595
1014
 
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/
1015
+ spec "[!mtvw8] includes 'Aliases:' section if action has any aliases." do
1016
+ metadata = @registry.metadata_get("alitest1")
1017
+ output = @builder.build_help_message(metadata, all: true)
1018
+ ok {output} == <<"END"
1019
+ \e[1mtestapp alitest1\e[0m --- sample action
1020
+
1021
+ \e[1;34mUsage:\e[0m
1022
+ $ \e[1mtestapp alitest1\e[0m [<options>] <xx> [<yy> [<zz>...]]
1023
+
1024
+ \e[1;34mOptions:\e[0m
1025
+ \e[2m -h, --help : print help message\e[0m
1026
+ -a, --aa : option A
1027
+ --bb : option B
1028
+
1029
+ \e[1;34mAliases:\e[0m
1030
+ alitest1x : alias for 'alitest1'
1031
+ alitest1y : alias for 'alitest1 --aa'
1032
+ alitest1z : alias for 'alitest1 --aa foobar'
602
1033
  END
603
1034
  end
604
1035
 
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")
1036
+ spec "[!vcg9w] not include 'Options:' section if action has no options." do
1037
+ metadata = @registry.metadata_get("noopt")
1038
+ x = @builder.build_help_message(metadata, all: true)
1039
+ ok {x} == <<"END"
1040
+ \e[1mtestapp noopt\e[0m --- no options
1041
+
1042
+ \e[1;34mUsage:\e[0m
1043
+ $ \e[1mtestapp noopt\e[0m [<options>] <aa> [<bb>]
1044
+
1045
+ \e[1;34mOptions:\e[0m
1046
+ \e[2m -h, --help : print help message\e[0m
1047
+ END
609
1048
  end
610
1049
 
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
1050
+ spec "[!1auu5] not include '[<options>]' in 'Usage:'section if action has no options." do
1051
+ metadata = @registry.metadata_get("debuginfo")
1052
+ x = @builder.build_help_message(metadata)
1053
+ ok {x} == <<"END"
1054
+ \e[1mtestapp debuginfo\e[0m --- hidden action
621
1055
 
622
- Aliases:
623
- yes : alias of 'yo-yo' action
1056
+ \e[1;34mUsage:\e[0m
1057
+ $ \e[1mtestapp debuginfo\e[0m
624
1058
  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
1059
+ end
1060
+
1061
+ end
1062
+
1063
+
1064
+ topic '#section_preamble()' do
1065
+
1066
+ spec "[!a6nk4] returns preamble of action help message." do
1067
+ metadata = @registry.metadata_get("hello")
1068
+ x = @builder.__send__(:section_preamble, metadata)
1069
+ ok {x} == <<"END"
1070
+ \e[1mtestapp hello\e[0m --- greeting message
634
1071
  END
635
1072
  end
636
1073
 
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")
1074
+ spec "[!imxdq] includes `config.app_command`, not `config.app_name`, into preamble." do
1075
+ @config.app_name = "TestApp1"
1076
+ @config.app_command = "testapp1"
1077
+ metadata = @registry.metadata_get("hello")
1078
+ x = @builder.__send__(:section_preamble, metadata)
1079
+ ok {x} == <<"END"
1080
+ \e[1mtestapp1 hello\e[0m --- greeting message
1081
+ END
1082
+ end
645
1083
 
646
- \e[34mExample:\e[0m (see https://...)
647
- $ echo foobar
1084
+ spec "[!7uy4f] includes `detail:` kwarg value of `@action.()` if specified." do
1085
+ @config.app_command = "testapp1"
1086
+ metadata = @registry.metadata_get("prepostamble")
1087
+ x = @builder.__send__(:section_preamble, metadata)
1088
+ ok {x} == <<"END"
1089
+ \e[1mtestapp1 prepostamble\e[0m --- preamble and postamble
648
1090
 
649
- \e[34mTips:\e[0m
650
- * foobar
1091
+ See https://....
651
1092
  END
652
1093
  end
653
1094
 
654
1095
  end
655
1096
 
656
1097
 
657
- topic '#build_aliases()' do
1098
+ topic '#section_usage()' do
658
1099
 
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
1100
+ spec "[!jca5d] not add '[<options>]' if action has no options." do
1101
+ metadata = @registry.metadata_get("noopt")
1102
+ x = @builder.__send__(:section_usage, metadata)
1103
+ ok {x}.NOT.include?("[<options>]")
1104
+ ok {x} == <<"END"
1105
+ \e[1;34mUsage:\e[0m
1106
+ $ \e[1mtestapp noopt\e[0m <aa> [<bb>]
1107
+ END
1108
+ #
1109
+ metadata = @registry.metadata_get("debuginfo") # has a hidden option
1110
+ x = @builder.__send__(:section_usage, metadata)
1111
+ ok {x}.NOT.include?("[<options>]")
1112
+ ok {x} == <<"END"
1113
+ \e[1;34mUsage:\e[0m
1114
+ $ \e[1mtestapp debuginfo\e[0m
1115
+ END
669
1116
  end
670
1117
 
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)
1118
+ spec "[!h5bp4] if `usage:` kwarg specified in `@action.()`, use it as usage string." do
1119
+ metadata = @registry.metadata_get("usagesample1")
1120
+ x = @builder.__send__(:section_usage, metadata)
1121
+ ok {x} == <<"END"
1122
+ \e[1;34mUsage:\e[0m
1123
+ $ \e[1mtestapp usagesample1\e[0m input.txt > output.txt
1124
+ END
677
1125
  end
678
1126
 
679
- after do
680
- restore_index()
1127
+ spec "[!nfuxz] `usage:` kwarg can be a string or an array of string." do
1128
+ metadata = @registry.metadata_get("usagesample2")
1129
+ x = @builder.__send__(:section_usage, metadata)
1130
+ ok {x} == <<"END"
1131
+ \e[1;34mUsage:\e[0m
1132
+ $ \e[1mtestapp usagesample2\e[0m [<options>] input.txt | less
1133
+ $ \e[1mtestapp usagesample2\e[0m [<options>] input.txt > output.txt
1134
+ END
681
1135
  end
682
1136
 
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
1137
+ spec "[!z3lh9] if `usage:` kwarg not specified in `@action.()`, generates usage string from method parameters." do
1138
+ metadata = @registry.metadata_get("argsample")
1139
+ x = @builder.__send__(:section_usage, metadata)
1140
+ ok {x}.include?("[<options>] <aa> <bb> [<cc> [<dd> [<rest>...]]]")
688
1141
  end
689
1142
 
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
1143
+ spec "[!iuctx] returns 'Usage:' section of action help message." do
1144
+ metadata = @registry.metadata_get("argsample")
1145
+ x = @builder.__send__(:section_usage, metadata)
1146
+ ok {x} == <<"END"
1147
+ \e[1;34mUsage:\e[0m
1148
+ $ \e[1mtestapp argsample\e[0m [<options>] <aa> <bb> [<cc> [<dd> [<rest>...]]]
697
1149
  END
698
1150
  end
699
1151
 
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/
1152
+ end
1153
+
1154
+
1155
+ topic '#section_descriptin()' do
1156
+
1157
+ spec "[!zeujz] returns 'Description:' section if action description is set." do
1158
+ metadata = @registry.metadata_get("hello")
1159
+ bkup = nil
1160
+ metadata.instance_eval {
1161
+ bkup = @description; @description = "Example of description."
1162
+ }
1163
+ at_end { metadata.instance_eval { @description = bkup } }
1164
+ str = @builder.__send__(:section_description, metadata)
1165
+ ok {str} == <<~"END"
1166
+ \e[1;34mDescription:\e[0m
1167
+ Example of description.
1168
+ END
704
1169
  end
705
1170
 
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
1171
+ spec "[!0zffw] returns nil if action description is nil." do
1172
+ metadata = @registry.metadata_get("hello")
1173
+ metadata.instance_eval { @description = nil }
1174
+ str = @builder.__send__(:section_description, metadata)
1175
+ ok {str} == nil
1176
+ end
1177
+
1178
+ end
1179
+
1180
+
1181
+ topic '#section_options()' do
1182
+
1183
+ spec "[!pafgs] returns 'Options:' section of help message." do
1184
+ metadata = @registry.metadata_get("hello")
1185
+ x = @builder.__send__(:section_options, metadata)
1186
+ ok {x} == <<"END"
1187
+ \e[1;34mOptions:\e[0m
1188
+ -l, --lang=<lang> : language name (en/fr/it)
715
1189
  END
716
1190
  end
717
1191
 
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
1192
+ spec "[!85wus] returns nil if action has no options." do
1193
+ metadata = @registry.metadata_get("noopt")
1194
+ x = @builder.__send__(:section_options, metadata)
1195
+ ok {x} == nil
1196
+ end
1197
+
1198
+ end
1199
+
1200
+
1201
+ topic '#section_aliases()' do
1202
+
1203
+ spec "[!kjpt9] returns 'Aliases:' section of help message." do
1204
+ action = "alitest1"
1205
+ x = @registry.metadata_each.any? {|md| md.alias? && md.action == action }
1206
+ ok {x} == true
1207
+ #
1208
+ metadata = @registry.metadata_get("alitest1")
1209
+ output = @builder.__send__(:section_aliases, metadata)
1210
+ ok {output} == <<"END"
1211
+ \e[1;34mAliases:\e[0m
1212
+ alitest1x : alias for 'alitest1'
1213
+ alitest1y : alias for 'alitest1 --aa'
1214
+ alitest1z : alias for 'alitest1 --aa foobar'
727
1215
  END
728
- end
729
1216
  end
730
1217
 
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
1218
+ spec "[!cjr0q] returns nil if action has no options." do
1219
+ action = "argsample"
1220
+ x = @registry.metadata_each.any? {|md| md.alias? && md.action == action }
1221
+ ok {x} == false
1222
+ #
1223
+ metadata = @registry.metadata_get(action)
1224
+ output = @builder.__send__(:section_aliases, metadata)
1225
+ ok {output} == nil
736
1226
  end
737
1227
 
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
1228
+ end
1229
+
1230
+
1231
+ topic '#section_postamble()' do
1232
+
1233
+ spec "[!q1jee] returns postamble of help message if `postamble:` kwarg specified in `@action.()`." do
1234
+ metadata = @registry.metadata_get("prepostamble")
1235
+ x = @builder.__send__(:section_postamble, metadata)
1236
+ ok {x} == <<"END"
1237
+ \e[1;34mExamples:\e[0m
1238
+ $ echo
1239
+
1240
+ (Tips: blabla)
746
1241
  END
747
1242
  end
748
1243
 
1244
+ spec "[!jajse] returns nil if postamble is not set." do
1245
+ metadata = @registry.metadata_get("hello")
1246
+ x = @builder.__send__(:section_postamble, metadata)
1247
+ ok {x} == nil
1248
+ end
1249
+
749
1250
  end
750
1251
 
751
1252