docs for CMOP::Instance
[gitmo/Class-MOP.git] / t / 073_make_mutable.t
index bc83ae6..1ad0c96 100644 (file)
@@ -1,16 +1,12 @@
-#!/usr/bin/perl
-
 use strict;
 use warnings;
 
-use Test::More tests => 104;
+use Test::More tests => 114;
 use Test::Exception;
 
 use Scalar::Util;
 
-BEGIN {
-    use_ok('Class::MOP');
-}
+use Class::MOP;
 
 {
     package Foo;
@@ -45,26 +41,40 @@ BEGIN {
 {
     my $meta = Baz->meta;
     is($meta->name, 'Baz', '... checking the Baz metaclass');
-    my @orig_keys = sort keys %$meta;
+    my %orig_keys = map { $_ => 1 } grep { !/^_/ } keys %$meta;
+    # Since this has no default it won't be present yet, but it will
+    # be after the class is made immutable.
+    $orig_keys{immutable_transformer} = 1;
 
     lives_ok {$meta->make_immutable; } '... changed Baz to be immutable';
     ok(!$meta->is_mutable,              '... our class is no longer mutable');
     ok($meta->is_immutable,             '... our class is now immutable');
     ok(!$meta->make_immutable,          '... make immutable now returns nothing');
+    ok($meta->get_method_map->{new},    '... inlined constructor created');
+    ok($meta->has_method('new'),        '... inlined constructor created for sure');    
+    ok($meta->immutable_transformer->inlined_constructor,
+       '... transformer says it did inline the constructor');
 
     lives_ok { $meta->make_mutable; }  '... changed Baz to be mutable';
     ok($meta->is_mutable,               '... our class is mutable');
     ok(!$meta->is_immutable,            '... our class is not immutable');
     ok(!$meta->make_mutable,            '... make mutable now returns nothing');
+    ok(!$meta->get_method_map->{new},   '... inlined constructor removed');
+    ok(!$meta->has_method('new'),        '... inlined constructor removed for sure');    
+    ok(!$meta->immutable_transformer->inlined_constructor,
+       '... transformer says it did not inline the constructor');
 
-    my @new_keys = sort keys %$meta;
-    is_deeply(\@orig_keys, \@new_keys, '... no straneous hashkeys');
+    my %new_keys = map { $_ => 1 } grep { !/^_/ } keys %$meta;
+    is_deeply(\%orig_keys, \%new_keys, '... no extraneous hashkeys');
 
     isa_ok($meta, 'Class::MOP::Class', '... Baz->meta isa Class::MOP::Class');
 
     ok( $meta->add_method('xyz', sub{'xxx'}), '... added method');
     is( Baz->xyz, 'xxx',                      '... method xyz works');
+
+    ok(! $meta->has_method('zxy')             ,'...  we dont have the aliased method yet');    
     ok( $meta->alias_method('zxy',sub{'xxx'}),'... aliased method');
+    ok( $meta->has_method('zxy')             ,'...  the aliased method does register');    
     is( Baz->zxy, 'xxx',                      '... method zxy works');
     ok( $meta->remove_method('xyz'),          '... removed method');
     ok( $meta->remove_method('zxy'),          '... removed aliased method');
@@ -88,6 +98,9 @@ BEGIN {
     ok( $meta->$_  , "... ${_} works")
       for qw(get_meta_instance       compute_all_applicable_attributes
              class_precedence_list  get_method_map );
+
+    lives_ok {$meta->make_immutable; } '... changed Baz to be immutable again';
+    ok($meta->get_method_map->{new},    '... inlined constructor recreated');
 }
 
 {
@@ -122,7 +135,8 @@ BEGIN {
 
     ok(Baz->meta->is_immutable,  'Superclass is immutable');
     my $meta = Baz->meta->create_anon_class(superclasses => ['Baz']);
-    my @orig_keys  = sort keys %$meta;
+    my %orig_keys = map { $_ => 1 } grep { !/^_/ } keys %$meta;
+    $orig_keys{immutable_transformer} = 1;
     my @orig_meths = sort { $a->{name} cmp $b->{name} }
       $meta->compute_all_applicable_methods;
     ok($meta->is_anon_class,                  'We have an anon metaclass');
@@ -146,11 +160,11 @@ BEGIN {
     ok($meta->is_anon_class,          '... still marked as an anon class');
     my $instance = $meta->new_object;
 
-    my @new_keys  = sort keys %$meta;
+    my %new_keys  = map { $_ => 1 } grep { !/^_/ } keys %$meta;
     my @new_meths = sort { $a->{name} cmp $b->{name} }
       $meta->compute_all_applicable_methods;
-    is_deeply(\@orig_keys, \@new_keys, '... no straneous hashkeys');
-    is_deeply(\@orig_meths, \@new_meths, '... no straneous methods');
+    is_deeply(\%orig_keys, \%new_keys, '... no extraneous hashkeys');
+    is_deeply(\@orig_meths, \@new_meths, '... no extraneous methods');
 
     isa_ok($meta, 'Class::MOP::Class', '... Anon class isa Class::MOP::Class');
 
@@ -216,3 +230,12 @@ BEGIN {
       for qw(get_meta_instance       compute_all_applicable_attributes
              class_precedence_list  get_method_map );
 }
+
+{
+    Foo->meta->make_immutable;
+    Bar->meta->make_immutable;
+    Bar->meta->make_mutable;
+
+    isnt( Foo->meta->immutable_transformer, Bar->meta->immutable_transformer,
+          'Foo and Bar should have different immutable transformer objects' );
+}