2 use warnings FATAL => 'all';
4 use Test::More tests => 49;
6 use Test::Deep '!blessed';
7 use Test::NoWarnings 1.04 ':early';
8 use Scalar::Util 'blessed';
15 # nothing special going on here
19 with 'MooseX::ConfigFromFile';
20 sub get_config_from_file
22 my ($class, $file) = @_;
23 $loaded_file{$file}++;
26 around BUILDARGS => sub {
27 my ($orig, $class) = (shift, shift);
28 my $args = $class->$orig(@_);
29 $constructor_args{$class} = $args;
33 my $class = blessed($_[0]) || $_[0];
34 $configfile_sub{$class}++;
41 my $obj = Generic->new_with_config;
42 is($obj->configfile, undef, 'no configfile set');
43 cmp_deeply(\%loaded_file, {}, 'no files loaded');
45 $constructor_args{blessed($obj)},
47 'correct constructor args passed',
61 with 'MooseX::ConfigFromFile';
62 sub get_config_from_file
64 my ($class, $file) = @_;
65 $loaded_file{$file}++;
68 around BUILDARGS => sub {
69 my ($orig, $class) = (shift, shift);
70 my $args = $class->$orig(@_);
71 $constructor_args{$class} = $args;
75 my $class = blessed($_[0]) || $_[0];
76 $configfile_sub{$class}++;
83 my $obj = Moose::Meta::Class->create_anon_class(
84 superclasses => ['Base'],
85 roles => ['GenericRole'],
86 )->name->new_with_config;
87 is($obj->configfile, undef, 'no configfile set');
88 cmp_deeply(\%loaded_file, {}, 'no files loaded');
90 $constructor_args{blessed($obj)},
92 'correct constructor args passed',
99 # this is a classic legacy usecase from old documentation that we must
100 # continue to support
102 package OverriddenDefault;
105 has '+configfile' => (
106 default => 'OverriddenDefault file',
112 my $obj = OverriddenDefault->new_with_config;
113 is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden default');
114 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
121 package OverriddenDefaultMethod;
124 has '+configfile' => (
125 default => sub { shift->__my_configfile },
131 my $obj = OverriddenDefaultMethod->new_with_config;
132 is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden default');
133 is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once');
134 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
141 # legacy usecase, and configfile init_arg has been changed
143 package OverriddenDefaultAndChangedName;
146 has '+configfile' => (
147 init_arg => 'my_configfile',
148 default => 'OverriddenDefaultAndChangedName file',
154 my $obj = OverriddenDefaultAndChangedName->new_with_config;
155 is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden default');
157 $constructor_args{blessed($obj)},
158 { my_configfile => blessed($obj) . ' file' },
159 'correct constructor args passed',
161 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
167 # "reader" method is overridden to provide for configfile default
169 package OverriddenMethod;
172 around configfile => sub { my $orig = shift; shift->__my_configfile };
177 my $obj = OverriddenMethod->new_with_config;
178 is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub');
179 # this is not fixable - the reader method has been shadowed
180 # is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once');
181 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
188 package OverriddenMethodAsRole;
191 around configfile => sub { my $orig = shift; shift->__my_configfile };
196 my $obj = Moose::Meta::Class->create_anon_class(
197 superclasses => ['Base'],
198 roles => ['OverriddenMethodAsRole'],
199 )->name->new_with_config;
200 is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub');
201 # this is not fixable - the reader method has been shadowed
202 # is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once');
203 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
210 # overridable method for configfile default, and configfile init_arg is changed
212 package OverriddenMethodAndChangedName;
215 has '+configfile' => (
216 init_arg => 'my_configfile',
218 around configfile => sub { my $orig = shift; shift->__my_configfile };
223 my $obj = OverriddenMethodAndChangedName->new_with_config;
224 is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub');
226 $constructor_args{blessed($obj)},
227 { my_configfile => blessed($obj) . ' file' },
228 'correct constructor args passed',
230 # this is not fixable - the reader method has been shadowed
231 # is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once');
232 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
239 package OverriddenMethodAndChangedNameAsRole;
242 use MooseX::Types::Path::Tiny 'Path';
243 use MooseX::Types::Moose 'Undef';
249 predicate => 'has_configfile',
250 do { try { require MooseX::Getopt; (traits => ['Getopt']) } },
252 # it sucks that we have to do this rather than using a builder, but some old code
253 # simply swaps in a new default sub into the attr definition
254 default => sub { shift->_get_default_configfile },
256 # this is the overridden bit
257 init_arg => 'my_configfile',
259 around configfile => sub { my $orig = shift; shift->__my_configfile };
264 my $obj = Moose::Meta::Class->create_anon_class(
265 superclasses => ['Base'],
266 roles => ['OverriddenMethodAndChangedNameAsRole'],
267 )->name->new_with_config;
268 is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub');
270 $constructor_args{blessed($obj)},
271 { my_configfile => blessed($obj) . ' file' },
272 'correct constructor args passed',
274 # this is not fixable - the reader method has been shadowed
275 # is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once');
276 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
283 # newly-supported overridable method for configfile default
288 sub _get_default_configfile { shift->__my_configfile }
293 my $obj = NewSub->new_with_config;
294 is($obj->configfile, blessed($obj) . ' file', 'configfile set via new sub');
296 $constructor_args{blessed($obj)},
297 { configfile => blessed($obj) . ' file' },
298 'correct constructor args passed',
300 is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once');
301 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
308 package NewSubAsRole;
311 sub _get_default_configfile { shift->__my_configfile }
316 my $obj = Moose::Meta::Class->create_anon_class(
317 superclasses => ['Base'],
318 roles => ['NewSubAsRole'],
319 )->name->new_with_config;
320 is($obj->configfile, blessed($obj) . ' file', 'configfile set via new sub');
322 $constructor_args{blessed($obj)},
323 { configfile => blessed($obj) . ' file' },
324 'correct constructor args passed',
326 is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once');
327 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
334 # newly-supported overridable method for configfile default, and configfile
335 # init_arg has been changed
337 package NewSubAndChangedName;
340 has '+configfile' => (
341 init_arg => 'my_configfile',
343 sub _get_default_configfile { shift->__my_configfile }
348 my $obj = NewSubAndChangedName->new_with_config;
349 is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub');
351 $constructor_args{blessed($obj)},
352 { my_configfile => blessed($obj) . ' file' },
353 'correct constructor args passed',
355 is($configfile_sub{blessed($obj)}, 1, 'configfile was calculated just once');
356 is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');