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