Add assert_valid() to Meta::TypeConstraint
[gitmo/Mouse.git] / t / 040_type_constraints / failing / 012_container_type_coercion.t
CommitLineData
b2b106d7 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
b910be93 6use Test::More tests => 9;
b2b106d7 7use Test::Exception;
8
9BEGIN {
10 use_ok('Mouse::Util::TypeConstraints');
b2b106d7 11}
12
13my $r = Mouse::Util::TypeConstraints->get_type_constraint_registry;
14
15# Array of Ints
16
17my $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);
22isa_ok($array_of_ints, 'Mouse::Meta::TypeConstraint::Parameterized');
23isa_ok($array_of_ints, 'Mouse::Meta::TypeConstraint');
24
25$r->add_type_constraint($array_of_ints);
26
27is(find_type_constraint('ArrayRef[Int]'), $array_of_ints, '... found the type we just added');
28
29# Hash of Ints
30
31my $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);
36isa_ok($hash_of_ints, 'Mouse::Meta::TypeConstraint::Parameterized');
37isa_ok($hash_of_ints, 'Mouse::Meta::TypeConstraint');
38
39$r->add_type_constraint($hash_of_ints);
40
41is(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
62my $foo = Foo->new(bar => { one => 1, two => 2, three => 3 });
63isa_ok($foo, 'Foo');
64
65is_deeply([ sort @{$foo->bar} ], [ 1, 2, 3 ], '... our coercion worked!');
66
67