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
$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(
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',
'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,
);
+# 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',
+);
+