File: 30finder-invoke.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 (99 lines) | stat: -rw-r--r-- 2,003 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/perl

use v5.14;
use warnings;

use Test2::V0;

use Commandable::Finder::Packages;

my $cmd_opts;
my $cmd_args;

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

   use constant COMMAND_ARGS => (
      { name => "arg", description => "the argument", optional => 1 }
   );

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

   sub run {
      $cmd_opts = shift;
      $cmd_args = [ @_ ];
   }
}

my $cmd2_args;
package MyTest::Command::cmd2 {
   use constant COMMAND_NAME => "cmd2";
   use constant COMMAND_DESC => "the cmd2 command";
   use constant COMMAND_ARGS => (
      { name => "arg", description => "the argument" },
   );
   sub run {
      $cmd2_args = [ @_ ];
   }
}

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

   allow_multiple_commands => 1,
);

# no args
{
   undef $cmd_args;

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

   ok( defined $cmd_args, 'cmd command invoked' );
   is( $cmd_args, [], 'cmd command given no args' );
}

# one arg
{
   undef $cmd_args;

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

   is( $cmd_args, [ "argument" ], 'cmd command given one arg' );
}

# one option
{
   undef $cmd_args;
   undef $cmd_opts;

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

   is( $cmd_args, [], 'cmd command given one option' );
   is( $cmd_opts, { verbose => 1 }, 'cmd command given one option' );
}

# two options
{
   undef $cmd_args;
   undef $cmd_opts;

   $finder->find_and_invoke_list( qw( cmd --verbose --target=red ) );

   is( $cmd_args, [], 'cmd command given two options' );
   is( $cmd_opts, { verbose => 1, target => "red" }, 'cmd command given two options' );
}

# multiple commands
{
   undef $cmd_args;
   undef $cmd_opts;

   $finder->find_and_invoke_list( qw( cmd arg cmd2 arg2 ) );
}

done_testing;