make all warnings fatal in tests
[gitmo/MooseX-Getopt.git] / t / 006_metaclass_traits.t
CommitLineData
adbe3e57 1use strict;
aec09248 2use warnings FATAL => 'all';
adbe3e57 3
9fbb5be9 4use Test::More tests => 70;
adbe3e57 5use Test::Moose;
9fbb5be9 6use Test::NoWarnings 1.04 ':early';
adbe3e57 7
8BEGIN {
9 use_ok('MooseX::Getopt');
10}
11
12{
13 package App;
14 use Moose;
2557b526 15
adbe3e57 16 with 'MooseX::Getopt';
17
18 has 'data' => (
2557b526 19 traits => [ 'MooseX::Getopt::Meta::Attribute::Trait' ],
adbe3e57 20 is => 'ro',
21 isa => 'Str',
22 default => 'file.dat',
23 cmd_flag => 'f',
24 );
25
26 has 'cow' => (
2557b526 27 traits => [ 'Getopt' ],
adbe3e57 28 is => 'ro',
29 isa => 'Str',
30 default => 'moo',
31 cmd_aliases => [qw/ moocow m c /],
32 );
33
34 has 'horse' => (
2557b526 35 traits => [ 'Getopt' ],
adbe3e57 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',
2557b526 51 isa => 'Bool',
adbe3e57 52 );
2557b526 53
adbe3e57 54 has 'libs' => (
55 is => 'ro',
56 isa => 'ArrayRef',
57 default => sub { [] },
2557b526 58 );
59
adbe3e57 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' => (
2557b526 73 traits => [ 'Getopt' ],
adbe3e57 74 is => 'ro',
75 isa => 'Int',
76 default => 832,
77 cmd_flag => 'p',
78 );
adbe3e57 79}
80
81foreach 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');
2557b526 85
adbe3e57 86 can_ok($attr, 'cmd_flag');
2557b526 87 can_ok($attr, 'cmd_aliases');
adbe3e57 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');
2557b526 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');
adbe3e57 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');
2557b526 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');
adbe3e57 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');
2557b526 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');
adbe3e57 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');
2557b526 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');
adbe3e57 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');
2557b526 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');
adbe3e57 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');
2557b526 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');
adbe3e57 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}