show the first line here when testing with a harness
[gitmo/Moose.git] / t / type_constraints / container_type_coercion.t
CommitLineData
b17a3035 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
b17a3035 7
28fdde7f 8use Moose::Util::TypeConstraints;
9use Moose::Meta::TypeConstraint::Parameterized;
b17a3035 10
d9b40005 11my $r = Moose::Util::TypeConstraints->get_type_constraint_registry;
b17a3035 12
13# Array of Ints
14
0fbd4b0a 15my $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 20isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
b17a3035 21isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint');
22
23$r->add_type_constraint($array_of_ints);
24
25is(find_type_constraint('ArrayRef[Int]'), $array_of_ints, '... found the type we just added');
26
27# Hash of Ints
28
0fbd4b0a 29my $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 34isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
b17a3035 35isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint');
36
37$r->add_type_constraint($hash_of_ints);
38
39is(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
60my $foo = Foo->new(bar => { one => 1, two => 2, three => 3 });
61isa_ok($foo, 'Foo');
62
63is_deeply([ sort @{$foo->bar} ], [ 1, 2, 3 ], '... our coercion worked!');
64
a28e50e4 65done_testing;