6c4119100768b957c8763720dff1096cb395b728
[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 => 19;
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 %constructor_args;
13
14
15 # nothing special going on here
16 {
17     package Generic;
18     use Moose;
19     with 'MooseX::SimpleConfig';
20     sub get_config_from_file
21     {
22         my ($class, $file) = @_;
23         $loaded_file{$file}++;
24         +{}
25     }
26     around BUILDARGS => sub {
27         my ($orig, $class) = (shift, shift);
28         my $args = $class->$orig(@_);
29         $constructor_args{$class} = $args;
30     };
31     sub __my_configfile
32     {
33         my $class = blessed($_[0]) || $_[0];
34         $class . ' file'
35     }
36 }
37
38 is(
39     exception {
40         my $obj = Generic->new_with_config;
41         is($obj->configfile, undef, 'no configfile set');
42         cmp_deeply(\%loaded_file, {}, 'no files loaded');
43         cmp_deeply(
44             $constructor_args{blessed($obj)},
45             { },
46             'correct constructor args passed',
47         );
48     },
49     undef,
50     'no exceptions',
51 );
52
53
54 # this is a classic legacy usecase from old documentation that we must
55 # continue to support
56 {
57     package OverriddenDefault;
58     use Moose;
59     extends 'Generic';
60     has '+configfile' => (
61         default => 'OverriddenDefault file',
62     );
63 }
64
65 is(
66     exception {
67         my $obj = OverriddenDefault->new_with_config;
68         is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden default');
69         is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
70     },
71     undef,
72     'no exceptions',
73 );
74
75 # legacy usecase, and configfile init_arg has been changed
76 {
77     package OverriddenDefaultAndChangedName;
78     use Moose;
79     extends 'Generic';
80     has '+configfile' => (
81         init_arg => 'my_configfile',
82         default => 'OverriddenDefaultAndChangedName file',
83     );
84 }
85
86 is(
87     exception {
88         my $obj = OverriddenDefaultAndChangedName->new_with_config;
89         is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden default');
90         cmp_deeply(
91             $constructor_args{blessed($obj)},
92             {  my_configfile => blessed($obj) . ' file' },
93             'correct constructor args passed',
94         );
95         is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
96     },
97     undef,
98     'no exceptions',
99 );
100
101 # "reader" method is overridden to provide for configfile default
102 {
103     package OverriddenMethod;
104     use Moose;
105     extends 'Generic';
106     around configfile => sub { my $orig = shift; shift->__my_configfile };
107 }
108
109 is(
110     exception {
111         my $obj = OverriddenMethod->new_with_config;
112         is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub');
113         is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
114     },
115     undef,
116     'no exceptions',
117 );
118
119
120 # overridable method for configfile default, and configfile init_arg is changed
121 {
122     package OverriddenMethodAndChangedName;
123     use Moose;
124     extends 'Generic';
125     has '+configfile' => (
126         init_arg => 'my_configfile',
127     );
128     around configfile => sub { my $orig = shift; shift->__my_configfile };
129 }
130
131 is(
132     exception {
133         my $obj = OverriddenMethodAndChangedName->new_with_config;
134         is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub');
135         cmp_deeply(
136             $constructor_args{blessed($obj)},
137             {  my_configfile => blessed($obj) . ' file' },
138             'correct constructor args passed',
139         );
140         is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from');
141     },
142     undef,
143     'no exceptions',
144 );
145