6 use Test::More tests => 263;
9 use_ok('MooseX::Getopt');
16 with 'MooseX::Getopt';
19 metaclass => 'MooseX::Getopt::Meta::Attribute',
22 default => 'file.dat',
27 metaclass => 'Getopt',
31 cmd_aliases => [qw/ moocow m c /],
35 metaclass => 'MooseX::Getopt::Meta::Attribute',
57 default => sub { [] },
63 default => sub { {} },
66 has '_private_stuff' => (
72 has '_private_stuff_cmdline' => (
73 metaclass => 'MooseX::Getopt::Meta::Attribute',
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');
90 foreach my $parser_name (qw(MooseX::Getopt::Parser::Long MooseX::Getopt::Parser::Descriptive MooseX::Getopt::Parser::Default)) {
92 if ($parser_name eq 'MooseX::Getopt::Parser::Descriptive') {
93 eval { require Getopt::Long::Descriptive };
94 skip "Getopt::Long::Descriptive not installed", 78 if $@;
100 my $parser = $parser_name->new;
101 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
103 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
104 isa_ok($getopt, 'MooseX::Getopt::Session');
106 my $app = App->new_with_options( getopt => $getopt );
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');
117 local @ARGV = ('--verbose', '--length', 50);
119 my $parser = $parser_name->new;
120 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
122 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
123 isa_ok($getopt, 'MooseX::Getopt::Session');
125 my $app = App->new_with_options( getopt => $getopt );
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');
136 local @ARGV = ('--verbose', '-f', 'foo.txt');
138 my $parser = $parser_name->new;
139 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
141 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
142 isa_ok($getopt, 'MooseX::Getopt::Session');
144 my $app = App->new_with_options( getopt => $getopt );
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');
151 is_deeply($app->libs, [], '... libs is [] as expected');
152 is_deeply($app->details, {}, '... details is {} as expected');
156 local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib');
158 my $parser = $parser_name->new;
159 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
161 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
162 isa_ok($getopt, 'MooseX::Getopt::Session');
164 my $app = App->new_with_options( getopt => $getopt );
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');
177 local @ARGV = ('--details', 'os=mac', '--details', 'name=foo');
179 my $parser = $parser_name->new;
180 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
182 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
183 isa_ok($getopt, 'MooseX::Getopt::Session');
185 my $app = App->new_with_options( getopt => $getopt );
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');
198 # Test negation on booleans too ...
199 local @ARGV = ('--noverbose');
201 my $parser = $parser_name->new;
202 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
204 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
205 isa_ok($getopt, 'MooseX::Getopt::Session');
207 my $app = App->new_with_options( getopt => $getopt );
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');
217 # Test cmd_alias without cmd_flag
219 local @ARGV = ('--cow', '42');
221 my $parser = $parser_name->new;
222 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
224 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
225 isa_ok($getopt, 'MooseX::Getopt::Session');
227 my $app = App->new_with_options( getopt => $getopt );
229 is($app->cow, 42, 'cmd_alias, but not using it');
232 local @ARGV = ('--moocow', '88');
234 my $parser = $parser_name->new;
235 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
237 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
238 isa_ok($getopt, 'MooseX::Getopt::Session');
240 my $app = App->new_with_options( getopt => $getopt );
242 is($app->cow, 88, 'cmd_alias, using long one');
245 local @ARGV = ('-c', '99');
247 my $parser = $parser_name->new;
248 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
250 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
251 isa_ok($getopt, 'MooseX::Getopt::Session');
253 my $app = App->new_with_options( getopt => $getopt );
255 is($app->cow, 99, 'cmd_alias, using short one');
258 # Test cmd_alias + cmd_flag
260 local @ARGV = ('--horsey', '123');
262 my $parser = $parser_name->new;
263 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
265 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
266 isa_ok($getopt, 'MooseX::Getopt::Session');
268 my $app = App->new_with_options( getopt => $getopt );
270 is($app->horse, 123, 'cmd_alias+cmd_flag, using flag');
273 local @ARGV = ('-x', '321');
275 my $parser = $parser_name->new;
276 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
278 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
279 isa_ok($getopt, 'MooseX::Getopt::Session');
281 my $app = App->new_with_options( getopt => $getopt );
283 is($app->horse, 321, 'cmd_alias+cmd_flag, using alias');
286 # Test _foo + cmd_flag
288 local @ARGV = ('-p', '666');
290 my $parser = $parser_name->new;
291 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
293 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
294 isa_ok($getopt, 'MooseX::Getopt::Session');
296 my $app = App->new_with_options( getopt => $getopt );
298 is($app->_private_stuff_cmdline, 666, '_foo + cmd_flag');
303 my @args = ('-p', 12345, '-c', 99, '-');
307 my $parser = $parser_name->new;
308 ok(ref($parser) =~ /^MooseX::Getopt::Parser::/, '... parser object is created');
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');
323 # Test default parser
327 my $app = App->new_with_options;
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');
338 local @ARGV = ('--verbose', '--length', 50);
340 my $app = App->new_with_options;
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');