2 use warnings FATAL => 'all';
4 use Test::Requires 'MooseX::SimpleConfig'; # skip all if not reuqired
5 use Test::More tests => 33;
7 use Test::Deep '!blessed';
8 use Test::NoWarnings 1.04 ':early';
9 use Scalar::Util 'blessed';
16 # nothing special going on here
20 with 'MooseX::SimpleConfig';
21 sub get_config_from_file
23 my ($class, $file) = @_;
24 $loaded_file{$file}++;
27 around BUILDARGS => sub {
28 my ($orig, $class) = (shift, shift);
29 my $args = $class->$orig(@_);
30 $constructor_args{$class} = $args;
34 my $class = blessed($_[0]) || $_[0];
35 $configfile_sub{$class}++;
42 my $obj = Generic->new_with_config;
43 is($obj->configfile, undef, 'no configfile set');
44 cmp_deeply(\%loaded_file, {}, 'no files loaded');
46 $constructor_args{blessed($obj)},
48 'correct constructor args passed',
56 # this is a classic legacy usecase from old documentation that we must
59 package OverriddenDefault;
62 has '+configfile' => (
63 default => 'OverriddenDefault file',
69 my $obj = OverriddenDefault->new_with_config;
70 is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden default');
71 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
78 package OverriddenDefaultMethod;
81 has '+configfile' => (
82 default => sub { shift->__my_configfile },
88 my $obj = OverriddenDefaultMethod->new_with_config;
89 is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden default');
90 is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once');
91 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
98 # legacy usecase, and configfile init_arg has been changed
100 package OverriddenDefaultAndChangedName;
103 has '+configfile' => (
104 init_arg => 'my_configfile',
105 default => 'OverriddenDefaultAndChangedName file',
111 my $obj = OverriddenDefaultAndChangedName->new_with_config;
112 is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden default');
114 $constructor_args{blessed($obj)},
115 { my_configfile => blessed($obj) . ' file' },
116 'correct constructor args passed',
118 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
124 # "reader" method is overridden to provide for configfile default
126 package OverriddenMethod;
129 around configfile => sub { my $orig = shift; shift->__my_configfile };
134 my $obj = OverriddenMethod->new_with_config;
135 is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub');
136 # this is not fixable - the reader method has been shadowed
137 # is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once');
138 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
145 # overridable method for configfile default, and configfile init_arg is changed
147 package OverriddenMethodAndChangedName;
150 has '+configfile' => (
151 init_arg => 'my_configfile',
153 around configfile => sub { my $orig = shift; shift->__my_configfile };
158 my $obj = OverriddenMethodAndChangedName->new_with_config;
159 is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub');
161 $constructor_args{blessed($obj)},
162 { my_configfile => blessed($obj) . ' file' },
163 'correct constructor args passed',
165 # this is not fixable - the reader method has been shadowed
166 # is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once');
167 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
173 # newly-supported overridable method for configfile default
178 sub _get_default_configfile { shift->__my_configfile }
183 my $obj = WrapperSub->new_with_config;
184 is($obj->configfile, blessed($obj) . ' file', 'configfile set via new sub');
186 $constructor_args{blessed($obj)},
187 { configfile => blessed($obj) . ' file' },
188 'correct constructor args passed',
190 is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once');
191 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
197 # newly-supported overridable method for configfile default, and configfile
198 # init_arg has been changed
200 package WrapperSubAndChangedName;
203 has '+configfile' => (
204 init_arg => 'my_configfile',
206 sub _get_default_configfile { shift->__my_configfile }
211 my $obj = WrapperSubAndChangedName->new_with_config;
212 is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub');
214 $constructor_args{blessed($obj)},
215 { my_configfile => blessed($obj) . ' file' },
216 'correct constructor args passed',
218 is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once');
219 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');