9 if ( !eval { 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/;
84 my $app = App::DefaultConfigFile->new_with_options;
85 isa_ok( $app, 'App::DefaultConfigFile' );
88 ok( !$app->config_from_override,
89 '... config_from_override false as expected' );
91 is( $app->configfile, '/notused/default',
92 '... configfile is /notused/default as expected' );
98 local @ARGV = qw( --configfile /notused --required_from_argv 1 );
101 my $app = App->new_with_options;
102 isa_ok( $app, 'App' );
107 my $app = App::DefaultConfigFile->new_with_options;
108 isa_ok( $app, 'App::DefaultConfigFile' );
111 ok( $app->config_from_override,
112 '... config_from_override true as expected' );
114 is( $app->configfile, '/notused',
115 '... configfile is /notused as expected' );
119 # Required arg not supplied from cmdline
121 local @ARGV = qw( --configfile /notused );
122 throws_ok { App->new_with_options } qr/Required option missing: required_from_argv/;
125 # Config file value overriden from cmdline
127 local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
129 my $app = App->new_with_options;
130 isa_ok( $app, 'App' );
132 is( $app->required_from_config, 'override',
133 '... required_from_config is override as expected' );
135 is( $app->optional_from_config, 'from_config_2',
136 '... optional_from_config is from_config_2 as expected' );
141 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
143 my $app = App->new_with_options;
144 isa_ok( $app, 'App' );
146 is( $app->required_from_config, 'noconfig',
147 '... required_from_config is noconfig as expected' );
149 ok( !defined $app->optional_from_config,
150 '... optional_from_config is undef as expected' );
154 package BaseApp::WithConfig;
156 with 'MooseX::ConfigFromFile';
158 sub get_config_from_file { return {}; }
162 package DerivedApp::Getopt;
164 extends 'BaseApp::WithConfig';
165 with 'MooseX::Getopt';
168 # With DerivedApp, the Getopt role was applied at a different level
169 # than the ConfigFromFile role
171 lives_ok { DerivedApp::Getopt->new_with_options } 'Can create DerivedApp';
177 is( $app->required_from_config, 'from_config_1',
178 '... required_from_config is from_config_1 as expected' );
180 is( $app->optional_from_config, 'from_config_2',
181 '... optional_from_config is from_config_2 as expected' );
183 is( $app->required_from_argv, '1',
184 '... required_from_argv is 1 as expected' );