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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
#!/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 => "the one command";
use constant COMMAND_OPTS => (
{ name => "verbose|v", description => "verbose option", mode => "inc" },
{ name => "target|t=", description => "target option" },
{ name => "multi", description => "multi option", multi => 1 },
{ name => "hyphenated-name|h", description => "option with hyphen in its name" },
{ name => "number|n=i", description => "number option" },
{ name => "count|c=u", description => "count option" },
{ name => "size=f", description => "float option" },
);
sub run {}
}
package MyTest::Command::two {
use constant COMMAND_NAME => "two";
use constant COMMAND_DESC => "the two command";
use constant COMMAND_OPTS => (
{ name => "with-default", description => "default option", default => "value" },
);
sub run {}
}
package MyTest::Command::three {
use constant COMMAND_NAME => "three";
use constant COMMAND_DESC => "the three command";
use constant COMMAND_OPTS => (
{ name => "silent", description => "silent option", mode => "bool", default => 1 },
);
sub run {}
}
my $finder = Commandable::Finder::Packages->new(
base => "MyTest::Command",
);
# no opt
{
my $cmd = $finder->find_command( "one" );
my $inv = Commandable::Invocation->new( "" );
is( [ $finder->parse_invocation( $cmd, $inv ) ], [ {} ],
'$cmd->parse_invocation with no options' );
ok( !length $inv->peek_remaining, '->parse_invocation consumed input' );
}
# opt by longname
{
my $cmd = $finder->find_command( "one" );
my $inv = Commandable::Invocation->new( "--verbose" );
is( [ $finder->parse_invocation( $cmd, $inv ) ], [ { verbose => 1 } ],
'$cmd->parse_invocation with longname' );
ok( !length $inv->peek_remaining, '->parse_invocation consumed input' );
}
# opt by shortname
{
my $cmd = $finder->find_command( "one" );
my $inv = Commandable::Invocation->new( "-v" );
is( [ $finder->parse_invocation( $cmd, $inv ) ], [ { verbose => 1 } ],
'$cmd->parse_invocation with shortname' );
ok( !length $inv->peek_remaining, '->parse_invocation consumed input' );
}
# hyphen converts to underscore
{
my $cmd = $finder->find_command( "one" );
my $inv = Commandable::Invocation->new( "-h" );
is( [ $finder->parse_invocation( $cmd, $inv ) ], [ { hyphenated_name => 1 } ],
'$cmd->parse_invocation with hyphenated name' );
ok( !length $inv->peek_remaining, '->parse_invocation consumed input' );
}
# opt with value (space)
{
my $cmd = $finder->find_command( "one" );
my $inv = Commandable::Invocation->new( "--target TARG" );
is( [ $finder->parse_invocation( $cmd, $inv ) ], [ { target => "TARG" } ],
'$cmd->parse_invocation with space-separated value' );
ok( !length $inv->peek_remaining, '->parse_invocation consumed input' );
}
# opt with value (equals)
{
my $cmd = $finder->find_command( "one" );
my $inv = Commandable::Invocation->new( "--target=TARG" );
is( [ $finder->parse_invocation( $cmd, $inv ) ], [ { target => "TARG" } ],
'$cmd->parse_invocation with equals-separated value' );
ok( !length $inv->peek_remaining, '->parse_invocation consumed input' );
}
# multi value
{
my $cmd = $finder->find_command( "one" );
my $inv = Commandable::Invocation->new( "--multi=one --multi two" );
is( [ $finder->parse_invocation( $cmd, $inv ) ], [ { multi => [ qw(one two) ] } ],
'$cmd->parse_invocation with repeated value' );
ok( !length $inv->peek_remaining, '->parse_invocation consumed input' );
}
# opt with default value
{
my $cmd = $finder->find_command( "two" );
my $inv = Commandable::Invocation->new( "" );
is( [ $finder->parse_invocation( $cmd, $inv ) ], [ { with_default => "value" } ],
'$cmd->parse_invocation with default option' );
ok( !length $inv->peek_remaining, '->parse_invocation consumed input' );
}
# negatable opt with default value
{
my $cmd = $finder->find_command( "three" );
my $inv = Commandable::Invocation->new( "" );
is( [ $finder->parse_invocation( $cmd, $inv ) ], [ { silent => 1 } ],
'$cmd->parse_invocation with negatable option' );
ok( !length $inv->peek_remaining, '->parse_invocation consumed input' );
}
# negated opt with default value
{
my $cmd = $finder->find_command( "three" );
my $inv = Commandable::Invocation->new( "--no-silent" );
is( [ $finder->parse_invocation( $cmd, $inv ) ], [ { silent => !!0 } ],
'$cmd->parse_invocation with negated option' );
ok( !length $inv->peek_remaining, '->parse_invocation consumed input' );
}
# incrementable opt
{
my $cmd = $finder->find_command( "one" );
my $inv = Commandable::Invocation->new( "-v -v -v" );
is( [ $finder->parse_invocation( $cmd, $inv ) ], [ { verbose => 3 } ],
'$cmd->parse_invocation with repeated incrementable option' );
ok( !length $inv->peek_remaining, '->parse_invocation consumed input' );
}
# incrementable opts can't take values
{
my $cmd = $finder->find_command( "one" );
my $inv = Commandable::Invocation->new( "-v3" );
like( dies { $finder->parse_invocation( $cmd, $inv ) },
qr/^Unexpected value for parameter verbose/,
'$cmd->parse_invocation fails with value to incrementable option' );
}
# typed options
{
my $cmd = $finder->find_command( "one" );
ok( lives {
is( [ $finder->parse_invocation( $cmd,
Commandable::Invocation->new( "-n1" ) ) ],
[ { number => 1 } ],
'$cmd->parse_invocation with integer-numerical option' );
} );
like( dies { $finder->parse_invocation( $cmd,
Commandable::Invocation->new( "-nBAD" ) ) },
qr/^Value for --number option must be an integer/,
'$cmd->parse_invocation fails with non-integer value' );
ok( lives {
is( [ $finder->parse_invocation( $cmd,
Commandable::Invocation->new( "-c5" ) ) ],
[ { count => 5 } ],
'$cmd->parse_invocation with integer count option' );
} );
like( dies { $finder->parse_invocation( $cmd,
Commandable::Invocation->new( "-c-5" ) ) },
qr/^Value for --count option must be a non-negative integer/,
'$cmd->parse_invocation fails with negative count' );
ok( lives {
is( [ $finder->parse_invocation( $cmd,
Commandable::Invocation->new( "--size=1.234" ) ) ],
[ { size => 1.234 } ],
'$cmd->parse_invocation with size option' );
} );
like( dies { $finder->parse_invocation( $cmd,
Commandable::Invocation->new( "--size=BAD" ) ) },
qr/^Value for --size option must be a floating-point number/,
'$cmd->parse_invocation fails with bad size' );
}
done_testing;
|