On the advice of autarch, force reblessing to target only subclasses. We can always...
[gitmo/Class-MOP.git] / t / 046-rebless.t
CommitLineData
3d9e4646 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
9b71b643 6use Test::More tests => 27;
3d9e4646 7use Test::Exception;
8use Scalar::Util 'blessed';
9
10{
11 package Parent;
12 use metaclass;
13
14 sub new { bless {} => shift }
15 sub whoami { "parent" }
16 sub parent { "parent" }
17
18 package Child;
19 use metaclass;
20 use base qw/Parent/;
21
22 sub whoami { "child" }
23 sub child { "child" }
24
25 package LeftField;
26 use metaclass;
27
28 sub new { bless {} => shift }
29 sub whoami { "leftfield" }
30 sub myhax { "areleet" }
31}
32
33# basic tests
34my $foo = Parent->new;
35is(blessed($foo), 'Parent', 'Parent->new gives a Parent');
36is($foo->whoami, "parent", 'Parent->whoami gives parent');
37is($foo->parent, "parent", 'Parent->parent gives parent');
38dies_ok { $foo->child } "Parent->child method doesn't exist";
39
40$foo->meta->rebless_instance($foo, "Child");
41is(blessed($foo), 'Child', 'rebless_instance really reblessed the instance');
42is($foo->whoami, "child", 'reblessed->whoami gives child');
43is($foo->parent, "parent", 'reblessed->parent gives parent');
44is($foo->child, "child", 'reblessed->child gives child');
45
9b71b643 46throws_ok { $foo->meta->rebless_instance($foo, "LeftField") } qr/You may rebless only into a subclass. \(LeftField\) is not a subclass of \(Child\)\./;
47throws_ok { $foo->meta->rebless_instance($foo, "NonExistent") } qr/You may rebless only into a subclass. \(NonExistent\) is not a subclass of \(Child\)\./;
3d9e4646 48
49# make sure our ->meta is still sane
50my $bar = Parent->new;
51is(blessed($bar), 'Parent', "sanity check");
52is(blessed($bar->meta), 'Class::MOP::Class', "meta gives a Class::MOP::Class");
53is($bar->meta->name, 'Parent', "this Class::MOP::Class instance is for Parent");
54
55ok($bar->meta->has_method('new'), 'metaclass has "new" method');
56ok($bar->meta->has_method('whoami'), 'metaclass has "whoami" method');
57ok($bar->meta->has_method('parent'), 'metaclass has "parent" method');
58
59is(blessed($bar->meta->new_object), 'Parent', 'new_object gives a Parent');
60
61$bar->meta->rebless_instance($bar, "Child");
62is(blessed($bar), 'Child', "rebless really reblessed");
63is(blessed($bar->meta), 'Class::MOP::Class', "meta gives a Class::MOP::Class");
64is($bar->meta->name, 'Child', "this Class::MOP::Class instance is for Child");
65
66ok($bar->meta->find_method_by_name('new'), 'metaclass has "new" method');
67ok($bar->meta->find_method_by_name('parent'), 'metaclass has "parent" method');
68ok(!$bar->meta->has_method('new'), 'no "new" method in this class');
69ok(!$bar->meta->has_method('parent'), 'no "parent" method in this class');
70ok($bar->meta->has_method('whoami'), 'metaclass has "whoami" method');
71ok($bar->meta->has_method('child'), 'metaclass has "child" method');
72
73is(blessed($bar->meta->new_object), 'Child', 'new_object gives a Child');
74