Commit | Line | Data |
b2b106d7 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
b910be93 |
6 | use Test::More tests => 9; |
b2b106d7 |
7 | use Test::Exception; |
8 | |
9 | BEGIN { |
10 | use_ok('Mouse::Util::TypeConstraints'); |
b2b106d7 |
11 | } |
12 | |
13 | my $r = Mouse::Util::TypeConstraints->get_type_constraint_registry; |
14 | |
15 | # Array of Ints |
16 | |
17 | my $array_of_ints = Mouse::Meta::TypeConstraint::Parameterized->new( |
18 | name => 'ArrayRef[Int]', |
19 | parent => find_type_constraint('ArrayRef'), |
20 | type_parameter => find_type_constraint('Int'), |
21 | ); |
22 | isa_ok($array_of_ints, 'Mouse::Meta::TypeConstraint::Parameterized'); |
23 | isa_ok($array_of_ints, 'Mouse::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 | |
31 | my $hash_of_ints = Mouse::Meta::TypeConstraint::Parameterized->new( |
32 | name => 'HashRef[Int]', |
33 | parent => find_type_constraint('HashRef'), |
34 | type_parameter => find_type_constraint('Int'), |
35 | ); |
36 | isa_ok($hash_of_ints, 'Mouse::Meta::TypeConstraint::Parameterized'); |
37 | isa_ok($hash_of_ints, 'Mouse::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 Mouse; |
48 | use Mouse::Util::TypeConstraints; |
49 | |
50 | coerce 'ArrayRef[Int]' |
51 | => from 'HashRef[Int]' |
52 | => via { [ values %$_ ] }; |
53 | |
54 | has 'bar' => ( |
55 | is => 'ro', |
56 | isa => 'ArrayRef[Int]', |
57 | coerce => 1, |
58 | ); |
59 | |
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 | |
67 | |