remove unneeded use of Test::Fatal
[gitmo/Moose.git] / t / 040_type_constraints / 012_container_type_coercion.t
CommitLineData
b17a3035 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
b17a3035 7
d03bd989 8BEGIN {
9 use_ok('Moose::Util::TypeConstraints');
10 use_ok('Moose::Meta::TypeConstraint::Parameterized');
b17a3035 11}
12
d9b40005 13my $r = Moose::Util::TypeConstraints->get_type_constraint_registry;
b17a3035 14
15# Array of Ints
16
0fbd4b0a 17my $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 22isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
b17a3035 23isa_ok($array_of_ints, 'Moose::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
0fbd4b0a 31my $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 36isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
b17a3035 37isa_ok($hash_of_ints, 'Moose::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 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
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
a28e50e4 67done_testing;