Commit | Line | Data |
8034a232 |
1 | use strict; |
2 | use warnings; |
3 | |
f63e6310 |
4 | use Test::More tests => 6; |
8034a232 |
5 | |
6 | BEGIN { |
7 | use_ok('MooseX::Getopt'); |
8 | } |
9 | |
10 | { |
11 | package App; |
12 | use Moose; |
13 | use Moose::Util::TypeConstraints; |
2557b526 |
14 | |
8034a232 |
15 | use Scalar::Util 'looks_like_number'; |
2557b526 |
16 | |
8034a232 |
17 | with 'MooseX::Getopt'; |
18 | |
19 | subtype 'ArrayOfInts' |
20 | => as 'ArrayRef' |
21 | => where { scalar (grep { looks_like_number($_) } @$_) }; |
2557b526 |
22 | |
8034a232 |
23 | MooseX::Getopt::OptionTypeMap->add_option_type_to_map( |
24 | 'ArrayOfInts' => '=i@' |
25 | ); |
2557b526 |
26 | |
8034a232 |
27 | has 'nums' => ( |
28 | is => 'ro', |
29 | isa => 'ArrayOfInts', |
30 | default => sub { [0] } |
2557b526 |
31 | ); |
32 | |
8034a232 |
33 | } |
34 | |
35 | { |
36 | local @ARGV = (); |
37 | |
38 | my $app = App->new_with_options; |
39 | isa_ok($app, 'App'); |
2557b526 |
40 | |
41 | is_deeply($app->nums, [0], '... nums is [0] as expected'); |
8034a232 |
42 | } |
43 | |
44 | { |
45 | local @ARGV = ('--nums', 3, '--nums', 5); |
46 | |
47 | my $app = App->new_with_options; |
48 | isa_ok($app, 'App'); |
2557b526 |
49 | |
50 | is_deeply($app->nums, [3, 5], '... nums is [3, 5] as expected'); |
8034a232 |
51 | } |
52 | |
f63e6310 |
53 | # Make sure it really used our =i@, instead of falling back |
54 | # to =s@ via the type system, and test that exceptions work |
55 | # while we're at it. |
56 | eval { |
57 | local @ARGV = ('--nums', 3, '--nums', 'foo'); |
58 | |
59 | my $app = App->new_with_options; |
60 | }; |
61 | like($@, qr/Value "foo" invalid/, 'Numeric constraint enforced'); |