method stuff worked out
[gitmo/Class-MOP.git] / t / 003_methods.t
CommitLineData
0882828e 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More no_plan => 1;
7use Test::Exception;
8
9BEGIN {
10 use_ok('Class::MOP');
11 use_ok('Class::MOP::Class');
12}
13
14{
15 package Foo;
16
17 # import a sub
18 use Scalar::Util 'blessed';
19
20 # define a sub in package
21 sub bar { 'Foo::bar' }
60d90bbc 22 *baz = \&bar;
23
24 # We hateses the "used only once" warnings
25 { my $temp = \&Foo::baz }
26
27 package main;
28
29 sub Foo::blah { $_[0]->Foo::baz() }
30
31 {
32 no strict 'refs';
33 *{'Foo::bling'} = sub { '$$Bling$$' };
34 *{'Foo::bang'} = Sub::Name::subname 'Foo::bang' => sub { '!BANG!' };
35 }
0882828e 36}
37
38my $Foo = Foo->meta;
39
40my $foo = sub { 'Foo::foo' };
41
42lives_ok {
43 $Foo->add_method('foo' => $foo);
44} '... we added the method successfully';
45
46ok($Foo->has_method('foo'), '... Foo->has_method(foo) (defined with Sub::Name)');
47ok(!$Foo->has_method('blessed'), '... !Foo->has_method(blessed) (imported into Foo)');
48ok($Foo->has_method('bar'), '... Foo->has_method(bar) (defined in Foo)');
60d90bbc 49ok($Foo->has_method('baz'), '... Foo->has_method(baz) (typeglob aliased within Foo)');
50ok($Foo->has_method('blah'), '... Foo->has_method(blah) (defined in main:: using fully qualified package name)');
51ok(!$Foo->has_method('bling'), '... !Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))');
52ok($Foo->has_method('bang'), '... Foo->has_method(bang) (defined in main:: using symbol tables and Sub::Name)');
0882828e 53
54is($Foo->get_method('foo'), $foo, '... Foo->get_method(foo) == \&foo');
55
60d90bbc 56is(Foo->foo(), 'Foo::foo', '... Foo->foo() returns "Foo::foo"');
57is(Foo->bar(), 'Foo::bar', '... Foo->bar() returns "Foo::bar"');
58is(Foo->baz(), 'Foo::bar', '... Foo->baz() returns "Foo::bar" (because it is aliased to &bar)');