* t/*.t: Fixed test plans.
[gitmo/MooseX-Getopt.git] / t / 008_configfromfile.t
CommitLineData
62c4891a 1#!/usr/bin/env perl
2
3use strict;
4use warnings;
5
6use Test::Exception;
7use Test::More;
8
78a71ae5 9if ( !eval { require MooseX::ConfigFromFile } )
62c4891a 10{
11 plan skip_all => 'Test requires MooseX::ConfigFromFile';
12}
13else
14{
7c1cb743 15 plan tests => 123;
62c4891a 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
10ed52cb 77foreach 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 };
7c1cb743 81 skip "Getopt::Long::Descriptive not installed", 41 if $@;
10ed52cb 82 }
62c4891a 83
10ed52cb 84 # No config specified
85 {
86 local @ARGV = qw( --required_from_argv 1 );
62c4891a 87
10ed52cb 88 throws_ok {
89 my $parser = $parser_name->new;
90 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
62c4891a 91
10ed52cb 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');
62c4891a 95
10ed52cb 96 App->new_with_options( getopt => $getopt );
97 } qr/required_from_config/;
62c4891a 98
10ed52cb 99 {
100 my $parser = $parser_name->new;
101 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
62c4891a 102
10ed52cb 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');
62c4891a 106
10ed52cb 107 my $app = App::DefaultConfigFile->new_with_options( getopt => $getopt );
108 isa_ok( $app, 'App::DefaultConfigFile' );
109 app_ok( $app );
62c4891a 110
10ed52cb 111 ok( !$app->config_from_override,
112 '... config_from_override false as expected' );
62c4891a 113
10ed52cb 114 is( $app->configfile, '/notused/default',
115 '... configfile is /notused/default as expected' );
116 }
117 }
62c4891a 118
10ed52cb 119 # Config specified
120 {
121 local @ARGV = qw( --configfile /notused --required_from_argv 1 );
62c4891a 122
10ed52cb 123 {
124 my $parser = $parser_name->new;
125 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
62c4891a 126
10ed52cb 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');
62c4891a 130
10ed52cb 131 my $app = App->new_with_options( getopt => $getopt );
132 isa_ok( $app, 'App' );
133 app_ok( $app );
134 }
62c4891a 135
10ed52cb 136 {
137 my $parser = $parser_name->new;
138 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
62c4891a 139
10ed52cb 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');
62c4891a 143
10ed52cb 144 my $app = App::DefaultConfigFile->new_with_options( getopt => $getopt );
145 isa_ok( $app, 'App::DefaultConfigFile' );
146 app_ok( $app );
62c4891a 147
10ed52cb 148 ok( $app->config_from_override,
149 '... config_from_override true as expected' );
62c4891a 150
10ed52cb 151 is( $app->configfile, '/notused',
152 '... configfile is /notused as expected' );
153 }
154 }
62c4891a 155
10ed52cb 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');
9f1ec7c0 162
10ed52cb 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');
9f1ec7c0 166
10ed52cb 167 App->new_with_options( getopt => $getopt );
168 } qr/required_from_argv/;
169 }
9f1ec7c0 170
10ed52cb 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 );
9f1ec7c0 195
10ed52cb 196 my $parser = $parser_name->new;
197 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
62c4891a 198
10ed52cb 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');
62c4891a 202
10ed52cb 203 my $app = App->new_with_options( getopt => $getopt );
204 isa_ok( $app, 'App' );
62c4891a 205
10ed52cb 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 }
62c4891a 256}