Commit | Line | Data |
0882828e |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More no_plan => 1; |
7 | use Test::Exception; |
8 | |
9 | BEGIN { |
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 | |
38 | my $Foo = Foo->meta; |
39 | |
40 | my $foo = sub { 'Foo::foo' }; |
41 | |
42 | lives_ok { |
43 | $Foo->add_method('foo' => $foo); |
44 | } '... we added the method successfully'; |
45 | |
46 | ok($Foo->has_method('foo'), '... Foo->has_method(foo) (defined with Sub::Name)'); |
47 | ok(!$Foo->has_method('blessed'), '... !Foo->has_method(blessed) (imported into Foo)'); |
48 | ok($Foo->has_method('bar'), '... Foo->has_method(bar) (defined in Foo)'); |
60d90bbc |
49 | ok($Foo->has_method('baz'), '... Foo->has_method(baz) (typeglob aliased within Foo)'); |
50 | ok($Foo->has_method('blah'), '... Foo->has_method(blah) (defined in main:: using fully qualified package name)'); |
51 | ok(!$Foo->has_method('bling'), '... !Foo->has_method(bling) (defined in main:: using symbol tables (no Sub::Name))'); |
52 | ok($Foo->has_method('bang'), '... Foo->has_method(bang) (defined in main:: using symbol tables and Sub::Name)'); |
0882828e |
53 | |
54 | is($Foo->get_method('foo'), $foo, '... Foo->get_method(foo) == \&foo'); |
55 | |
60d90bbc |
56 | is(Foo->foo(), 'Foo::foo', '... Foo->foo() returns "Foo::foo"'); |
57 | is(Foo->bar(), 'Foo::bar', '... Foo->bar() returns "Foo::bar"'); |
58 | is(Foo->baz(), 'Foo::bar', '... Foo->baz() returns "Foo::bar" (because it is aliased to &bar)'); |