if configfile is a coderef call it and write some tests for it
[gitmo/MooseX-Getopt.git] / t / 008_configfromfile.t
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 use Test::Exception;
7 use Test::More;
8 use File::Spec;
9
10 if ( !eval { require MooseX::ConfigFromFile } )
11 {
12     plan skip_all => 'Test requires MooseX::ConfigFromFile';
13 }
14 else
15 {
16     plan tests => 37;
17 }
18
19 {
20     package App;
21
22     use Moose;
23     with 'MooseX::Getopt';
24     with 'MooseX::ConfigFromFile';
25
26     has 'config_from_override' => (
27         is       => 'ro',
28         isa      => 'Bool',
29         default  => 0,
30     );
31
32     has 'optional_from_config' => (
33         is        => 'ro',
34         isa       => 'Str',
35         required  => 0,
36     );
37
38     has 'required_from_config' => (
39         is        => 'ro',
40         isa       => 'Str',
41         required  => 1,
42     );
43
44     has 'required_from_argv' => (
45         is        => 'ro',
46         isa       => 'Str',
47         required  => 1,
48     );
49
50     sub get_config_from_file
51     {
52         my ( $class, $file ) = @_;
53
54         my %config = (
55             required_from_config => 'from_config_1',
56             optional_from_config => 'from_config_2',
57         );
58
59         my $cpath = File::Spec->canonpath('/notused/default');
60         if ( $file ne $cpath ) {
61             $config{config_from_override} = 1;
62         }
63
64         return \%config;
65     }
66 }
67
68 {
69     package App::DefaultConfigFile;
70
71     use Moose;
72     extends 'App';
73
74     has '+configfile' => (
75         default => File::Spec->canonpath('/notused/default'),
76     );
77 }
78
79 {
80     package App::DefaultConfigFileCodeRef;
81
82     use Moose;
83     extends 'App';
84
85     has '+configfile' => (
86         default => sub { return File::Spec->canonpath('/notused/default') },
87     );
88 }
89
90 # No config specified
91 {
92     local @ARGV = qw( --required_from_argv 1 );
93
94     throws_ok { App->new_with_options } qr/Required option missing: required_from_config/;
95
96     {
97         my $app = App::DefaultConfigFile->new_with_options;
98         isa_ok( $app, 'App::DefaultConfigFile' );
99         app_ok( $app );
100
101         ok(  !$app->config_from_override,
102             '... config_from_override false as expected' );
103
104         is( $app->configfile, File::Spec->canonpath('/notused/default'),
105             '... configfile is /notused/default as expected' );
106     }
107 }
108
109 # No config specified
110 {
111     local @ARGV = qw( --required_from_argv 1 );
112
113     {
114         my $app = App::DefaultConfigFileCodeRef->new_with_options;
115         isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
116         app_ok( $app );
117
118         ok(  !$app->config_from_override,
119             '... config_from_override false as expected' );
120
121         is( $app->configfile, File::Spec->canonpath('/notused/default'),
122             '... configfile is /notused/default as expected' );
123     }
124 }
125
126 # Config specified
127 {
128     local @ARGV = qw( --configfile /notused --required_from_argv 1 );
129
130     {
131         my $app = App->new_with_options;
132         isa_ok( $app, 'App' );
133         app_ok( $app );
134     }
135
136     {
137         my $app = App::DefaultConfigFile->new_with_options;
138         isa_ok( $app, 'App::DefaultConfigFile' );
139         app_ok( $app );
140
141         ok( $app->config_from_override,
142              '... config_from_override true as expected' );
143
144         is( $app->configfile, File::Spec->canonpath('/notused'),
145             '... configfile is /notused as expected' );
146     }
147     {
148         my $app = App::DefaultConfigFileCodeRef->new_with_options;
149         isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
150         app_ok( $app );
151
152         ok( $app->config_from_override,
153              '... config_from_override true as expected' );
154
155         is( $app->configfile, File::Spec->canonpath('/notused'),
156             '... configfile is /notused as expected' );
157     }
158 }
159
160 # Required arg not supplied from cmdline
161 {
162     local @ARGV = qw( --configfile /notused );
163     throws_ok { App->new_with_options } qr/Required option missing: required_from_argv/;
164 }
165
166 # Config file value overriden from cmdline
167 {
168     local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
169
170     my $app = App->new_with_options;
171     isa_ok( $app, 'App' );
172
173     is( $app->required_from_config, 'override',
174         '... required_from_config is override as expected' );
175
176     is( $app->optional_from_config, 'from_config_2',
177         '... optional_from_config is from_config_2 as expected' );
178 }
179
180 # No config file
181 {
182     local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
183
184     my $app = App->new_with_options;
185     isa_ok( $app, 'App' );
186
187     is( $app->required_from_config, 'noconfig',
188         '... required_from_config is noconfig as expected' );
189
190     ok( !defined $app->optional_from_config,
191         '... optional_from_config is undef as expected' );
192 }
193
194 {
195     package BaseApp::WithConfig;
196     use Moose;
197     with 'MooseX::ConfigFromFile';
198
199     sub get_config_from_file { return {}; }
200 }
201
202 {
203     package DerivedApp::Getopt;
204     use Moose;
205     extends 'BaseApp::WithConfig';
206     with 'MooseX::Getopt';
207 }
208
209 # With DerivedApp, the Getopt role was applied at a different level
210 # than the ConfigFromFile role
211 {
212     lives_ok { DerivedApp::Getopt->new_with_options } 'Can create DerivedApp';
213 }
214
215 sub app_ok {
216     my $app = shift;
217
218     is( $app->required_from_config, 'from_config_1',
219         '... required_from_config is from_config_1 as expected' );
220
221     is( $app->optional_from_config, 'from_config_2',
222         '... optional_from_config is from_config_2 as expected' );
223
224     is( $app->required_from_argv, '1',
225         '... required_from_argv is 1 as expected' );
226 }