From: Tomas Doran Date: Tue, 8 May 2012 07:53:17 +0000 (+0100) Subject: Type coercions cause confusing and wrong ->new methods. X-Git-Tag: v0.091005~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2a79705da24eff6d54bd679e7938223872ecf8ac;p=gitmo%2FMoo.git Type coercions cause confusing and wrong ->new methods. --- diff --git a/xt/moo-coercion-construction-bug.t b/xt/moo-coercion-construction-bug.t new file mode 100644 index 0000000..47b8b94 --- /dev/null +++ b/xt/moo-coercion-construction-bug.t @@ -0,0 +1,40 @@ +use strict; +use warnings; +use Test::More; + +{ + package MyTest::Role; + use Moo::Role; + use Sub::Quote; + + has test_attr => ( + isa => quote_sub(q{ die $_[0] . "not an object" unless Scalar::Util::blessed($_[0]) }), + coerce => quote_sub(q{ + return $_[0] if Scalar::Util::blessed($_[0]); + die; + }), + is => 'ro', + required => 1, + ); +} + +{ + package MyTest::ClassOne; + use Moo; + + with 'MyTest::Role'; + +} +{ + package MyTest::ClassTwo; + use Moo; + + with 'MyTest::Role'; +} + +my $t = MyTest::ClassOne->new(test_attr => bless {}, 'Bar'); +my $n = MyTest::ClassTwo->new( test_attr => $t); +is ref($n), 'MyTest::ClassTwo'; + +done_testing; +