9 if ( !require MooseX::ConfigFromFile )
11 plan skip_all => 'Test requires MooseX::ConfigFromFile';
22 with 'MooseX::Getopt';
23 with 'MooseX::ConfigFromFile';
25 has 'config_from_override' => (
31 has 'optional_from_config' => (
37 has 'required_from_config' => (
43 has 'required_from_argv' => (
49 sub get_config_from_file
51 my ( $class, $file ) = @_;
54 required_from_config => 'from_config_1',
55 optional_from_config => 'from_config_2',
58 if ( $file ne '/notused/default' ) {
59 $config{config_from_override} = 1;
67 package App::DefaultConfigFile;
72 has '+configfile' => (
73 default => '/notused/default',
79 local @ARGV = qw( --required_from_argv 1 );
81 throws_ok { App->new_with_options } qr/Required option missing: required_from_config/;
85 local $TODO = 'Cannot supply required args from config';
87 my $app = eval { App::DefaultConfigFile->new_with_options };
88 isa_ok( $app, 'App::DefaultConfigFile' );
91 ok( $app && !$app->config_from_override,
92 '... config_from_override false as expected' );
94 is( $app && $app->configfile, '/notused/default',
95 '... configfile is /notused/default as expected' );
101 local @ARGV = qw( --configfile /notused --required_from_argv 1 );
104 local $TODO = 'Cannot supply required args from config';
106 my $app = eval { App->new_with_options };
107 isa_ok( $app, 'App' );
112 local $TODO = 'Cannot supply required args from config';
114 my $app = eval { App::DefaultConfigFile->new_with_options };
115 isa_ok( $app, 'App::DefaultConfigFile' );
118 is ( $app && $app->config_from_override,
119 '... config_from_override true as expected' );
121 is( $app && $app->configfile, '/notused',
122 '... configfile is /notused as expected' );
126 # Required arg not supplied from cmdline
128 local @ARGV = qw( --configfile /notused );
129 throws_ok { App->new_with_options } qr/Required option missing: required_from_argv/;
132 # Config file value overriden from cmdline
134 local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
136 my $app = App->new_with_options;
137 isa_ok( $app, 'App' );
139 is( $app->required_from_config, 'override',
140 '... required_from_config is override as expected' );
142 is( $app->optional_from_config, 'from_config_2',
143 '... optional_from_config is from_config_2 as expected' );
148 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
150 my $app = App->new_with_options;
151 isa_ok( $app, 'App' );
153 is( $app->required_from_config, 'noconfig',
154 '... required_from_config is noconfig as expected' );
156 ok( !defined $app->optional_from_config,
157 '... optional_from_config is undef as expected' );
163 is( $app && $app->required_from_config, 'from_config_1',
164 '... required_from_config is from_config_1 as expected' );
166 is( $app && $app->optional_from_config, 'from_config_2',
167 '... optional_from_config is from_config_2 as expected' );
169 is( $app && $app->required_from_argv, '1',
170 '... required_from_argv is 1 as expected' );