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