48dca2132bf7b190b4cb30e8bcfd37c5a6a7ffa5
[gitmo/MooseX-Getopt.git] / t / 001_basic.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 185;
7
8 BEGIN {
9     use_ok('MooseX::Getopt');
10 }
11
12 {
13     package App;
14     use Moose;
15
16     with 'MooseX::Getopt';
17
18     has 'data' => (
19         metaclass => 'MooseX::Getopt::Meta::Attribute',
20         is        => 'ro',
21         isa       => 'Str',
22         default   => 'file.dat',
23         cmd_flag  => 'f',
24     );
25
26     has 'cow' => (
27         metaclass   => 'Getopt',
28         is          => 'ro',
29         isa         => 'Str',
30         default     => 'moo',
31         cmd_aliases => [qw/ moocow m c /],
32     );
33
34     has 'horse' => (
35         metaclass   => 'MooseX::Getopt::Meta::Attribute',
36         is          => 'ro',
37         isa         => 'Str',
38         default     => 'bray',
39         cmd_flag    => 'horsey',
40         cmd_aliases => 'x',
41     );
42
43     has 'length' => (
44         is      => 'ro',
45         isa     => 'Int',
46         default => 24
47     );
48
49     has 'verbose' => (
50         is     => 'ro',
51         isa    => 'Bool',
52     );
53
54     has 'libs' => (
55         is      => 'ro',
56         isa     => 'ArrayRef',
57         default => sub { [] },
58     );
59
60     has 'details' => (
61         is      => 'ro',
62         isa     => 'HashRef',
63         default => sub { {} },
64     );
65
66     has '_private_stuff' => (
67         is      => 'ro',
68         isa     => 'Int',
69         default => 713
70     );
71
72     has '_private_stuff_cmdline' => (
73         metaclass => 'MooseX::Getopt::Meta::Attribute',
74         is        => 'ro',
75         isa       => 'Int',
76         default   => 832,
77         cmd_flag  => 'p',
78     );
79
80 }
81
82 foreach my $attr_name (qw(data cow horse _private_stuff_cmdline)) {
83     my $attr = App->meta->get_attribute($attr_name);
84     isa_ok($attr, 'Moose::Meta::Attribute');
85     isa_ok($attr, 'MooseX::Getopt::Meta::Attribute');
86     can_ok($attr, 'cmd_flag');
87     can_ok($attr, 'cmd_aliases');
88 }
89
90 foreach my $parser_name (qw(MooseX::Getopt::Parser::Long MooseX::Getopt::Parser::Descriptive)) {
91     SKIP: {
92         if ($parser_name eq 'MooseX::Getopt::Parser::Descriptive') {
93             eval { require Getopt::Long::Descriptive };
94             skip "Getopt::Long::Descriptive not installed", 78 if $@;
95         }
96
97         {
98             local @ARGV = ();
99
100             my $parser = $parser_name->new;
101             isa_ok($parser, $parser_name);
102
103             my $getopt = MooseX::Getopt::Session->new( parser => $parser );
104             isa_ok($getopt, 'MooseX::Getopt::Session');
105
106             my $app = App->new_with_options( getopt => $getopt );
107             isa_ok($app, 'App');
108
109             ok(!$app->verbose, '... verbosity is off as expected');
110             is($app->length, 24, '... length is 24 as expected');
111             is($app->data, 'file.dat', '... data is file.dat as expected');
112             is_deeply($app->libs, [], '... libs is [] as expected');
113             is_deeply($app->details, {}, '... details is {} as expected');
114         }
115
116         {
117             local @ARGV = ('--verbose', '--length', 50);
118
119             my $parser = $parser_name->new;
120             isa_ok($parser, $parser_name);
121
122             my $getopt = MooseX::Getopt::Session->new( parser => $parser );
123             isa_ok($getopt, 'MooseX::Getopt::Session');
124
125             my $app = App->new_with_options( getopt => $getopt );
126             isa_ok($app, 'App');
127
128             ok($app->verbose, '... verbosity is turned on as expected');
129             is($app->length, 50, '... length is 50 as expected');
130             is($app->data, 'file.dat', '... data is file.dat as expected');
131             is_deeply($app->libs, [], '... libs is [] as expected');
132             is_deeply($app->details, {}, '... details is {} as expected');
133         }
134
135         {
136             local @ARGV = ('--verbose', '-f', 'foo.txt');
137
138             my $parser = $parser_name->new;
139             isa_ok($parser, $parser_name);
140
141             my $getopt = MooseX::Getopt::Session->new( parser => $parser );
142             isa_ok($getopt, 'MooseX::Getopt::Session');
143
144             my $app = App->new_with_options( getopt => $getopt );
145             isa_ok($app, 'App');
146
147             ok($app->verbose, '... verbosity is turned on as expected');
148             is($app->length, 24, '... length is 24 as expected');
149             is($app->data, 'foo.txt', '... data is foo.txt as expected');
150
151             is_deeply($app->libs, [], '... libs is [] as expected');
152             is_deeply($app->details, {}, '... details is {} as expected');
153         }
154
155         {
156             local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib');
157
158             my $parser = $parser_name->new;
159             isa_ok($parser, $parser_name);
160
161             my $getopt = MooseX::Getopt::Session->new( parser => $parser );
162             isa_ok($getopt, 'MooseX::Getopt::Session');
163
164             my $app = App->new_with_options( getopt => $getopt );
165             isa_ok($app, 'App');
166
167             ok($app->verbose, '... verbosity is turned on as expected');
168             is($app->length, 24, '... length is 24 as expected');
169             is($app->data, 'file.dat', '... data is foo.txt as expected');
170             is_deeply($app->libs,
171             ['libs/', 'includes/lib'],
172             '... libs is [libs/, includes/lib] as expected');
173             is_deeply($app->details, {}, '... details is {} as expected');
174         }
175
176         {
177             local @ARGV = ('--details', 'os=mac', '--details', 'name=foo');
178
179             my $parser = $parser_name->new;
180             isa_ok($parser, $parser_name);
181
182             my $getopt = MooseX::Getopt::Session->new( parser => $parser );
183             isa_ok($getopt, 'MooseX::Getopt::Session');
184
185             my $app = App->new_with_options( getopt => $getopt );
186             isa_ok($app, 'App');
187
188             ok(!$app->verbose, '... verbosity is turned on as expected');
189             is($app->length, 24, '... length is 24 as expected');
190             is($app->data, 'file.dat', '... data is foo.txt as expected');
191             is_deeply($app->libs, [], '... libs is [] as expected');
192             is_deeply($app->details,
193             { os => 'mac', name => 'foo' },
194             '... details is { os => mac, name => foo } as expected');
195         }
196
197         {
198             # Test negation on booleans too ...
199             local @ARGV = ('--noverbose');
200
201             my $parser = $parser_name->new;
202             isa_ok($parser, $parser_name);
203
204             my $getopt = MooseX::Getopt::Session->new( parser => $parser );
205             isa_ok($getopt, 'MooseX::Getopt::Session');
206
207             my $app = App->new_with_options( getopt => $getopt );
208             isa_ok($app, 'App');
209
210             ok(!$app->verbose, '... verbosity is turned off as expected');
211             is($app->length, 24, '... length is 24 as expected');
212             is($app->data, 'file.dat', '... file is file.dat as expected');
213             is_deeply($app->libs, [], '... libs is [] as expected');
214             is_deeply($app->details, {}, '... details is {} as expected');
215         }
216
217         # Test cmd_alias without cmd_flag
218         {
219             local @ARGV = ('--cow', '42');
220
221             my $parser = $parser_name->new;
222             isa_ok($parser, $parser_name);
223
224             my $getopt = MooseX::Getopt::Session->new( parser => $parser );
225             isa_ok($getopt, 'MooseX::Getopt::Session');
226
227             my $app = App->new_with_options( getopt => $getopt );
228             isa_ok($app, 'App');
229             is($app->cow, 42, 'cmd_alias, but not using it');
230         }
231         {
232             local @ARGV = ('--moocow', '88');
233
234             my $parser = $parser_name->new;
235             isa_ok($parser, $parser_name);
236
237             my $getopt = MooseX::Getopt::Session->new( parser => $parser );
238             isa_ok($getopt, 'MooseX::Getopt::Session');
239
240             my $app = App->new_with_options( getopt => $getopt );
241             isa_ok($app, 'App');
242             is($app->cow, 88, 'cmd_alias, using long one');
243         }
244         {
245             local @ARGV = ('-c', '99');
246
247             my $parser = $parser_name->new;
248             isa_ok($parser, $parser_name);
249
250             my $getopt = MooseX::Getopt::Session->new( parser => $parser );
251             isa_ok($getopt, 'MooseX::Getopt::Session');
252
253             my $app = App->new_with_options( getopt => $getopt );
254             isa_ok($app, 'App');
255             is($app->cow, 99, 'cmd_alias, using short one');
256         }
257
258         # Test cmd_alias + cmd_flag
259         {
260             local @ARGV = ('--horsey', '123');
261
262             my $parser = $parser_name->new;
263             isa_ok($parser, $parser_name);
264
265             my $getopt = MooseX::Getopt::Session->new( parser => $parser );
266             isa_ok($getopt, 'MooseX::Getopt::Session');
267
268             my $app = App->new_with_options( getopt => $getopt );
269             isa_ok($app, 'App');
270             is($app->horse, 123, 'cmd_alias+cmd_flag, using flag');
271         }
272         {
273             local @ARGV = ('-x', '321');
274
275             my $parser = $parser_name->new;
276             isa_ok($parser, $parser_name);
277
278             my $getopt = MooseX::Getopt::Session->new( parser => $parser );
279             isa_ok($getopt, 'MooseX::Getopt::Session');
280
281             my $app = App->new_with_options( getopt => $getopt );
282             isa_ok($app, 'App');
283             is($app->horse, 321, 'cmd_alias+cmd_flag, using alias');
284         }
285
286         # Test _foo + cmd_flag
287         {
288             local @ARGV = ('-p', '666');
289
290             my $parser = $parser_name->new;
291             isa_ok($parser, $parser_name);
292
293             my $getopt = MooseX::Getopt::Session->new( parser => $parser );
294             isa_ok($getopt, 'MooseX::Getopt::Session');
295
296             my $app = App->new_with_options( getopt => $getopt );
297             isa_ok($app, 'App');
298             is($app->_private_stuff_cmdline, 666, '_foo + cmd_flag');
299         }
300
301         # Test ARGV support
302         {
303             my @args = ('-p', 12345, '-c', 99, '-');
304
305             local @ARGV = @args;
306
307             my $parser = $parser_name->new;
308             isa_ok($parser, $parser_name);
309
310             my $getopt = MooseX::Getopt::Session->new( parser => $parser );
311             isa_ok($getopt, 'MooseX::Getopt::Session');
312
313             my $app = App->new_with_options( getopt => $getopt );
314             isa_ok($app, 'App');
315             is_deeply($app->ARGV, \@args, 'ARGV accessor');
316             is_deeply(\@ARGV, \@args, '@ARGV unmangled');
317             is_deeply($app->extra_argv, ['-'], 'extra_argv accessor');
318         }
319
320     }
321 }
322
323 # Test default parser
324 {
325     local @ARGV = ();
326
327     my $app = App->new_with_options;
328     isa_ok($app, 'App');
329
330     ok(!$app->verbose, '... verbosity is off as expected');
331     is($app->length, 24, '... length is 24 as expected');
332     is($app->data, 'file.dat', '... data is file.dat as expected');
333     is_deeply($app->libs, [], '... libs is [] as expected');
334     is_deeply($app->details, {}, '... details is {} as expected');
335 }
336
337 {
338     local @ARGV = ('--verbose', '--length', 50);
339
340     my $app = App->new_with_options;
341     isa_ok($app, 'App');
342
343     ok($app->verbose, '... verbosity is turned on as expected');
344     is($app->length, 50, '... length is 50 as expected');
345     is($app->data, 'file.dat', '... data is file.dat as expected');
346     is_deeply($app->libs, [], '... libs is [] as expected');
347     is_deeply($app->details, {}, '... details is {} as expected');
348 }