view release on metacpan or search on metacpan
lib/MooX/Options/Role.pm view on Meta::CPAN
my %options_data = $class->_options_data;
my %options_config = $class->_options_config;
if ( defined $options_config{skip_options} ) {
delete @options_data{ @{ $options_config{skip_options} } };
}
my ( $options, $has_to_split, $all_options )
= _options_prepare_descriptive( \%options_data );
local @ARGV = @ARGV if $options_config{protect_argv};
@ARGV = _options_fix_argv( \%options_data, $has_to_split, $all_options );
my @flavour;
if ( defined $options_config{flavour} ) {
push @flavour, { getopt_conf => $options_config{flavour} };
}
my $prog_name = $class->_options_prog_name();
# create usage str
t/02-autosplit_warning_on_required_param.t view on Meta::CPAN
documentation => 'this is mandatory',
format => 's@',
required => 1,
autosplit => ",",
);
1;
}
{
local @ARGV = ('--treq');
trap { my $opt = t->new_with_options(); };
like $trap->stderr, qr/Option treq requires an argument/, 'stdout ok';
unlike $trap->stderr, qr/Use of uninitialized/, 'stderr ok';
}
done_testing;
t/06-failure.t view on Meta::CPAN
trap {
eval "#line ${\(__LINE__+1 . ' ' . __FILE__)}\n" . <<__EOF__
{
package NonNegatableNegated;
use Moo;
use MooX::Options;
option 'legal' => (is => 'rw');
1;
}
local \@ARGV = ('--no-legal');
NonNegatableNegated->new_with_options;
__EOF__
;
};
like $trap->stderr, qr/^Unknown\soption:\sno_legal/x, 'Unexisting negation';
done_testing;
t/07-flavour.t view on Meta::CPAN
option 'bool' => ( is => 'ro' );
1;
}
for my $noflavour (qw/plain plain2/) {
subtest "unknown option $noflavour" => sub {
note "Without flavour $noflavour";
{
local @ARGV = ('anarg');
my $plain = $noflavour->new_with_options();
is_deeply( [@ARGV], ['anarg'], "anarg is left" );
}
{
local @ARGV = ( '--bool', 'anarg' );
my $plain = $noflavour->new_with_options();
is( $plain->bool, 1, "bool was set" );
is_deeply( [@ARGV], ['anarg'], "anarg is left" );
}
{
local @ARGV = ( '--bool', 'anarg', '--unknown_option' );
my @r = trap { $noflavour->new_with_options() };
is( $trap->exit, 1, "exit code ok" );
like(
$trap->stderr,
qr/Unknown option: unknown_option/,
"and a warning from GLD"
);
like( $trap->stderr, qr/USAGE:/, "died with usage message" );
}
};
}
subtest "flavour" => sub {
note "With flavour";
{
local @ARGV = ('anarg');
my $flavour_test = FlavourTest->new_with_options();
is_deeply( [@ARGV], ['anarg'], "anarg is left" );
}
{
local @ARGV = ( '--bool', 'anarg' );
my $flavour_test = FlavourTest->new_with_options();
is( $flavour_test->bool, 1, "bool was set" );
is_deeply( [@ARGV], ['anarg'], "anarg is left" );
}
{
local @ARGV = ( '--bool', 'anarg', '--unknown_option' );
my $flavour_test = FlavourTest->new_with_options();
is( $flavour_test->bool, 1, "bool was set" );
is_deeply(
[@ARGV],
[ 'anarg', '--unknown_option' ],
"anarg and unknown_option are left"
);
}
};
t/08-hidden.t view on Meta::CPAN
use MooX::Options;
option 'visible_option' => ( is => 'ro', doc => 'visible' );
option 'hidden_option_by_doc' =>
( is => 'ro', format => 's', doc => 'hidden' );
option 'hidden_option' =>
( is => 'ro', format => 's', hidden => 1, doc => 'not visible' );
1;
}
trap { local @ARGV = qw(--help); t->new_with_options };
unlike $trap->stdout, qr/hidden_option_by_doc:/, 'hidden by doc';
unlike $trap->stdout, qr/hidden_option:/, 'hidden by option';
like $trap->stdout, qr/visible_option:/, 'visible option';
{
local @ARGV = qw(--hidden_option_by_doc=test1 --hidden_option=test2);
my $o = t->new_with_options;
is $o->hidden_option_by_doc, 'test1', 'hidden by doc exists';
is $o->hidden_option, 'test2', 'hidden by option exists';
}
done_testing;
t/09-isa_check.t view on Meta::CPAN
is => 'ro',
doc => 'this is mandatory',
format => 's@',
isa => sub { die "boop\n" },
);
1;
}
{
local @ARGV = (qw/--hero batman/);
trap { my $opt = t->new_with_options(); };
like $trap->stderr, qr/^boop/, 'stdout ok';
like $trap->stderr, qr/USAGE/, 'stderr ok';
}
done_testing;
t/13-moox-cmd.t view on Meta::CPAN
if ($@) {
plan skip_all => 'Need MooX::Cmd (0.007) for this test';
exit 0;
}
}
use lib File::Spec->catdir( $RealBin, qw(lib) );
use MooXCmdTest;
trap {
local @ARGV = ('-h');
MooXCmdTest->new_with_cmd;
};
like $trap->stdout, qr{USAGE:\s\d{2}\Q-moox-cmd.t [-h]\E},
'base command help ok'
or diag( explain($trap) );
like $trap->stdout, qr{\QSUB COMMANDS AVAILABLE: test1, test3\E},
'sub base command help ok';
trap {
t/15-multiple-split-options.t view on Meta::CPAN
package TestMultipleSplitOptions;
use Moo;
use MooX::Options;
option 'opt' => ( is => 'ro', format => 'i@', autosplit => ',' );
option 'opt2' => ( is => 'ro', format => 'i@', autosplit => ',' );
1;
}
local @ARGV = ( '--opt', '1,2', '--opt2', '3,4' );
my $opt = TestMultipleSplitOptions->new_with_options;
is_deeply $opt->opt, [ 1, 2 ], 'opt got split correctly';
is_deeply $opt->opt2, [ 3, 4 ], 'opt2 got split correctly';
done_testing;
t/16-namespace_clean.t view on Meta::CPAN
BEGIN {
use Module::Runtime qw(use_module);
eval { use_module("TestNamespaceClean") }
or plan skip_all => "This test needs namespace::clean";
}
ok( TestNamespaceClean->new, 'TestNamespaceClean is a package' );
{
local @ARGV = ( '--foo', '12' );
my $i = TestNamespaceClean->new_with_options;
is $i->foo, 12, 'value save properly';
}
done_testing;
t/18-option-of-attr.t view on Meta::CPAN
package TestOptOfAttr;
use Moo;
use MooX::Options;
with "RoleOptOfAttr";
option '+opt' => ( format => 's' );
}
local @ARGV = ( '--opt', 'foo' );
my $opt = TestOptOfAttr->new_with_options;
is $opt->opt, 'foo', 'option of option is not changed for separated args';
local @ARGV = ('--opt=bar');
my $opt2 = TestOptOfAttr->new_with_options;
is $opt2->opt, 'bar', 'option of option is not changed for glued args';
done_testing;
t/19-option-of-option.t view on Meta::CPAN
{
package TestOptOfOpt;
use Moo;
use MooX::Options;
option 'opt' => ( is => 'ro', format => 's' );
1;
}
local @ARGV = ( '--opt', '--opt -y -my-options' );
my $opt = TestOptOfOpt->new_with_options;
is $opt->opt, '--opt -y -my-options',
'option of option is not changed for separated args';
local @ARGV = ('--opt=--opt -y -my-options');
my $opt2 = TestOptOfOpt->new_with_options;
is $opt2->opt, '--opt -y -my-options',
'option of option is not changed for glued args';
local @ARGV = ('--op=--opt -y -my-options');
my $opt3 = TestOptOfOpt->new_with_options;
is $opt3->opt, '--opt -y -my-options',
'option of option is not changed for shortened args';
done_testing;
t/21-role.t view on Meta::CPAN
package testSkipOpt;
use Moo;
use MooX::Options
skip_options => [qw/multi/],
flavour => [qw( pass_through )];
with 'myRole';
1;
}
{
local @ARGV;
@ARGV = ();
my $opt = testRole->new_with_options;
ok( !$opt->multi, 'multi not set' );
}
{
local @ARGV;
@ARGV = ('--multi');
my $opt = testRole->new_with_options;
ok( $opt->multi, 'multi set' );
trap {
$opt->options_usage;
};
like(
$trap->stdout,
qr/\-\-multi\s+multi\sthreading\smode/x,
"usage method is properly set"
);
}
{
local @ARGV;
@ARGV = ();
my $opt = testRole2->new_with_options;
ok( !$opt->multi, 'multi not set' );
}
{
local @ARGV;
@ARGV = ('--multi');
my $opt = testRole2->new_with_options;
ok( $opt->multi, 'multi set' );
trap {
$opt->options_usage;
};
like(
$trap->stdout,
qr/\-\-multi\s+multi\sthreading\smode/x,
"usage method is properly set"
);
}
{
local @ARGV;
@ARGV = ('--multi');
my $opt = testSkipOpt->new_with_options;
ok( !$opt->multi, 'multi not set' );
trap {
$opt->options_usage;
};
ok( $trap->stdout !~ /\-\-multi\s+multi\sthreading\smode/x,
"usage method is properly set" );
}
t/23-string_with_zero_value.t view on Meta::CPAN
use strict;
use warnings all => 'FATAL';
use Test::More;
package Foo;
use Moo;
use MooX::Options;
option start_from => ( is => "ro", format => "s" );
package main;
local @ARGV = qw/--start_from 0/;
my $f = Foo->new_with_options;
my $n = $f->start_from;
is $n, 0, 'option with value 0 works';
$n++;
is $n, 1, 'and can be increment';
done_testing;
t/24-usage_string.t view on Meta::CPAN
option 'hero' => (
is => 'ro',
doc => 'this is mandatory',
format => 's@',
);
1;
}
{
local @ARGV = (qw/--bad-option/);
trap { my $opt = t->new_with_options(); };
like $trap->stderr,
qr/usage: myprogram <hi> \[-h\] \[long options/,
'stderr has correct usage';
}
done_testing;
t/25-with_config.t view on Meta::CPAN
isa_ok( $t, "MyTestWithConfig" )
or skip "MyTestWithConfig Instantiation failure", 4;
is $t->p1, 1, 'p1 fetch from config';
is_deeply $t->p2, [ 1, 2, 3 ], '... and also p2';
ok $t->can('config_prefix'), '... config prefix defined';
ok $t->can('config_dirs'), '... config dirs defined';
ok $t->can('config_files'), '... config files defined';
}
SKIP: {
local @ARGV = ( '--config_prefix', '25-with_config_2.t' );
my $t = trap { MyTestWithConfig->new_with_options() };
isa_ok( $t, "MyTestWithConfig" )
or skip "MyTestWithConfig Instantiation failure", 2;
is $t->p1, 2, 'p1 fetch from config';
is_deeply $t->p2, [ 3, 4, 5 ], '... and also p2';
}
SKIP: {
local @ARGV = ( '--p1', '2' );
my $t = trap { MyTestWithConfig->new_with_options() };
isa_ok( $t, "MyTestWithConfig" )
or skip "MyTestWithConfig Instantiation failure", 2;
is $t->p1, 2, 'p1 fetch from cmdline';
is_deeply $t->p2, [ 1, 2, 3 ], '... and p2 from config';
}
SKIP: {
local @ARGV = ( '--p1', '2' );
my $t = trap { MyTestWithConfig->new_with_options( p1 => 3 ) };
isa_ok( $t, "MyTestWithConfig" )
or skip "MyTestWithConfig Instantiation failure", 2;
is $t->p1, 3, 'p1 fetch from params';
is_deeply $t->p2, [ 1, 2, 3 ], '... and p2 from config';
}
eval <<EOF
package MyTestWithConfigRole;
use Moo::Role;
t/26-localization.t view on Meta::CPAN
},
{ msgid => "long options ...",
msgstr => "ausgedehnte Parameter ..."
},
]
}
);
}
{
local @ARGV = ('--help');
my $opt = trap { t->new_with_options(); };
like( $trap->stdout,
qr{USAGE:\s\Q$Script\E\s\[\-h\]\s\[long\soptions\s\.\.\.\]}x,
"Usage itself" );
like( $trap->stdout, qr{this\sis\snegatable}x, "--neg usage" );
like( $trap->stdout, qr{this\sis\smandatory}x, "--req usage" );
like( $trap->stdout, qr{show\sa\sshort\shelp\smessage}x,
"--usage usage" );
like( $trap->stdout, qr{show\sa\scompact\shelp\smessage}x, "-h usage" );
like( $trap->stdout, qr{show\sa\slong\shelp\smessage}x, "--help usage" );
like( $trap->stdout, qr{show\sthe\smanual}x, "--man usage" );
}
Locale::TextDomain::OO->instance()->language("de");
Locale::TextDomain::OO->instance()->category("LC_MESSAGES");
Locale::TextDomain::OO->instance()->domain("example");
{
local @ARGV = ('--help');
my $opt = trap { t->new_with_options(); };
like( $trap->stdout,
qr{AUFRUF:\s\Q$Script\E\s\[\-h\]\s\Q[ausgedehnte Parameter ...]\E}x,
"Usage itself" );
like( $trap->stdout, qr{dieser\sist\snegierbar}x, "--neg aufruf" );
like( $trap->stdout, qr{dies\sist\serforderlich}x, "--req aufruf" );
like( $trap->stdout, qr{Zeigt\seine\skurze\sHilfe}x, "--usage aufruf" );
like( $trap->stdout, qr{Zeigt\seine\skompakte\sHilfe}x, "-h aufruf" );
like(
$trap->stdout,
t/27-short.t view on Meta::CPAN
use MooX::Options;
option 'lololo' => ( is => 'ro', format => 'i', short => 'l|lolo' );
option 'rororo' => ( is => 'ro', format => 'i', short => 'r|roro' );
option 'lorimi' => ( is => 'ro', format => 'i', short => 'm|lori' );
1;
}
SCOPE:
{
local @ARGV = ( '--r', '1', '--l', '2', '--m', 4 );
my $opt = TestSeveralMultipleShort->new_with_options;
is $opt->rororo, 1, 'rororo got shortened correctly';
is $opt->lololo, 2, 'lololo got shortened correctly';
is $opt->lorimi, 4, 'lorimi got shortened correctly';
}
SCOPE:
{
local @ARGV = ( '--lolo', '2', '--roro', '3', '--lori', '5' );
my $opt = TestSeveralMultipleShort->new_with_options;
is $opt->lololo, 2, 'lololo got shortened correctly';
is $opt->rororo, 3, 'rororo got shortened correctly';
is $opt->lorimi, 5, 'lorimi got shortened correctly';
}
trap {
local @ARGV = ( '--ro', '1', '--lo', '1', '--lo', 2 );
TestSeveralMultipleShort->new_with_options;
};
like $trap->stderr, qr/Option lo is ambiguous/,
"Unable to abbreviate and not argv fix";
done_testing;
use Module::Runtime qw(use_module);
local $ENV{TEST_FORCE_COLUMN_SIZE} = 78;
my @params = qw/bool counter empty verbose/;
subtest "no args" => sub {
note "no args";
local @ARGV = ();
my $t = t->new_with_options();
ok( $t->can($_), "$_ defined" ) for @params;
is( $t->$_, undef, "$_ values is undef" ) for @params;
is( $t->has_default, 'foo', 'Default works correctly' );
done_testing();
};
subtest "args value" => sub {
note "args value with repeatable";
local @ARGV
= ( ( map {"--$_"} @params ), '--counter', '--counter' );
my $t = t->new_with_options();
note "bool ", $t->bool;
note "counter ", $t->counter;
note "empty ", $t->empty;
ok( $t->$_, "$_ values is defined" ) for @params;
is( $t->bool, 1, "bool is well defined" );
is( $t->counter, 3, "counter is well defined" );
is( $t->empty, 1, "empty is well defined" );
is( $t->verbose, 1, "verbose is well defined" );
done_testing();
};
subtest "negatable" => sub {
note "negatable";
{
local @ARGV = ( '--empty', '--no-empty' );
my $t = t->new_with_options();
is( $t->empty, 0, "empty is well defined" );
}
{
local @ARGV = ( '--em', '--no-em' );
my $t = t->new_with_options();
is( $t->empty, 0, "empty is well defined" );
}
done_testing();
};
subtest "negativable" => sub {
note "negativable";
local @ARGV = ( '--verbose', '--no-verbose' );
my $t = t->new_with_options();
is( $t->verbose, 0, "verbose is well defined" );
done_testing();
};
subtest "passthrough" => sub {
note "passthrough";
local @ARGV = ( '--split', '1', '--split=2', '--', '3' );
my $t = t->new_with_options();
is_deeply( $t->split, [ 1, 2 ], "split separated correctly" );
done_testing();
};
subtest "negate_other" => sub {
note "negate other";
local @ARGV = ('--no-used');
my $t = t->new_with_options();
is( $t->unused, 1, "unused is well defined" );
done_testing();
};
subtest "split" => sub {
note "split";
{
local @ARGV = ('--split=1');
my $t = t->new_with_options();
is_deeply( $t->split, [1], "split one arg" );
}
{
local @ARGV = ( '--split=1', '--split=2' );
my $t = t->new_with_options();
is_deeply( $t->split, [ 1, 2 ], "split two arg" );
}
SKIP: {
eval { use_module("Data::Record"); use_module("Regexp::Common"); }
or skip "This test needs Data::Record and Regexp::Common", 1;
local @ARGV = ('--split=1,2');
my $t = t->new_with_options();
is_deeply( $t->split, [ 1, 2 ], "split one arg autosplit" );
}
SKIP: {
eval { use_module("Data::Record"); use_module("Regexp::Common"); }
or skip "This test needs Data::Record and Regexp::Common", 1;
local @ARGV = ( '--split=1', '--split=2', '--split=3,4' );
my $t = t->new_with_options();
is_deeply(
$t->split,
[ 1, 2, 3, 4 ],
"split three arg with autosplit"
);
}
SKIP: {
eval { use_module("Data::Record"); use_module("Regexp::Common"); }
or skip "This test needs Data::Record and Regexp::Common", 1;
local @ARGV = ( '--split', '1', '--split', '2', '--split', '3,4' );
my $t = t->new_with_options();
is_deeply(
$t->split,
[ 1, 2, 3, 4 ],
"split three arg with autosplit and space"
);
}
done_testing();
};
subtest "test required" => sub {
note "test required";
{
local @ARGV = ();
my @r = trap { r->new_with_options };
is( $trap->exit, 1, "missing args, exit 1" );
like( $trap->stderr, qr/^str_req is missing/, "str_reg is missing" );
}
{
local @ARGV = ('--str_req=ok');
my $t = r->new_with_options;
is( $t->str_req, 'ok', 'str req is ok' );
}
{
local @ARGV = ();
my @r = trap { multi_req->new_with_options };
is( $trap->exit, 1, "missing args exit 1" );
my @missing = $trap->stderr =~ /(multi_\d is missing)\n/g;
my @target_isa;
{ no strict 'refs'; @target_isa = @{"multi_req::ISA"} };
if ( multi_req->isa('Moose::Object') || multi_req->isa('Mo::Object') )
{
is( scalar @missing, 1, "only one missing for moose" );
}
else {
is( scalar @missing, 3, "multi is missing" );
}
}
done_testing();
};
subtest "test help" => sub {
note "test help";
{
local @ARGV = ('--help');
my @r = trap { r->new_with_options };
is( $trap->exit, 0, "help, exit 0" );
ok( $trap->stdout !~ /^str_req is missing/, "str_reg is missing" );
}
done_testing();
};
subtest "test short help" => sub {
note "test short help";
{
local @ARGV = ('--usage');
my @r = trap { r->new_with_options };
is( $trap->exit, 0, "help, exit 0" );
ok( $trap->stdout !~ /^str_req is missing/, "str_reg is missing" );
like $trap->stdout,
qr{USAGE:\s\d{2}\-m\w+\Q.t [ --str_req=String ] | [ --usage ] [ -h ] [ --help ] [ --man ]\E},
'usage message ok';
}
done_testing();
};
subtest "test man" => sub {
note "test man";
{
local @ARGV = ('--man');
my @r = trap { r->new_with_options };
is( $trap->exit, 0, "man, exit 0" );
ok( $trap->stdout !~ /^str_req is missing/, "str_reg is missing" );
}
done_testing();
};
subtest "value override" => sub {
note "value override";
{
local @ARGV = ();
my $t = r->new_with_options( str_req => "ok" );
is( $t->str_req, 'ok', 'str req is ok' );
}
{
local @ARGV = ('--str_req=ko');
my $t = r->new_with_options( str_req => "ok" );
is( $t->str_req, 'ok', 'str req is override with ok' );
}
done_testing();
};
subtest "split_complexe_str" => sub {
note "split on complexe str";
eval { use_module("Data::Record"); use_module("Regexp::Common"); }
or plan skip_all => "This test needs Data::Record and Regexp::Common";
{
local @ARGV = ("--split_str=a,b,c");
my $t = sp_str->new_with_options();
is_deeply( $t->split_str, [qw/a b c/], 'str req is ok' );
}
{
local @ARGV = ("--split-str=a,b,c");
my $t = sp_str->new_with_options();
is_deeply( $t->split_str, [qw/a b c/], 'str req is ok' );
}
{
local @ARGV = ("--split_conflict_str1=a,b,c");
my $t = sp_str->new_with_options();
is_deeply( $t->split_conflict_str1, [qw/a b c/], 'str req is ok' );
}
{
local @ARGV = ("--split_conflict-str1=a,b,c");
my $t = sp_str->new_with_options();
is_deeply( $t->split_conflict_str1, [qw/a b c/], 'str req is ok' );
}
{
local @ARGV = ("--split-conflict_str1=a,b,c");
my $t = sp_str->new_with_options();
is_deeply( $t->split_conflict_str1, [qw/a b c/], 'str req is ok' );
}
{
local @ARGV = ("--split-conflict-str1=a,b,c");
my $t = sp_str->new_with_options();
is_deeply( $t->split_conflict_str1, [qw/a b c/], 'str req is ok' );
}
{
local @ARGV = ('--split_str=a,"b,c",d');
my $t = sp_str->new_with_options();
is_deeply( $t->split_str, [ 'a', 'b,c', 'd' ], 'str req is ok' );
}
{
local @ARGV = ('--split-str=a,"b,c",d');
my $t = sp_str->new_with_options();
is_deeply( $t->split_str, [ 'a', 'b,c', 'd' ], 'str req is ok' );
}
done_testing();
};
subtest "split_complexe_str_short" => sub {
note "split on complexe str short";
eval { use_module("Data::Record"); use_module("Regexp::Common"); }
or plan skip_all => "This test needs Data::Record and Regexp::Common";
{
local @ARGV = ("-z=a");
my $t = sp_str_short->new_with_options();
is_deeply( $t->split_str, [qw/a/], 'str req is ok' );
}
{
local @ARGV = ("-z=a,b,c");
my $t = sp_str_short->new_with_options();
is_deeply( $t->split_str, [qw/a b c/], 'str req is ok' );
}
{
local @ARGV = ('-z=a,"b,c",d');
my $t = sp_str_short->new_with_options();
is_deeply( $t->split_str, [ 'a', 'b,c', 'd' ], 'str req is ok' );
}
{
local @ARGV = ( '-z', 'a,"b,c",d' );
my $t = sp_str_short->new_with_options();
is_deeply( $t->split_str, [ 'a', 'b,c', 'd' ], 'str req is ok' );
}
done_testing();
};
subtest "split_str_shorter_name" => sub {
note "shorter long split";
eval { use_module("Data::Record"); use_module("Regexp::Common"); }
or plan skip_all => "This test needs Data::Record and Regexp::Common";
{
local @ARGV = ("--split_st=a,b,c");
my $t = sp_str->new_with_options();
is_deeply( $t->split_str, [qw/a b c/], 'str req is ok' );
}
{
local @ARGV = ("--split-st=a,b,c");
my $t = sp_str->new_with_options();
is_deeply( $t->split_str, [qw/a b c/], 'str req is ok' );
}
note "shorter long split with conflict";
{
local @ARGV = ("--split_co=a,b,c");
trap {
sp_str->new_with_options();
};
like $trap->stderr, qr/Option\ssplit_co\sis\sambiguous/,
'conflict detected';
local @ARGV = ("--split-co=a,b,c");
trap {
sp_str->new_with_options();
};
like $trap->stderr, qr/Option\ssplit_co\sis\sambiguous/,
'conflict detected';
}
done_testing();
};
subtest "should_die_ok" => sub {
view all matches for this distributionview release on metacpan - search on metacpan