4 use Test::More tests => 70;
6 use Test::NoWarnings 1.04 ':early';
9 use_ok('MooseX::Getopt');
16 with 'MooseX::Getopt';
19 metaclass => 'Getopt',
22 default => 'file.dat',
27 metaclass => 'Getopt',
31 cmd_aliases => [qw/ moocow m c /],
57 default => sub { [] },
63 default => sub { {} },
66 has '_private_stuff' => (
72 has '_private_stuff_cmdline' => (
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 if ($attr_name eq 'data' or $attr_name eq 'cow')
87 isa_ok($attr, 'MooseX::Getopt::Meta::Attribute');
91 does_ok($attr, 'MooseX::Getopt::Meta::Attribute::Trait');
93 can_ok($attr, 'cmd_flag');
94 can_ok($attr, 'cmd_aliases');
100 my $app = App->new_with_options;
103 ok(!$app->verbose, '... verbosity is off as expected');
104 is($app->length, 24, '... length is 24 as expected');
105 is($app->data, 'file.dat', '... data is file.dat as expected');
106 is_deeply($app->libs, [], '... libs is [] as expected');
107 is_deeply($app->details, {}, '... details is {} as expected');
111 local @ARGV = ('--verbose', '--length', 50);
113 my $app = App->new_with_options;
116 ok($app->verbose, '... verbosity is turned on as expected');
117 is($app->length, 50, '... length is 50 as expected');
118 is($app->data, 'file.dat', '... data is file.dat as expected');
119 is_deeply($app->libs, [], '... libs is [] as expected');
120 is_deeply($app->details, {}, '... details is {} as expected');
124 local @ARGV = ('--verbose', '-f', 'foo.txt');
126 my $app = App->new_with_options;
129 ok($app->verbose, '... verbosity is turned on as expected');
130 is($app->length, 24, '... length is 24 as expected');
131 is($app->data, 'foo.txt', '... data is foo.txt as expected');
132 is_deeply($app->libs, [], '... libs is [] as expected');
133 is_deeply($app->details, {}, '... details is {} as expected');
137 local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib');
139 my $app = App->new_with_options;
142 ok($app->verbose, '... verbosity is turned on as expected');
143 is($app->length, 24, '... length is 24 as expected');
144 is($app->data, 'file.dat', '... data is foo.txt as expected');
145 is_deeply($app->libs,
146 ['libs/', 'includes/lib'],
147 '... libs is [libs/, includes/lib] as expected');
148 is_deeply($app->details, {}, '... details is {} as expected');
152 local @ARGV = ('--details', 'os=mac', '--details', 'name=foo');
154 my $app = App->new_with_options;
157 ok(!$app->verbose, '... verbosity is turned on as expected');
158 is($app->length, 24, '... length is 24 as expected');
159 is($app->data, 'file.dat', '... data is foo.txt as expected');
160 is_deeply($app->libs, [], '... libs is [] as expected');
161 is_deeply($app->details,
162 { os => 'mac', name => 'foo' },
163 '... details is { os => mac, name => foo } as expected');
167 # Test negation on booleans too ...
168 local @ARGV = ('--noverbose');
170 my $app = App->new_with_options;
173 ok(!$app->verbose, '... verbosity is turned off as expected');
174 is($app->length, 24, '... length is 24 as expected');
175 is($app->data, 'file.dat', '... file is file.dat as expected');
176 is_deeply($app->libs, [], '... libs is [] as expected');
177 is_deeply($app->details, {}, '... details is {} as expected');
180 # Test cmd_alias without cmd_flag
182 local @ARGV = ('--cow', '42');
183 my $app = App->new_with_options;
185 is($app->cow, 42, 'cmd_alias, but not using it');
188 local @ARGV = ('--moocow', '88');
189 my $app = App->new_with_options;
191 is($app->cow, 88, 'cmd_alias, using long one');
194 local @ARGV = ('-c', '99');
195 my $app = App->new_with_options;
197 is($app->cow, 99, 'cmd_alias, using short one');
200 # Test cmd_alias + cmd_flag
202 local @ARGV = ('--horsey', '123');
203 my $app = App->new_with_options;
205 is($app->horse, 123, 'cmd_alias+cmd_flag, using flag');
208 local @ARGV = ('-x', '321');
209 my $app = App->new_with_options;
211 is($app->horse, 321, 'cmd_alias+cmd_flag, using alias');
214 # Test _foo + cmd_flag
216 local @ARGV = ('-p', '666');
217 my $app = App->new_with_options;
219 is($app->_private_stuff_cmdline, 666, '_foo + cmd_flag');
224 my @args = ('-p', 12345, '-c', 99, '-');
226 my $app = App->new_with_options;
228 is_deeply($app->ARGV, \@args, 'ARGV accessor');
229 is_deeply(\@ARGV, \@args, '@ARGV unmangled');
230 is_deeply($app->extra_argv, ['-'], 'extra_argv accessor');