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