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