remove useless shebangs in tests
[gitmo/MooseX-Getopt.git] / t / 006_metaclass_traits.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 69;
5 use Test::Moose;
6
7 BEGIN {
8     use_ok('MooseX::Getopt');
9 }
10
11 {
12     package App;
13     use Moose;
14
15     with 'MooseX::Getopt';
16
17     has 'data' => (
18         traits    => [ 'MooseX::Getopt::Meta::Attribute::Trait' ],
19         is        => 'ro',
20         isa       => 'Str',
21         default   => 'file.dat',
22         cmd_flag  => 'f',
23     );
24
25     has 'cow' => (
26         traits      => [ 'Getopt' ],
27         is          => 'ro',
28         isa         => 'Str',
29         default     => 'moo',
30         cmd_aliases => [qw/ moocow m c /],
31     );
32
33     has 'horse' => (
34         traits      => [ 'Getopt' ],
35         is          => 'ro',
36         isa         => 'Str',
37         default     => 'bray',
38         cmd_flag    => 'horsey',
39         cmd_aliases => 'x',
40     );
41
42     has 'length' => (
43         is      => 'ro',
44         isa     => 'Int',
45         default => 24
46     );
47
48     has 'verbose' => (
49         is     => 'ro',
50         isa    => 'Bool',
51     );
52
53     has 'libs' => (
54         is      => 'ro',
55         isa     => 'ArrayRef',
56         default => sub { [] },
57     );
58
59     has 'details' => (
60         is      => 'ro',
61         isa     => 'HashRef',
62         default => sub { {} },
63     );
64
65     has '_private_stuff' => (
66         is      => 'ro',
67         isa     => 'Int',
68         default => 713
69     );
70
71     has '_private_stuff_cmdline' => (
72         traits    => [ 'Getopt' ],
73         is        => 'ro',
74         isa       => 'Int',
75         default   => 832,
76         cmd_flag  => 'p',
77     );
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 }