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