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