X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F001_mouse%2F043-parameterized-type.t;h=531febbcbb8ad91109e660fbfda6d24bc25f4045;hb=HEAD;hp=3e223e227c73692412d5df48dd8fee7303756656;hpb=fc83f4cf2ef7708120a216bddc285b93082d7958;p=gitmo%2FMouse.git diff --git a/t/001_mouse/043-parameterized-type.t b/t/001_mouse/043-parameterized-type.t index 3e223e2..531febb 100644 --- a/t/001_mouse/043-parameterized-type.t +++ b/t/001_mouse/043-parameterized-type.t @@ -1,11 +1,12 @@ -#!/usr/bin/env perl +#!perl use strict; use warnings; -use Test::More tests => 54; +use Test::More; use Test::Exception; use Tie::Hash; use Tie::Array; + { { package My::Role; @@ -69,30 +70,30 @@ use Tie::Array; # check bad args throws_ok { Foo->new( foo => { a => 'b' }); - } qr/Attribute \(foo\) does not pass the type constraint because: Validation failed for 'HashRef\[Int\]' failed with value/, "Bad args for hash throws an exception"; + } qr/Attribute \(foo\) does not pass the type constraint because: Validation failed for 'HashRef\[Int\]' with value/, "Bad args for hash throws an exception"; throws_ok { Foo->new( bar => [ a => 'b' ]); - } qr/Attribute \(bar\) does not pass the type constraint because: Validation failed for 'ArrayRef\[Int\]' failed with value/, "Bad args for array throws an exception"; + } qr/Attribute \(bar\) does not pass the type constraint because: Validation failed for 'ArrayRef\[Int\]' with value/, "Bad args for array throws an exception"; throws_ok { Foo->new( complex => [ { a => 1, b => 1 }, { c => "d", e => "f" } ] ) - } qr/Attribute \(complex\) does not pass the type constraint because: Validation failed for 'ArrayRef\[HashRef\[Int\]\]' failed with value/, "Bad args for complex types throws an exception"; + } qr/Attribute \(complex\) does not pass the type constraint because: Validation failed for 'ArrayRef\[HashRef\[Int\]\]' with value/, "Bad args for complex types throws an exception"; throws_ok { Foo->new( my_class => [ 10 ] ); - } qr/Attribute \(my_class\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Class\]' failed with value/; + } qr/Attribute \(my_class\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Class\]' with value/; throws_ok { Foo->new( my_class => [ {foo => 'bar'} ] ); - } qr/Attribute \(my_class\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Class\]' failed with value/; + } qr/Attribute \(my_class\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Class\]' with value/; throws_ok { Foo->new( my_role => [ 20 ] ); - } qr/Attribute \(my_role\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Role\]' failed with value/; + } qr/Attribute \(my_role\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Role\]' with value/; throws_ok { Foo->new( my_role => [ {foo => 'bar'} ] ); - } qr/Attribute \(my_role\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Role\]' failed with value/; + } qr/Attribute \(my_role\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Role\]' with value/; } { @@ -122,11 +123,12 @@ use Tie::Array; my $bar = Bar->new(list => [ qw(a b c) ]); is_deeply( $bar->list, \@list, "list is as expected"); - } "coercion works"; + } "coercion works" + or diag( Mouse::Util::TypeConstraints::find_type_constraint("Bar::List")->dump ); throws_ok { Bar->new(list => [ { 1 => 2 }, 2, 3 ]); - } qr/Attribute \(list\) does not pass the type constraint because: Validation failed for 'Bar::List' failed with value/, "Bad coercion parameter throws an error"; + } qr/Attribute \(list\) does not pass the type constraint because: Validation failed for 'Bar::List' with value/, "Bad coercion parameter throws an error"; } use Mouse::Util::TypeConstraints; @@ -222,3 +224,38 @@ else{ # under Moose } is_deeply \%th_clone, \%th, 'the hash iterator is initialized'; + + +for my $i(1 .. 2) { + diag "derived from parameterized types #$i"; + + my $myhashref = subtype 'MyHashRef', + as 'HashRef[Value]', + where { keys %$_ > 1 }; + + ok $myhashref->is_a_type_of('HashRef'), "$myhashref"; + ok $myhashref->check({ a => 43, b => 100 }); + ok $myhashref->check({ a => 43, b => 3.14 }); + ok !$myhashref->check({}); + ok !$myhashref->check({ a => 42, b => [] }); + + is $myhashref->type_parameter, 'Value'; + + $myhashref = subtype 'H', as 'MyHashRef[Int]'; + + ok $myhashref->is_a_type_of('HashRef'), "$myhashref"; + ok $myhashref->check({ a => 43, b => 100 }); + ok $myhashref->check({ a => 43, b => 100, c => 0 }); + ok !$myhashref->check({}), 'empty hash'; + ok !$myhashref->check({ foo => 42 }); + ok !$myhashref->check({ a => 43, b => "foo" }); + ok !$myhashref->check({ a => 42, b => [] }); + ok !$myhashref->check({ a => 42, b => undef }); + ok !$myhashref->check([42]); + ok !$myhashref->check("foo"); + + is $myhashref->type_parameter, 'Int'; +} + +done_testing; +