6 use Test::More tests => 173;
10 use_ok('MooseX::Getopt');
17 with 'MooseX::Getopt';
20 traits => [ 'MooseX::Getopt::Meta::Attribute::Trait' ],
23 default => 'file.dat',
28 traits => [ 'Getopt' ],
32 cmd_aliases => [qw/ moocow m c /],
36 traits => [ 'Getopt' ],
58 default => sub { [] },
64 default => sub { {} },
67 has '_private_stuff' => (
73 has '_private_stuff_cmdline' => (
74 traits => [ 'Getopt' ],
83 foreach my $attr_name (qw(data cow horse _private_stuff_cmdline)) {
84 my $attr = App->meta->get_attribute($attr_name);
85 isa_ok($attr, 'Moose::Meta::Attribute');
86 does_ok($attr, 'MooseX::Getopt::Meta::Attribute::Trait');
88 can_ok($attr, 'cmd_flag');
89 can_ok($attr, 'cmd_aliases');
92 foreach my $parser_name (qw(MooseX::Getopt::Parser::Long MooseX::Getopt::Parser::Descriptive)) {
94 if ($parser_name eq 'MooseX::Getopt::Parser::Descriptive') {
95 eval { require Getopt::Long::Descriptive };
96 skip "Getopt::Long::Descriptive not installed", 78 if $@;
102 my $parser = $parser_name->new;
103 isa_ok($parser, $parser_name);
105 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
106 isa_ok($getopt, 'MooseX::Getopt::Session');
108 my $app = App->new_with_options( getopt => $getopt );
111 ok(!$app->verbose, '... verbosity is off as expected');
112 is($app->length, 24, '... length is 24 as expected');
113 is($app->data, 'file.dat', '... data is file.dat as expected');
114 is_deeply($app->libs, [], '... libs is [] as expected');
115 is_deeply($app->details, {}, '... details is {} as expected');
119 local @ARGV = ('--verbose', '--length', 50);
121 my $parser = $parser_name->new;
122 isa_ok($parser, $parser_name);
124 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
125 isa_ok($getopt, 'MooseX::Getopt::Session');
127 my $app = App->new_with_options( getopt => $getopt );
130 ok($app->verbose, '... verbosity is turned on as expected');
131 is($app->length, 50, '... length is 50 as expected');
132 is($app->data, 'file.dat', '... data is file.dat as expected');
133 is_deeply($app->libs, [], '... libs is [] as expected');
134 is_deeply($app->details, {}, '... details is {} as expected');
138 local @ARGV = ('--verbose', '-f', 'foo.txt');
140 my $parser = $parser_name->new;
141 isa_ok($parser, $parser_name);
143 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
144 isa_ok($getopt, 'MooseX::Getopt::Session');
146 my $app = App->new_with_options( getopt => $getopt );
149 ok($app->verbose, '... verbosity is turned on as expected');
150 is($app->length, 24, '... length is 24 as expected');
151 is($app->data, 'foo.txt', '... data is foo.txt as expected');
152 is_deeply($app->libs, [], '... libs is [] as expected');
153 is_deeply($app->details, {}, '... details is {} as expected');
157 local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib');
159 my $parser = $parser_name->new;
160 isa_ok($parser, $parser_name);
162 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
163 isa_ok($getopt, 'MooseX::Getopt::Session');
165 my $app = App->new_with_options( getopt => $getopt );
168 ok($app->verbose, '... verbosity is turned on as expected');
169 is($app->length, 24, '... length is 24 as expected');
170 is($app->data, 'file.dat', '... data is foo.txt as expected');
171 is_deeply($app->libs,
172 ['libs/', 'includes/lib'],
173 '... libs is [libs/, includes/lib] as expected');
174 is_deeply($app->details, {}, '... details is {} as expected');
178 local @ARGV = ('--details', 'os=mac', '--details', 'name=foo');
180 my $parser = $parser_name->new;
181 isa_ok($parser, $parser_name);
183 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
184 isa_ok($getopt, 'MooseX::Getopt::Session');
186 my $app = App->new_with_options( getopt => $getopt );
189 ok(!$app->verbose, '... verbosity is turned on as expected');
190 is($app->length, 24, '... length is 24 as expected');
191 is($app->data, 'file.dat', '... data is foo.txt as expected');
192 is_deeply($app->libs, [], '... libs is [] as expected');
193 is_deeply($app->details,
194 { os => 'mac', name => 'foo' },
195 '... details is { os => mac, name => foo } as expected');
199 # Test negation on booleans too ...
200 local @ARGV = ('--noverbose');
202 my $parser = $parser_name->new;
203 isa_ok($parser, $parser_name);
205 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
206 isa_ok($getopt, 'MooseX::Getopt::Session');
208 my $app = App->new_with_options( getopt => $getopt );
211 ok(!$app->verbose, '... verbosity is turned off as expected');
212 is($app->length, 24, '... length is 24 as expected');
213 is($app->data, 'file.dat', '... file is file.dat as expected');
214 is_deeply($app->libs, [], '... libs is [] as expected');
215 is_deeply($app->details, {}, '... details is {} as expected');
218 # Test cmd_alias without cmd_flag
220 local @ARGV = ('--cow', '42');
222 my $parser = $parser_name->new;
223 isa_ok($parser, $parser_name);
225 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
226 isa_ok($getopt, 'MooseX::Getopt::Session');
228 my $app = App->new_with_options( getopt => $getopt );
230 is($app->cow, 42, 'cmd_alias, but not using it');
233 local @ARGV = ('--moocow', '88');
235 my $parser = $parser_name->new;
236 isa_ok($parser, $parser_name);
238 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
239 isa_ok($getopt, 'MooseX::Getopt::Session');
241 my $app = App->new_with_options( getopt => $getopt );
243 is($app->cow, 88, 'cmd_alias, using long one');
246 local @ARGV = ('-c', '99');
248 my $parser = $parser_name->new;
249 isa_ok($parser, $parser_name);
251 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
252 isa_ok($getopt, 'MooseX::Getopt::Session');
254 my $app = App->new_with_options( getopt => $getopt );
256 is($app->cow, 99, 'cmd_alias, using short one');
259 # Test cmd_alias + cmd_flag
261 local @ARGV = ('--horsey', '123');
263 my $parser = $parser_name->new;
264 isa_ok($parser, $parser_name);
266 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
267 isa_ok($getopt, 'MooseX::Getopt::Session');
269 my $app = App->new_with_options( getopt => $getopt );
271 is($app->horse, 123, 'cmd_alias+cmd_flag, using flag');
274 local @ARGV = ('-x', '321');
276 my $parser = $parser_name->new;
277 isa_ok($parser, $parser_name);
279 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
280 isa_ok($getopt, 'MooseX::Getopt::Session');
282 my $app = App->new_with_options( getopt => $getopt );
284 is($app->horse, 321, 'cmd_alias+cmd_flag, using alias');
287 # Test _foo + cmd_flag
289 local @ARGV = ('-p', '666');
291 my $parser = $parser_name->new;
292 isa_ok($parser, $parser_name);
294 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
295 isa_ok($getopt, 'MooseX::Getopt::Session');
297 my $app = App->new_with_options( getopt => $getopt );
299 is($app->_private_stuff_cmdline, 666, '_foo + cmd_flag');
304 my @args = ('-p', 12345, '-c', 99, '-');
307 my $parser = $parser_name->new;
308 isa_ok($parser, $parser_name);
310 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
311 isa_ok($getopt, 'MooseX::Getopt::Session');
313 my $app = App->new_with_options( getopt => $getopt );
315 is_deeply($app->ARGV, \@args, 'ARGV accessor');
316 is_deeply(\@ARGV, \@args, '@ARGV unmangled');
317 is_deeply($app->extra_argv, ['-'], 'extra_argv accessor');