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