X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F040_type_constraints%2F005_util_type_coercion.t;h=86a158ca37c16e59b766b37dd64380320a4ab7b0;hb=32a1dc89aa0691a28ac39820648fc0d2960d6cd4;hp=e6313092cdb69cd0cff56d5b98ad376c7df466bd;hpb=a0f8153dc5f0032469d14b8f96ba0a7be56839e3;p=gitmo%2FMoose.git diff --git a/t/040_type_constraints/005_util_type_coercion.t b/t/040_type_constraints/005_util_type_coercion.t index e631309..86a158c 100644 --- a/t/040_type_constraints/005_util_type_coercion.t +++ b/t/040_type_constraints/005_util_type_coercion.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 14; +use Test::More tests => 26; use Test::Exception; BEGIN { @@ -13,24 +13,24 @@ BEGIN { { package HTTPHeader; use Moose; - + has 'array' => (is => 'ro'); - has 'hash' => (is => 'ro'); + has 'hash' => (is => 'ro'); } -subtype Header => - => as Object +subtype Header => + => as Object => where { $_->isa('HTTPHeader') }; - -coerce Header - => from ArrayRef + +coerce Header + => from ArrayRef => via { HTTPHeader->new(array => $_[0]) } - => from HashRef + => from HashRef => via { HTTPHeader->new(hash => $_[0]) }; - -Moose::Util::TypeConstraints->export_type_constraints_as_functions(); - + +Moose::Util::TypeConstraints->export_type_constraints_as_functions(); + my $header = HTTPHeader->new(); isa_ok($header, 'HTTPHeader'); @@ -38,39 +38,65 @@ ok(Header($header), '... this passed the type test'); ok(!Header([]), '... this did not pass the type test'); ok(!Header({}), '... this did not pass the type test'); -my $coercion = find_type_constraint('Header')->coercion; -isa_ok($coercion, 'Moose::Meta::TypeCoercion'); +my $anon_type = subtype Object => where { $_->isa('HTTPHeader') }; -{ - my $coerced = $coercion->coerce([ 1, 2, 3 ]); - isa_ok($coerced, 'HTTPHeader'); - - is_deeply( - $coerced->array(), - [ 1, 2, 3 ], - '... got the right array'); - is($coerced->hash(), undef, '... nothing assigned to the hash'); -} +lives_ok { + coerce $anon_type + => from ArrayRef + => via { HTTPHeader->new(array => $_[0]) } + => from HashRef + => via { HTTPHeader->new(hash => $_[0]) }; +} 'coercion of anonymous subtype succeeds'; -{ - my $coerced = $coercion->coerce({ one => 1, two => 2, three => 3 }); - isa_ok($coerced, 'HTTPHeader'); - - is_deeply( - $coerced->hash(), - { one => 1, two => 2, three => 3 }, - '... got the right hash'); - is($coerced->array(), undef, '... nothing assigned to the array'); -} +foreach my $coercion ( + find_type_constraint('Header')->coercion, + $anon_type->coercion + ) { -{ - my $scalar_ref = \(my $var); - my $coerced = $coercion->coerce($scalar_ref); - is($coerced, $scalar_ref, '... got back what we put in'); -} + isa_ok($coercion, 'Moose::Meta::TypeCoercion'); -{ - my $coerced = $coercion->coerce("Foo"); - is($coerced, "Foo", '... got back what we put in'); + { + my $coerced = $coercion->coerce([ 1, 2, 3 ]); + isa_ok($coerced, 'HTTPHeader'); + + is_deeply( + $coerced->array(), + [ 1, 2, 3 ], + '... got the right array'); + is($coerced->hash(), undef, '... nothing assigned to the hash'); + } + + { + my $coerced = $coercion->coerce({ one => 1, two => 2, three => 3 }); + isa_ok($coerced, 'HTTPHeader'); + + is_deeply( + $coerced->hash(), + { one => 1, two => 2, three => 3 }, + '... got the right hash'); + is($coerced->array(), undef, '... nothing assigned to the array'); + } + + { + my $scalar_ref = \(my $var); + my $coerced = $coercion->coerce($scalar_ref); + is($coerced, $scalar_ref, '... got back what we put in'); + } + + { + my $coerced = $coercion->coerce("Foo"); + is($coerced, "Foo", '... got back what we put in'); + } } +subtype 'StrWithTrailingX' + => as 'Str' + => where { /X$/ }; + +coerce 'StrWithTrailingX' + => from 'Str' + => via { $_ . 'X' }; + +my $tc = find_type_constraint('StrWithTrailingX'); +is($tc->coerce("foo"), "fooX", "coerce when needed"); +is($tc->coerce("fooX"), "fooX", "do not coerce when unneeded");