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