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