Commit | Line | Data |
5dac17c3 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
adbe3e57 |
6 | use Test::More tests => 69; |
5dac17c3 |
7 | |
8 | BEGIN { |
9 | use_ok('MooseX::Getopt'); |
10 | } |
11 | |
12 | { |
13 | package App; |
14 | use Moose; |
2557b526 |
15 | |
5dac17c3 |
16 | with 'MooseX::Getopt'; |
17 | |
18 | has 'data' => ( |
2557b526 |
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' => ( |
2557b526 |
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' => ( |
2557b526 |
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', |
2557b526 |
51 | isa => 'Bool', |
8034a232 |
52 | ); |
2557b526 |
53 | |
8034a232 |
54 | has 'libs' => ( |
55 | is => 'ro', |
56 | isa => 'ArrayRef', |
57 | default => sub { [] }, |
2557b526 |
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' => ( |
2557b526 |
73 | metaclass => 'MooseX::Getopt::Meta::Attribute', |
b1b9da6a |
74 | is => 'ro', |
75 | isa => 'Int', |
76 | default => 832, |
77 | cmd_flag => 'p', |
78 | ); |
2557b526 |
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'); |
2557b526 |
87 | can_ok($attr, 'cmd_aliases'); |
adbe3e57 |
88 | } |
89 | |
5dac17c3 |
90 | { |
91 | local @ARGV = (); |
92 | |
93 | my $app = App->new_with_options; |
94 | isa_ok($app, 'App'); |
95 | |
96 | ok(!$app->verbose, '... verbosity is off as expected'); |
2557b526 |
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'); |
5dac17c3 |
101 | } |
102 | |
103 | { |
8034a232 |
104 | local @ARGV = ('--verbose', '--length', 50); |
5dac17c3 |
105 | |
106 | my $app = App->new_with_options; |
107 | isa_ok($app, 'App'); |
108 | |
109 | ok($app->verbose, '... verbosity is turned on as expected'); |
2557b526 |
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'); |
8034a232 |
114 | } |
115 | |
116 | { |
117 | local @ARGV = ('--verbose', '-f', 'foo.txt'); |
118 | |
119 | my $app = App->new_with_options; |
120 | isa_ok($app, 'App'); |
121 | |
122 | ok($app->verbose, '... verbosity is turned on as expected'); |
2557b526 |
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'); |
5dac17c3 |
127 | } |
128 | |
129 | { |
8034a232 |
130 | local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib'); |
5dac17c3 |
131 | |
132 | my $app = App->new_with_options; |
133 | isa_ok($app, 'App'); |
134 | |
135 | ok($app->verbose, '... verbosity is turned on as expected'); |
2557b526 |
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'); |
8034a232 |
142 | } |
143 | |
144 | { |
145 | local @ARGV = ('--details', 'os=mac', '--details', 'name=foo'); |
146 | |
147 | my $app = App->new_with_options; |
148 | isa_ok($app, 'App'); |
149 | |
150 | ok(!$app->verbose, '... verbosity is turned on as expected'); |
2557b526 |
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'); |
5dac17c3 |
157 | } |
158 | |
159 | { |
8034a232 |
160 | # Test negation on booleans too ... |
161 | local @ARGV = ('--noverbose'); |
5dac17c3 |
162 | |
163 | my $app = App->new_with_options; |
164 | isa_ok($app, 'App'); |
165 | |
166 | ok(!$app->verbose, '... verbosity is turned off as expected'); |
2557b526 |
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'); |
5dac17c3 |
171 | } |
172 | |
de75868f |
173 | # Test cmd_alias without cmd_flag |
174 | { |
175 | local @ARGV = ('--cow', '42'); |
176 | my $app = App->new_with_options; |
177 | isa_ok($app, 'App'); |
178 | is($app->cow, 42, 'cmd_alias, but not using it'); |
179 | } |
180 | { |
181 | local @ARGV = ('--moocow', '88'); |
182 | my $app = App->new_with_options; |
183 | isa_ok($app, 'App'); |
184 | is($app->cow, 88, 'cmd_alias, using long one'); |
185 | } |
186 | { |
187 | local @ARGV = ('-c', '99'); |
188 | my $app = App->new_with_options; |
189 | isa_ok($app, 'App'); |
190 | is($app->cow, 99, 'cmd_alias, using short one'); |
191 | } |
5dac17c3 |
192 | |
de75868f |
193 | # Test cmd_alias + cmd_flag |
194 | { |
195 | local @ARGV = ('--horsey', '123'); |
196 | my $app = App->new_with_options; |
197 | isa_ok($app, 'App'); |
198 | is($app->horse, 123, 'cmd_alias+cmd_flag, using flag'); |
199 | } |
200 | { |
201 | local @ARGV = ('-x', '321'); |
202 | my $app = App->new_with_options; |
203 | isa_ok($app, 'App'); |
204 | is($app->horse, 321, 'cmd_alias+cmd_flag, using alias'); |
205 | } |
b1b9da6a |
206 | |
207 | # Test _foo + cmd_flag |
208 | { |
209 | local @ARGV = ('-p', '666'); |
210 | my $app = App->new_with_options; |
211 | isa_ok($app, 'App'); |
212 | is($app->_private_stuff_cmdline, 666, '_foo + cmd_flag'); |
213 | } |
a4fb037c |
214 | |
215 | # Test ARGV support |
216 | { |
f63e6310 |
217 | my @args = ('-p', 12345, '-c', 99, '-'); |
a4fb037c |
218 | local @ARGV = @args; |
219 | my $app = App->new_with_options; |
220 | isa_ok($app, 'App'); |
f63e6310 |
221 | is_deeply($app->ARGV, \@args, 'ARGV accessor'); |
222 | is_deeply(\@ARGV, \@args, '@ARGV unmangled'); |
223 | is_deeply($app->extra_argv, ['-'], 'extra_argv accessor'); |
a4fb037c |
224 | } |