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