From: Piotr Roszatycki Date: Wed, 12 Nov 2008 19:33:08 +0000 (+0000) Subject: * MooseX::Getopt::Parser::Descriptive: fixed regression: does not require CLI param... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=053fa19e136de81f8144bd403b5da17c2dbd0c01;p=gitmo%2FMooseX-Getopt.git * MooseX::Getopt::Parser::Descriptive: fixed regression: does not require CLI param if is explict options. * MooseX::Getopt::Session: params attribute handles params default for session. * t/*.t: More test units for both parsers. --- diff --git a/lib/MooseX/Getopt.pm b/lib/MooseX/Getopt.pm index 1e7c91a..48ea79e 100644 --- a/lib/MooseX/Getopt.pm +++ b/lib/MooseX/Getopt.pm @@ -49,7 +49,8 @@ sub new_with_options { my $getopt = defined $params{getopt} ? $params{getopt} : $class->_default_getopt_session->new( - classes_filter => sub { $_ eq $class } + classes_filter => sub { $_ eq $class }, + params => \%params, ); my %options = $getopt->options; @@ -58,8 +59,9 @@ sub new_with_options { ARGV => [ $getopt->argv ], # backward compatibility extra_argv => [ $getopt->extra_argv ], # backward compatibility getopt => $getopt, - %params, # explicit params to ->new - %options, # params from CLI + %{ $getopt->params }, # params from session object + %params, # explicit params to ->new + %options, # params from CLI ); }; diff --git a/lib/MooseX/Getopt/Parser/Descriptive.pm b/lib/MooseX/Getopt/Parser/Descriptive.pm index ad547ba..4688df6 100644 --- a/lib/MooseX/Getopt/Parser/Descriptive.pm +++ b/lib/MooseX/Getopt/Parser/Descriptive.pm @@ -52,7 +52,10 @@ sub build_options { $doc = $attr->documentation if $attr->has_documentation; $doc = ' ' unless $doc; - my $is_required = $attr->is_required && !$attr->has_default && !$attr->has_builder; + my $is_required = !exists $getopt->params->{$name} + && $attr->is_required + && !$attr->has_default + && !$attr->has_builder; push @opts, [ $opt_string => $doc, diff --git a/lib/MooseX/Getopt/Session.pm b/lib/MooseX/Getopt/Session.pm index 81f4b26..60f496c 100644 --- a/lib/MooseX/Getopt/Session.pm +++ b/lib/MooseX/Getopt/Session.pm @@ -26,6 +26,13 @@ has 'classes_filter' => ( default => sub { sub { 1 } }, ); +# Explicite parameters for new_with_options +has 'params' => ( + is => 'rw', + isa => 'HashRef', + default => sub { {} }, +); + # Original @ARGV values has 'argv' => ( is => 'rw', diff --git a/t/001_basic.t b/t/001_basic.t index 3b171a1..48dca21 100644 --- a/t/001_basic.t +++ b/t/001_basic.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 173; +use Test::More tests => 185; BEGIN { use_ok('MooseX::Getopt'); @@ -319,3 +319,30 @@ foreach my $parser_name (qw(MooseX::Getopt::Parser::Long MooseX::Getopt::Parser: } } + +# Test default parser +{ + local @ARGV = (); + + my $app = App->new_with_options; + isa_ok($app, 'App'); + + ok(!$app->verbose, '... verbosity is off as expected'); + is($app->length, 24, '... length is 24 as expected'); + is($app->data, 'file.dat', '... data is file.dat as expected'); + is_deeply($app->libs, [], '... libs is [] as expected'); + is_deeply($app->details, {}, '... details is {} as expected'); +} + +{ + local @ARGV = ('--verbose', '--length', 50); + + my $app = App->new_with_options; + isa_ok($app, 'App'); + + ok($app->verbose, '... verbosity is turned on as expected'); + is($app->length, 50, '... length is 50 as expected'); + is($app->data, 'file.dat', '... data is file.dat as expected'); + is_deeply($app->libs, [], '... libs is [] as expected'); + is_deeply($app->details, {}, '... details is {} as expected'); +} diff --git a/t/004_nogetop.t b/t/004_nogetop.t index c26006d..e4a1c5a 100644 --- a/t/004_nogetop.t +++ b/t/004_nogetop.t @@ -112,7 +112,6 @@ foreach my $parser_name (qw(MooseX::Getopt::Parser::Long MooseX::Getopt::Parser: local @ARGV = (qw/--private_stuff 317/); throws_ok { - my $parser = $parser_name->new; isa_ok($parser, $parser_name); @@ -120,7 +119,6 @@ foreach my $parser_name (qw(MooseX::Getopt::Parser::Long MooseX::Getopt::Parser: isa_ok($getopt, 'MooseX::Getopt::Session'); App->new_with_options( getopt => $getopt ) - } qr/Unknown option: private_stuff/; } diff --git a/t/005_strict.t b/t/005_strict.t index 8a8d634..d706747 100644 --- a/t/005_strict.t +++ b/t/005_strict.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 10; +use Test::More tests => 27; use Test::Exception; BEGIN { @@ -80,29 +80,60 @@ BEGIN { } -{ - local @ARGV = (); +foreach my $parser_name (qw(MooseX::Getopt::Parser::Long MooseX::Getopt::Parser::Descriptive)) { + SKIP: { + if ($parser_name eq 'MooseX::Getopt::Parser::Descriptive') { + eval { require Getopt::Long::Descriptive }; + skip "Getopt::Long::Descriptive not installed", 13 if $@; + } - my $app = App->new_with_options; - isa_ok( $app, 'App' ); + { + local @ARGV = (); - ok( !$app->verbose, '... verbosity is off as expected' ); - is( $app->length, 24, '... length is 24 as expected' ); - is( $app->data, 'file.dat', '... data is file.dat as expected' ); - is_deeply( $app->libs, [], '... libs is [] as expected' ); - is_deeply( $app->details, {}, '... details is {} as expected' ); - is($app->private_stuff, 713, '... private stuff is 713 as expected'); -} + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); -{ - local @ARGV = (qw/--private_stuff 317/); + my $getopt = MooseX::Getopt::Session->new( parser => $parser ); + isa_ok($getopt, 'MooseX::Getopt::Session'); - throws_ok { App->new_with_options } qr/Unknown option: private_stuff/; -} + my $app = App->new_with_options( getopt => $getopt ); + isa_ok( $app, 'App' ); -{ - local @ARGV = (qw/--length 100/); + ok( !$app->verbose, '... verbosity is off as expected' ); + is( $app->length, 24, '... length is 24 as expected' ); + is( $app->data, 'file.dat', '... data is file.dat as expected' ); + is_deeply( $app->libs, [], '... libs is [] as expected' ); + is_deeply( $app->details, {}, '... details is {} as expected' ); + is($app->private_stuff, 713, '... private stuff is 713 as expected'); + } - throws_ok { App->new_with_options } qr/Unknown option: length/; -} + { + local @ARGV = (qw/--private_stuff 317/); + + throws_ok { + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); + my $getopt = MooseX::Getopt::Session->new( parser => $parser ); + isa_ok($getopt, 'MooseX::Getopt::Session'); + + App->new_with_options( getopt => $getopt ); + } qr/Unknown option: private_stuff/; + } + + { + local @ARGV = (qw/--length 100/); + + throws_ok { + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); + + my $getopt = MooseX::Getopt::Session->new( parser => $parser ); + isa_ok($getopt, 'MooseX::Getopt::Session'); + + App->new_with_options( getopt => $getopt ); + } qr/Unknown option: length/; + } + + } +} diff --git a/t/006_metaclass_traits.t b/t/006_metaclass_traits.t index 18c3011..aebe315 100644 --- a/t/006_metaclass_traits.t +++ b/t/006_metaclass_traits.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 69; +use Test::More tests => 173; use Test::Moose; BEGIN { @@ -13,11 +13,11 @@ BEGIN { { package App; use Moose; - + with 'MooseX::Getopt'; has 'data' => ( - traits => [ 'MooseX::Getopt::Meta::Attribute::Trait' ], + traits => [ 'MooseX::Getopt::Meta::Attribute::Trait' ], is => 'ro', isa => 'Str', default => 'file.dat', @@ -25,7 +25,7 @@ BEGIN { ); has 'cow' => ( - traits => [ 'Getopt' ], + traits => [ 'Getopt' ], is => 'ro', isa => 'Str', default => 'moo', @@ -33,7 +33,7 @@ BEGIN { ); has 'horse' => ( - traits => [ 'Getopt' ], + traits => [ 'Getopt' ], is => 'ro', isa => 'Str', default => 'bray', @@ -49,15 +49,15 @@ BEGIN { has 'verbose' => ( is => 'ro', - isa => 'Bool', + isa => 'Bool', ); - + has 'libs' => ( is => 'ro', isa => 'ArrayRef', default => sub { [] }, - ); - + ); + has 'details' => ( is => 'ro', isa => 'HashRef', @@ -71,156 +71,251 @@ BEGIN { ); has '_private_stuff_cmdline' => ( - traits => [ 'Getopt' ], + traits => [ 'Getopt' ], is => 'ro', isa => 'Int', default => 832, cmd_flag => 'p', ); - + } foreach my $attr_name (qw(data cow horse _private_stuff_cmdline)) { my $attr = App->meta->get_attribute($attr_name); isa_ok($attr, 'Moose::Meta::Attribute'); does_ok($attr, 'MooseX::Getopt::Meta::Attribute::Trait'); - + can_ok($attr, 'cmd_flag'); - can_ok($attr, 'cmd_aliases'); + can_ok($attr, 'cmd_aliases'); } -{ - local @ARGV = (); +foreach my $parser_name (qw(MooseX::Getopt::Parser::Long MooseX::Getopt::Parser::Descriptive)) { + SKIP: { + if ($parser_name eq 'MooseX::Getopt::Parser::Descriptive') { + eval { require Getopt::Long::Descriptive }; + skip "Getopt::Long::Descriptive not installed", 78 if $@; + } - my $app = App->new_with_options; - isa_ok($app, 'App'); + { + local @ARGV = (); - ok(!$app->verbose, '... verbosity is off as expected'); - is($app->length, 24, '... length is 24 as expected'); - is($app->data, 'file.dat', '... data is file.dat as expected'); - is_deeply($app->libs, [], '... libs is [] as expected'); - is_deeply($app->details, {}, '... details is {} as expected'); -} + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); -{ - local @ARGV = ('--verbose', '--length', 50); + my $getopt = MooseX::Getopt::Session->new( parser => $parser ); + isa_ok($getopt, 'MooseX::Getopt::Session'); - my $app = App->new_with_options; - isa_ok($app, 'App'); + my $app = App->new_with_options( getopt => $getopt ); + isa_ok($app, 'App'); - ok($app->verbose, '... verbosity is turned on as expected'); - is($app->length, 50, '... length is 50 as expected'); - is($app->data, 'file.dat', '... data is file.dat as expected'); - is_deeply($app->libs, [], '... libs is [] as expected'); - is_deeply($app->details, {}, '... details is {} as expected'); -} + ok(!$app->verbose, '... verbosity is off as expected'); + is($app->length, 24, '... length is 24 as expected'); + is($app->data, 'file.dat', '... data is file.dat as expected'); + is_deeply($app->libs, [], '... libs is [] as expected'); + is_deeply($app->details, {}, '... details is {} as expected'); + } -{ - local @ARGV = ('--verbose', '-f', 'foo.txt'); + { + local @ARGV = ('--verbose', '--length', 50); - my $app = App->new_with_options; - isa_ok($app, 'App'); + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); - ok($app->verbose, '... verbosity is turned on as expected'); - is($app->length, 24, '... length is 24 as expected'); - is($app->data, 'foo.txt', '... data is foo.txt as expected'); - is_deeply($app->libs, [], '... libs is [] as expected'); - is_deeply($app->details, {}, '... details is {} as expected'); -} + my $getopt = MooseX::Getopt::Session->new( parser => $parser ); + isa_ok($getopt, 'MooseX::Getopt::Session'); -{ - local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib'); - - my $app = App->new_with_options; - isa_ok($app, 'App'); - - ok($app->verbose, '... verbosity is turned on as expected'); - is($app->length, 24, '... length is 24 as expected'); - is($app->data, 'file.dat', '... data is foo.txt as expected'); - is_deeply($app->libs, - ['libs/', 'includes/lib'], - '... libs is [libs/, includes/lib] as expected'); - is_deeply($app->details, {}, '... details is {} as expected'); -} + my $app = App->new_with_options( getopt => $getopt ); + isa_ok($app, 'App'); -{ - local @ARGV = ('--details', 'os=mac', '--details', 'name=foo'); - - my $app = App->new_with_options; - isa_ok($app, 'App'); - - ok(!$app->verbose, '... verbosity is turned on as expected'); - is($app->length, 24, '... length is 24 as expected'); - is($app->data, 'file.dat', '... data is foo.txt as expected'); - is_deeply($app->libs, [], '... libs is [] as expected'); - is_deeply($app->details, - { os => 'mac', name => 'foo' }, - '... details is { os => mac, name => foo } as expected'); -} + ok($app->verbose, '... verbosity is turned on as expected'); + is($app->length, 50, '... length is 50 as expected'); + is($app->data, 'file.dat', '... data is file.dat as expected'); + is_deeply($app->libs, [], '... libs is [] as expected'); + is_deeply($app->details, {}, '... details is {} as expected'); + } -{ - # Test negation on booleans too ... - local @ARGV = ('--noverbose'); + { + local @ARGV = ('--verbose', '-f', 'foo.txt'); - my $app = App->new_with_options; - isa_ok($app, 'App'); + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); - ok(!$app->verbose, '... verbosity is turned off as expected'); - is($app->length, 24, '... length is 24 as expected'); - is($app->data, 'file.dat', '... file is file.dat as expected'); - is_deeply($app->libs, [], '... libs is [] as expected'); - is_deeply($app->details, {}, '... details is {} as expected'); -} + my $getopt = MooseX::Getopt::Session->new( parser => $parser ); + isa_ok($getopt, 'MooseX::Getopt::Session'); -# Test cmd_alias without cmd_flag -{ - local @ARGV = ('--cow', '42'); - my $app = App->new_with_options; - isa_ok($app, 'App'); - is($app->cow, 42, 'cmd_alias, but not using it'); -} -{ - local @ARGV = ('--moocow', '88'); - my $app = App->new_with_options; - isa_ok($app, 'App'); - is($app->cow, 88, 'cmd_alias, using long one'); -} -{ - local @ARGV = ('-c', '99'); - my $app = App->new_with_options; - isa_ok($app, 'App'); - is($app->cow, 99, 'cmd_alias, using short one'); -} + my $app = App->new_with_options( getopt => $getopt ); + isa_ok($app, 'App'); -# Test cmd_alias + cmd_flag -{ - local @ARGV = ('--horsey', '123'); - my $app = App->new_with_options; - isa_ok($app, 'App'); - is($app->horse, 123, 'cmd_alias+cmd_flag, using flag'); -} -{ - local @ARGV = ('-x', '321'); - my $app = App->new_with_options; - isa_ok($app, 'App'); - is($app->horse, 321, 'cmd_alias+cmd_flag, using alias'); -} + ok($app->verbose, '... verbosity is turned on as expected'); + is($app->length, 24, '... length is 24 as expected'); + is($app->data, 'foo.txt', '... data is foo.txt as expected'); + is_deeply($app->libs, [], '... libs is [] as expected'); + is_deeply($app->details, {}, '... details is {} as expected'); + } -# Test _foo + cmd_flag -{ - local @ARGV = ('-p', '666'); - my $app = App->new_with_options; - isa_ok($app, 'App'); - is($app->_private_stuff_cmdline, 666, '_foo + cmd_flag'); -} + { + local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib'); -# Test ARGV support -{ - my @args = ('-p', 12345, '-c', 99, '-'); - local @ARGV = @args; - my $app = App->new_with_options; - isa_ok($app, 'App'); - is_deeply($app->ARGV, \@args, 'ARGV accessor'); - is_deeply(\@ARGV, \@args, '@ARGV unmangled'); - is_deeply($app->extra_argv, ['-'], 'extra_argv accessor'); + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); + + my $getopt = MooseX::Getopt::Session->new( parser => $parser ); + isa_ok($getopt, 'MooseX::Getopt::Session'); + + my $app = App->new_with_options( getopt => $getopt ); + isa_ok($app, 'App'); + + ok($app->verbose, '... verbosity is turned on as expected'); + is($app->length, 24, '... length is 24 as expected'); + is($app->data, 'file.dat', '... data is foo.txt as expected'); + is_deeply($app->libs, + ['libs/', 'includes/lib'], + '... libs is [libs/, includes/lib] as expected'); + is_deeply($app->details, {}, '... details is {} as expected'); + } + + { + local @ARGV = ('--details', 'os=mac', '--details', 'name=foo'); + + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); + + my $getopt = MooseX::Getopt::Session->new( parser => $parser ); + isa_ok($getopt, 'MooseX::Getopt::Session'); + + my $app = App->new_with_options( getopt => $getopt ); + isa_ok($app, 'App'); + + ok(!$app->verbose, '... verbosity is turned on as expected'); + is($app->length, 24, '... length is 24 as expected'); + is($app->data, 'file.dat', '... data is foo.txt as expected'); + is_deeply($app->libs, [], '... libs is [] as expected'); + is_deeply($app->details, + { os => 'mac', name => 'foo' }, + '... details is { os => mac, name => foo } as expected'); + } + + { + # Test negation on booleans too ... + local @ARGV = ('--noverbose'); + + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); + + my $getopt = MooseX::Getopt::Session->new( parser => $parser ); + isa_ok($getopt, 'MooseX::Getopt::Session'); + + my $app = App->new_with_options( getopt => $getopt ); + isa_ok($app, 'App'); + + ok(!$app->verbose, '... verbosity is turned off as expected'); + is($app->length, 24, '... length is 24 as expected'); + is($app->data, 'file.dat', '... file is file.dat as expected'); + is_deeply($app->libs, [], '... libs is [] as expected'); + is_deeply($app->details, {}, '... details is {} as expected'); + } + + # Test cmd_alias without cmd_flag + { + local @ARGV = ('--cow', '42'); + + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); + + my $getopt = MooseX::Getopt::Session->new( parser => $parser ); + isa_ok($getopt, 'MooseX::Getopt::Session'); + + my $app = App->new_with_options( getopt => $getopt ); + isa_ok($app, 'App'); + is($app->cow, 42, 'cmd_alias, but not using it'); + } + { + local @ARGV = ('--moocow', '88'); + + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); + + my $getopt = MooseX::Getopt::Session->new( parser => $parser ); + isa_ok($getopt, 'MooseX::Getopt::Session'); + + my $app = App->new_with_options( getopt => $getopt ); + isa_ok($app, 'App'); + is($app->cow, 88, 'cmd_alias, using long one'); + } + { + local @ARGV = ('-c', '99'); + + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); + + my $getopt = MooseX::Getopt::Session->new( parser => $parser ); + isa_ok($getopt, 'MooseX::Getopt::Session'); + + my $app = App->new_with_options( getopt => $getopt ); + isa_ok($app, 'App'); + is($app->cow, 99, 'cmd_alias, using short one'); + } + + # Test cmd_alias + cmd_flag + { + local @ARGV = ('--horsey', '123'); + + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); + + my $getopt = MooseX::Getopt::Session->new( parser => $parser ); + isa_ok($getopt, 'MooseX::Getopt::Session'); + + my $app = App->new_with_options( getopt => $getopt ); + isa_ok($app, 'App'); + is($app->horse, 123, 'cmd_alias+cmd_flag, using flag'); + } + { + local @ARGV = ('-x', '321'); + + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); + + my $getopt = MooseX::Getopt::Session->new( parser => $parser ); + isa_ok($getopt, 'MooseX::Getopt::Session'); + + my $app = App->new_with_options( getopt => $getopt ); + isa_ok($app, 'App'); + is($app->horse, 321, 'cmd_alias+cmd_flag, using alias'); + } + + # Test _foo + cmd_flag + { + local @ARGV = ('-p', '666'); + + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); + + my $getopt = MooseX::Getopt::Session->new( parser => $parser ); + isa_ok($getopt, 'MooseX::Getopt::Session'); + + my $app = App->new_with_options( getopt => $getopt ); + isa_ok($app, 'App'); + is($app->_private_stuff_cmdline, 666, '_foo + cmd_flag'); + } + + # Test ARGV support + { + my @args = ('-p', 12345, '-c', 99, '-'); + local @ARGV = @args; + + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); + + my $getopt = MooseX::Getopt::Session->new( parser => $parser ); + isa_ok($getopt, 'MooseX::Getopt::Session'); + + my $app = App->new_with_options( getopt => $getopt ); + isa_ok($app, 'App'); + is_deeply($app->ARGV, \@args, 'ARGV accessor'); + is_deeply(\@ARGV, \@args, '@ARGV unmangled'); + is_deeply($app->extra_argv, ['-'], 'extra_argv accessor'); + } + + } } diff --git a/t/007_nogetopt_trait.t b/t/007_nogetopt_trait.t index 5ccef57..a8e89c3 100644 --- a/t/007_nogetopt_trait.t +++ b/t/007_nogetopt_trait.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 9; +use Test::More tests => 23; use Test::Exception; BEGIN { @@ -81,22 +81,46 @@ BEGIN { } -{ - local @ARGV = (); +foreach my $parser_name (qw(MooseX::Getopt::Parser::Long MooseX::Getopt::Parser::Descriptive)) { + SKIP: { + if ($parser_name eq 'MooseX::Getopt::Parser::Descriptive') { + eval { require Getopt::Long::Descriptive }; + skip "Getopt::Long::Descriptive not installed", 11 if $@; + } - my $app = App->new_with_options; - isa_ok( $app, 'App' ); + { + local @ARGV = (); - ok( !$app->verbose, '... verbosity is off as expected' ); - is( $app->length, 24, '... length is 24 as expected' ); - is( $app->data, 'file.dat', '... data is file.dat as expected' ); - is_deeply( $app->libs, [], '... libs is [] as expected' ); - is_deeply( $app->details, {}, '... details is {} as expected' ); - is($app->private_stuff, 713, '... private stuff is 713 as expected'); -} + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); -{ - local @ARGV = (qw/--private_stuff 317/); + my $getopt = MooseX::Getopt::Session->new( parser => $parser ); + isa_ok($getopt, 'MooseX::Getopt::Session'); + + my $app = App->new_with_options( getopt => $getopt ); + isa_ok( $app, 'App' ); + + ok( !$app->verbose, '... verbosity is off as expected' ); + is( $app->length, 24, '... length is 24 as expected' ); + is( $app->data, 'file.dat', '... data is file.dat as expected' ); + is_deeply( $app->libs, [], '... libs is [] as expected' ); + is_deeply( $app->details, {}, '... details is {} as expected' ); + is($app->private_stuff, 713, '... private stuff is 713 as expected'); + } + + { + local @ARGV = (qw/--private_stuff 317/); + + throws_ok { + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); + + my $getopt = MooseX::Getopt::Session->new( parser => $parser ); + isa_ok($getopt, 'MooseX::Getopt::Session'); + + App->new_with_options( getopt => $getopt ); + } qr/Unknown option: private_stuff/; + } - throws_ok { App->new_with_options } qr/Unknown option: private_stuff/; + } } diff --git a/t/009_gld_and_explicit_options.t b/t/009_gld_and_explicit_options.t index 266ff8c..e99a216 100644 --- a/t/009_gld_and_explicit_options.t +++ b/t/009_gld_and_explicit_options.t @@ -6,44 +6,62 @@ use warnings; use Test::More; use Test::Exception; -BEGIN { +BEGIN { eval 'use Getopt::Long::Descriptive;'; plan skip_all => "Getopt::Long::Descriptive required for this test" if $@; - plan tests => 5; + plan tests => 10; use_ok('MooseX::Getopt'); } { package Testing::Foo; use Moose; - + with 'MooseX::Getopt'; - + has 'bar' => ( is => 'ro', - isa => 'Int', + isa => 'Int', required => 1, ); - + has 'baz' => ( is => 'ro', - isa => 'Int', - required => 1, - ); + isa => 'Int', + required => 1, + ); } -@ARGV = qw(--bar 10); +# Explicite parser +{ + local @ARGV = qw(--bar 10); -my $foo; -lives_ok { - $foo = Testing::Foo->new_with_options(baz => 100); -} '... this should work'; -isa_ok($foo, 'Testing::Foo'); + my $parser = MooseX::Getopt::Parser::Descriptive->new; + isa_ok($parser, 'MooseX::Getopt::Parser::Descriptive'); -is($foo->bar, 10, '... got the right values'); -is($foo->baz, 100, '... got the right values'); + my %params = (baz => 100); + my $getopt = MooseX::Getopt::Session->new(parser => $parser, params => \%params); + my $foo; + lives_ok { + $foo = Testing::Foo->new_with_options(getopt => $getopt); + } '... this should work'; + isa_ok($foo, 'Testing::Foo'); + is($foo->bar, 10, '... got the right values'); + is($foo->baz, 100, '... got the right values'); +} +# Default parser +{ + local @ARGV = qw(--bar 10); + my $foo; + lives_ok { + $foo = Testing::Foo->new_with_options(baz => 100); + } '... this should work'; + isa_ok($foo, 'Testing::Foo'); + is($foo->bar, 10, '... got the right values'); + is($foo->baz, 100, '... got the right values'); +}