848fe53545c4900ff21c88c4239d0987e9ebc38a
[gitmo/MooseX-Getopt.git] / t / 006_metaclass_traits.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use Test::More tests => 70;
5 use Test::Moose;
6 use Test::NoWarnings 1.04 ':early';
7
8 BEGIN {
9     use_ok('MooseX::Getopt');
10 }
11
12 {
13     package App;
14     use Moose;
15
16     with 'MooseX::Getopt';
17
18     has 'data' => (
19         traits    => [ 'MooseX::Getopt::Meta::Attribute::Trait' ],
20         is        => 'ro',
21         isa       => 'Str',
22         default   => 'file.dat',
23         cmd_flag  => 'f',
24     );
25
26     has 'cow' => (
27         traits      => [ 'Getopt' ],
28         is          => 'ro',
29         isa         => 'Str',
30         default     => 'moo',
31         cmd_aliases => [qw/ moocow m c /],
32     );
33
34     has 'horse' => (
35         traits      => [ 'Getopt' ],
36         is          => 'ro',
37         isa         => 'Str',
38         default     => 'bray',
39         cmd_flag    => 'horsey',
40         cmd_aliases => 'x',
41     );
42
43     has 'length' => (
44         is      => 'ro',
45         isa     => 'Int',
46         default => 24
47     );
48
49     has 'verbose' => (
50         is     => 'ro',
51         isa    => 'Bool',
52     );
53
54     has 'libs' => (
55         is      => 'ro',
56         isa     => 'ArrayRef',
57         default => sub { [] },
58     );
59
60     has 'details' => (
61         is      => 'ro',
62         isa     => 'HashRef',
63         default => sub { {} },
64     );
65
66     has '_private_stuff' => (
67         is      => 'ro',
68         isa     => 'Int',
69         default => 713
70     );
71
72     has '_private_stuff_cmdline' => (
73         traits    => [ 'Getopt' ],
74         is        => 'ro',
75         isa       => 'Int',
76         default   => 832,
77         cmd_flag  => 'p',
78     );
79 }
80
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');
84     does_ok($attr, 'MooseX::Getopt::Meta::Attribute::Trait');
85
86     can_ok($attr, 'cmd_flag');
87     can_ok($attr, 'cmd_aliases');
88 }
89
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');
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');
101 }
102
103 {
104     local @ARGV = ('--verbose', '--length', 50);
105
106     my $app = App->new_with_options;
107     isa_ok($app, 'App');
108
109     ok($app->verbose, '... verbosity is turned on as expected');
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');
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');
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');
127 }
128
129 {
130     local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib');
131
132     my $app = App->new_with_options;
133     isa_ok($app, 'App');
134
135     ok($app->verbose, '... verbosity is turned on as expected');
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');
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');
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');
157 }
158
159 {
160     # Test negation on booleans too ...
161     local @ARGV = ('--noverbose');
162
163     my $app = App->new_with_options;
164     isa_ok($app, 'App');
165
166     ok(!$app->verbose, '... verbosity is turned off as expected');
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');
171 }
172
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 }
192
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 }
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 }
214
215 # Test ARGV support
216 {
217     my @args = ('-p', 12345, '-c', 99, '-');
218     local @ARGV = @args;
219     my $app = App->new_with_options;
220     isa_ok($app, 'App');
221     is_deeply($app->ARGV, \@args, 'ARGV accessor');
222     is_deeply(\@ARGV, \@args, '@ARGV unmangled');
223     is_deeply($app->extra_argv, ['-'], 'extra_argv accessor');
224 }