if configfile is a coderef call it and write some tests for it
[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;
d1e5e425 8use File::Spec;
62c4891a 9
78a71ae5 10if ( !eval { require MooseX::ConfigFromFile } )
62c4891a 11{
12 plan skip_all => 'Test requires MooseX::ConfigFromFile';
13}
14else
15{
aa03fdad 16 plan tests => 37;
62c4891a 17}
18
19{
20 package App;
21
22 use Moose;
23 with 'MooseX::Getopt';
24 with 'MooseX::ConfigFromFile';
25
26 has 'config_from_override' => (
27 is => 'ro',
28 isa => 'Bool',
29 default => 0,
30 );
31
32 has 'optional_from_config' => (
33 is => 'ro',
34 isa => 'Str',
35 required => 0,
36 );
37
38 has 'required_from_config' => (
39 is => 'ro',
40 isa => 'Str',
41 required => 1,
42 );
43
44 has 'required_from_argv' => (
45 is => 'ro',
46 isa => 'Str',
47 required => 1,
48 );
49
50 sub get_config_from_file
51 {
52 my ( $class, $file ) = @_;
53
54 my %config = (
55 required_from_config => 'from_config_1',
56 optional_from_config => 'from_config_2',
57 );
58
d1e5e425 59 my $cpath = File::Spec->canonpath('/notused/default');
60 if ( $file ne $cpath ) {
62c4891a 61 $config{config_from_override} = 1;
62 }
63
64 return \%config;
65 }
66}
67
68{
69 package App::DefaultConfigFile;
70
71 use Moose;
72 extends 'App';
73
74 has '+configfile' => (
d1e5e425 75 default => File::Spec->canonpath('/notused/default'),
62c4891a 76 );
77}
78
aa03fdad 79{
80 package App::DefaultConfigFileCodeRef;
81
82 use Moose;
83 extends 'App';
84
85 has '+configfile' => (
86 default => sub { return File::Spec->canonpath('/notused/default') },
87 );
88}
89
62c4891a 90# No config specified
91{
92 local @ARGV = qw( --required_from_argv 1 );
93
94 throws_ok { App->new_with_options } qr/Required option missing: required_from_config/;
95
4e086633 96 {
97 my $app = App::DefaultConfigFile->new_with_options;
62c4891a 98 isa_ok( $app, 'App::DefaultConfigFile' );
99 app_ok( $app );
100
4e086633 101 ok( !$app->config_from_override,
62c4891a 102 '... config_from_override false as expected' );
103
d1e5e425 104 is( $app->configfile, File::Spec->canonpath('/notused/default'),
62c4891a 105 '... configfile is /notused/default as expected' );
106 }
107}
108
aa03fdad 109# No config specified
110{
111 local @ARGV = qw( --required_from_argv 1 );
112
113 {
114 my $app = App::DefaultConfigFileCodeRef->new_with_options;
115 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
116 app_ok( $app );
117
118 ok( !$app->config_from_override,
119 '... config_from_override false as expected' );
120
121 is( $app->configfile, File::Spec->canonpath('/notused/default'),
122 '... configfile is /notused/default as expected' );
123 }
124}
125
62c4891a 126# Config specified
127{
128 local @ARGV = qw( --configfile /notused --required_from_argv 1 );
129
4e086633 130 {
131 my $app = App->new_with_options;
62c4891a 132 isa_ok( $app, 'App' );
133 app_ok( $app );
134 }
135
4e086633 136 {
137 my $app = App::DefaultConfigFile->new_with_options;
62c4891a 138 isa_ok( $app, 'App::DefaultConfigFile' );
139 app_ok( $app );
aa03fdad 140
141 ok( $app->config_from_override,
142 '... config_from_override true as expected' );
143
144 is( $app->configfile, File::Spec->canonpath('/notused'),
145 '... configfile is /notused as expected' );
146 }
147 {
148 my $app = App::DefaultConfigFileCodeRef->new_with_options;
149 isa_ok( $app, 'App::DefaultConfigFileCodeRef' );
150 app_ok( $app );
62c4891a 151
4e086633 152 ok( $app->config_from_override,
62c4891a 153 '... config_from_override true as expected' );
154
d1e5e425 155 is( $app->configfile, File::Spec->canonpath('/notused'),
62c4891a 156 '... configfile is /notused as expected' );
157 }
158}
159
160# Required arg not supplied from cmdline
161{
162 local @ARGV = qw( --configfile /notused );
163 throws_ok { App->new_with_options } qr/Required option missing: required_from_argv/;
164}
165
166# Config file value overriden from cmdline
167{
168 local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
169
170 my $app = App->new_with_options;
171 isa_ok( $app, 'App' );
172
173 is( $app->required_from_config, 'override',
174 '... required_from_config is override as expected' );
175
176 is( $app->optional_from_config, 'from_config_2',
177 '... optional_from_config is from_config_2 as expected' );
178}
179
180# No config file
181{
182 local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
183
184 my $app = App->new_with_options;
185 isa_ok( $app, 'App' );
186
187 is( $app->required_from_config, 'noconfig',
188 '... required_from_config is noconfig as expected' );
189
190 ok( !defined $app->optional_from_config,
191 '... optional_from_config is undef as expected' );
192}
193
9f1ec7c0 194{
195 package BaseApp::WithConfig;
196 use Moose;
197 with 'MooseX::ConfigFromFile';
198
199 sub get_config_from_file { return {}; }
200}
201
202{
203 package DerivedApp::Getopt;
204 use Moose;
205 extends 'BaseApp::WithConfig';
206 with 'MooseX::Getopt';
207}
208
209# With DerivedApp, the Getopt role was applied at a different level
210# than the ConfigFromFile role
211{
212 lives_ok { DerivedApp::Getopt->new_with_options } 'Can create DerivedApp';
213}
214
62c4891a 215sub app_ok {
216 my $app = shift;
217
4e086633 218 is( $app->required_from_config, 'from_config_1',
62c4891a 219 '... required_from_config is from_config_1 as expected' );
220
4e086633 221 is( $app->optional_from_config, 'from_config_2',
62c4891a 222 '... optional_from_config is from_config_2 as expected' );
223
4e086633 224 is( $app->required_from_argv, '1',
62c4891a 225 '... required_from_argv is 1 as expected' );
226}