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