File: 32finder-global-opts.t

package info (click to toggle)
libcommandable-perl 0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 228 kB
  • sloc: perl: 1,530; makefile: 2
file content (76 lines) | stat: -rw-r--r-- 1,707 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/perl

use v5.14;
use warnings;

use Test2::V0;

use Commandable::Finder::Packages;

my $cmd_opts;

package MyTest::Command::cmd {
   use constant COMMAND_NAME => "cmd";
   use constant COMMAND_DESC => "the cmd command";

   use constant COMMAND_OPTS => (
      { name => "verbose|v", description => "verbose option" },
      { name => "target|t=", description => "target option" },
   );

   sub run {
      $cmd_opts = shift;
   }
}

my $finder = Commandable::Finder::Packages->new(
   base => "MyTest::Command",
);

my $THREE;
$finder->add_global_options(
   { name => "one", into => \my $ONE },
   { name => "two=", into => \my $TWO, default => 444 },
   { name => "three=", into => sub { $THREE = $_[1] } },
);

{
   undef $ONE; undef $TWO; undef $THREE;

   $finder->find_and_invoke_list( qw( --one --two=222 --three=three cmd ) );

   is( $ONE, T(), '$ONE is true after --one' );
   is( $TWO, 222, '$TWO is 222 after --two=222' );
   is( $THREE, "three", '$THREE is three after --three=three' );
}

# mixed ordering
{
   undef $ONE; undef $TWO; undef $THREE;

   $finder->find_and_invoke_list( qw( cmd --three=later ) );

   is( $THREE, "later", '$THREE is parsed even after command name' );
}

# command-specific opts still work
{
   undef $TWO; undef $cmd_opts;

   $finder->find_and_invoke_list( qw( cmd --three=abc --target=def ) );

   is( $THREE, "abc",                  '$THREE is parsed with command opt' );
   is( $cmd_opts, { target => "def" }, 'target is parsed with command opt' );
}

# defaults
{
   undef $ONE; undef $TWO;

   $finder->find_and_invoke_list( qw( cmd ) );

   is( $ONE, F(), '$ONE defaults false' );
   is( $TWO, 444, '$TWO defaults to 444' );
}

done_testing;