Type coercions cause confusing and wrong ->new methods.
Tomas Doran [Tue, 8 May 2012 07:53:17 +0000 (08:53 +0100)]
xt/moo-coercion-construction-bug.t [new file with mode: 0644]

diff --git a/xt/moo-coercion-construction-bug.t b/xt/moo-coercion-construction-bug.t
new file mode 100644 (file)
index 0000000..47b8b94
--- /dev/null
@@ -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;
+