Debian Codegraph @

t/unit/App/Yath/Option.t

raw
1 use Test2::V0 -target => 'App::Yath::Option';
2
3 use Test2::Harness::Settings;
4
5 subtest types => sub {
6 ok($CLASS->valid_type($_), "'$_' is a valid type") for qw/b c s m d D h H/;
7 ok(!$CLASS->valid_type('x'), "'x' is not a valid type");
8
9 is($CLASS->canon_type($_), 'b', "Converted '$_' to 'b'") for qw/bool boolean/;
10 is($CLASS->canon_type($_), 'c', "Converted '$_' to 'c'") for qw/count counter counting/;
11 is($CLASS->canon_type($_), 's', "Converted '$_' to 's'") for qw/scalar string number/;
12 is($CLASS->canon_type($_), 'm', "Converted '$_' to 'm'") for qw/multi multiple list array/;
13 is($CLASS->canon_type($_), 'd', "Converted '$_' to 'd'") for qw/default def/;
14 is($CLASS->canon_type($_), 'D', "Converted '$_' to 'D'") for qw/multi-def multiple-default list-default array-default/;
15 is($CLASS->canon_type($_), 'h', "Converted '$_' to 'h'") for qw/hash/;
16 is($CLASS->canon_type($_), 'H', "Converted '$_' to 'H'") for qw/hash-list/;
17
18 for my $t (qw/s m h H/) {
19 my $one = bless {type => $t}, $CLASS;
20 is($one->requires_arg(), T(), "type '$t' requires an arg");
21 is($one->allows_arg(), T(), "type '$t' does allow an arg");
22 }
23
24 for my $t (qw/d D/) {
25 my $one = bless {type => $t}, $CLASS;
26 is($one->requires_arg(), F(), "type '$t' does not require an arg");
27 is($one->allows_arg(), T(), "type '$t' does allow an arg");
28 }
29
30 for my $t (qw/b c/) {
31 my $one = bless {type => $t}, $CLASS;
32 is($one->requires_arg(), F(), "type '$t' does not require an arg");
33 is($one->allows_arg(), F(), "type '$t' does not allow an arg");
34 }
35 };
36
37 subtest init => sub {
38 like(
39 dies { $CLASS->new() },
40 qr/You must specify 'title' or both 'field' and 'name'/,
41 "Need 'title', or 'field' and 'name'"
42 );
43
44 like(
45 dies { $CLASS->new(title => 'foo') },
46 qr/The 'prefix' attribute is required/,
47 "prefix is required"
48 );
49
50 like(
51 dies { $CLASS->new(title => 'foo', prefix => 'xxx', alt => 'xxx') },
52 qr/The 'alt' attribute must be an array-ref/,
53 "Alt, when present must be an arrayref"
54 );
55
56 my $one = $CLASS->new(title => 'foo-bar_baz', prefix => 'xxx');
57 isa_ok($one, [$CLASS], "Instance of $CLASS");
58 is($one->title, 'foo-bar_baz', "set title");
59 is($one->field, 'foo_bar_baz', "field has underscores");
60 is($one->name, 'foo-bar-baz', "name has dashes");
61 is($one->type, 'b', "Default type is boolean");
62
63 $one = $CLASS->new(title => 'foo-bar_baz', prefix => 'xxx', from_plugin => 1);
64 is($one->title, 'foo-bar_baz', "set title");
65 is($one->field, 'foo_bar_baz', "field has underscores");
66 is($one->name, 'xxx-foo-bar-baz', "name has dashes, prefix is in place if it is a plugin option");
67 is($one->type, 'b', "Default type is boolean");
68
69 {
70 package Foo;
71 Test2::Harness::Util::HashBase->import(qw/bar/);
72 }
73
74 like(
75 dies { $CLASS->new(title => 'baz', prefix => 'xxx', builds => 'Foo') },
76 qr/class 'Foo' does not have a 'baz' method/,
77 "If the option is supposed to build a specific class, make sure the class knows"
78 );
79
80 ok($CLASS->new(title => 'bar', prefix => 'xxx', builds => 'Foo'), "Construction is fine if build package has the right method");
81
82 ok($CLASS->new(title => 'bar', prefix => 'xxx', type => 's'), "'s' is a valid type");
83 is($CLASS->new(title => 'bar', prefix => 'xxx', type => 'scalar')->type, 's', "'scalar' is a valid type, turns into 's'");
84
85 like(
86 dies { $CLASS->new(title => 'bar', prefix => 'xxx', type => 'uhg') },
87 qr/Invalid type 'uhg'/,
88 "Type must be valid"
89 );
90
91 is($CLASS->new(title => 'foo', prefix => 'xxx', default => 'foo')->default, 'foo', "Simple string default is fine");
92 is($CLASS->new(title => 'foo', prefix => 'xxx', default => 123)->default, 123, "Simple number default is fine");
93 is($CLASS->new(title => 'foo', prefix => 'xxx', default => \&T)->default, exact_ref(\&T), "Can use a coderef for default");
94 like(
95 dies { $CLASS->new(title => 'foo', prefix => 'xxx', default => []) },
96 qr/'default' must be a simple scalar, or a coderef, got a 'ARRAY/,
97 "Cannot use a non-coderef ref as a default"
98 );
99
100 for my $attr (qw/normalize action/) {
101 is($CLASS->new(title => 'foo', prefix => 'xxx', $attr => \&T)->$attr, exact_ref(\&T), "Can set $attr to a coderef");
102 is($CLASS->new(title => 'foo', prefix => 'xxx', $attr => undef)->$attr, undef, "Can set $attr to undef");
103
104 like(
105 dies { $CLASS->new(title => 'foo', prefix => 'xxx', $attr => []) },
106 qr/'$attr' must be undef, or a coderef, got 'ARRAY/,
107 "Cannot use a non-coderef ref with $attr"
108 );
109
110 like(
111 dies { $CLASS->new(title => 'foo', prefix => 'xxx', $attr => 1) },
112 qr/'$attr' must be undef, or a coderef, got 'not a ref'/,
113 "Cannot use a scalar with $attr"
114 );
115 }
116
117 $one = $CLASS->new(title => 'foo', prefix => 'xxx');
118 is($one->trace, array { item __PACKAGE__; item __FILE__; item __LINE__ - 1; etc; }, "Got correct trace");
119 is($one->category, 'NO CATEGORY - FIX ME', "Default category");
120 is($one->description, 'NO DESCRIPTION - FIX ME', "Default description");
121
122 like(
123 dies { $CLASS->new(title => 'foo', prefix => 'xxx', foo => 'bar') },
124 qr/'foo' is not a valid option attribute/,
125 "All construction args must be valid"
126 );
127 };
128
129 subtest applicable => sub {
130 my $options = 'foo';
131 my $one = $CLASS->new(title => 'foo', prefix => 'xxx');
132 is($one->applicable($options), T(), "Unless a callback was provided and option is always applicable.");
133
134 my $args;
135 $one = $CLASS->new(title => 'foo', prefix => 'xxx', applicable => sub {$args = [@_]; 0});
136
137 is($one->applicable($options), F(), "Used value from callback");
138 is($args, [exact_ref($one), $options], "Callback got the necessary args");
139 };
140
141 subtest long_args => sub {
142 my $one = $CLASS->new(title => 'foo', prefix => 'xxx');
143 is([$one->long_args], [qw/foo/], "Got long args");
144
145 $one = $CLASS->new(title => 'foo', prefix => 'xxx', alt => [qw/a b c/]);
146 is([$one->long_args], [qw/foo a b c/], "Got long args");
147 };
148
149 subtest option_slot => sub {
150 my $one = $CLASS->new(title => 'foo', prefix => 'xxx');
151
152 my $settings = Test2::Harness::Settings->new();
153
154 ok(my $slot = $one->option_slot($settings), "Got the slot");
155 is($$slot, undef, "slot is a reference pointing to a scalar with an undef value");
156 is($settings->xxx->foo, undef, "Vivified in settings");
157 $$slot = 123;
158 is($settings->xxx->foo, 123, "Setting the slotref sets it in settings");
159
160 like(
161 dies { $one->option_slot() },
162 qr/A settings instance is required/,
163 "Need to pass in settings"
164 );
165 };
166
167 subtest get_default => sub {
168 my $new = sub { $CLASS->new(title => 'foo', prefix => 'xxx', @_) };
169 is($new->(type => 's')->get_default, undef, "default for scalar is undef");
170 is($new->(type => 'd')->get_default, undef, "default for 'd' is undef");
171 is($new->(type => 'b')->get_default, 0, "default for boolean is 0");
172 is($new->(type => 'c')->get_default, 0, "default for count is 0");
173 is($new->(type => 'm')->get_default, [], "default for multi is an empty array");
174 is($new->(type => 'D')->get_default, [], "default for multi-d is an empty array");
175 is($new->(type => 'h')->get_default, {}, "default for hash is an empty hash");
176 is($new->(type => 'H')->get_default, {}, "default for multi-hash is an empty hash");
177
178 is($new->(type => 's', default => 123)->get_default, 123, "Used simple default");
179 is($new->(type => 's', default => sub { 'xxx' })->get_default, 'xxx', "Used default generator");
180 };
181
182 subtest get_normalized => sub {
183 my $new = sub { $CLASS->new(title => 'foo', prefix => 'xxx', @_) };
184
185 is($new->(type => 'b')->get_normalized('a'), 1, "Boolean normalized to true");
186 is($new->(type => 'b')->get_normalized(''), 0, "Boolean normalized to false");
187
188 is($new->(type => 's')->get_normalized('foo'), 'foo', "Normalize does not change most things");
189
190 is($new->(type => 'h')->get_normalized('foo=bar'), ['foo', 'bar'], "Simple hash parse/normalize");
191 is($new->(type => 'h')->get_normalized('foo=bar=baz,bat'), ['foo', 'bar=baz,bat'], "Do not do anything special for 'h' values");
192 is($new->(type => 'h')->get_normalized('foo'), ['foo', 1], "Value is 1 if nothing is specified");
193
194 is($new->(type => 'H')->get_normalized('foo=bar'), ['foo', ['bar']], "Simple multi-hash parse/normalize");
195 is($new->(type => 'H')->get_normalized('foo=bar=baz,bat'), ['foo', ['bar=baz', 'bat']], "Split 'H' by comma");
196 is($new->(type => 'H')->get_normalized('foo'), ['foo', []], "Value is [] if nothing is specified");
197 };
198
199 subtest handle => sub {
200 require App::Yath::Options;
201 my $options = App::Yath::Options->new();
202 my $new = sub { $CLASS->new(title => 'foo', prefix => 'xxx', @_), Test2::Harness::Settings->new() };
203
204 my ($one, $settings) = $new->(type => 'c');
205 $one->handle(1, $settings, $options);
206 is($settings->xxx->foo, 1, "increment by 1");
207 $one->handle('a', $settings, $options);
208 is($settings->xxx->foo, 2, "increment by 1 again");
209
210 ($one, $settings) = $new->(type => 'm');
211 $one->handle('a', $settings, $options);
212 is($settings->xxx->foo, ['a'], "Pushed value");
213 $one->handle('b', $settings, $options);
214 is($settings->xxx->foo, ['a', 'b'], "Pushed value again");
215
216 ($one, $settings) = $new->(type => 'D');
217 $one->handle('a', $settings, $options);
218 is($settings->xxx->foo, ['a'], "Pushed value");
219 $one->handle('b', $settings, $options);
220 is($settings->xxx->foo, ['a', 'b'], "Pushed value again");
221
222 ($one, $settings) = $new->(type => 'h');
223 $one->handle('foo=bar', $settings, $options);
224 is($settings->xxx->foo, {'@' => ['foo'], foo => 'bar'}, "Set value and added it to the list key");
225 $one->handle('foo=baz', $settings, $options);
226 is($settings->xxx->foo, {'@' => ['foo'], foo => 'baz'}, "Reset value, not duplicated in the list key");
227 $one->handle('fog=baz', $settings, $options);
228 is($settings->xxx->foo, {'@' => ['foo', 'fog'], foo => 'baz', fog => 'baz'}, "Set second key");
229
230 ($one, $settings) = $new->(type => 'H');
231 $one->handle('foo=bar', $settings, $options);
232 is($settings->xxx->foo, {'@' => ['foo'], foo => ['bar']}, "Set value and added it to the list key");
233 $one->handle('foo=baz,bat', $settings, $options);
234 is($settings->xxx->foo, {'@' => ['foo'], foo => ['bar', 'baz', 'bat']}, "Added more values");
235 $one->handle('fog', $settings, $options);
236 is($settings->xxx->foo, {'@' => ['foo', 'fog'], foo => ['bar', 'baz', 'bat'], fog => []}, "Set second key");
237
238 my $args;
239 ($one, $settings) = $new->(type => 'H', action => sub {
240 my ($prefix, $field, $raw, $norm, $slot, $settings, $handler) = @_;
241 $args = [@_];
242 $handler->($slot, $norm);
243 return 'xxx';
244 });
245
246 is($one->handle('foo=baz,bat', $settings, $options), 'xxx', "Returned value from action");
247 is($settings->xxx->foo, {'@' => ['foo'], foo => ['baz', 'bat']}, "Set value via handler");
248 is(
249 $args,
250 [
251 $one->prefix,
252 $one->field,
253 "foo=baz,bat",
254 [foo => ['baz', 'bat']],
255 exact_ref($one->option_slot($settings)),
256 exact_ref($settings),
257 meta { prop reftype => 'CODE' },
258 exact_ref($options),
259 ],
260 "Got args"
261 );
262 };
263
264 subtest handle_negation => sub {
265 require App::Yath::Options;
266 my $options = App::Yath::Options->new();
267 my $new = sub { $CLASS->new(title => 'foo', prefix => 'xxx', @_), Test2::Harness::Settings->new() };
268
269 for my $type (qw/b c/) {
270 my ($one, $settings) = $new->(type => $type);
271 $one->handle(1, $settings, $options);
272 is($settings->xxx->foo, 1, "'$type' Is set");
273 $one->handle_negation($settings, $options);
274 is($settings->xxx->foo, 0, "'$type' Cleared");
275 }
276
277 for my $type (qw/m D/) {
278 my ($one, $settings) = $new->(type => $type);
279 $one->handle('abc', $settings, $options);
280 is($settings->xxx->foo, ['abc'], "'$type' Is set");
281 $one->handle_negation($settings, $options);
282 is($settings->xxx->foo, [], "'$type' Cleared");
283 }
284
285 for my $type (qw/h H/) {
286 my ($one, $settings) = $new->(type => $type);
287 $one->handle('abc', $settings, $options);
288 is($settings->xxx->foo, {'@' => ['abc'], abc => T()}, "'$type' Is set");
289 $one->handle_negation($settings, $options);
290 is($settings->xxx->foo, {}, "'$type' Cleared");
291 }
292
293 my ($one, $settings) = $new->(type => 's');
294 $one->handle('abc', $settings, $options);
295 is($settings->xxx->foo, 'abc', "'s' Is set");
296 $one->handle_negation($settings, $options);
297 is($settings->xxx->foo, undef, "'s' Cleared");
298 };
299
300 subtest trace_string => sub {
301 my $one = $CLASS->new(prefix => 'xxx', title => 'foo', trace => ['Foo', 'foo.pm', 42]);
302 is($one->trace_string(), "foo.pm line 42", "Valid trace string");
303 };
304
305 subtest cli_docs => sub {
306 my $one = $CLASS->new(
307 type => 'b',
308 prefix => 'xxx',
309 title => 'foo',
310 short => 'F',
311 description => 'This is foo bar baz bat gsdgdsgfsdd',
312 );
313
314 require Test2::Util::Term;
315 my $c = mock 'Test2::Util::Term' => (
316 override => [term_size => sub { 10 }], # Default to super small to make sure we do something sane
317 );
318
319 is($one->cli_docs, "--foo, -F, --no-foo\n This is foo bar baz bat gsdgdsgfsdd", "Got docs");
320
321 $one = $CLASS->new(
322 type => 'H',
323 prefix => 'xxx',
324 title => 'foo',
325 short => 'F',
326 description => 'This is foo bar baz bat gsdgdsgfsdd',
327 );
328
329 chomp(my $res = <<' EOT');
330 --foo KEY=VAL1,VAL2,..., --foo=KEY=VAL1,VAL2,..., -F KEY=VAL1,VAL2,...
331 -F=KEY=VAL1,VAL2,..., --no-foo
332 This is foo bar baz bat gsdgdsgfsdd
333
334 Note: Can be specified multiple times. If the same key is listed multiple times the value lists will be appended together.
335 EOT
336
337 is($one->cli_docs, $res, "Got more complex docs");
338
339 $one = $CLASS->new(
340 type => 'H',
341 prefix => 'xxx',
342 title => 'foo',
343 alt => ['bar', 'baz'],
344 short => 'F',
345 description => 'This is foo bar baz bat gsdgdsgfsdd',
346 long_examples => [' KEY=VALX,VALY,...', '=KEY=VALX,VALY,...'],
347 short_examples => [' KEY=VALX,VALY,...', '=KEY=VALX,VALY,...'],
348 );
349
350 chomp($res = <<' EOT');
351 --foo KEY=VALX,VALY,..., --foo=KEY=VALX,VALY,..., --bar KEY=VALX,VALY,...
352 --bar=KEY=VALX,VALY,..., --baz KEY=VALX,VALY,..., --baz=KEY=VALX,VALY,...
353 -F KEY=VALX,VALY,..., -F=KEY=VALX,VALY,..., --no-foo
354 This is foo bar baz bat gsdgdsgfsdd
355
356 Note: Can be specified multiple times. If the same key is listed multiple times the value lists will be appended together.
357 EOT
358
359 is($one->cli_docs, $res, "Got more complex docs with custom examples");
360 };
361
362 subtest pod_docs => sub {
363 my $one = $CLASS->new(
364 type => 'b',
365 prefix => 'xxx',
366 title => 'foo',
367 short => 'F',
368 description => 'This is foo bar baz bat gsdgdsgfsdd',
369 );
370
371 require Test2::Util::Term;
372 my $c = mock 'Test2::Util::Term' => (
373 override => [term_size => sub { 10 }], # Default to super small to make sure we do something sane
374 );
375
376 is($one->pod_docs, <<' EOT', "Got docs");
377 =item --foo
378
379 =item -F
380
381 =item --no-foo
382
383 This is foo bar baz bat gsdgdsgfsdd
384 EOT
385
386 $one = $CLASS->new(
387 type => 'H',
388 prefix => 'xxx',
389 title => 'foo',
390 short => 'F',
391 description => 'This is foo bar baz bat gsdgdsgfsdd',
392 );
393
394 is($one->pod_docs, <<' EOT', "Got more complex docs");
395 =item --foo KEY=VAL1,VAL2,...
396
397 =item --foo=KEY=VAL1,VAL2,...
398
399 =item -F KEY=VAL1,VAL2,...
400
401 =item -F=KEY=VAL1,VAL2,...
402
403 =item --no-foo
404
405 This is foo bar baz bat gsdgdsgfsdd
406
407 Can be specified multiple times. If the same key is listed multiple times the value lists will be appended together.
408 EOT
409
410 $one = $CLASS->new(
411 type => 'H',
412 prefix => 'xxx',
413 title => 'foo',
414 alt => ['bar', 'baz'],
415 short => 'F',
416 description => 'This is foo bar baz bat gsdgdsgfsdd',
417 long_examples => [' KEY=VALX,VALY,...', '=KEY=VALX,VALY,...'],
418 short_examples => [' KEY=VALX,VALY,...', '=KEY=VALX,VALY,...'],
419 );
420
421 is($one->pod_docs, <<' EOT', "Got more complex docs with custom examples");
422 =item --foo KEY=VALX,VALY,...
423
424 =item --foo=KEY=VALX,VALY,...
425
426 =item --bar KEY=VALX,VALY,...
427
428 =item --bar=KEY=VALX,VALY,...
429
430 =item --baz KEY=VALX,VALY,...
431
432 =item --baz=KEY=VALX,VALY,...
433
434 =item -F KEY=VALX,VALY,...
435
436 =item -F=KEY=VALX,VALY,...
437
438 =item --no-foo
439
440 This is foo bar baz bat gsdgdsgfsdd
441
442 Can be specified multiple times. If the same key is listed multiple times the value lists will be appended together.
443 EOT
444 };
445
446
447 done_testing;