6 use Test::More tests => 13;
8 use Scalar::Util 'blessed';
12 use_ok("Moose::Util::TypeConstraints");
28 has lazy_classname => (
31 default => sub { "Parent" },
34 has type_constrained => (
48 has '+lazy_classname' => (
49 default => sub { "Child" },
52 has '+type_constrained' => (
58 my $foo = Parent->new;
59 my $bar = Parent->new;
61 is(blessed($foo), 'Parent', 'Parent->new gives a Parent object');
62 is($foo->name, undef, 'No name yet');
63 is($foo->lazy_classname, 'Parent', "lazy attribute initialized");
64 lives_ok { $foo->type_constrained(10.5) } "Num type constraint for now..";
66 # try to rebless, except it will fail due to Child's stricter type constraint
67 throws_ok { $foo->meta->rebless_instance($foo => 'Child') } qr/^Attribute \(type_constrained\) does not pass the type constraint \(Int\) with '10\.5'/;
68 throws_ok { $bar->meta->rebless_instance($bar => 'Child') } qr/^Attribute \(type_constrained\) does not pass the type constraint \(Int\) with '5\.5'/;
70 $foo->type_constrained(10);
71 $bar->type_constrained(5);
73 $foo->meta->rebless_instance($foo => 'Child');
74 $bar->meta->rebless_instance($bar => 'Child');
76 is(blessed($foo), 'Child', 'successfully reblessed into Child');
77 is($foo->name, 'Junior', "Child->name's default came through");
79 is($foo->lazy_classname, 'Parent', "lazy attribute was already initialized");
80 is($bar->lazy_classname, 'Child', "lazy attribute just now initialized");
82 throws_ok { $foo->type_constrained(10.5) } qr/^Attribute \(type_constrained\) does not pass the type constraint \(Int\) with 10\.5 /;