Commit | Line | Data |
5dac17c3 |
1 | use strict; |
2 | use warnings; |
3 | |
9fbb5be9 |
4 | use Test::More tests => 70; |
195fb408 |
5 | use Test::Moose; |
9fbb5be9 |
6 | use Test::NoWarnings 1.04 ':early'; |
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' => ( |
195fb408 |
19 | metaclass => 'Getopt', |
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' => ( |
195fb408 |
35 | traits => ['Getopt'], |
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' => ( |
195fb408 |
73 | traits => ['Getopt'], |
b1b9da6a |
74 | is => 'ro', |
75 | isa => 'Int', |
76 | default => 832, |
77 | cmd_flag => 'p', |
78 | ); |
5dac17c3 |
79 | } |
80 | |
adbe3e57 |
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'); |
195fb408 |
84 | if ($attr_name eq 'data' or $attr_name eq 'cow') |
85 | { |
86 | isa_ok($attr, 'MooseX::Getopt::Meta::Attribute'); |
87 | } |
88 | else |
89 | { |
90 | does_ok($attr, 'MooseX::Getopt::Meta::Attribute::Trait'); |
91 | } |
adbe3e57 |
92 | can_ok($attr, 'cmd_flag'); |
2557b526 |
93 | can_ok($attr, 'cmd_aliases'); |
adbe3e57 |
94 | } |
95 | |
5dac17c3 |
96 | { |
97 | local @ARGV = (); |
98 | |
99 | my $app = App->new_with_options; |
100 | isa_ok($app, 'App'); |
101 | |
102 | ok(!$app->verbose, '... verbosity is off as expected'); |
2557b526 |
103 | is($app->length, 24, '... length is 24 as expected'); |
104 | is($app->data, 'file.dat', '... data is file.dat as expected'); |
105 | is_deeply($app->libs, [], '... libs is [] as expected'); |
106 | is_deeply($app->details, {}, '... details is {} as expected'); |
5dac17c3 |
107 | } |
108 | |
109 | { |
8034a232 |
110 | local @ARGV = ('--verbose', '--length', 50); |
5dac17c3 |
111 | |
112 | my $app = App->new_with_options; |
113 | isa_ok($app, 'App'); |
114 | |
115 | ok($app->verbose, '... verbosity is turned on as expected'); |
2557b526 |
116 | is($app->length, 50, '... length is 50 as expected'); |
117 | is($app->data, 'file.dat', '... data is file.dat as expected'); |
118 | is_deeply($app->libs, [], '... libs is [] as expected'); |
119 | is_deeply($app->details, {}, '... details is {} as expected'); |
8034a232 |
120 | } |
121 | |
122 | { |
123 | local @ARGV = ('--verbose', '-f', 'foo.txt'); |
124 | |
125 | my $app = App->new_with_options; |
126 | isa_ok($app, 'App'); |
127 | |
128 | ok($app->verbose, '... verbosity is turned on as expected'); |
2557b526 |
129 | is($app->length, 24, '... length is 24 as expected'); |
130 | is($app->data, 'foo.txt', '... data is foo.txt as expected'); |
131 | is_deeply($app->libs, [], '... libs is [] as expected'); |
132 | is_deeply($app->details, {}, '... details is {} as expected'); |
5dac17c3 |
133 | } |
134 | |
135 | { |
8034a232 |
136 | local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib'); |
5dac17c3 |
137 | |
138 | my $app = App->new_with_options; |
139 | isa_ok($app, 'App'); |
140 | |
141 | ok($app->verbose, '... verbosity is turned on as expected'); |
2557b526 |
142 | is($app->length, 24, '... length is 24 as expected'); |
143 | is($app->data, 'file.dat', '... data is foo.txt as expected'); |
144 | is_deeply($app->libs, |
145 | ['libs/', 'includes/lib'], |
146 | '... libs is [libs/, includes/lib] as expected'); |
147 | is_deeply($app->details, {}, '... details is {} as expected'); |
8034a232 |
148 | } |
149 | |
150 | { |
151 | local @ARGV = ('--details', 'os=mac', '--details', 'name=foo'); |
152 | |
153 | my $app = App->new_with_options; |
154 | isa_ok($app, 'App'); |
155 | |
156 | ok(!$app->verbose, '... verbosity is turned on as expected'); |
2557b526 |
157 | is($app->length, 24, '... length is 24 as expected'); |
158 | is($app->data, 'file.dat', '... data is foo.txt as expected'); |
159 | is_deeply($app->libs, [], '... libs is [] as expected'); |
160 | is_deeply($app->details, |
161 | { os => 'mac', name => 'foo' }, |
162 | '... details is { os => mac, name => foo } as expected'); |
5dac17c3 |
163 | } |
164 | |
165 | { |
8034a232 |
166 | # Test negation on booleans too ... |
167 | local @ARGV = ('--noverbose'); |
5dac17c3 |
168 | |
169 | my $app = App->new_with_options; |
170 | isa_ok($app, 'App'); |
171 | |
172 | ok(!$app->verbose, '... verbosity is turned off as expected'); |
2557b526 |
173 | is($app->length, 24, '... length is 24 as expected'); |
174 | is($app->data, 'file.dat', '... file is file.dat as expected'); |
175 | is_deeply($app->libs, [], '... libs is [] as expected'); |
176 | is_deeply($app->details, {}, '... details is {} as expected'); |
5dac17c3 |
177 | } |
178 | |
de75868f |
179 | # Test cmd_alias without cmd_flag |
180 | { |
181 | local @ARGV = ('--cow', '42'); |
182 | my $app = App->new_with_options; |
183 | isa_ok($app, 'App'); |
184 | is($app->cow, 42, 'cmd_alias, but not using it'); |
185 | } |
186 | { |
187 | local @ARGV = ('--moocow', '88'); |
188 | my $app = App->new_with_options; |
189 | isa_ok($app, 'App'); |
190 | is($app->cow, 88, 'cmd_alias, using long one'); |
191 | } |
192 | { |
193 | local @ARGV = ('-c', '99'); |
194 | my $app = App->new_with_options; |
195 | isa_ok($app, 'App'); |
196 | is($app->cow, 99, 'cmd_alias, using short one'); |
197 | } |
5dac17c3 |
198 | |
de75868f |
199 | # Test cmd_alias + cmd_flag |
200 | { |
201 | local @ARGV = ('--horsey', '123'); |
202 | my $app = App->new_with_options; |
203 | isa_ok($app, 'App'); |
204 | is($app->horse, 123, 'cmd_alias+cmd_flag, using flag'); |
205 | } |
206 | { |
207 | local @ARGV = ('-x', '321'); |
208 | my $app = App->new_with_options; |
209 | isa_ok($app, 'App'); |
210 | is($app->horse, 321, 'cmd_alias+cmd_flag, using alias'); |
211 | } |
b1b9da6a |
212 | |
213 | # Test _foo + cmd_flag |
214 | { |
215 | local @ARGV = ('-p', '666'); |
216 | my $app = App->new_with_options; |
217 | isa_ok($app, 'App'); |
218 | is($app->_private_stuff_cmdline, 666, '_foo + cmd_flag'); |
219 | } |
a4fb037c |
220 | |
221 | # Test ARGV support |
222 | { |
f63e6310 |
223 | my @args = ('-p', 12345, '-c', 99, '-'); |
a4fb037c |
224 | local @ARGV = @args; |
225 | my $app = App->new_with_options; |
226 | isa_ok($app, 'App'); |
f63e6310 |
227 | is_deeply($app->ARGV, \@args, 'ARGV accessor'); |
228 | is_deeply(\@ARGV, \@args, '@ARGV unmangled'); |
229 | is_deeply($app->extra_argv, ['-'], 'extra_argv accessor'); |
a4fb037c |
230 | } |