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' => (
81 foreach my $attr_name (qw(data cow horse _private_stuff_cmdline)) {
82 my $attr = App->meta->get_attribute($attr_name);
83 isa_ok($attr, 'Moose::Meta::Attribute');
84 if ($attr_name eq 'data' or $attr_name eq 'cow')
86 isa_ok($attr, 'MooseX::Getopt::Meta::Attribute');
90 does_ok($attr, 'MooseX::Getopt::Meta::Attribute::Trait');
92 can_ok($attr, 'cmd_flag');
93 can_ok($attr, 'cmd_aliases');
99 my $app = App->new_with_options;
102 ok(!$app->verbose, '... verbosity is off as expected');
103 is($app->length, 24, '... length is 24 as expected');
104 is($app->data, 'file.dat', '... data is file.dat as expected');
105 is_deeply($app->libs, [], '... libs is [] as expected');
106 is_deeply($app->details, {}, '... details is {} as expected');
110 local @ARGV = ('--verbose', '--length', 50);
112 my $app = App->new_with_options;
115 ok($app->verbose, '... verbosity is turned on as expected');
116 is($app->length, 50, '... length is 50 as expected');
117 is($app->data, 'file.dat', '... data is file.dat as expected');
118 is_deeply($app->libs, [], '... libs is [] as expected');
119 is_deeply($app->details, {}, '... details is {} as expected');
123 local @ARGV = ('--verbose', '-f', 'foo.txt');
125 my $app = App->new_with_options;
128 ok($app->verbose, '... verbosity is turned on as expected');
129 is($app->length, 24, '... length is 24 as expected');
130 is($app->data, 'foo.txt', '... data is foo.txt as expected');
131 is_deeply($app->libs, [], '... libs is [] as expected');
132 is_deeply($app->details, {}, '... details is {} as expected');
136 local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib');
138 my $app = App->new_with_options;
141 ok($app->verbose, '... verbosity is turned on as expected');
142 is($app->length, 24, '... length is 24 as expected');
143 is($app->data, 'file.dat', '... data is foo.txt as expected');
144 is_deeply($app->libs,
145 ['libs/', 'includes/lib'],
146 '... libs is [libs/, includes/lib] as expected');
147 is_deeply($app->details, {}, '... details is {} as expected');
151 local @ARGV = ('--details', 'os=mac', '--details', 'name=foo');
153 my $app = App->new_with_options;
156 ok(!$app->verbose, '... verbosity is turned on as expected');
157 is($app->length, 24, '... length is 24 as expected');
158 is($app->data, 'file.dat', '... data is foo.txt as expected');
159 is_deeply($app->libs, [], '... libs is [] as expected');
160 is_deeply($app->details,
161 { os => 'mac', name => 'foo' },
162 '... details is { os => mac, name => foo } as expected');
166 # Test negation on booleans too ...
167 local @ARGV = ('--noverbose');
169 my $app = App->new_with_options;
172 ok(!$app->verbose, '... verbosity is turned off as expected');
173 is($app->length, 24, '... length is 24 as expected');
174 is($app->data, 'file.dat', '... file is file.dat as expected');
175 is_deeply($app->libs, [], '... libs is [] as expected');
176 is_deeply($app->details, {}, '... details is {} as expected');
179 # Test cmd_alias without cmd_flag
181 local @ARGV = ('--cow', '42');
182 my $app = App->new_with_options;
184 is($app->cow, 42, 'cmd_alias, but not using it');
187 local @ARGV = ('--moocow', '88');
188 my $app = App->new_with_options;
190 is($app->cow, 88, 'cmd_alias, using long one');
193 local @ARGV = ('-c', '99');
194 my $app = App->new_with_options;
196 is($app->cow, 99, 'cmd_alias, using short one');
199 # Test cmd_alias + cmd_flag
201 local @ARGV = ('--horsey', '123');
202 my $app = App->new_with_options;
204 is($app->horse, 123, 'cmd_alias+cmd_flag, using flag');
207 local @ARGV = ('-x', '321');
208 my $app = App->new_with_options;
210 is($app->horse, 321, 'cmd_alias+cmd_flag, using alias');
213 # Test _foo + cmd_flag
215 local @ARGV = ('-p', '666');
216 my $app = App->new_with_options;
218 is($app->_private_stuff_cmdline, 666, '_foo + cmd_flag');
223 my @args = ('-p', 12345, '-c', 99, '-');
225 my $app = App->new_with_options;
227 is_deeply($app->ARGV, \@args, 'ARGV accessor');
228 is_deeply(\@ARGV, \@args, '@ARGV unmangled');
229 is_deeply($app->extra_argv, ['-'], 'extra_argv accessor');