use warnings tester with fewer dependencies, issues
[gitmo/MooseX-Getopt.git] / t / 002_custom_option_type.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use Test::More tests => 7;
5 use Test::Warnings;
6 use Test::Fatal;
7
8 BEGIN {
9     use_ok('MooseX::Getopt');
10 }
11
12 {
13     package App;
14     use Moose;
15     use Moose::Util::TypeConstraints;
16
17     use Scalar::Util 'looks_like_number';
18
19     with 'MooseX::Getopt';
20
21     subtype 'ArrayOfInts'
22         => as 'ArrayRef'
23         => where { scalar (grep { looks_like_number($_) } @$_)  };
24
25     MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
26         'ArrayOfInts' => '=i@'
27     );
28
29     has 'nums' => (
30         is      => 'ro',
31         isa     => 'ArrayOfInts',
32         default => sub { [0] }
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 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 );