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