From: Shawn M Moore Date: Sun, 13 Jan 2008 00:06:54 +0000 (+0000) Subject: Add tests to make sure the new reblessing works with Moosey bits X-Git-Tag: 0_35~25 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=af3c1f96eba8c73587e278f83552241b1f2eb53c;p=gitmo%2FMoose.git Add tests to make sure the new reblessing works with Moosey bits --- diff --git a/t/010_basics/012_rebless.t b/t/010_basics/012_rebless.t new file mode 100644 index 0000000..8b61297 --- /dev/null +++ b/t/010_basics/012_rebless.t @@ -0,0 +1,82 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 13; +use Test::Exception; +use Scalar::Util 'blessed'; + +BEGIN { + use_ok('Moose'); + use_ok("Moose::Util::TypeConstraints"); +} + +subtype 'Positive' + => as 'Num' + => where { $_ > 0 }; + +{ + package Parent; + use Moose; + + has name => ( + is => 'rw', + isa => 'Str', + ); + + has lazy_classname => ( + is => 'ro', + lazy => 1, + default => sub { "Parent" }, + ); + + has type_constrained => ( + is => 'rw', + isa => 'Num', + default => 5.5, + ); + + package Child; + use Moose; + extends 'Parent'; + + has '+name' => ( + default => 'Junior', + ); + + has '+lazy_classname' => ( + default => sub { "Child" }, + ); + + has '+type_constrained' => ( + isa => 'Int', + default => 100, + ); +} + +my $foo = Parent->new; +my $bar = Parent->new; + +is(blessed($foo), 'Parent', 'Parent->new gives a Parent object'); +is($foo->name, undef, 'No name yet'); +is($foo->lazy_classname, 'Parent', "lazy attribute initialized"); +lives_ok { $foo->type_constrained(10.5) } "Num type constraint for now.."; + +# try to rebless, except it will fail due to Child's stricter type constraint +throws_ok { $foo->meta->rebless_instance($foo => 'Child') } qr/^Attribute \(type_constrained\) does not pass the type constraint \(Int\) with '10\.5'/; +throws_ok { $bar->meta->rebless_instance($bar => 'Child') } qr/^Attribute \(type_constrained\) does not pass the type constraint \(Int\) with '5\.5'/; + +$foo->type_constrained(10); +$bar->type_constrained(5); + +$foo->meta->rebless_instance($foo => 'Child'); +$bar->meta->rebless_instance($bar => 'Child'); + +is(blessed($foo), 'Child', 'successfully reblessed into Child'); +is($foo->name, 'Junior', "Child->name's default came through"); + +is($foo->lazy_classname, 'Parent', "lazy attribute was already initialized"); +is($bar->lazy_classname, 'Child', "lazy attribute just now initialized"); + +throws_ok { $foo->type_constrained(10.5) } qr/^Attribute \(type_constrained\) does not pass the type constraint \(Int\) with 10\.5 /;