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
|
#!/usr/bin/perl
use v5.14;
use warnings;
use Test2::V0;
use Commandable::Invocation;
use Commandable::Finder::Packages;
package MyTest::Command::one {
use constant COMMAND_NAME => "one";
use constant COMMAND_DESC => "a basic command";
use constant COMMAND_ARGS => (
{ name => "arg", description => "the argument" }
);
sub run {}
}
package MyTest::Command::optarg {
use constant COMMAND_NAME => "optarg";
use constant COMMAND_DESC => "a command with an optional argument";
use constant COMMAND_ARGS => (
{ name => "arg", description => "the argument", optional => 1 }
);
sub run {}
}
package MyTest::Command::slurpyarg {
use constant COMMAND_NAME => "slurpyarg";
use constant COMMAND_DESC => "a command with a slurpy argument";
use constant COMMAND_ARGS => (
{ name => "args", description => "the arguments", slurpy => 1 }
);
sub run {}
}
my $finder = Commandable::Finder::Packages->new(
base => "MyTest::Command",
);
# mandatory arg
{
my $cmd = $finder->find_command( "one" );
my $inv = Commandable::Invocation->new( "value" );
is( [ $finder->parse_invocation( $cmd, $inv ) ], [qw( value )],
'$cmd->parse_invocation with mandatory argument' );
ok( !length $inv->peek_remaining, '->parse_invocation consumed input' );
like(
dies { $finder->parse_invocation( $cmd, Commandable::Invocation->new( "" ) ) },
qr/^Expected a value for 'arg' argument/,
'$cmd->parse_invocation fails with no argument' );
}
# optional arg
{
my $cmd = $finder->find_command( "optarg" );
my $inv = Commandable::Invocation->new( "value" );
is( [ $finder->parse_invocation( $cmd, $inv ) ], [qw( value )],
'$cmd->parse_invocation with optional argument present' );
ok( !length $inv->peek_remaining, '->parse_invocation consumed input' );
is( [ $finder->parse_invocation( $cmd, Commandable::Invocation->new( "" ) ) ], [],
'$cmd->parse_invocation with optional argument absent' );
}
# slurpy arg
{
my $cmd = $finder->find_command( "slurpyarg" );
my $inv = Commandable::Invocation->new( "x y z" );
is( [ $finder->parse_invocation( $cmd, $inv ) ], [ [qw( x y z )] ],
'$cmd->parse_invocation with slurpy argument' );
ok( !length $inv->peek_remaining, '->parse_invocation consumed input' );
}
done_testing;
|