TODO tests for loading required attributes from config
[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 ( !require MooseX::ConfigFromFile )
10 {
11     plan skip_all => 'Test requires MooseX::ConfigFromFile';
12 }
13 else
14 {
15     plan tests => 24;
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     TODO: {
85         local $TODO = 'Cannot supply required args from config';
86
87         my $app = eval { App::DefaultConfigFile->new_with_options };
88         isa_ok( $app, 'App::DefaultConfigFile' );
89         app_ok( $app );
90
91         ok( $app && !$app->config_from_override,
92             '... config_from_override false as expected' );
93
94         is( $app && $app->configfile, '/notused/default',
95             '... configfile is /notused/default as expected' );
96     }
97 }
98
99 # Config specified
100 {
101     local @ARGV = qw( --configfile /notused --required_from_argv 1 );
102
103     TODO: {
104         local $TODO = 'Cannot supply required args from config';
105
106         my $app = eval { App->new_with_options };
107         isa_ok( $app, 'App' );
108         app_ok( $app );
109     }
110
111     TODO: {
112         local $TODO = 'Cannot supply required args from config';
113
114         my $app = eval { App::DefaultConfigFile->new_with_options };
115         isa_ok( $app, 'App::DefaultConfigFile' );
116         app_ok( $app );
117
118         is ( $app && $app->config_from_override,
119              '... config_from_override true as expected' );
120
121         is( $app && $app->configfile, '/notused',
122             '... configfile is /notused as expected' );
123     }
124 }
125
126 # Required arg not supplied from cmdline
127 {
128     local @ARGV = qw( --configfile /notused );
129     throws_ok { App->new_with_options } qr/Required option missing: required_from_argv/;
130 }
131
132 # Config file value overriden from cmdline
133 {
134     local @ARGV = qw( --configfile /notused --required_from_argv 1 --required_from_config override );
135
136     my $app = App->new_with_options;
137     isa_ok( $app, 'App' );
138
139     is( $app->required_from_config, 'override',
140         '... required_from_config is override as expected' );
141
142     is( $app->optional_from_config, 'from_config_2',
143         '... optional_from_config is from_config_2 as expected' );
144 }
145
146 # No config file
147 {
148     local @ARGV = qw( --required_from_argv 1 --required_from_config noconfig );
149
150     my $app = App->new_with_options;
151     isa_ok( $app, 'App' );
152
153     is( $app->required_from_config, 'noconfig',
154         '... required_from_config is noconfig as expected' );
155
156     ok( !defined $app->optional_from_config,
157         '... optional_from_config is undef as expected' );
158 }
159
160 sub app_ok {
161     my $app = shift;
162
163     is( $app && $app->required_from_config, 'from_config_1',
164         '... required_from_config is from_config_1 as expected' );
165
166     is( $app && $app->optional_from_config, 'from_config_2',
167         '... optional_from_config is from_config_2 as expected' );
168
169     is( $app && $app->required_from_argv, '1',
170         '... required_from_argv is 1 as expected' );
171 }