Constants _should_ be reported as methods.
[gitmo/Class-MOP.git] / t / 003_methods.t
index a176441..53813db 100644 (file)
@@ -1,7 +1,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 67;
+use Test::More;
 use Test::Exception;
 
 use Scalar::Util qw/reftype/;
@@ -207,7 +207,6 @@ is_deeply(
 is( $Foo->remove_method('foo')->body, $foo, '... removed the foo method' );
 ok( !$Foo->has_method('foo'),
     '... !Foo->has_method(foo) we just removed it' );
-ok( !$Foo->get_method_map->{foo}, 'foo is not in the method map' );
 dies_ok { Foo->foo } '... cannot call Foo->foo because it is not there';
 
 is_deeply(
@@ -288,7 +287,8 @@ my $new_method = Bar->meta->get_method('objecty');
 isnt( $method, $new_method,
     'add_method clones method objects as they are added' );
 is( $new_method->original_method, $method,
-    '... the cloned method has the correct original method' );
+    '... the cloned method has the correct original method' )
+        or diag $new_method->dump;
 
 {
     package CustomAccessor;
@@ -319,7 +319,9 @@ is( $new_method->original_method, $method,
         }
     );
 
-    $meta->add_method( 'new', sub { return bless {}, shift } );
+    sub new {
+        return bless {}, shift;
+    }
 }
 
 {
@@ -330,6 +332,50 @@ is( $new_method->original_method, $method,
 
     is(
         $o->{custom_store}, $str,
-        'Custom glob-assignment-created accessor is still method modifier is added'
+        'Custom glob-assignment-created accessor still has method modifier'
     );
 }
+
+{
+    # Since the sub reference below is not a closure, Perl caches it and uses
+    # the same reference each time through the loop. See RT #48985 for the
+    # bug.
+    foreach my $ns ( qw( Foo2 Bar2 Baz2 ) ) {
+        my $meta = Class::MOP::Class->create($ns);
+
+        my $sub = sub { };
+
+        $meta->add_method( 'foo', $sub );
+
+        my $method = $meta->get_method('foo');
+        ok( $method, 'Got the foo method back' );
+    }
+}
+
+{
+    package HasConstants;
+
+    use constant FOO => 1;
+    use constant BAR => [];
+    use constant BAZ => {};
+
+    sub quux  {1}
+    sub thing {1}
+}
+
+my $HC = Class::MOP::Class->initialize('HasConstants');
+
+is_deeply(
+    [ sort $HC->get_method_list ],
+    [qw( BAR BAZ FOO quux thing )],
+    'get_method_list handles constants properly'
+);
+
+is_deeply(
+    [ sort map { $_->name } $HC->_get_local_methods ],
+    [qw( BAR BAZ FOO quux thing )],
+    '_get_local_methods handles constants properly'
+);
+
+
+done_testing;