X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F05_default_sub.t;h=0160167100c232495ff613ff48bd14ca943a0070;hb=aba5bd40f387410d217769b4d8a53339975bfb65;hp=c91a777bb843f4288185619da9fce3972363e975;hpb=bd567467c8bb9e746958c1ba11f93ca727a569ba;p=gitmo%2FMooseX-ConfigFromFile.git diff --git a/t/05_default_sub.t b/t/05_default_sub.t index c91a777..0160167 100644 --- a/t/05_default_sub.t +++ b/t/05_default_sub.t @@ -2,14 +2,15 @@ use strict; use warnings FATAL => 'all'; use Test::Requires 'MooseX::SimpleConfig'; # skip all if not reuqired -use Test::More tests => 10; +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 %default_sub; +my %configfile_sub; +my %constructor_args; # nothing special going on here @@ -17,13 +18,35 @@ my %default_sub; package Generic; use Moose; with 'MooseX::SimpleConfig'; - sub get_config_from_file { } + sub get_config_from_file + { + my ($class, $file) = @_; + $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]; + $configfile_sub{$class}++; + $class . ' file' + } } is( exception { 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', @@ -35,13 +58,7 @@ is( { package OverriddenDefault; use Moose; - with 'MooseX::SimpleConfig'; - sub get_config_from_file - { - my ($class, $file) = @_; - $loaded_file{$file}++; - +{} - } + extends 'Generic'; has '+configfile' => ( default => 'OverriddenDefault file', ); @@ -57,34 +74,151 @@ 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; + 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; - with 'MooseX::SimpleConfig'; - sub get_config_from_file { - my ($class, $file) = @_; - $loaded_file{$file}++; - +{} - } - - around configfile => sub { - my $class = blessed($_[1]) || $_[1]; - $default_sub{$class}++; - $class . ' file' - }; + extends 'Generic'; + 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'); + # 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', +); + + +# 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', + ); + # 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, + 'no exceptions', +);