6 use Test::More tests => 27;
8 use Scalar::Util 'blessed';
14 sub new { bless {} => shift }
15 sub whoami { "parent" }
16 sub parent { "parent" }
22 sub whoami { "child" }
28 sub new { bless {} => shift }
29 sub whoami { "leftfield" }
30 sub myhax { "areleet" }
34 my $foo = Parent->new;
35 is(blessed($foo), 'Parent', 'Parent->new gives a Parent');
36 is($foo->whoami, "parent", 'Parent->whoami gives parent');
37 is($foo->parent, "parent", 'Parent->parent gives parent');
38 dies_ok { $foo->child } "Parent->child method doesn't exist";
40 Child->meta->rebless_instance($foo);
41 is(blessed($foo), 'Child', 'rebless_instance really reblessed the instance');
42 is($foo->whoami, "child", 'reblessed->whoami gives child');
43 is($foo->parent, "parent", 'reblessed->parent gives parent');
44 is($foo->child, "child", 'reblessed->child gives child');
46 throws_ok { LeftField->meta->rebless_instance($foo) }
47 qr/You may rebless only into a subclass of \(Child\), of which \(LeftField\) isn't\./;
49 throws_ok { Class::MOP::Class->initialize("NonExistent")->rebless_instance($foo) }
50 qr/You may rebless only into a subclass of \(Child\), of which \(NonExistent\) isn't\./;
52 # make sure our ->meta is still sane
53 my $bar = Parent->new;
54 is(blessed($bar), 'Parent', "sanity check");
55 is(blessed($bar->meta), 'Class::MOP::Class', "meta gives a Class::MOP::Class");
56 is($bar->meta->name, 'Parent', "this Class::MOP::Class instance is for Parent");
58 ok($bar->meta->has_method('new'), 'metaclass has "new" method');
59 ok($bar->meta->has_method('whoami'), 'metaclass has "whoami" method');
60 ok($bar->meta->has_method('parent'), 'metaclass has "parent" method');
62 is(blessed($bar->meta->new_object), 'Parent', 'new_object gives a Parent');
64 Child->meta->rebless_instance($bar);
65 is(blessed($bar), 'Child', "rebless really reblessed");
66 is(blessed($bar->meta), 'Class::MOP::Class', "meta gives a Class::MOP::Class");
67 is($bar->meta->name, 'Child', "this Class::MOP::Class instance is for Child");
69 ok($bar->meta->find_method_by_name('new'), 'metaclass has "new" method');
70 ok($bar->meta->find_method_by_name('parent'), 'metaclass has "parent" method');
71 ok(!$bar->meta->has_method('new'), 'no "new" method in this class');
72 ok(!$bar->meta->has_method('parent'), 'no "parent" method in this class');
73 ok($bar->meta->has_method('whoami'), 'metaclass has "whoami" method');
74 ok($bar->meta->has_method('child'), 'metaclass has "child" method');
76 is(blessed($bar->meta->new_object), 'Child', 'new_object gives a Child');