Commit | Line | Data |
5dac17c3 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
19b87ede |
6 | use Test::More tests => 173; |
5dac17c3 |
7 | |
8 | BEGIN { |
9 | use_ok('MooseX::Getopt'); |
10 | } |
11 | |
12 | { |
13 | package App; |
14 | use Moose; |
19b87ede |
15 | |
5dac17c3 |
16 | with 'MooseX::Getopt'; |
17 | |
18 | has 'data' => ( |
19b87ede |
19 | metaclass => 'MooseX::Getopt::Meta::Attribute', |
5dac17c3 |
20 | is => 'ro', |
21 | isa => 'Str', |
22 | default => 'file.dat', |
23 | cmd_flag => 'f', |
24 | ); |
25 | |
de75868f |
26 | has 'cow' => ( |
19b87ede |
27 | metaclass => 'Getopt', |
de75868f |
28 | is => 'ro', |
29 | isa => 'Str', |
30 | default => 'moo', |
31 | cmd_aliases => [qw/ moocow m c /], |
32 | ); |
33 | |
34 | has 'horse' => ( |
19b87ede |
35 | metaclass => 'MooseX::Getopt::Meta::Attribute', |
de75868f |
36 | is => 'ro', |
37 | isa => 'Str', |
38 | default => 'bray', |
39 | cmd_flag => 'horsey', |
61c9baa9 |
40 | cmd_aliases => 'x', |
de75868f |
41 | ); |
42 | |
5dac17c3 |
43 | has 'length' => ( |
44 | is => 'ro', |
45 | isa => 'Int', |
46 | default => 24 |
47 | ); |
48 | |
49 | has 'verbose' => ( |
50 | is => 'ro', |
19b87ede |
51 | isa => 'Bool', |
8034a232 |
52 | ); |
19b87ede |
53 | |
8034a232 |
54 | has 'libs' => ( |
55 | is => 'ro', |
56 | isa => 'ArrayRef', |
57 | default => sub { [] }, |
19b87ede |
58 | ); |
59 | |
8034a232 |
60 | has 'details' => ( |
61 | is => 'ro', |
62 | isa => 'HashRef', |
63 | default => sub { {} }, |
b1b9da6a |
64 | ); |
65 | |
66 | has '_private_stuff' => ( |
67 | is => 'ro', |
68 | isa => 'Int', |
69 | default => 713 |
70 | ); |
71 | |
72 | has '_private_stuff_cmdline' => ( |
19b87ede |
73 | metaclass => 'MooseX::Getopt::Meta::Attribute', |
b1b9da6a |
74 | is => 'ro', |
75 | isa => 'Int', |
76 | default => 832, |
77 | cmd_flag => 'p', |
78 | ); |
19b87ede |
79 | |
5dac17c3 |
80 | } |
81 | |
adbe3e57 |
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'); |
19b87ede |
87 | can_ok($attr, 'cmd_aliases'); |
adbe3e57 |
88 | } |
89 | |
19b87ede |
90 | foreach my $parser_name (qw(MooseX::Getopt::Parser::Long MooseX::Getopt::Parser::Descriptive)) { |
91 | SKIP: { |
92 | if ($parser_name eq 'MooseX::Getopt::Parser::Descriptive') { |
93 | eval { require Getopt::Long::Descriptive }; |
94 | skip "Getopt::Long::Descriptive not installed", 78 if $@; |
95 | } |
5dac17c3 |
96 | |
19b87ede |
97 | { |
98 | local @ARGV = (); |
5dac17c3 |
99 | |
19b87ede |
100 | my $parser = $parser_name->new; |
101 | isa_ok($parser, $parser_name); |
5dac17c3 |
102 | |
19b87ede |
103 | my $getopt = MooseX::Getopt::Session->new( parser => $parser ); |
104 | isa_ok($getopt, 'MooseX::Getopt::Session'); |
5dac17c3 |
105 | |
19b87ede |
106 | my $app = App->new_with_options( getopt => $getopt ); |
107 | isa_ok($app, 'App'); |
5dac17c3 |
108 | |
19b87ede |
109 | ok(!$app->verbose, '... verbosity is off as expected'); |
110 | is($app->length, 24, '... length is 24 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'); |
114 | } |
8034a232 |
115 | |
19b87ede |
116 | { |
117 | local @ARGV = ('--verbose', '--length', 50); |
8034a232 |
118 | |
19b87ede |
119 | my $parser = $parser_name->new; |
120 | isa_ok($parser, $parser_name); |
8034a232 |
121 | |
19b87ede |
122 | my $getopt = MooseX::Getopt::Session->new( parser => $parser ); |
123 | isa_ok($getopt, 'MooseX::Getopt::Session'); |
5dac17c3 |
124 | |
19b87ede |
125 | my $app = App->new_with_options( getopt => $getopt ); |
126 | isa_ok($app, 'App'); |
8034a232 |
127 | |
19b87ede |
128 | ok($app->verbose, '... verbosity is turned on as expected'); |
129 | is($app->length, 50, '... length is 50 as expected'); |
130 | is($app->data, 'file.dat', '... data is file.dat as expected'); |
131 | is_deeply($app->libs, [], '... libs is [] as expected'); |
132 | is_deeply($app->details, {}, '... details is {} as expected'); |
133 | } |
5dac17c3 |
134 | |
19b87ede |
135 | { |
136 | local @ARGV = ('--verbose', '-f', 'foo.txt'); |
5dac17c3 |
137 | |
19b87ede |
138 | my $parser = $parser_name->new; |
139 | isa_ok($parser, $parser_name); |
5dac17c3 |
140 | |
19b87ede |
141 | my $getopt = MooseX::Getopt::Session->new( parser => $parser ); |
142 | isa_ok($getopt, 'MooseX::Getopt::Session'); |
5dac17c3 |
143 | |
19b87ede |
144 | my $app = App->new_with_options( getopt => $getopt ); |
145 | isa_ok($app, 'App'); |
5dac17c3 |
146 | |
19b87ede |
147 | ok($app->verbose, '... verbosity is turned on as expected'); |
148 | is($app->length, 24, '... length is 24 as expected'); |
149 | is($app->data, 'foo.txt', '... data is foo.txt as expected'); |
b1b9da6a |
150 | |
19b87ede |
151 | is_deeply($app->libs, [], '... libs is [] as expected'); |
152 | is_deeply($app->details, {}, '... details is {} as expected'); |
153 | } |
a4fb037c |
154 | |
19b87ede |
155 | { |
156 | local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib'); |
157 | |
158 | my $parser = $parser_name->new; |
159 | isa_ok($parser, $parser_name); |
160 | |
161 | my $getopt = MooseX::Getopt::Session->new( parser => $parser ); |
162 | isa_ok($getopt, 'MooseX::Getopt::Session'); |
163 | |
164 | my $app = App->new_with_options( getopt => $getopt ); |
165 | isa_ok($app, 'App'); |
166 | |
167 | ok($app->verbose, '... verbosity is turned on as expected'); |
168 | is($app->length, 24, '... length is 24 as expected'); |
169 | is($app->data, 'file.dat', '... data is foo.txt as expected'); |
170 | is_deeply($app->libs, |
171 | ['libs/', 'includes/lib'], |
172 | '... libs is [libs/, includes/lib] as expected'); |
173 | is_deeply($app->details, {}, '... details is {} as expected'); |
174 | } |
175 | |
176 | { |
177 | local @ARGV = ('--details', 'os=mac', '--details', 'name=foo'); |
178 | |
179 | my $parser = $parser_name->new; |
180 | isa_ok($parser, $parser_name); |
181 | |
182 | my $getopt = MooseX::Getopt::Session->new( parser => $parser ); |
183 | isa_ok($getopt, 'MooseX::Getopt::Session'); |
184 | |
185 | my $app = App->new_with_options( getopt => $getopt ); |
186 | isa_ok($app, 'App'); |
187 | |
188 | ok(!$app->verbose, '... verbosity is turned on as expected'); |
189 | is($app->length, 24, '... length is 24 as expected'); |
190 | is($app->data, 'file.dat', '... data is foo.txt as expected'); |
191 | is_deeply($app->libs, [], '... libs is [] as expected'); |
192 | is_deeply($app->details, |
193 | { os => 'mac', name => 'foo' }, |
194 | '... details is { os => mac, name => foo } as expected'); |
195 | } |
196 | |
197 | { |
198 | # Test negation on booleans too ... |
199 | local @ARGV = ('--noverbose'); |
200 | |
201 | my $parser = $parser_name->new; |
202 | isa_ok($parser, $parser_name); |
203 | |
204 | my $getopt = MooseX::Getopt::Session->new( parser => $parser ); |
205 | isa_ok($getopt, 'MooseX::Getopt::Session'); |
206 | |
207 | my $app = App->new_with_options( getopt => $getopt ); |
208 | isa_ok($app, 'App'); |
209 | |
210 | ok(!$app->verbose, '... verbosity is turned off as expected'); |
211 | is($app->length, 24, '... length is 24 as expected'); |
212 | is($app->data, 'file.dat', '... file is file.dat as expected'); |
213 | is_deeply($app->libs, [], '... libs is [] as expected'); |
214 | is_deeply($app->details, {}, '... details is {} as expected'); |
215 | } |
216 | |
217 | # Test cmd_alias without cmd_flag |
218 | { |
219 | local @ARGV = ('--cow', '42'); |
220 | |
221 | my $parser = $parser_name->new; |
222 | isa_ok($parser, $parser_name); |
223 | |
224 | my $getopt = MooseX::Getopt::Session->new( parser => $parser ); |
225 | isa_ok($getopt, 'MooseX::Getopt::Session'); |
226 | |
227 | my $app = App->new_with_options( getopt => $getopt ); |
228 | isa_ok($app, 'App'); |
229 | is($app->cow, 42, 'cmd_alias, but not using it'); |
230 | } |
231 | { |
232 | local @ARGV = ('--moocow', '88'); |
233 | |
234 | my $parser = $parser_name->new; |
235 | isa_ok($parser, $parser_name); |
236 | |
237 | my $getopt = MooseX::Getopt::Session->new( parser => $parser ); |
238 | isa_ok($getopt, 'MooseX::Getopt::Session'); |
239 | |
240 | my $app = App->new_with_options( getopt => $getopt ); |
241 | isa_ok($app, 'App'); |
242 | is($app->cow, 88, 'cmd_alias, using long one'); |
243 | } |
244 | { |
245 | local @ARGV = ('-c', '99'); |
246 | |
247 | my $parser = $parser_name->new; |
248 | isa_ok($parser, $parser_name); |
249 | |
250 | my $getopt = MooseX::Getopt::Session->new( parser => $parser ); |
251 | isa_ok($getopt, 'MooseX::Getopt::Session'); |
252 | |
253 | my $app = App->new_with_options( getopt => $getopt ); |
254 | isa_ok($app, 'App'); |
255 | is($app->cow, 99, 'cmd_alias, using short one'); |
256 | } |
257 | |
258 | # Test cmd_alias + cmd_flag |
259 | { |
260 | local @ARGV = ('--horsey', '123'); |
261 | |
262 | my $parser = $parser_name->new; |
263 | isa_ok($parser, $parser_name); |
264 | |
265 | my $getopt = MooseX::Getopt::Session->new( parser => $parser ); |
266 | isa_ok($getopt, 'MooseX::Getopt::Session'); |
267 | |
268 | my $app = App->new_with_options( getopt => $getopt ); |
269 | isa_ok($app, 'App'); |
270 | is($app->horse, 123, 'cmd_alias+cmd_flag, using flag'); |
271 | } |
272 | { |
273 | local @ARGV = ('-x', '321'); |
274 | |
275 | my $parser = $parser_name->new; |
276 | isa_ok($parser, $parser_name); |
277 | |
278 | my $getopt = MooseX::Getopt::Session->new( parser => $parser ); |
279 | isa_ok($getopt, 'MooseX::Getopt::Session'); |
280 | |
281 | my $app = App->new_with_options( getopt => $getopt ); |
282 | isa_ok($app, 'App'); |
283 | is($app->horse, 321, 'cmd_alias+cmd_flag, using alias'); |
284 | } |
285 | |
286 | # Test _foo + cmd_flag |
287 | { |
288 | local @ARGV = ('-p', '666'); |
289 | |
290 | my $parser = $parser_name->new; |
291 | isa_ok($parser, $parser_name); |
292 | |
293 | my $getopt = MooseX::Getopt::Session->new( parser => $parser ); |
294 | isa_ok($getopt, 'MooseX::Getopt::Session'); |
295 | |
296 | my $app = App->new_with_options( getopt => $getopt ); |
297 | isa_ok($app, 'App'); |
298 | is($app->_private_stuff_cmdline, 666, '_foo + cmd_flag'); |
299 | } |
300 | |
301 | # Test ARGV support |
302 | { |
303 | my @args = ('-p', 12345, '-c', 99, '-'); |
304 | |
305 | local @ARGV = @args; |
306 | |
307 | my $parser = $parser_name->new; |
308 | isa_ok($parser, $parser_name); |
309 | |
310 | my $getopt = MooseX::Getopt::Session->new( parser => $parser ); |
311 | isa_ok($getopt, 'MooseX::Getopt::Session'); |
312 | |
313 | my $app = App->new_with_options( getopt => $getopt ); |
314 | isa_ok($app, 'App'); |
315 | is_deeply($app->ARGV, \@args, 'ARGV accessor'); |
316 | is_deeply(\@ARGV, \@args, '@ARGV unmangled'); |
317 | is_deeply($app->extra_argv, ['-'], 'extra_argv accessor'); |
318 | } |
319 | |
320 | } |
a4fb037c |
321 | } |