Commit | Line | Data |
b17a3035 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
b17a3035 |
7 | |
d03bd989 |
8 | BEGIN { |
9 | use_ok('Moose::Util::TypeConstraints'); |
10 | use_ok('Moose::Meta::TypeConstraint::Parameterized'); |
b17a3035 |
11 | } |
12 | |
d9b40005 |
13 | my $r = Moose::Util::TypeConstraints->get_type_constraint_registry; |
b17a3035 |
14 | |
15 | # Array of Ints |
16 | |
0fbd4b0a |
17 | my $array_of_ints = Moose::Meta::TypeConstraint::Parameterized->new( |
b17a3035 |
18 | name => 'ArrayRef[Int]', |
19 | parent => find_type_constraint('ArrayRef'), |
0fbd4b0a |
20 | type_parameter => find_type_constraint('Int'), |
b17a3035 |
21 | ); |
0fbd4b0a |
22 | isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized'); |
b17a3035 |
23 | isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint'); |
24 | |
25 | $r->add_type_constraint($array_of_ints); |
26 | |
27 | is(find_type_constraint('ArrayRef[Int]'), $array_of_ints, '... found the type we just added'); |
28 | |
29 | # Hash of Ints |
30 | |
0fbd4b0a |
31 | my $hash_of_ints = Moose::Meta::TypeConstraint::Parameterized->new( |
b17a3035 |
32 | name => 'HashRef[Int]', |
33 | parent => find_type_constraint('HashRef'), |
0fbd4b0a |
34 | type_parameter => find_type_constraint('Int'), |
b17a3035 |
35 | ); |
0fbd4b0a |
36 | isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint::Parameterized'); |
b17a3035 |
37 | isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint'); |
38 | |
39 | $r->add_type_constraint($hash_of_ints); |
40 | |
41 | is(find_type_constraint('HashRef[Int]'), $hash_of_ints, '... found the type we just added'); |
42 | |
43 | ## now attempt a coercion |
44 | |
45 | { |
46 | package Foo; |
47 | use Moose; |
48 | use Moose::Util::TypeConstraints; |
d03bd989 |
49 | |
b17a3035 |
50 | coerce 'ArrayRef[Int]' |
51 | => from 'HashRef[Int]' |
52 | => via { [ values %$_ ] }; |
d03bd989 |
53 | |
b17a3035 |
54 | has 'bar' => ( |
55 | is => 'ro', |
56 | isa => 'ArrayRef[Int]', |
57 | coerce => 1, |
58 | ); |
d03bd989 |
59 | |
b17a3035 |
60 | } |
61 | |
62 | my $foo = Foo->new(bar => { one => 1, two => 2, three => 3 }); |
63 | isa_ok($foo, 'Foo'); |
64 | |
65 | is_deeply([ sort @{$foo->bar} ], [ 1, 2, 3 ], '... our coercion worked!'); |
66 | |
a28e50e4 |
67 | done_testing; |