6 use Test::More tests => 69;
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');
95 my $app = App->new_with_options;
98 ok(!$app->verbose, '... verbosity is off as expected');
99 is($app->length, 24, '... length is 24 as expected');
100 is($app->data, 'file.dat', '... data is file.dat as expected');
101 is_deeply($app->libs, [], '... libs is [] as expected');
102 is_deeply($app->details, {}, '... details is {} as expected');
106 local @ARGV = ('--verbose', '--length', 50);
108 my $app = App->new_with_options;
111 ok($app->verbose, '... verbosity is turned on as expected');
112 is($app->length, 50, '... length is 50 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', '-f', 'foo.txt');
121 my $app = App->new_with_options;
124 ok($app->verbose, '... verbosity is turned on as expected');
125 is($app->length, 24, '... length is 24 as expected');
126 is($app->data, 'foo.txt', '... data is foo.txt as expected');
127 is_deeply($app->libs, [], '... libs is [] as expected');
128 is_deeply($app->details, {}, '... details is {} as expected');
132 local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib');
134 my $app = App->new_with_options;
137 ok($app->verbose, '... verbosity is turned on as expected');
138 is($app->length, 24, '... length is 24 as expected');
139 is($app->data, 'file.dat', '... data is foo.txt as expected');
140 is_deeply($app->libs,
141 ['libs/', 'includes/lib'],
142 '... libs is [libs/, includes/lib] as expected');
143 is_deeply($app->details, {}, '... details is {} as expected');
147 local @ARGV = ('--details', 'os=mac', '--details', 'name=foo');
149 my $app = App->new_with_options;
152 ok(!$app->verbose, '... verbosity is turned on as expected');
153 is($app->length, 24, '... length is 24 as expected');
154 is($app->data, 'file.dat', '... data is foo.txt as expected');
155 is_deeply($app->libs, [], '... libs is [] as expected');
156 is_deeply($app->details,
157 { os => 'mac', name => 'foo' },
158 '... details is { os => mac, name => foo } as expected');
162 # Test negation on booleans too ...
163 local @ARGV = ('--noverbose');
165 my $app = App->new_with_options;
168 ok(!$app->verbose, '... verbosity is turned off as expected');
169 is($app->length, 24, '... length is 24 as expected');
170 is($app->data, 'file.dat', '... file is file.dat as expected');
171 is_deeply($app->libs, [], '... libs is [] as expected');
172 is_deeply($app->details, {}, '... details is {} as expected');
175 # Test cmd_alias without cmd_flag
177 local @ARGV = ('--cow', '42');
178 my $app = App->new_with_options;
180 is($app->cow, 42, 'cmd_alias, but not using it');
183 local @ARGV = ('--moocow', '88');
184 my $app = App->new_with_options;
186 is($app->cow, 88, 'cmd_alias, using long one');
189 local @ARGV = ('-c', '99');
190 my $app = App->new_with_options;
192 is($app->cow, 99, 'cmd_alias, using short one');
195 # Test cmd_alias + cmd_flag
197 local @ARGV = ('--horsey', '123');
198 my $app = App->new_with_options;
200 is($app->horse, 123, 'cmd_alias+cmd_flag, using flag');
203 local @ARGV = ('-x', '321');
204 my $app = App->new_with_options;
206 is($app->horse, 321, 'cmd_alias+cmd_flag, using alias');
209 # Test _foo + cmd_flag
211 local @ARGV = ('-p', '666');
212 my $app = App->new_with_options;
214 is($app->_private_stuff_cmdline, 666, '_foo + cmd_flag');
219 my @args = ('-p', 12345, '-c', 99, '-');
221 my $app = App->new_with_options;
223 is_deeply($app->ARGV, \@args, 'ARGV accessor');
224 is_deeply(\@ARGV, \@args, '@ARGV unmangled');
225 is_deeply($app->extra_argv, ['-'], 'extra_argv accessor');