X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F05_default_sub.t;h=60bceffc2f52d8250a1ea3281d678624266260cd;hb=e8e7174e5fbcaf83f2fdd7089f9ed05c37b559f5;hp=6c4119100768b957c8763720dff1096cb395b728;hpb=8b4a3e7640107c8fa677673024b83ba3315393f9;p=gitmo%2FMooseX-ConfigFromFile.git diff --git a/t/05_default_sub.t b/t/05_default_sub.t index 6c41191..60bceff 100644 --- a/t/05_default_sub.t +++ b/t/05_default_sub.t @@ -1,14 +1,14 @@ use strict; use warnings FATAL => 'all'; -use Test::Requires 'MooseX::SimpleConfig'; # skip all if not reuqired -use Test::More tests => 19; +use Test::More tests => 33; use Test::Fatal; use Test::Deep '!blessed'; use Test::NoWarnings 1.04 ':early'; use Scalar::Util 'blessed'; my %loaded_file; +my %configfile_sub; my %constructor_args; @@ -16,7 +16,7 @@ my %constructor_args; { package Generic; use Moose; - with 'MooseX::SimpleConfig'; + with 'MooseX::ConfigFromFile'; sub get_config_from_file { my ($class, $file) = @_; @@ -31,6 +31,7 @@ my %constructor_args; sub __my_configfile { my $class = blessed($_[0]) || $_[0]; + $configfile_sub{$class}++; $class . ' file' } } @@ -72,6 +73,27 @@ is( 'no exceptions', ); +{ + package OverriddenDefaultMethod; + use Moose; + extends 'Generic'; + has '+configfile' => ( + default => sub { shift->__my_configfile }, + ); +} + +is( + exception { + my $obj = OverriddenDefaultMethod->new_with_config; + is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden default'); + is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once'); + is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from'); + }, + undef, + 'no exceptions', +); + + # legacy usecase, and configfile init_arg has been changed { package OverriddenDefaultAndChangedName; @@ -110,6 +132,8 @@ is( exception { my $obj = OverriddenMethod->new_with_config; is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub'); + # this is not fixable - the reader method has been shadowed + # is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once'); is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from'); }, undef, @@ -137,6 +161,60 @@ is( { my_configfile => blessed($obj) . ' file' }, 'correct constructor args passed', ); + # this is not fixable - the reader method has been shadowed + # is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once'); + is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from'); + }, + undef, + 'no exceptions', +); + +# newly-supported overridable method for configfile default +{ + package WrapperSub; + use Moose; + extends 'Generic'; + sub _get_default_configfile { shift->__my_configfile } +} + +is( + exception { + my $obj = WrapperSub->new_with_config; + is($obj->configfile, blessed($obj) . ' file', 'configfile set via new sub'); + cmp_deeply( + $constructor_args{blessed($obj)}, + { configfile => blessed($obj) . ' file' }, + 'correct constructor args passed', + ); + is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once'); + is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from'); + }, + undef, + 'no exceptions', +); + +# newly-supported overridable method for configfile default, and configfile +# init_arg has been changed +{ + package WrapperSubAndChangedName; + use Moose; + extends 'Generic'; + has '+configfile' => ( + init_arg => 'my_configfile', + ); + sub _get_default_configfile { shift->__my_configfile } +} + +is( + exception { + my $obj = WrapperSubAndChangedName->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($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once'); is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from'); }, undef,