X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F006_metaclass_traits.t;fp=t%2F006_metaclass_traits.t;h=aebe3156f625c4d850ba5ab0d217a62a5ef257a5;hb=053fa19e136de81f8144bd403b5da17c2dbd0c01;hp=18c3011eb5a094cb7733c268eb6d501876c6a919;hpb=19b87ede54eb20f76fafd42a944d56eb0f031dcb;p=gitmo%2FMooseX-Getopt.git 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'); + } + + } }