From: Dave Rolsky Date: Tue, 30 Jun 2009 19:40:56 +0000 (-0500) Subject: Hacking on making immutable a real(ish) trait so it doesn't show up in X-Git-Tag: 0.89~14 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=73d6ccc20ff64d64e105d0c109fc642a560750c6;p=gitmo%2FClass-MOP.git Hacking on making immutable a real(ish) trait so it doesn't show up in the inheritance hierarchy at all. --- diff --git a/t/074_immutable_custom_trait.t b/t/074_immutable_custom_trait.t new file mode 100644 index 0000000..f6c8d2d --- /dev/null +++ b/t/074_immutable_custom_trait.t @@ -0,0 +1,76 @@ +use strict; +use warnings; + +use Test::More 'no_plan'; +use Test::Exception; + +use Class::MOP; + +{ + + package My::Meta; + + use strict; + use warnings; + use metaclass; + + use base 'Class::MOP::Class'; + + sub initialize { + shift->SUPER::initialize( + @_, + immutable_trait => 'My::Meta::Class::Immutable::Trait', + ); + } +} + +{ + package My::Meta::Class::Immutable::Trait; + + use MRO::Compat; + use base 'Class::MOP::Class::Immutable::Trait'; + + sub another_method { 42 } + + sub superclasses { + my $orig = shift; + my $self = shift; + $self->$orig(@_); + } +} + +{ + package Foo; + + use strict; + use warnings; + use metaclass; + + __PACKAGE__->meta->add_attribute('foo'); + + __PACKAGE__->meta->make_immutable; +} + +{ + package Bar; + + use strict; + use warnings; + use metaclass 'My::Meta'; + + use base 'Foo'; + + __PACKAGE__->meta->add_attribute('bar'); + + ::lives_ok { __PACKAGE__->meta->make_immutable } + 'can safely make a class immutable when it has a custom metaclass and immutable trait'; +} + +{ + can_ok( Bar->meta, 'another_method' ); + is( Bar->meta->another_method, 42, 'another_method returns expected value' ); + is_deeply( + [ Bar->meta->superclasses ], ['Foo'], + 'Bar->meta->superclasses returns expected value after immutabilization' + ); +}