test for warnings everywhere
[gitmo/MooseX-Getopt.git] / t / 102_basic_basic.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 70;
5 use Test::NoWarnings 1.04 ':early';
6
7 BEGIN {
8     use_ok('MooseX::Getopt::Basic');
9 }
10
11 {
12     package App;
13     use Moose;
14
15     with 'MooseX::Getopt::Basic';
16
17     has 'data' => (
18         metaclass => 'MooseX::Getopt::Meta::Attribute',
19         is        => 'ro',
20         isa       => 'Str',
21         default   => 'file.dat',
22         cmd_flag  => 'f',
23     );
24
25     has 'cow' => (
26         metaclass   => 'Getopt',
27         is          => 'ro',
28         isa         => 'Str',
29         default     => 'moo',
30         cmd_aliases => [qw/ moocow m c /],
31     );
32
33     has 'horse' => (
34         metaclass   => 'MooseX::Getopt::Meta::Attribute',
35         is          => 'ro',
36         isa         => 'Str',
37         default     => 'bray',
38         cmd_flag    => 'horsey',
39         cmd_aliases => 'x',
40     );
41
42     has 'length' => (
43         is      => 'ro',
44         isa     => 'Int',
45         default => 24
46     );
47
48     has 'verbose' => (
49         is     => 'ro',
50         isa    => 'Bool',
51     );
52
53     has 'libs' => (
54         is      => 'ro',
55         isa     => 'ArrayRef',
56         default => sub { [] },
57     );
58
59     has 'details' => (
60         is      => 'ro',
61         isa     => 'HashRef',
62         default => sub { {} },
63     );
64
65     has '_private_stuff' => (
66         is      => 'ro',
67         isa     => 'Int',
68         default => 713
69     );
70
71     has '_private_stuff_cmdline' => (
72         metaclass => 'MooseX::Getopt::Meta::Attribute',
73         is        => 'ro',
74         isa       => 'Int',
75         default   => 832,
76         cmd_flag  => 'p',
77     );
78
79 }
80
81 foreach my $attr_name (qw(data cow horse _private_stuff_cmdline)) {
82     my $attr = App->meta->get_attribute($attr_name);
83     isa_ok($attr, 'Moose::Meta::Attribute');
84     isa_ok($attr, 'MooseX::Getopt::Meta::Attribute');
85     can_ok($attr, 'cmd_flag');
86     can_ok($attr, 'cmd_aliases');
87 }
88
89 {
90     local @ARGV = ();
91
92     my $app = App->new_with_options;
93     isa_ok($app, 'App');
94
95     ok(!$app->verbose, '... verbosity is off as expected');
96     is($app->length, 24, '... length is 24 as expected');
97     is($app->data, 'file.dat', '... data is file.dat as expected');
98     is_deeply($app->libs, [], '... libs is [] as expected');
99     is_deeply($app->details, {}, '... details is {} as expected');
100 }
101
102 {
103     local @ARGV = ('--verbose', '--length', 50);
104
105     my $app = App->new_with_options;
106     isa_ok($app, 'App');
107
108     ok($app->verbose, '... verbosity is turned on as expected');
109     is($app->length, 50, '... length is 50 as expected');
110     is($app->data, 'file.dat', '... data is file.dat as expected');
111     is_deeply($app->libs, [], '... libs is [] as expected');
112     is_deeply($app->details, {}, '... details is {} as expected');
113 }
114
115 {
116     local @ARGV = ('--verbose', '-f', 'foo.txt');
117
118     my $app = App->new_with_options;
119     isa_ok($app, 'App');
120
121     ok($app->verbose, '... verbosity is turned on as expected');
122     is($app->length, 24, '... length is 24 as expected');
123     is($app->data, 'foo.txt', '... data is foo.txt as expected');
124     is_deeply($app->libs, [], '... libs is [] as expected');
125     is_deeply($app->details, {}, '... details is {} as expected');
126 }
127
128 {
129     local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib');
130
131     my $app = App->new_with_options;
132     isa_ok($app, 'App');
133
134     ok($app->verbose, '... verbosity is turned on as expected');
135     is($app->length, 24, '... length is 24 as expected');
136     is($app->data, 'file.dat', '... data is foo.txt as expected');
137     is_deeply($app->libs,
138     ['libs/', 'includes/lib'],
139     '... libs is [libs/, includes/lib] as expected');
140     is_deeply($app->details, {}, '... details is {} as expected');
141 }
142
143 {
144     local @ARGV = ('--details', 'os=mac', '--details', 'name=foo');
145
146     my $app = App->new_with_options;
147     isa_ok($app, 'App');
148
149     ok(!$app->verbose, '... verbosity is turned on as expected');
150     is($app->length, 24, '... length is 24 as expected');
151     is($app->data, 'file.dat', '... data is foo.txt as expected');
152     is_deeply($app->libs, [], '... libs is [] as expected');
153     is_deeply($app->details,
154     { os => 'mac', name => 'foo' },
155     '... details is { os => mac, name => foo } as expected');
156 }
157
158 {
159     # Test negation on booleans too ...
160     local @ARGV = ('--noverbose');
161
162     my $app = App->new_with_options;
163     isa_ok($app, 'App');
164
165     ok(!$app->verbose, '... verbosity is turned off as expected');
166     is($app->length, 24, '... length is 24 as expected');
167     is($app->data, 'file.dat', '... file is file.dat as expected');
168     is_deeply($app->libs, [], '... libs is [] as expected');
169     is_deeply($app->details, {}, '... details is {} as expected');
170 }
171
172 # Test cmd_alias without cmd_flag
173 {
174     local @ARGV = ('--cow', '42');
175     my $app = App->new_with_options;
176     isa_ok($app, 'App');
177     is($app->cow, 42, 'cmd_alias, but not using it');
178 }
179 {
180     local @ARGV = ('--moocow', '88');
181     my $app = App->new_with_options;
182     isa_ok($app, 'App');
183     is($app->cow, 88, 'cmd_alias, using long one');
184 }
185 {
186     local @ARGV = ('-c', '99');
187     my $app = App->new_with_options;
188     isa_ok($app, 'App');
189     is($app->cow, 99, 'cmd_alias, using short one');
190 }
191
192 # Test cmd_alias + cmd_flag
193 {
194     local @ARGV = ('--horsey', '123');
195     my $app = App->new_with_options;
196     isa_ok($app, 'App');
197     is($app->horse, 123, 'cmd_alias+cmd_flag, using flag');
198 }
199 {
200     local @ARGV = ('-x', '321');
201     my $app = App->new_with_options;
202     isa_ok($app, 'App');
203     is($app->horse, 321, 'cmd_alias+cmd_flag, using alias');
204 }
205
206 # Test _foo + cmd_flag
207 {
208     local @ARGV = ('-p', '666');
209     my $app = App->new_with_options;
210     isa_ok($app, 'App');
211     is($app->_private_stuff_cmdline, 666, '_foo + cmd_flag');
212 }
213
214 # Test ARGV support
215 {
216     my @args = ('-p', 12345, '-c', 99, '-');
217     local @ARGV = @args;
218     my $app = App->new_with_options;
219     isa_ok($app, 'App');
220     is_deeply($app->ARGV, \@args, 'ARGV accessor');
221     is_deeply(\@ARGV, \@args, '@ARGV unmangled');
222     is_deeply($app->extra_argv, ['-'], 'extra_argv accessor');
223 }