support scalar cmd_aliases, so that singular aliases are not so clunky to type
[gitmo/MooseX-Getopt.git] / t / 001_basic.t
CommitLineData
5dac17c3 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
de75868f 6use Test::More tests => 47;
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 { {} },
64 );
5dac17c3 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');
8034a232 77 is_deeply($app->libs, [], '... libs is [] as expected');
78 is_deeply($app->details, {}, '... details is {} as expected');
5dac17c3 79}
80
81{
8034a232 82 local @ARGV = ('--verbose', '--length', 50);
5dac17c3 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');
8034a232 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');
5dac17c3 105}
106
107{
8034a232 108 local @ARGV = ('--verbose', '--libs', 'libs/', '--libs', 'includes/lib');
5dac17c3 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');
8034a232 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');
5dac17c3 135}
136
137{
8034a232 138 # Test negation on booleans too ...
139 local @ARGV = ('--noverbose');
5dac17c3 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');
8034a232 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');
5dac17c3 149}
150
de75868f 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}
5dac17c3 170
de75868f 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}