fix bump-version script
[gitmo/Moo.git] / xt / moox-types-coercion.t
CommitLineData
d885d85e 1use strictures 1;
2use Test::More;
5fe1827a 3use Test::Fatal;
d885d85e 4
5{
6 package ClassWithTypes;
7 $INC{'ClassWithTypes.pm'} = __FILE__;
8 use Moo;
9 use MooX::Types::MooseLike::Base qw(ArrayRef);
10
11 has split_comma => (is => 'ro', isa => ArrayRef, coerce => sub { [ split /,/, $_[0] ] } );
12 has split_space => (is => 'ro', isa => ArrayRef, coerce => sub { [ split / /, $_[0] ] } );
5fe1827a 13 has bad_coerce => (is => 'ro', isa => ArrayRef, coerce => sub { $_[0] } );
d885d85e 14}
15
16my $o = ClassWithTypes->new(split_comma => 'a,b c,d', split_space => 'a,b c,d');
17is_deeply $o->split_comma, ['a','b c','d'], 'coerce with prebuilt type works';
18is_deeply $o->split_space, ['a,b','c,d'], ' ... and with different coercion on same type';
19
20{
21 package MooseSubclassWithTypes;
22 use Moose;
23 extends 'ClassWithTypes';
24}
25
26my $o2 = MooseSubclassWithTypes->new(split_comma => 'a,b c,d', split_space => 'a,b c,d');
27is_deeply $o2->split_comma, ['a','b c','d'], 'moose subclass has correct coercion';
28is_deeply $o2->split_space, ['a,b','c,d'], ' ... and with different coercion on same type';
29
5fe1827a 30like
31 exception { MooseSubclassWithTypes->new(bad_coerce => 1) },
32 qr/Validation failed for 'ArrayRef' with value/,
33 'inflated type has correct name';
34
d885d85e 35done_testing;