0.12
[gitmo/MooseX-Getopt.git] / t / 006_metaclass_traits.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 69;
7 use Test::Moose;
8
9 BEGIN {
10     use_ok('MooseX::Getopt');
11 }
12
13 {
14     package App;
15     use Moose;
16     
17     with 'MooseX::Getopt';
18
19     has 'data' => (
20         traits    => [ 'MooseX::Getopt::Meta::Attribute::Trait' ],        
21         is        => 'ro',
22         isa       => 'Str',
23         default   => 'file.dat',
24         cmd_flag  => 'f',
25     );
26
27     has 'cow' => (
28         traits      => [ 'Getopt' ],        
29         is          => 'ro',
30         isa         => 'Str',
31         default     => 'moo',
32         cmd_aliases => [qw/ moocow m c /],
33     );
34
35     has 'horse' => (
36         traits      => [ 'Getopt' ], 
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',
52         isa    => 'Bool',       
53     );
54     
55     has 'libs' => (
56         is      => 'ro',
57         isa     => 'ArrayRef',
58         default => sub { [] },
59     ); 
60     
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' => (
74         traits    => [ 'Getopt' ],        
75         is        => 'ro',
76         isa       => 'Int',
77         default   => 832,
78         cmd_flag  => 'p',
79     );
80   
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');
87     
88     can_ok($attr, 'cmd_flag');
89     can_ok($attr, 'cmd_aliases');    
90 }
91
92 {
93     local @ARGV = ();
94
95     my $app = App->new_with_options;
96     isa_ok($app, 'App');
97
98     ok(!$app->verbose, '... verbosity is off as expected');
99     is($app->length, 24, '... length is 24 as expected');    
100     is($app->data, 'file.dat', '... data is file.dat as expected');        
101     is_deeply($app->libs, [], '... libs is [] as expected'); 
102     is_deeply($app->details, {}, '... details is {} as expected');           
103 }
104
105 {
106     local @ARGV = ('--verbose', '--length', 50);
107
108     my $app = App->new_with_options;
109     isa_ok($app, 'App');
110
111     ok($app->verbose, '... verbosity is turned on as expected');
112     is($app->length, 50, '... length is 50 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 }
117
118 {
119     local @ARGV = ('--verbose', '-f', 'foo.txt');
120
121     my $app = App->new_with_options;
122     isa_ok($app, 'App');
123
124     ok($app->verbose, '... verbosity is turned on as expected');
125     is($app->length, 24, '... length is 24 as expected');    
126     is($app->data, 'foo.txt', '... data is foo.txt as expected'); 
127     is_deeply($app->libs, [], '... libs is [] as expected');    
128     is_deeply($app->details, {}, '... details is {} as expected');                             
129 }
130
131 {
132     local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib');
133
134     my $app = App->new_with_options;
135     isa_ok($app, 'App');
136
137     ok($app->verbose, '... verbosity is turned on as expected');
138     is($app->length, 24, '... length is 24 as expected');    
139     is($app->data, 'file.dat', '... data is foo.txt as expected'); 
140     is_deeply($app->libs, 
141     ['libs/', 'includes/lib'], 
142     '... libs is [libs/, includes/lib] as expected');   
143     is_deeply($app->details, {}, '... details is {} as expected');                              
144 }
145
146 {
147     local @ARGV = ('--details', 'os=mac', '--details', 'name=foo');
148
149     my $app = App->new_with_options;
150     isa_ok($app, 'App');
151
152     ok(!$app->verbose, '... verbosity is turned on as expected');
153     is($app->length, 24, '... length is 24 as expected');    
154     is($app->data, 'file.dat', '... data is foo.txt as expected'); 
155     is_deeply($app->libs, [], '... libs is [] as expected');    
156     is_deeply($app->details, 
157     { os => 'mac', name => 'foo' }, 
158     '... details is { os => mac, name => foo } as expected');                              
159 }
160
161 {
162     # Test negation on booleans too ...
163     local @ARGV = ('--noverbose');
164
165     my $app = App->new_with_options;
166     isa_ok($app, 'App');
167
168     ok(!$app->verbose, '... verbosity is turned off as expected');
169     is($app->length, 24, '... length is 24 as expected');    
170     is($app->data, 'file.dat', '... file is file.dat as expected');   
171     is_deeply($app->libs, [], '... libs is [] as expected');                
172     is_deeply($app->details, {}, '... details is {} as expected');               
173 }
174
175 # Test cmd_alias without cmd_flag
176 {
177     local @ARGV = ('--cow', '42');
178     my $app = App->new_with_options;
179     isa_ok($app, 'App');
180     is($app->cow, 42, 'cmd_alias, but not using it');
181 }
182 {
183     local @ARGV = ('--moocow', '88');
184     my $app = App->new_with_options;
185     isa_ok($app, 'App');
186     is($app->cow, 88, 'cmd_alias, using long one');
187 }
188 {
189     local @ARGV = ('-c', '99');
190     my $app = App->new_with_options;
191     isa_ok($app, 'App');
192     is($app->cow, 99, 'cmd_alias, using short one');
193 }
194
195 # Test cmd_alias + cmd_flag
196 {
197     local @ARGV = ('--horsey', '123');
198     my $app = App->new_with_options;
199     isa_ok($app, 'App');
200     is($app->horse, 123, 'cmd_alias+cmd_flag, using flag');
201 }
202 {
203     local @ARGV = ('-x', '321');
204     my $app = App->new_with_options;
205     isa_ok($app, 'App');
206     is($app->horse, 321, 'cmd_alias+cmd_flag, using alias');
207 }
208
209 # Test _foo + cmd_flag
210 {
211     local @ARGV = ('-p', '666');
212     my $app = App->new_with_options;
213     isa_ok($app, 'App');
214     is($app->_private_stuff_cmdline, 666, '_foo + cmd_flag');
215 }
216
217 # Test ARGV support
218 {
219     my @args = ('-p', 12345, '-c', 99, '-');
220     local @ARGV = @args;
221     my $app = App->new_with_options;
222     isa_ok($app, 'App');
223     is_deeply($app->ARGV, \@args, 'ARGV accessor');
224     is_deeply(\@ARGV, \@args, '@ARGV unmangled');
225     is_deeply($app->extra_argv, ['-'], 'extra_argv accessor');
226 }