X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F002_custom_option_type.t;fp=t%2F002_custom_option_type.t;h=3ccdc7ec6f1d7fc8b6929ae19d51b019e9280a08;hb=19b87ede54eb20f76fafd42a944d56eb0f031dcb;hp=fc4625051984f0dc2afaff64af9cb38f2b82aef9;hpb=dd012666739d0d4f7f629b22b047a4c8117764a9;p=gitmo%2FMooseX-Getopt.git diff --git a/t/002_custom_option_type.t b/t/002_custom_option_type.t index fc46250..3ccdc7e 100644 --- a/t/002_custom_option_type.t +++ b/t/002_custom_option_type.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 6; +use Test::More tests => 21; BEGIN { use_ok('MooseX::Getopt'); @@ -13,51 +13,79 @@ BEGIN { package App; use Moose; use Moose::Util::TypeConstraints; - + use Scalar::Util 'looks_like_number'; - + with 'MooseX::Getopt'; subtype 'ArrayOfInts' => as 'ArrayRef' => where { scalar (grep { looks_like_number($_) } @$_) }; - + MooseX::Getopt::OptionTypeMap->add_option_type_to_map( 'ArrayOfInts' => '=i@' ); - + has 'nums' => ( is => 'ro', isa => 'ArrayOfInts', default => sub { [0] } - ); - + ); + } -{ - 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", 10 if $@; + } - my $app = App->new_with_options; - isa_ok($app, 'App'); - - is_deeply($app->nums, [0], '... nums is [0] as expected'); -} + { + local @ARGV = (); -{ - local @ARGV = ('--nums', 3, '--nums', 5); + my $parser = $parser_name->new; + isa_ok($parser, $parser_name); - my $app = App->new_with_options; - isa_ok($app, 'App'); - - is_deeply($app->nums, [3, 5], '... nums is [3, 5] as expected'); -} + 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'); -# Make sure it really used our =i@, instead of falling back -# to =s@ via the type system, and test that exceptions work -# while we're at it. -eval { - local @ARGV = ('--nums', 3, '--nums', 'foo'); + is_deeply($app->nums, [0], '... nums is [0] as expected'); + } - my $app = App->new_with_options; -}; -like($@, qr/Value "foo" invalid/, 'Numeric constraint enforced'); + { + local @ARGV = ('--nums', 3, '--nums', 5); + + 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->nums, [3, 5], '... nums is [3, 5] as expected'); + } + + # Make sure it really used our =i@, instead of falling back + # to =s@ via the type system, and test that exceptions work + # while we're at it. + eval { + local @ARGV = ('--nums', 3, '--nums', '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 ); + }; + like($@, qr/Value "foo" invalid/, 'Numeric constraint enforced'); + + } +}