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