work in progress, tests are failing, and parameterized role is not flexible enough...
[gitmo/MooseX-Getopt.git] / t / 002_custom_option_type.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 6;
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' => { getopt_conf => [] };
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 {
38     local @ARGV = ();
39
40     my $app = App->new_with_options;
41     isa_ok($app, 'App');
42
43     is_deeply($app->nums, [0], '... nums is [0] as expected');
44 }
45
46 {
47     local @ARGV = ('--nums', 3, '--nums', 5);
48
49     my $app = App->new_with_options;
50     isa_ok($app, 'App');
51
52     is_deeply($app->nums, [3, 5], '... nums is [3, 5] as expected');
53 }
54
55 # Make sure it really used our =i@, instead of falling back
56 #  to =s@ via the type system, and test that exceptions work
57 #  while we're at it.
58 eval {
59     local @ARGV = ('--nums', 3, '--nums', 'foo');
60
61     my $app = App->new_with_options;
62 };
63 like($@, qr/Value "foo" invalid/, 'Numeric constraint enforced');