support scalar cmd_aliases, so that singular aliases are not so clunky to type
[gitmo/MooseX-Getopt.git] / t / 001_basic.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 47;
7
8 BEGIN {
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
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',
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 }
67
68 {
69     local @ARGV = ();
70
71     my $app = App->new_with_options;
72     isa_ok($app, 'App');
73
74     ok(!$app->verbose, '... verbosity is off as expected');
75     is($app->length, 24, '... length is 24 as expected');    
76     is($app->data, 'file.dat', '... data is file.dat as expected');        
77     is_deeply($app->libs, [], '... libs is [] as expected'); 
78     is_deeply($app->details, {}, '... details is {} as expected');           
79 }
80
81 {
82     local @ARGV = ('--verbose', '--length', 50);
83
84     my $app = App->new_with_options;
85     isa_ok($app, 'App');
86
87     ok($app->verbose, '... verbosity is turned on as expected');
88     is($app->length, 50, '... length is 50 as expected');    
89     is($app->data, 'file.dat', '... data is file.dat as expected'); 
90     is_deeply($app->libs, [], '... libs is [] as expected');  
91     is_deeply($app->details, {}, '... details is {} as expected');                            
92 }
93
94 {
95     local @ARGV = ('--verbose', '-f', 'foo.txt');
96
97     my $app = App->new_with_options;
98     isa_ok($app, 'App');
99
100     ok($app->verbose, '... verbosity is turned on as expected');
101     is($app->length, 24, '... length is 24 as expected');    
102     is($app->data, 'foo.txt', '... data is foo.txt as expected'); 
103     is_deeply($app->libs, [], '... libs is [] as expected');    
104     is_deeply($app->details, {}, '... details is {} as expected');                             
105 }
106
107 {
108     local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib');
109
110     my $app = App->new_with_options;
111     isa_ok($app, 'App');
112
113     ok($app->verbose, '... verbosity is turned on as expected');
114     is($app->length, 24, '... length is 24 as expected');    
115     is($app->data, 'file.dat', '... data is foo.txt as expected'); 
116     is_deeply($app->libs, 
117     ['libs/', 'includes/lib'], 
118     '... libs is [libs/, includes/lib] as expected');   
119     is_deeply($app->details, {}, '... details is {} as expected');                              
120 }
121
122 {
123     local @ARGV = ('--details', 'os=mac', '--details', 'name=foo');
124
125     my $app = App->new_with_options;
126     isa_ok($app, 'App');
127
128     ok(!$app->verbose, '... verbosity is turned on as expected');
129     is($app->length, 24, '... length is 24 as expected');    
130     is($app->data, 'file.dat', '... data is foo.txt as expected'); 
131     is_deeply($app->libs, [], '... libs is [] as expected');    
132     is_deeply($app->details, 
133     { os => 'mac', name => 'foo' }, 
134     '... details is { os => mac, name => foo } as expected');                              
135 }
136
137 {
138     # Test negation on booleans too ...
139     local @ARGV = ('--noverbose');
140
141     my $app = App->new_with_options;
142     isa_ok($app, 'App');
143
144     ok(!$app->verbose, '... verbosity is turned off as expected');
145     is($app->length, 24, '... length is 24 as expected');    
146     is($app->data, 'file.dat', '... file is file.dat as expected');   
147     is_deeply($app->libs, [], '... libs is [] as expected');                
148     is_deeply($app->details, {}, '... details is {} as expected');               
149 }
150
151 # Test cmd_alias without cmd_flag
152 {
153     local @ARGV = ('--cow', '42');
154     my $app = App->new_with_options;
155     isa_ok($app, 'App');
156     is($app->cow, 42, 'cmd_alias, but not using it');
157 }
158 {
159     local @ARGV = ('--moocow', '88');
160     my $app = App->new_with_options;
161     isa_ok($app, 'App');
162     is($app->cow, 88, 'cmd_alias, using long one');
163 }
164 {
165     local @ARGV = ('-c', '99');
166     my $app = App->new_with_options;
167     isa_ok($app, 'App');
168     is($app->cow, 99, 'cmd_alias, using short one');
169 }
170
171 # Test cmd_alias + cmd_flag
172 {
173     local @ARGV = ('--horsey', '123');
174     my $app = App->new_with_options;
175     isa_ok($app, 'App');
176     is($app->horse, 123, 'cmd_alias+cmd_flag, using flag');
177 }
178 {
179     local @ARGV = ('-x', '321');
180     my $app = App->new_with_options;
181     isa_ok($app, 'App');
182     is($app->horse, 321, 'cmd_alias+cmd_flag, using alias');
183 }