test for warnings everywhere
[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 {
37     local @ARGV = ();
38
39     my $app = App->new_with_options;
40     isa_ok($app, 'App');
41
42     is_deeply($app->nums, [0], '... nums is [0] as expected');
43 }
44
45 {
46     local @ARGV = ('--nums', 3, '--nums', 5);
47
48     my $app = App->new_with_options;
49     isa_ok($app, 'App');
50
51     is_deeply($app->nums, [3, 5], '... nums is [3, 5] as expected');
52 }
53
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.
57 eval {
58     local @ARGV = ('--nums', 3, '--nums', 'foo');
59
60     my $app = App->new_with_options;
61 };
62 like($@, qr/Value "foo" invalid/, 'Numeric constraint enforced');