merging the immutable branch into trunk
[gitmo/Class-MOP.git] / t / 006_new_and_clone_metaclasses.t
CommitLineData
2a7575a6 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a740253a 6use Test::More tests => 36;
2a7575a6 7use Test::Exception;
8
9BEGIN {
10 use_ok('Class::MOP');
11}
12
13# make sure the Class::MOP::Class->meta does the right thing
14
15my $meta = Class::MOP::Class->meta();
16isa_ok($meta, 'Class::MOP::Class');
17
c23184fc 18my $new_meta = $meta->new_object('package' => 'Class::MOP::Class');
2a7575a6 19isa_ok($new_meta, 'Class::MOP::Class');
20is($new_meta, $meta, '... it still creates the singleton');
21
22my $cloned_meta = $meta->clone_object($meta);
23isa_ok($cloned_meta, 'Class::MOP::Class');
24is($cloned_meta, $meta, '... it creates the singleton even if you try to clone it');
25
26# make sure other metaclasses do the right thing
27
28{
29 package Foo;
30 use metaclass;
31}
32
33my $foo_meta = Foo->meta;
34isa_ok($foo_meta, 'Class::MOP::Class');
35
c23184fc 36is($meta->new_object('package' => 'Foo'), $foo_meta, '... got the right Foo->meta singleton');
2a7575a6 37is($meta->clone_object($foo_meta), $foo_meta, '... cloning got the right Foo->meta singleton');
38
39# make sure subclassed of Class::MOP::Class do the right thing
40
41{
42 package MyMetaClass;
43 use base 'Class::MOP::Class';
44}
45
46my $my_meta = MyMetaClass->meta;
47isa_ok($my_meta, 'Class::MOP::Class');
48
c23184fc 49my $new_my_meta = $my_meta->new_object('package' => 'MyMetaClass');
2a7575a6 50isa_ok($new_my_meta, 'Class::MOP::Class');
51is($new_my_meta, $my_meta, '... even subclasses still create the singleton');
52
53my $cloned_my_meta = $meta->clone_object($my_meta);
54isa_ok($cloned_my_meta, 'Class::MOP::Class');
55is($cloned_my_meta, $my_meta, '... and subclasses creates the singleton even if you try to clone it');
56
c23184fc 57is($my_meta->new_object('package' => 'Foo'), $foo_meta, '... got the right Foo->meta singleton (w/subclass)');
2a7575a6 58is($meta->clone_object($foo_meta), $foo_meta, '... cloning got the right Foo->meta singleton (w/subclass)');
59
60# now create a metaclass for real
61
c23184fc 62my $bar_meta = $my_meta->new_object('package' => 'Bar');
2a7575a6 63isa_ok($bar_meta, 'Class::MOP::Class');
64
65is($bar_meta->name, 'Bar', '... got the right name for the Bar metaclass');
66is($bar_meta->version, undef, '... Bar does not exists, so it has no version');
67
68$bar_meta->superclasses('Foo');
69
70# check with MyMetaClass
71
72{
73 package Baz;
74 use metaclass 'MyMetaClass';
75}
76
77my $baz_meta = Baz->meta;
78isa_ok($baz_meta, 'Class::MOP::Class');
79isa_ok($baz_meta, 'MyMetaClass');
80
c23184fc 81is($my_meta->new_object('package' => 'Baz'), $baz_meta, '... got the right Baz->meta singleton');
2a7575a6 82is($my_meta->clone_object($baz_meta), $baz_meta, '... cloning got the right Baz->meta singleton');
83
526ba574 84$baz_meta->superclasses('Bar');
85
2a7575a6 86# now create a regular objects for real
87
88my $foo = $foo_meta->new_object();
89isa_ok($foo, 'Foo');
90
91my $bar = $bar_meta->new_object();
92isa_ok($bar, 'Bar');
93isa_ok($bar, 'Foo');
94
5659d76e 95my $baz = $baz_meta->new_object();
96isa_ok($baz, 'Baz');
97isa_ok($baz, 'Bar');
98isa_ok($baz, 'Foo');
99
2a7575a6 100my $cloned_foo = $foo_meta->clone_object($foo);
101isa_ok($cloned_foo, 'Foo');
102
103isnt($cloned_foo, $foo, '... $cloned_foo is a new object different from $foo');
104
105# check some errors
106
107dies_ok {
108 $foo_meta->clone_object($meta);
109} '... this dies as expected';
110
a740253a 111# test stuff
112
113{
114 package FooBar;
115 use metaclass;
116
117 FooBar->meta->add_attribute('test');
118}
119
120my $attr = FooBar->meta->get_attribute('test');
121isa_ok($attr, 'Class::MOP::Attribute');
122
123my $attr_clone = $attr->clone();
124isa_ok($attr_clone, 'Class::MOP::Attribute');
125
126isnt($attr, $attr_clone, '... we successfully cloned our attributes');
127is($attr->associated_class,
128 $attr_clone->associated_class,
129 '... we successfully did not clone our associated metaclass');
130