X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F05_default_sub.t;h=6c4119100768b957c8763720dff1096cb395b728;hb=8b4a3e7640107c8fa677673024b83ba3315393f9;hp=cfe826b0a018c35a3479c72bee4f4790813a03f8;hpb=233548ddae7e1b8af9ccdf3943c4973bd653d2fe;p=gitmo%2FMooseX-ConfigFromFile.git diff --git a/t/05_default_sub.t b/t/05_default_sub.t index cfe826b..6c41191 100644 --- a/t/05_default_sub.t +++ b/t/05_default_sub.t @@ -2,14 +2,14 @@ use strict; use warnings FATAL => 'all'; use Test::Requires 'MooseX::SimpleConfig'; # skip all if not reuqired -use Test::More tests => 11; +use Test::More tests => 19; use Test::Fatal; use Test::Deep '!blessed'; use Test::NoWarnings 1.04 ':early'; use Scalar::Util 'blessed'; my %loaded_file; -my %default_sub; +my %constructor_args; # nothing special going on here @@ -23,6 +23,16 @@ my %default_sub; $loaded_file{$file}++; +{} } + around BUILDARGS => sub { + my ($orig, $class) = (shift, shift); + my $args = $class->$orig(@_); + $constructor_args{$class} = $args; + }; + sub __my_configfile + { + my $class = blessed($_[0]) || $_[0]; + $class . ' file' + } } is( @@ -30,6 +40,11 @@ is( my $obj = Generic->new_with_config; is($obj->configfile, undef, 'no configfile set'); cmp_deeply(\%loaded_file, {}, 'no files loaded'); + cmp_deeply( + $constructor_args{blessed($obj)}, + { }, + 'correct constructor args passed', + ); }, undef, 'no exceptions', @@ -57,24 +72,44 @@ is( 'no exceptions', ); +# legacy usecase, and configfile init_arg has been changed +{ + package OverriddenDefaultAndChangedName; + use Moose; + extends 'Generic'; + has '+configfile' => ( + init_arg => 'my_configfile', + default => 'OverriddenDefaultAndChangedName file', + ); +} + +is( + exception { + my $obj = OverriddenDefaultAndChangedName->new_with_config; + is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden default'); + cmp_deeply( + $constructor_args{blessed($obj)}, + { my_configfile => blessed($obj) . ' file' }, + 'correct constructor args passed', + ); + is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from'); + }, + undef, + 'no exceptions', +); # "reader" method is overridden to provide for configfile default { package OverriddenMethod; use Moose; extends 'Generic'; - around configfile => sub { - my $class = blessed($_[1]) || $_[1]; - $default_sub{$class}++; - $class . ' file' - }; + around configfile => sub { my $orig = shift; shift->__my_configfile }; } is( exception { my $obj = OverriddenMethod->new_with_config; is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub'); - ok($default_sub{blessed($obj)}, 'default sub was called'); is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from'); }, undef, @@ -82,3 +117,29 @@ is( ); +# overridable method for configfile default, and configfile init_arg is changed +{ + package OverriddenMethodAndChangedName; + use Moose; + extends 'Generic'; + has '+configfile' => ( + init_arg => 'my_configfile', + ); + around configfile => sub { my $orig = shift; shift->__my_configfile }; +} + +is( + exception { + my $obj = OverriddenMethodAndChangedName->new_with_config; + is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub'); + cmp_deeply( + $constructor_args{blessed($obj)}, + { my_configfile => blessed($obj) . ' file' }, + 'correct constructor args passed', + ); + is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from'); + }, + undef, + 'no exceptions', +); +