10 if ( !eval { require MooseX::ConfigFromFile } )
12 plan skip_all => 'Test requires MooseX::ConfigFromFile';
23 with 'MooseX::Getopt';
24 with 'MooseX::ConfigFromFile';
26 has 'config_from_override' => (
32 has 'optional_from_config' => (
38 has 'required_from_config' => (
44 has 'required_from_argv' => (
50 sub get_config_from_file
52 my ( $class, $file ) = @_;
55 required_from_config => 'from_config_1',
56 optional_from_config => 'from_config_2',
59 my $cpath = File::Spec->canonpath('/notused/default');
60 if ( $file ne $cpath ) {
61 $config{config_from_override} = 1;
69 package App::DefaultConfigFile;
74 has '+configfile' => (
75 default => File::Spec->canonpath('/notused/default'),
81 local @ARGV = qw( --required_from_argv 1 );
83 throws_ok { App->new_with_options } qr/Required option missing: required_from_config/;
86 my $app = App::DefaultConfigFile->new_with_options;
87 isa_ok( $app, 'App::DefaultConfigFile' );
90 ok( !$app->config_from_override,
91 '... config_from_override false as expected' );
93 is( $app->configfile, File::Spec->canonpath('/notused/default'),
94 '... configfile is /notused/default as expected' );
100 local @ARGV = qw( --configfile /notused --required_from_argv 1 );
103 my $app = App->new_with_options;
104 isa_ok( $app, 'App' );
109 my $app = App::DefaultConfigFile->new_with_options;
110 isa_ok( $app, 'App::DefaultConfigFile' );
113 ok( $app->config_from_override,
114 '... config_from_override true as expected' );
116 is( $app->configfile, File::Spec->canonpath('/notused'),
117 '... configfile is /notused as expected' );
121 # Required arg not supplied from cmdline
123 local @ARGV = qw( --configfile /notused );
124 throws_ok { App->new_with_options } qr/Required option missing: required_from_argv/;
127 # Config file value overriden from cmdline
129 local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
131 my $app = App->new_with_options;
132 isa_ok( $app, 'App' );
134 is( $app->required_from_config, 'override',
135 '... required_from_config is override as expected' );
137 is( $app->optional_from_config, 'from_config_2',
138 '... optional_from_config is from_config_2 as expected' );
143 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
145 my $app = App->new_with_options;
146 isa_ok( $app, 'App' );
148 is( $app->required_from_config, 'noconfig',
149 '... required_from_config is noconfig as expected' );
151 ok( !defined $app->optional_from_config,
152 '... optional_from_config is undef as expected' );
156 package BaseApp::WithConfig;
158 with 'MooseX::ConfigFromFile';
160 sub get_config_from_file { return {}; }
164 package DerivedApp::Getopt;
166 extends 'BaseApp::WithConfig';
167 with 'MooseX::Getopt';
170 # With DerivedApp, the Getopt role was applied at a different level
171 # than the ConfigFromFile role
173 lives_ok { DerivedApp::Getopt->new_with_options } 'Can create DerivedApp';
179 is( $app->required_from_config, 'from_config_1',
180 '... required_from_config is from_config_1 as expected' );
182 is( $app->optional_from_config, 'from_config_2',
183 '... optional_from_config is from_config_2 as expected' );
185 is( $app->required_from_argv, '1',
186 '... required_from_argv is 1 as expected' );