0160167100c232495ff613ff48bd14ca943a0070
[gitmo/MooseX-ConfigFromFile.git] / t / 05_default_sub.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use Test::Requires 'MooseX::SimpleConfig';      # skip all if not reuqired
5 use Test::More tests => 33;
6 use Test::Fatal;
7 use Test::Deep '!blessed';
8 use Test::NoWarnings 1.04 ':early';
9 use Scalar::Util 'blessed';
10
11 my %loaded_file;
12 my %configfile_sub;
13 my %constructor_args;
14
15
16 # nothing special going on here
17 {
18     package Generic;
19     use Moose;
20     with 'MooseX::SimpleConfig';
21     sub get_config_from_file
22     {
23         my ($class, $file) = @_;
24         $loaded_file{$file}++;
25         +{}
26     }
27     around BUILDARGS => sub {
28         my ($orig, $class) = (shift, shift);
29         my $args = $class->$orig(@_);
30         $constructor_args{$class} = $args;
31     };
32     sub __my_configfile
33     {
34         my $class = blessed($_[0]) || $_[0];
35         $configfile_sub{$class}++;
36         $class . ' file'
37     }
38 }
39
40 is(
41     exception {
42         my $obj = Generic->new_with_config;
43         is($obj->configfile, undef, 'no configfile set');
44         cmp_deeply(\%loaded_file, {}, 'no files loaded');
45         cmp_deeply(
46             $constructor_args{blessed($obj)},
47             { },
48             'correct constructor args passed',
49         );
50     },
51     undef,
52     'no exceptions',
53 );
54
55
56 # this is a classic legacy usecase from old documentation that we must
57 # continue to support
58 {
59     package OverriddenDefault;
60     use Moose;
61     extends 'Generic';
62     has '+configfile' => (
63         default => 'OverriddenDefault file',
64     );
65 }
66
67 is(
68     exception {
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');
72     },
73     undef,
74     'no exceptions',
75 );
76
77 {
78     package OverriddenDefaultMethod;
79     use Moose;
80     extends 'Generic';
81     has '+configfile' => (
82         default => sub { shift->__my_configfile },
83     );
84 }
85
86 is(
87     exception {
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');
92     },
93     undef,
94     'no exceptions',
95 );
96
97
98 # legacy usecase, and configfile init_arg has been changed
99 {
100     package OverriddenDefaultAndChangedName;
101     use Moose;
102     extends 'Generic';
103     has '+configfile' => (
104         init_arg => 'my_configfile',
105         default => 'OverriddenDefaultAndChangedName file',
106     );
107 }
108
109 is(
110     exception {
111         my $obj = OverriddenDefaultAndChangedName->new_with_config;
112         is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden default');
113         cmp_deeply(
114             $constructor_args{blessed($obj)},
115             {  my_configfile => blessed($obj) . ' file' },
116             'correct constructor args passed',
117         );
118         is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
119     },
120     undef,
121     'no exceptions',
122 );
123
124 # "reader" method is overridden to provide for configfile default
125 {
126     package OverriddenMethod;
127     use Moose;
128     extends 'Generic';
129     around configfile => sub { my $orig = shift; shift->__my_configfile };
130 }
131
132 is(
133     exception {
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');
139     },
140     undef,
141     'no exceptions',
142 );
143
144
145 # overridable method for configfile default, and configfile init_arg is changed
146 {
147     package OverriddenMethodAndChangedName;
148     use Moose;
149     extends 'Generic';
150     has '+configfile' => (
151         init_arg => 'my_configfile',
152     );
153     around configfile => sub { my $orig = shift; shift->__my_configfile };
154 }
155
156 is(
157     exception {
158         my $obj = OverriddenMethodAndChangedName->new_with_config;
159         is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub');
160         cmp_deeply(
161             $constructor_args{blessed($obj)},
162             {  my_configfile => blessed($obj) . ' file' },
163             'correct constructor args passed',
164         );
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');
168     },
169     undef,
170     'no exceptions',
171 );
172
173 # newly-supported overridable method for configfile default
174 {
175     package WrapperSub;
176     use Moose;
177     extends 'Generic';
178     sub _get_default_configfile { shift->__my_configfile }
179 }
180
181 is(
182     exception {
183         my $obj = WrapperSub->new_with_config;
184         is($obj->configfile, blessed($obj) . ' file', 'configfile set via new sub');
185         cmp_deeply(
186             $constructor_args{blessed($obj)},
187             {  configfile => blessed($obj) . ' file' },
188             'correct constructor args passed',
189         );
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');
192     },
193     undef,
194     'no exceptions',
195 );
196
197 # newly-supported overridable method for configfile default, and configfile
198 # init_arg has been changed
199 {
200     package WrapperSubAndChangedName;
201     use Moose;
202     extends 'Generic';
203     has '+configfile' => (
204         init_arg => 'my_configfile',
205     );
206     sub _get_default_configfile { shift->__my_configfile }
207 }
208
209 is(
210     exception {
211         my $obj = WrapperSubAndChangedName->new_with_config;
212         is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub');
213         cmp_deeply(
214             $constructor_args{blessed($obj)},
215             {  my_configfile => blessed($obj) . ' file' },
216             'correct constructor args passed',
217         );
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');
220     },
221     undef,
222     'no exceptions',
223 );
224