Commit | Line | Data |
5dac17c3 |
1 | use strict; |
2 | use warnings; |
3 | |
adbe3e57 |
4 | use Test::More tests => 69; |
5dac17c3 |
5 | |
6 | BEGIN { |
7 | use_ok('MooseX::Getopt'); |
8 | } |
9 | |
10 | { |
11 | package App; |
12 | use Moose; |
2557b526 |
13 | |
5dac17c3 |
14 | with 'MooseX::Getopt'; |
15 | |
16 | has 'data' => ( |
2557b526 |
17 | metaclass => 'MooseX::Getopt::Meta::Attribute', |
5dac17c3 |
18 | is => 'ro', |
19 | isa => 'Str', |
20 | default => 'file.dat', |
21 | cmd_flag => 'f', |
22 | ); |
23 | |
de75868f |
24 | has 'cow' => ( |
2557b526 |
25 | metaclass => 'Getopt', |
de75868f |
26 | is => 'ro', |
27 | isa => 'Str', |
28 | default => 'moo', |
29 | cmd_aliases => [qw/ moocow m c /], |
30 | ); |
31 | |
32 | has 'horse' => ( |
2557b526 |
33 | metaclass => 'MooseX::Getopt::Meta::Attribute', |
de75868f |
34 | is => 'ro', |
35 | isa => 'Str', |
36 | default => 'bray', |
37 | cmd_flag => 'horsey', |
61c9baa9 |
38 | cmd_aliases => 'x', |
de75868f |
39 | ); |
40 | |
5dac17c3 |
41 | has 'length' => ( |
42 | is => 'ro', |
43 | isa => 'Int', |
44 | default => 24 |
45 | ); |
46 | |
47 | has 'verbose' => ( |
48 | is => 'ro', |
2557b526 |
49 | isa => 'Bool', |
8034a232 |
50 | ); |
2557b526 |
51 | |
8034a232 |
52 | has 'libs' => ( |
53 | is => 'ro', |
54 | isa => 'ArrayRef', |
55 | default => sub { [] }, |
2557b526 |
56 | ); |
57 | |
8034a232 |
58 | has 'details' => ( |
59 | is => 'ro', |
60 | isa => 'HashRef', |
61 | default => sub { {} }, |
b1b9da6a |
62 | ); |
63 | |
64 | has '_private_stuff' => ( |
65 | is => 'ro', |
66 | isa => 'Int', |
67 | default => 713 |
68 | ); |
69 | |
70 | has '_private_stuff_cmdline' => ( |
2557b526 |
71 | metaclass => 'MooseX::Getopt::Meta::Attribute', |
b1b9da6a |
72 | is => 'ro', |
73 | isa => 'Int', |
74 | default => 832, |
75 | cmd_flag => 'p', |
76 | ); |
2557b526 |
77 | |
5dac17c3 |
78 | } |
79 | |
adbe3e57 |
80 | foreach my $attr_name (qw(data cow horse _private_stuff_cmdline)) { |
81 | my $attr = App->meta->get_attribute($attr_name); |
82 | isa_ok($attr, 'Moose::Meta::Attribute'); |
83 | isa_ok($attr, 'MooseX::Getopt::Meta::Attribute'); |
84 | can_ok($attr, 'cmd_flag'); |
2557b526 |
85 | can_ok($attr, 'cmd_aliases'); |
adbe3e57 |
86 | } |
87 | |
5dac17c3 |
88 | { |
89 | local @ARGV = (); |
90 | |
91 | my $app = App->new_with_options; |
92 | isa_ok($app, 'App'); |
93 | |
94 | ok(!$app->verbose, '... verbosity is off as expected'); |
2557b526 |
95 | is($app->length, 24, '... length is 24 as expected'); |
96 | is($app->data, 'file.dat', '... data is file.dat as expected'); |
97 | is_deeply($app->libs, [], '... libs is [] as expected'); |
98 | is_deeply($app->details, {}, '... details is {} as expected'); |
5dac17c3 |
99 | } |
100 | |
101 | { |
8034a232 |
102 | local @ARGV = ('--verbose', '--length', 50); |
5dac17c3 |
103 | |
104 | my $app = App->new_with_options; |
105 | isa_ok($app, 'App'); |
106 | |
107 | ok($app->verbose, '... verbosity is turned on as expected'); |
2557b526 |
108 | is($app->length, 50, '... length is 50 as expected'); |
109 | is($app->data, 'file.dat', '... data is file.dat as expected'); |
110 | is_deeply($app->libs, [], '... libs is [] as expected'); |
111 | is_deeply($app->details, {}, '... details is {} as expected'); |
8034a232 |
112 | } |
113 | |
114 | { |
115 | local @ARGV = ('--verbose', '-f', 'foo.txt'); |
116 | |
117 | my $app = App->new_with_options; |
118 | isa_ok($app, 'App'); |
119 | |
120 | ok($app->verbose, '... verbosity is turned on as expected'); |
2557b526 |
121 | is($app->length, 24, '... length is 24 as expected'); |
122 | is($app->data, 'foo.txt', '... data is foo.txt as expected'); |
123 | is_deeply($app->libs, [], '... libs is [] as expected'); |
124 | is_deeply($app->details, {}, '... details is {} as expected'); |
5dac17c3 |
125 | } |
126 | |
127 | { |
8034a232 |
128 | local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib'); |
5dac17c3 |
129 | |
130 | my $app = App->new_with_options; |
131 | isa_ok($app, 'App'); |
132 | |
133 | ok($app->verbose, '... verbosity is turned on as expected'); |
2557b526 |
134 | is($app->length, 24, '... length is 24 as expected'); |
135 | is($app->data, 'file.dat', '... data is foo.txt as expected'); |
136 | is_deeply($app->libs, |
137 | ['libs/', 'includes/lib'], |
138 | '... libs is [libs/, includes/lib] as expected'); |
139 | is_deeply($app->details, {}, '... details is {} as expected'); |
8034a232 |
140 | } |
141 | |
142 | { |
143 | local @ARGV = ('--details', 'os=mac', '--details', 'name=foo'); |
144 | |
145 | my $app = App->new_with_options; |
146 | isa_ok($app, 'App'); |
147 | |
148 | ok(!$app->verbose, '... verbosity is turned on as expected'); |
2557b526 |
149 | is($app->length, 24, '... length is 24 as expected'); |
150 | is($app->data, 'file.dat', '... data is foo.txt as expected'); |
151 | is_deeply($app->libs, [], '... libs is [] as expected'); |
152 | is_deeply($app->details, |
153 | { os => 'mac', name => 'foo' }, |
154 | '... details is { os => mac, name => foo } as expected'); |
5dac17c3 |
155 | } |
156 | |
157 | { |
8034a232 |
158 | # Test negation on booleans too ... |
159 | local @ARGV = ('--noverbose'); |
5dac17c3 |
160 | |
161 | my $app = App->new_with_options; |
162 | isa_ok($app, 'App'); |
163 | |
164 | ok(!$app->verbose, '... verbosity is turned off as expected'); |
2557b526 |
165 | is($app->length, 24, '... length is 24 as expected'); |
166 | is($app->data, 'file.dat', '... file is file.dat as expected'); |
167 | is_deeply($app->libs, [], '... libs is [] as expected'); |
168 | is_deeply($app->details, {}, '... details is {} as expected'); |
5dac17c3 |
169 | } |
170 | |
de75868f |
171 | # Test cmd_alias without cmd_flag |
172 | { |
173 | local @ARGV = ('--cow', '42'); |
174 | my $app = App->new_with_options; |
175 | isa_ok($app, 'App'); |
176 | is($app->cow, 42, 'cmd_alias, but not using it'); |
177 | } |
178 | { |
179 | local @ARGV = ('--moocow', '88'); |
180 | my $app = App->new_with_options; |
181 | isa_ok($app, 'App'); |
182 | is($app->cow, 88, 'cmd_alias, using long one'); |
183 | } |
184 | { |
185 | local @ARGV = ('-c', '99'); |
186 | my $app = App->new_with_options; |
187 | isa_ok($app, 'App'); |
188 | is($app->cow, 99, 'cmd_alias, using short one'); |
189 | } |
5dac17c3 |
190 | |
de75868f |
191 | # Test cmd_alias + cmd_flag |
192 | { |
193 | local @ARGV = ('--horsey', '123'); |
194 | my $app = App->new_with_options; |
195 | isa_ok($app, 'App'); |
196 | is($app->horse, 123, 'cmd_alias+cmd_flag, using flag'); |
197 | } |
198 | { |
199 | local @ARGV = ('-x', '321'); |
200 | my $app = App->new_with_options; |
201 | isa_ok($app, 'App'); |
202 | is($app->horse, 321, 'cmd_alias+cmd_flag, using alias'); |
203 | } |
b1b9da6a |
204 | |
205 | # Test _foo + cmd_flag |
206 | { |
207 | local @ARGV = ('-p', '666'); |
208 | my $app = App->new_with_options; |
209 | isa_ok($app, 'App'); |
210 | is($app->_private_stuff_cmdline, 666, '_foo + cmd_flag'); |
211 | } |
a4fb037c |
212 | |
213 | # Test ARGV support |
214 | { |
f63e6310 |
215 | my @args = ('-p', 12345, '-c', 99, '-'); |
a4fb037c |
216 | local @ARGV = @args; |
217 | my $app = App->new_with_options; |
218 | isa_ok($app, 'App'); |
f63e6310 |
219 | is_deeply($app->ARGV, \@args, 'ARGV accessor'); |
220 | is_deeply(\@ARGV, \@args, '@ARGV unmangled'); |
221 | is_deeply($app->extra_argv, ['-'], 'extra_argv accessor'); |
a4fb037c |
222 | } |