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 { Child->meta->rebless_instance($foo) }
68 qr/^Attribute \(type_constrained\) does not pass the type constraint because\: Validation failed for 'Int' failed with value 10\.5/,
69 '... this failed cause of type check';
70 throws_ok { Child->meta->rebless_instance($bar) }
71 qr/^Attribute \(type_constrained\) does not pass the type constraint because\: Validation failed for 'Int' failed with value 5\.5/,
72 '... this failed cause of type check';;
74 $foo->type_constrained(10);
75 $bar->type_constrained(5);
77 Child->meta->rebless_instance($foo);
78 Child->meta->rebless_instance($bar);
80 is(blessed($foo), 'Child', 'successfully reblessed into Child');
81 is($foo->name, 'Junior', "Child->name's default came through");
83 is($foo->lazy_classname, 'Parent', "lazy attribute was already initialized");
84 is($bar->lazy_classname, 'Child', "lazy attribute just now initialized");
86 throws_ok { $foo->type_constrained(10.5) }
87 qr/^Attribute \(type_constrained\) does not pass the type constraint because\: Validation failed for 'Int' failed with value 10\.5/,
88 '... this failed cause of type check';