From: Jesse Luehrs Date: Thu, 30 Sep 2010 23:49:18 +0000 (-0500) Subject: add a bunch of anon class tests X-Git-Tag: 1.10~12 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=559201bf99d9c677a64df9317032fed6c0ca125f;p=gitmo%2FClass-MOP.git add a bunch of anon class tests --- diff --git a/t/048_anon_class_create_init.t b/t/048_anon_class_create_init.t index 42d4f14..6051f15 100644 --- a/t/048_anon_class_create_init.t +++ b/t/048_anon_class_create_init.t @@ -2,6 +2,7 @@ use strict; use warnings; use Test::More; +use Test::Exception; use Class::MOP; @@ -18,7 +19,92 @@ use Class::MOP; } -my $anon = MyMeta->create_anon_class( foo => 'this' ); -isa_ok( $anon, 'MyMeta' ); +{ + my $anon = MyMeta->create_anon_class( foo => 'this' ); + isa_ok( $anon, 'MyMeta' ); +} + +my $instance; + +{ + my $meta = Class::MOP::Class->create_anon_class; + $instance = $meta->new_object; +} +{ + my $meta = Class::MOP::class_of($instance); + Scalar::Util::weaken($meta); + ok($meta, "anon class is kept alive by existing instances"); + + undef $instance; + ok(!$meta, "anon class is collected once instances go away"); +} + +{ + $instance = Class::MOP::Class->create('Foo')->new_object; + my $meta = Class::MOP::Class->create_anon_class(superclasses => ['Foo']); + $meta->rebless_instance($instance); +} +{ + my $meta = Class::MOP::class_of($instance); + Scalar::Util::weaken($meta); + ok($meta, "anon class is kept alive by existing instances"); + + undef $instance; + ok(!$meta, "anon class is collected once instances go away"); +} + +{ + { + my $meta = Class::MOP::Class->create_anon_class; + { + my $submeta = Class::MOP::Class->create_anon_class( + superclasses => [$meta->name] + ); + $instance = $submeta->new_object; + } + { + my $submeta = Class::MOP::class_of($instance); + Scalar::Util::weaken($submeta); + ok($submeta, "anon class is kept alive by existing instances"); + + $meta->rebless_instance_back($instance); + ok(!$submeta, "reblessing away loses the metaclass"); + } + } + + my $meta = Class::MOP::class_of($instance); + Scalar::Util::weaken($meta); + ok($meta, "anon class is kept alive by existing instances"); +} + +{ + my $submeta = Class::MOP::Class->create_anon_class( + superclasses => [Class::MOP::Class->create_anon_class->name], + ); + my @superclasses = $submeta->superclasses; + { local $TODO = "anon class ancestry isn't preserved yet"; + ok(Class::MOP::class_of($superclasses[0]), + "superclasses are kept alive by their subclasses"); + } +} + +{ + my $meta_name; + { + $meta_name = Class::MOP::Class->create_anon_class( + superclasses => ['Class::MOP::Class'], + weaken => 0, + )->name; + } + local $TODO = "non-weak anon classes not implemented yet"; + ok(Class::MOP::class_of($meta_name), "metaclass still exists"); + { + my $bar_meta; + lives_ok { + $bar_meta = $meta_name->initialize('Bar'); + } "we can use the name on its own"; + isa_ok($bar_meta, $meta_name); + } +} done_testing;