6 use Test::More tests => 69;
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');
93 my $app = App->new_with_options;
96 ok(!$app->verbose, '... verbosity is off as expected');
97 is($app->length, 24, '... length is 24 as expected');
98 is($app->data, 'file.dat', '... data is file.dat as expected');
99 is_deeply($app->libs, [], '... libs is [] as expected');
100 is_deeply($app->details, {}, '... details is {} as expected');
104 local @ARGV = ('--verbose', '--length', 50);
106 my $app = App->new_with_options;
109 ok($app->verbose, '... verbosity is turned on as expected');
110 is($app->length, 50, '... length is 50 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', '-f', 'foo.txt');
119 my $app = App->new_with_options;
122 ok($app->verbose, '... verbosity is turned on as expected');
123 is($app->length, 24, '... length is 24 as expected');
124 is($app->data, 'foo.txt', '... data is foo.txt as expected');
125 is_deeply($app->libs, [], '... libs is [] as expected');
126 is_deeply($app->details, {}, '... details is {} as expected');
130 local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib');
132 my $app = App->new_with_options;
135 ok($app->verbose, '... verbosity is turned on as expected');
136 is($app->length, 24, '... length is 24 as expected');
137 is($app->data, 'file.dat', '... data is foo.txt as expected');
138 is_deeply($app->libs,
139 ['libs/', 'includes/lib'],
140 '... libs is [libs/, includes/lib] as expected');
141 is_deeply($app->details, {}, '... details is {} as expected');
145 local @ARGV = ('--details', 'os=mac', '--details', 'name=foo');
147 my $app = App->new_with_options;
150 ok(!$app->verbose, '... verbosity is turned on as expected');
151 is($app->length, 24, '... length is 24 as expected');
152 is($app->data, 'file.dat', '... data is foo.txt as expected');
153 is_deeply($app->libs, [], '... libs is [] as expected');
154 is_deeply($app->details,
155 { os => 'mac', name => 'foo' },
156 '... details is { os => mac, name => foo } as expected');
160 # Test negation on booleans too ...
161 local @ARGV = ('--noverbose');
163 my $app = App->new_with_options;
166 ok(!$app->verbose, '... verbosity is turned off as expected');
167 is($app->length, 24, '... length is 24 as expected');
168 is($app->data, 'file.dat', '... file is file.dat as expected');
169 is_deeply($app->libs, [], '... libs is [] as expected');
170 is_deeply($app->details, {}, '... details is {} as expected');
173 # Test cmd_alias without cmd_flag
175 local @ARGV = ('--cow', '42');
176 my $app = App->new_with_options;
178 is($app->cow, 42, 'cmd_alias, but not using it');
181 local @ARGV = ('--moocow', '88');
182 my $app = App->new_with_options;
184 is($app->cow, 88, 'cmd_alias, using long one');
187 local @ARGV = ('-c', '99');
188 my $app = App->new_with_options;
190 is($app->cow, 99, 'cmd_alias, using short one');
193 # Test cmd_alias + cmd_flag
195 local @ARGV = ('--horsey', '123');
196 my $app = App->new_with_options;
198 is($app->horse, 123, 'cmd_alias+cmd_flag, using flag');
201 local @ARGV = ('-x', '321');
202 my $app = App->new_with_options;
204 is($app->horse, 321, 'cmd_alias+cmd_flag, using alias');
207 # Test _foo + cmd_flag
209 local @ARGV = ('-p', '666');
210 my $app = App->new_with_options;
212 is($app->_private_stuff_cmdline, 666, '_foo + cmd_flag');
217 my @args = ('-p', 12345, '-c', 99, '-');
219 my $app = App->new_with_options;
221 is_deeply($app->ARGV, \@args, 'ARGV accessor');
222 is_deeply(\@ARGV, \@args, '@ARGV unmangled');
223 is_deeply($app->extra_argv, ['-'], 'extra_argv accessor');