Update versions, changelog, fix test when ConfigFromFile isn't available
[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;
8
78a71ae5 9if ( !eval { require MooseX::ConfigFromFile } )
62c4891a 10{
11 plan skip_all => 'Test requires MooseX::ConfigFromFile';
12}
13else
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
4e086633 83 {
84 my $app = App::DefaultConfigFile->new_with_options;
62c4891a 85 isa_ok( $app, 'App::DefaultConfigFile' );
86 app_ok( $app );
87
4e086633 88 ok( !$app->config_from_override,
62c4891a 89 '... config_from_override false as expected' );
90
4e086633 91 is( $app->configfile, '/notused/default',
62c4891a 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
4e086633 100 {
101 my $app = App->new_with_options;
62c4891a 102 isa_ok( $app, 'App' );
103 app_ok( $app );
104 }
105
4e086633 106 {
107 my $app = App::DefaultConfigFile->new_with_options;
62c4891a 108 isa_ok( $app, 'App::DefaultConfigFile' );
109 app_ok( $app );
110
4e086633 111 ok( $app->config_from_override,
62c4891a 112 '... config_from_override true as expected' );
113
4e086633 114 is( $app->configfile, '/notused',
62c4891a 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
153sub app_ok {
154 my $app = shift;
155
4e086633 156 is( $app->required_from_config, 'from_config_1',
62c4891a 157 '... required_from_config is from_config_1 as expected' );
158
4e086633 159 is( $app->optional_from_config, 'from_config_2',
62c4891a 160 '... optional_from_config is from_config_2 as expected' );
161
4e086633 162 is( $app->required_from_argv, '1',
62c4891a 163 '... required_from_argv is 1 as expected' );
164}