Getopt shouldn't require ConfigFromFile role applied at same level
[gitmo/MooseX-Getopt.git] / t / 008_configfromfile.t
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 use Test::Exception;
7 use Test::More;
8
9 if ( !eval { require MooseX::ConfigFromFile } )
10 {
11     plan skip_all => 'Test requires MooseX::ConfigFromFile';
12 }
13 else
14 {
15     plan tests => 25;
16 }
17
18 {
19     package App;
20
21     use Moose;
22     with 'MooseX::Getopt';
23     with 'MooseX::ConfigFromFile';
24
25     has 'config_from_override' => (
26         is       => 'ro',
27         isa      => 'Bool',
28         default  => 0,
29     );
30
31     has 'optional_from_config' => (
32         is        => 'ro',
33         isa       => 'Str',
34         required  => 0,
35     );
36
37     has 'required_from_config' => (
38         is        => 'ro',
39         isa       => 'Str',
40         required  => 1,
41     );
42
43     has 'required_from_argv' => (
44         is        => 'ro',
45         isa       => 'Str',
46         required  => 1,
47     );
48
49     sub get_config_from_file
50     {
51         my ( $class, $file ) = @_;
52
53         my %config = (
54             required_from_config => 'from_config_1',
55             optional_from_config => 'from_config_2',
56         );
57
58         if ( $file ne '/notused/default' ) {
59             $config{config_from_override} = 1;
60         }
61
62         return \%config;
63     }
64 }
65
66 {
67     package App::DefaultConfigFile;
68
69     use Moose;
70     extends 'App';
71
72     has '+configfile' => (
73         default => '/notused/default',
74     );
75 }
76
77 # No config specified
78 {
79     local @ARGV = qw( --required_from_argv 1 );
80
81     throws_ok { App->new_with_options } qr/Required option missing: required_from_config/;
82
83     {
84         my $app = App::DefaultConfigFile->new_with_options;
85         isa_ok( $app, 'App::DefaultConfigFile' );
86         app_ok( $app );
87
88         ok(  !$app->config_from_override,
89             '... config_from_override false as expected' );
90
91         is( $app->configfile, '/notused/default',
92             '... configfile is /notused/default as expected' );
93     }
94 }
95
96 # Config specified
97 {
98     local @ARGV = qw( --configfile /notused --required_from_argv 1 );
99
100     {
101         my $app = App->new_with_options;
102         isa_ok( $app, 'App' );
103         app_ok( $app );
104     }
105
106     {
107         my $app = App::DefaultConfigFile->new_with_options;
108         isa_ok( $app, 'App::DefaultConfigFile' );
109         app_ok( $app );
110
111         ok( $app->config_from_override,
112              '... config_from_override true as expected' );
113
114         is( $app->configfile, '/notused',
115             '... configfile is /notused as expected' );
116     }
117 }
118
119 # Required arg not supplied from cmdline
120 {
121     local @ARGV = qw( --configfile /notused );
122     throws_ok { App->new_with_options } qr/Required option missing: required_from_argv/;
123 }
124
125 # Config file value overriden from cmdline
126 {
127     local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
128
129     my $app = App->new_with_options;
130     isa_ok( $app, 'App' );
131
132     is( $app->required_from_config, 'override',
133         '... required_from_config is override as expected' );
134
135     is( $app->optional_from_config, 'from_config_2',
136         '... optional_from_config is from_config_2 as expected' );
137 }
138
139 # No config file
140 {
141     local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
142
143     my $app = App->new_with_options;
144     isa_ok( $app, 'App' );
145
146     is( $app->required_from_config, 'noconfig',
147         '... required_from_config is noconfig as expected' );
148
149     ok( !defined $app->optional_from_config,
150         '... optional_from_config is undef as expected' );
151 }
152
153 {
154     package BaseApp::WithConfig;
155     use Moose;
156     with 'MooseX::ConfigFromFile';
157
158     sub get_config_from_file { return {}; }
159 }
160
161 {
162     package DerivedApp::Getopt;
163     use Moose;
164     extends 'BaseApp::WithConfig';
165     with 'MooseX::Getopt';
166 }
167
168 # With DerivedApp, the Getopt role was applied at a different level
169 # than the ConfigFromFile role
170 {
171     lives_ok { DerivedApp::Getopt->new_with_options } 'Can create DerivedApp';
172 }
173
174 sub app_ok {
175     my $app = shift;
176
177     is( $app->required_from_config, 'from_config_1',
178         '... required_from_config is from_config_1 as expected' );
179
180     is( $app->optional_from_config, 'from_config_2',
181         '... optional_from_config is from_config_2 as expected' );
182
183     is( $app->required_from_argv, '1',
184         '... required_from_argv is 1 as expected' );
185 }