4 use Test::More tests => 38;
7 use Test::NoWarnings 1.04 ':early';
9 use Test::Requires 'MooseX::ConfigFromFile';
15 with 'MooseX::Getopt';
16 with 'MooseX::ConfigFromFile';
18 has 'config_from_override' => (
24 has 'optional_from_config' => (
30 has 'required_from_config' => (
36 has 'required_from_argv' => (
42 sub get_config_from_file
44 my ( $class, $file ) = @_;
47 required_from_config => 'from_config_1',
48 optional_from_config => 'from_config_2',
51 my $cpath = File::Spec->canonpath('/notused/default');
52 if ( $file ne $cpath ) {
53 $config{config_from_override} = 1;
61 package App::DefaultConfigFile;
66 has '+configfile' => (
67 default => File::Spec->canonpath('/notused/default'),
72 package App::DefaultConfigFileCodeRef;
77 has '+configfile' => (
78 default => sub { return File::Spec->canonpath('/notused/default') },
84 local @ARGV = qw( --required_from_argv 1 );
86 if ($Getopt::Long::Descriptive::VERSION >= 0.091) {
87 like exception { App->new_with_options }, qr/Mandatory parameter 'required_from_config' missing/;
90 like exception { App->new_with_options }, qr/Required option missing: required_from_config/;
94 my $app = App::DefaultConfigFile->new_with_options;
95 isa_ok( $app, 'App::DefaultConfigFile' );
98 ok( !$app->config_from_override,
99 '... config_from_override false as expected' );
101 is( $app->configfile, File::Spec->canonpath('/notused/default'),
102 '... configfile is /notused/default as expected' );
106 # No config specified
108 local @ARGV = qw( --required_from_argv 1 );
111 my $app = App::DefaultConfigFileCodeRef->new_with_options;
112 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
115 ok( !$app->config_from_override,
116 '... config_from_override false as expected' );
118 is( $app->configfile, File::Spec->canonpath('/notused/default'),
119 '... configfile is /notused/default as expected' );
125 local @ARGV = qw( --configfile /notused --required_from_argv 1 );
128 my $app = App->new_with_options;
129 isa_ok( $app, 'App' );
134 my $app = App::DefaultConfigFile->new_with_options;
135 isa_ok( $app, 'App::DefaultConfigFile' );
138 ok( $app->config_from_override,
139 '... config_from_override true as expected' );
141 is( $app->configfile, File::Spec->canonpath('/notused'),
142 '... configfile is /notused as expected' );
145 my $app = App::DefaultConfigFileCodeRef->new_with_options;
146 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
149 ok( $app->config_from_override,
150 '... config_from_override true as expected' );
152 is( $app->configfile, File::Spec->canonpath('/notused'),
153 '... configfile is /notused as expected' );
157 # Required arg not supplied from cmdline
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/;
164 like exception { App->new_with_options }, qr/Required option missing: required_from_argv/;
168 # Config file value overriden from cmdline
170 local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
172 my $app = App->new_with_options;
173 isa_ok( $app, 'App' );
175 is( $app->required_from_config, 'override',
176 '... required_from_config is override as expected' );
178 is( $app->optional_from_config, 'from_config_2',
179 '... optional_from_config is from_config_2 as expected' );
184 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
186 my $app = App->new_with_options;
187 isa_ok( $app, 'App' );
189 is( $app->required_from_config, 'noconfig',
190 '... required_from_config is noconfig as expected' );
192 ok( !defined $app->optional_from_config,
193 '... optional_from_config is undef as expected' );
197 package BaseApp::WithConfig;
199 with 'MooseX::ConfigFromFile';
201 sub get_config_from_file { return {}; }
205 package DerivedApp::Getopt;
207 extends 'BaseApp::WithConfig';
208 with 'MooseX::Getopt';
211 # With DerivedApp, the Getopt role was applied at a different level
212 # than the ConfigFromFile role
214 ok ! exception { DerivedApp::Getopt->new_with_options }, 'Can create DerivedApp';
220 is( $app->required_from_config, 'from_config_1',
221 '... required_from_config is from_config_1 as expected' );
223 is( $app->optional_from_config, 'from_config_2',
224 '... optional_from_config is from_config_2 as expected' );
226 is( $app->required_from_argv, '1',
227 '... required_from_argv is 1 as expected' );