49042ea89c5eceef4bf7b4f9cf80db46bc056d13
[gitmo/MooseX-Getopt.git] / t / 102_basic_basic.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use Test::More tests => 70;
5 use Test::Moose;
6 use Test::NoWarnings 1.04 ':early';
7
8 BEGIN {
9     use_ok('MooseX::Getopt::Basic');
10 }
11
12 {
13     package App;
14     use Moose;
15
16     with 'MooseX::Getopt::Basic';
17
18     has 'data' => (
19         traits    => ['Getopt'],
20         is        => 'ro',
21         isa       => 'Str',
22         default   => 'file.dat',
23         cmd_flag  => 'f',
24     );
25
26     has 'cow' => (
27         traits      => ['Getopt'],
28         is          => 'ro',
29         isa         => 'Str',
30         default     => 'moo',
31         cmd_aliases => [qw/ moocow m c /],
32     );
33
34     has 'horse' => (
35         traits      => ['Getopt'],
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',
51         isa    => 'Bool',
52     );
53
54     has 'libs' => (
55         is      => 'ro',
56         isa     => 'ArrayRef',
57         default => sub { [] },
58     );
59
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' => (
73         traits      => ['Getopt'],
74         is        => 'ro',
75         isa       => 'Int',
76         default   => 832,
77         cmd_flag  => 'p',
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     can_ok($attr, 'cmd_flag');
86     can_ok($attr, 'cmd_aliases');
87 }
88
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');
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');
100 }
101
102 {
103     local @ARGV = ('--verbose', '--length', 50);
104
105     my $app = App->new_with_options;
106     isa_ok($app, 'App');
107
108     ok($app->verbose, '... verbosity is turned on as expected');
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');
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');
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');
126 }
127
128 {
129     local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib');
130
131     my $app = App->new_with_options;
132     isa_ok($app, 'App');
133
134     ok($app->verbose, '... verbosity is turned on as expected');
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');
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');
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');
156 }
157
158 {
159     # Test negation on booleans too ...
160     local @ARGV = ('--noverbose');
161
162     my $app = App->new_with_options;
163     isa_ok($app, 'App');
164
165     ok(!$app->verbose, '... verbosity is turned off as expected');
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');
170 }
171
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 }
191
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 }
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 }
213
214 # Test ARGV support
215 {
216     my @args = ('-p', 12345, '-c', 99, '-');
217     local @ARGV = @args;
218     my $app = App->new_with_options;
219     isa_ok($app, 'App');
220     is_deeply($app->ARGV, \@args, 'ARGV accessor');
221     is_deeply(\@ARGV, \@args, '@ARGV unmangled');
222     is_deeply($app->extra_argv, ['-'], 'extra_argv accessor');
223 }