Type coercions cause confusing and wrong ->new methods.
[gitmo/Moo.git] / xt / moo-coercion-construction-bug.t
1 use strict;
2 use warnings;
3 use Test::More;
4
5 {
6     package MyTest::Role;
7     use Moo::Role;
8     use Sub::Quote;
9
10     has test_attr => (
11         isa => quote_sub(q{ die $_[0] . "not an object" unless Scalar::Util::blessed($_[0]) }),
12         coerce => quote_sub(q{
13             return $_[0] if Scalar::Util::blessed($_[0]);
14             die;
15         }),
16         is => 'ro',
17         required => 1,
18     );
19 }
20
21 {
22     package MyTest::ClassOne;
23     use Moo;
24
25     with 'MyTest::Role';
26
27 }
28 {
29     package MyTest::ClassTwo;
30     use Moo;
31
32     with 'MyTest::Role';
33 }
34
35 my $t = MyTest::ClassOne->new(test_attr => bless {}, 'Bar');
36 my $n = MyTest::ClassTwo->new( test_attr => $t);
37 is ref($n), 'MyTest::ClassTwo';
38
39 done_testing;
40