6 use Test::More tests => 11;
8 use Scalar::Util 'blessed';
10 use Moose::Util::TypeConstraints;
25 has lazy_classname => (
28 default => sub { "Parent" },
31 has type_constrained => (
45 has '+lazy_classname' => (
46 default => sub { "Child" },
49 has '+type_constrained' => (
55 my $foo = Parent->new;
56 my $bar = Parent->new;
58 is(blessed($foo), 'Parent', 'Parent->new gives a Parent object');
59 is($foo->name, undef, 'No name yet');
60 is($foo->lazy_classname, 'Parent', "lazy attribute initialized");
61 lives_ok { $foo->type_constrained(10.5) } "Num type constraint for now..";
63 # try to rebless, except it will fail due to Child's stricter type constraint
64 throws_ok { Child->meta->rebless_instance($foo) }
65 qr/^Attribute \(type_constrained\) does not pass the type constraint because\: Validation failed for 'Int' failed with value 10\.5/,
66 '... this failed cause of type check';
67 throws_ok { Child->meta->rebless_instance($bar) }
68 qr/^Attribute \(type_constrained\) does not pass the type constraint because\: Validation failed for 'Int' failed with value 5\.5/,
69 '... this failed cause of type check';;
71 $foo->type_constrained(10);
72 $bar->type_constrained(5);
74 Child->meta->rebless_instance($foo);
75 Child->meta->rebless_instance($bar);
77 is(blessed($foo), 'Child', 'successfully reblessed into Child');
78 is($foo->name, 'Junior', "Child->name's default came through");
80 is($foo->lazy_classname, 'Parent', "lazy attribute was already initialized");
81 is($bar->lazy_classname, 'Child', "lazy attribute just now initialized");
83 throws_ok { $foo->type_constrained(10.5) }
84 qr/^Attribute \(type_constrained\) does not pass the type constraint because\: Validation failed for 'Int' failed with value 10\.5/,
85 '... this failed cause of type check';