4 use Test::More tests => 70;
5 use Test::NoWarnings 1.04 ':early';
8 use_ok('MooseX::Getopt::Basic');
15 with 'MooseX::Getopt::Basic';
18 metaclass => 'MooseX::Getopt::Meta::Attribute',
21 default => 'file.dat',
26 metaclass => 'Getopt',
30 cmd_aliases => [qw/ moocow m c /],
34 metaclass => 'MooseX::Getopt::Meta::Attribute',
56 default => sub { [] },
62 default => sub { {} },
65 has '_private_stuff' => (
71 has '_private_stuff_cmdline' => (
72 metaclass => 'MooseX::Getopt::Meta::Attribute',
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 isa_ok($attr, 'MooseX::Getopt::Meta::Attribute');
85 can_ok($attr, 'cmd_flag');
86 can_ok($attr, 'cmd_aliases');
92 my $app = App->new_with_options;
95 ok(!$app->verbose, '... verbosity is off as expected');
96 is($app->length, 24, '... length is 24 as expected');
97 is($app->data, 'file.dat', '... data is file.dat as expected');
98 is_deeply($app->libs, [], '... libs is [] as expected');
99 is_deeply($app->details, {}, '... details is {} as expected');
103 local @ARGV = ('--verbose', '--length', 50);
105 my $app = App->new_with_options;
108 ok($app->verbose, '... verbosity is turned on as expected');
109 is($app->length, 50, '... length is 50 as expected');
110 is($app->data, 'file.dat', '... data is file.dat as expected');
111 is_deeply($app->libs, [], '... libs is [] as expected');
112 is_deeply($app->details, {}, '... details is {} as expected');
116 local @ARGV = ('--verbose', '-f', 'foo.txt');
118 my $app = App->new_with_options;
121 ok($app->verbose, '... verbosity is turned on as expected');
122 is($app->length, 24, '... length is 24 as expected');
123 is($app->data, 'foo.txt', '... data is foo.txt as expected');
124 is_deeply($app->libs, [], '... libs is [] as expected');
125 is_deeply($app->details, {}, '... details is {} as expected');
129 local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib');
131 my $app = App->new_with_options;
134 ok($app->verbose, '... verbosity is turned on as expected');
135 is($app->length, 24, '... length is 24 as expected');
136 is($app->data, 'file.dat', '... data is foo.txt as expected');
137 is_deeply($app->libs,
138 ['libs/', 'includes/lib'],
139 '... libs is [libs/, includes/lib] as expected');
140 is_deeply($app->details, {}, '... details is {} as expected');
144 local @ARGV = ('--details', 'os=mac', '--details', 'name=foo');
146 my $app = App->new_with_options;
149 ok(!$app->verbose, '... verbosity is turned on as expected');
150 is($app->length, 24, '... length is 24 as expected');
151 is($app->data, 'file.dat', '... data is foo.txt as expected');
152 is_deeply($app->libs, [], '... libs is [] as expected');
153 is_deeply($app->details,
154 { os => 'mac', name => 'foo' },
155 '... details is { os => mac, name => foo } as expected');
159 # Test negation on booleans too ...
160 local @ARGV = ('--noverbose');
162 my $app = App->new_with_options;
165 ok(!$app->verbose, '... verbosity is turned off as expected');
166 is($app->length, 24, '... length is 24 as expected');
167 is($app->data, 'file.dat', '... file is file.dat as expected');
168 is_deeply($app->libs, [], '... libs is [] as expected');
169 is_deeply($app->details, {}, '... details is {} as expected');
172 # Test cmd_alias without cmd_flag
174 local @ARGV = ('--cow', '42');
175 my $app = App->new_with_options;
177 is($app->cow, 42, 'cmd_alias, but not using it');
180 local @ARGV = ('--moocow', '88');
181 my $app = App->new_with_options;
183 is($app->cow, 88, 'cmd_alias, using long one');
186 local @ARGV = ('-c', '99');
187 my $app = App->new_with_options;
189 is($app->cow, 99, 'cmd_alias, using short one');
192 # Test cmd_alias + cmd_flag
194 local @ARGV = ('--horsey', '123');
195 my $app = App->new_with_options;
197 is($app->horse, 123, 'cmd_alias+cmd_flag, using flag');
200 local @ARGV = ('-x', '321');
201 my $app = App->new_with_options;
203 is($app->horse, 321, 'cmd_alias+cmd_flag, using alias');
206 # Test _foo + cmd_flag
208 local @ARGV = ('-p', '666');
209 my $app = App->new_with_options;
211 is($app->_private_stuff_cmdline, 666, '_foo + cmd_flag');
216 my @args = ('-p', 12345, '-c', 99, '-');
218 my $app = App->new_with_options;
220 is_deeply($app->ARGV, \@args, 'ARGV accessor');
221 is_deeply(\@ARGV, \@args, '@ARGV unmangled');
222 is_deeply($app->extra_argv, ['-'], 'extra_argv accessor');