spurious whitespace
[gitmo/MooseX-Getopt.git] / t / 002_custom_option_type.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 7;
5 use Test::NoWarnings 1.04 ':early';
6
7 BEGIN {
8     use_ok('MooseX::Getopt');
9 }
10
11 {
12     package App;
13     use Moose;
14     use Moose::Util::TypeConstraints;
15
16     use Scalar::Util 'looks_like_number';
17
18     with 'MooseX::Getopt';
19
20     subtype 'ArrayOfInts'
21         => as 'ArrayRef'
22         => where { scalar (grep { looks_like_number($_) } @$_)  };
23
24     MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
25         'ArrayOfInts' => '=i@'
26     );
27
28     has 'nums' => (
29         is      => 'ro',
30         isa     => 'ArrayOfInts',
31         default => sub { [0] }
32     );
33 }
34
35 {
36     local @ARGV = ();
37
38     my $app = App->new_with_options;
39     isa_ok($app, 'App');
40
41     is_deeply($app->nums, [0], '... nums is [0] as expected');
42 }
43
44 {
45     local @ARGV = ('--nums', 3, '--nums', 5);
46
47     my $app = App->new_with_options;
48     isa_ok($app, 'App');
49
50     is_deeply($app->nums, [3, 5], '... nums is [3, 5] as expected');
51 }
52
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');