* t/*.t: Fixed test plans.
[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
9 if ( !eval { require MooseX::ConfigFromFile } )
10 {
11     plan skip_all => 'Test requires MooseX::ConfigFromFile';
12 }
13 else
14 {
15     plan tests => 123;
16 }
17
18 {
19     package App;
20
21     use Moose;
22     with 'MooseX::Getopt';
23     with 'MooseX::ConfigFromFile';
24
25     has 'config_from_override' => (
26         is       => 'ro',
27         isa      => 'Bool',
28         default  => 0,
29     );
30
31     has 'optional_from_config' => (
32         is        => 'ro',
33         isa       => 'Str',
34         required  => 0,
35     );
36
37     has 'required_from_config' => (
38         is        => 'ro',
39         isa       => 'Str',
40         required  => 1,
41     );
42
43     has 'required_from_argv' => (
44         is        => 'ro',
45         isa       => 'Str',
46         required  => 1,
47     );
48
49     sub get_config_from_file
50     {
51         my ( $class, $file ) = @_;
52
53         my %config = (
54             required_from_config => 'from_config_1',
55             optional_from_config => 'from_config_2',
56         );
57
58         if ( $file ne '/notused/default' ) {
59             $config{config_from_override} = 1;
60         }
61
62         return \%config;
63     }
64 }
65
66 {
67     package App::DefaultConfigFile;
68
69     use Moose;
70     extends 'App';
71
72     has '+configfile' => (
73         default => '/notused/default',
74     );
75 }
76
77 foreach my $parser_name (qw(MooseX::Getopt::Parser::Long MooseX::Getopt::Parser::Descriptive MooseX::Getopt::Parser::Default)) {
78     SKIP: {
79         if ($parser_name eq 'MooseX::Getopt::Parser::Descriptive') {
80             eval { require Getopt::Long::Descriptive };
81             skip "Getopt::Long::Descriptive not installed", 41 if $@;
82         }
83
84         # No config specified
85         {
86             local @ARGV = qw( --required_from_argv 1 );
87
88             throws_ok {
89                 my $parser = $parser_name->new;
90                 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
91
92                 my $options = App->get_options_from_configfile;
93                 my $getopt = MooseX::Getopt::Session->new( parser => $parser, options => $options, classes_filter => sub { $_ eq 'App' } );
94                 isa_ok($getopt, 'MooseX::Getopt::Session');
95
96                 App->new_with_options( getopt => $getopt );
97             } qr/required_from_config/;
98
99             {
100                 my $parser = $parser_name->new;
101                 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
102
103                 my $options = App::DefaultConfigFile->get_options_from_configfile;
104                 my $getopt = MooseX::Getopt::Session->new( parser => $parser, options => $options, classes_filter => sub { $_ eq 'App::DefaultConfigFile' } );
105                 isa_ok($getopt, 'MooseX::Getopt::Session');
106
107                 my $app = App::DefaultConfigFile->new_with_options( getopt => $getopt );
108                 isa_ok( $app, 'App::DefaultConfigFile' );
109                 app_ok( $app );
110
111                 ok(  !$app->config_from_override,
112                     '... config_from_override false as expected' );
113
114                 is( $app->configfile, '/notused/default',
115                     '... configfile is /notused/default as expected' );
116             }
117         }
118
119         # Config specified
120         {
121             local @ARGV = qw( --configfile /notused --required_from_argv 1 );
122
123             {
124                 my $parser = $parser_name->new;
125                 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
126
127                 my $options = App->get_options_from_configfile;
128                 my $getopt = MooseX::Getopt::Session->new( parser => $parser, options => $options, classes_filter => sub { $_ eq 'App' } );
129                 isa_ok($getopt, 'MooseX::Getopt::Session');
130
131                 my $app = App->new_with_options( getopt => $getopt );
132                 isa_ok( $app, 'App' );
133                 app_ok( $app );
134             }
135
136             {
137                 my $parser = $parser_name->new;
138                 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
139
140                 my $options = App::DefaultConfigFile->get_options_from_configfile;
141                 my $getopt = MooseX::Getopt::Session->new( parser => $parser, options => $options, classes_filter => sub { $_ eq 'App::DefaultConfigFile' } );
142                 isa_ok($getopt, 'MooseX::Getopt::Session');
143
144                 my $app = App::DefaultConfigFile->new_with_options( getopt => $getopt );
145                 isa_ok( $app, 'App::DefaultConfigFile' );
146                 app_ok( $app );
147
148                 ok( $app->config_from_override,
149                      '... config_from_override true as expected' );
150
151                 is( $app->configfile, '/notused',
152                     '... configfile is /notused as expected' );
153             }
154         }
155
156         # Required arg not supplied from cmdline
157         {
158             local @ARGV = qw( --configfile /notused );
159             throws_ok {
160                 my $parser = $parser_name->new;
161                 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
162
163                 my $options = App->get_options_from_configfile;
164                 my $getopt = MooseX::Getopt::Session->new( parser => $parser, options => $options, classes_filter => sub { $_ eq 'App' } );
165                 isa_ok($getopt, 'MooseX::Getopt::Session');
166
167                 App->new_with_options( getopt => $getopt );
168             } qr/required_from_argv/;
169         }
170
171         # Config file value overriden from cmdline
172         {
173             local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
174
175             my $parser = $parser_name->new;
176             ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
177
178             my $options = App->get_options_from_configfile;
179             my $getopt = MooseX::Getopt::Session->new( parser => $parser, options => $options, classes_filter => sub { $_ eq 'App' } );
180             isa_ok($getopt, 'MooseX::Getopt::Session');
181
182             my $app = App->new_with_options( getopt => $getopt );
183             isa_ok( $app, 'App' );
184
185             is( $app->required_from_config, 'override',
186                 '... required_from_config is override as expected' );
187
188             is( $app->optional_from_config, 'from_config_2',
189                 '... optional_from_config is from_config_2 as expected' );
190         }
191
192         # No config file
193         {
194             local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
195
196             my $parser = $parser_name->new;
197             ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
198
199             my $options = App->get_options_from_configfile;
200             my $getopt = MooseX::Getopt::Session->new( parser => $parser, options => $options, classes_filter => sub { $_ eq 'App' } );
201             isa_ok($getopt, 'MooseX::Getopt::Session');
202
203             my $app = App->new_with_options( getopt => $getopt );
204             isa_ok( $app, 'App' );
205
206             is( $app->required_from_config, 'noconfig',
207                 '... required_from_config is noconfig as expected' );
208
209             ok( !defined $app->optional_from_config,
210                 '... optional_from_config is undef as expected' );
211         }
212
213         {
214             package BaseApp::WithConfig;
215             use Moose;
216             with 'MooseX::ConfigFromFile';
217
218             sub get_config_from_file { return {}; }
219         }
220
221         {
222             package DerivedApp::Getopt;
223             use Moose;
224             extends 'BaseApp::WithConfig';
225             with 'MooseX::Getopt';
226         }
227
228         # With DerivedApp, the Getopt role was applied at a different level
229         # than the ConfigFromFile role
230         {
231             lives_ok {
232                 my $parser = $parser_name->new;
233                 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
234
235                 my $getopt = MooseX::Getopt::Session->new( parser => $parser, classes_filter => sub { $_ eq 'DerivedApp::Getopt' } );
236                 isa_ok($getopt, 'MooseX::Getopt::Session');
237
238                 DerivedApp::Getopt->new_with_options( getopt => $getopt );
239             } 'Can create DerivedApp';
240         }
241
242         sub app_ok {
243             my $app = shift;
244
245             is( $app->required_from_config, 'from_config_1',
246                 '... required_from_config is from_config_1 as expected' );
247
248             is( $app->optional_from_config, 'from_config_2',
249                 '... optional_from_config is from_config_2 as expected' );
250
251             is( $app->required_from_argv, '1',
252                 '... required_from_argv is 1 as expected' );
253         }
254
255     }
256 }