From: Stevan Little Date: Sun, 19 Mar 2006 20:54:25 +0000 (+0000) Subject: coercion happens during obj construction too X-Git-Tag: 0_05~83 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=00867c44a4633c213399acae79a70062cedcb72f;p=gitmo%2FMoose.git coercion happens during obj construction too --- diff --git a/lib/Moose/Meta/Class.pm b/lib/Moose/Meta/Class.pm index e29786f..f2fe062 100644 --- a/lib/Moose/Meta/Class.pm +++ b/lib/Moose/Meta/Class.pm @@ -21,9 +21,14 @@ sub construct_instance { # if nothing was in the %params, we can use the # attribute's default value (if it has one) $val ||= $attr->default($instance) if $attr->has_default; - if (defined $val && $attr->has_type_constraint) { - (defined($attr->type_constraint->($val))) - || confess "Attribute () does not pass the type contraint with"; + if (defined $val) { + if ($attr->has_coercion) { + $val = $attr->coerce->($val); + } + if ($attr->has_type_constraint) { + (defined($attr->type_constraint->($val))) + || confess "Attribute () does not pass the type contraint with"; + } } $instance->{$attr->name} = $val; } diff --git a/t/005_basic.t b/t/005_basic.t index 19958a2..9321766 100644 --- a/t/005_basic.t +++ b/t/005_basic.t @@ -3,11 +3,9 @@ use strict; use warnings; -use Test::More tests => 10; +use Test::More tests => 26; use Test::Exception; -use Scalar::Util 'isweak'; - BEGIN { use_ok('Moose'); } @@ -35,39 +33,86 @@ BEGIN { has 'header' => (is => 'rw', isa => 'HTTPHeader', coerce => 1); } -my $engine = Engine->new(); -isa_ok($engine, 'Engine'); +{ + my $engine = Engine->new(); + isa_ok($engine, 'Engine'); + + # try with arrays + + lives_ok { + $engine->header([ 1, 2, 3 ]); + } '... type was coerced without incident'; + isa_ok($engine->header, 'HTTPHeader'); + + is_deeply( + $engine->header->array, + [ 1, 2, 3 ], + '... got the right array value of the header'); + ok(!defined($engine->header->hash), '... no hash value set'); + + # try with hash + + lives_ok { + $engine->header({ one => 1, two => 2, three => 3 }); + } '... type was coerced without incident'; + isa_ok($engine->header, 'HTTPHeader'); + + is_deeply( + $engine->header->hash, + { one => 1, two => 2, three => 3 }, + '... got the right hash value of the header'); + ok(!defined($engine->header->array), '... no array value set'); + + dies_ok { + $engine->header("Foo"); + } '... dies with the wrong type, even after coercion'; + + lives_ok { + $engine->header(HTTPHeader->new); + } '... lives with the right type, even after coercion'; +} -# try with arrays +{ + my $engine = Engine->new(header => [ 1, 2, 3 ]); + isa_ok($engine, 'Engine'); -$engine->header([ 1, 2, 3 ]); -isa_ok($engine->header, 'HTTPHeader'); + isa_ok($engine->header, 'HTTPHeader'); -is_deeply( - $engine->header->array, - [ 1, 2, 3 ], - '... got the right array value of the header'); -ok(!defined($engine->header->hash), '... no hash value set'); + is_deeply( + $engine->header->array, + [ 1, 2, 3 ], + '... got the right array value of the header'); + ok(!defined($engine->header->hash), '... no hash value set'); +} -# try with hash +{ + my $engine = Engine->new(header => { one => 1, two => 2, three => 3 }); + isa_ok($engine, 'Engine'); -$engine->header({ one => 1, two => 2, three => 3 }); -isa_ok($engine->header, 'HTTPHeader'); + isa_ok($engine->header, 'HTTPHeader'); -is_deeply( - $engine->header->hash, - { one => 1, two => 2, three => 3 }, - '... got the right hash value of the header'); -ok(!defined($engine->header->array), '... no array value set'); + is_deeply( + $engine->header->hash, + { one => 1, two => 2, three => 3 }, + '... got the right hash value of the header'); + ok(!defined($engine->header->array), '... no array value set'); +} -dies_ok { - $engine->header("Foo"); -} '... dies with the wrong type, even after coercion'; +{ + my $engine = Engine->new(header => HTTPHeader->new()); + isa_ok($engine, 'Engine'); -lives_ok { - $engine->header(HTTPHeader->new); -} '... lives with the right type, even after coercion'; + isa_ok($engine->header, 'HTTPHeader'); + ok(!defined($engine->header->hash), '... no hash value set'); + ok(!defined($engine->header->array), '... no array value set'); +} +dies_ok { + Engine->new(header => 'Foo'); +} '... dies correctly with bad params'; +dies_ok { + Engine->new(header => \(my $var)); +} '... dies correctly with bad params';