Merge branch 'stable'
[gitmo/Class-MOP.git] / t / 030_method.t
CommitLineData
cbd9f942 1use strict;
2use warnings;
3
86a4d873 4use Test::More;
871e9eb5 5use Test::Fatal;
cbd9f942 6
da88f307 7use Class::MOP;
8use Class::MOP::Method;
9
b38f3848 10my $method = Class::MOP::Method->wrap(
49ca2e97 11 sub {1},
b38f3848 12 package_name => 'main',
13 name => '__ANON__',
14);
49ca2e97 15is( $method->meta, Class::MOP::Method->meta,
16 '... instance and class both lead to the same meta' );
17
18is( $method->package_name, 'main', '... our package is main::' );
19is( $method->name, '__ANON__', '... our sub name is __ANON__' );
20is( $method->fully_qualified_name, 'main::__ANON__',
21 '... our subs full name is main::__ANON__' );
22is( $method->original_method, undef, '... no original_method ' );
23is( $method->original_package_name, 'main',
24 '... the original_package_name is the same as package_name' );
25is( $method->original_name, '__ANON__',
26 '... the original_name is the same as name' );
27is( $method->original_fully_qualified_name, 'main::__ANON__',
28 '... the original_fully_qualified_name is the same as fully_qualified_name'
29);
8048fe76 30
871e9eb5 31isnt( exception { Class::MOP::Method->wrap }, undef, q{... can't call wrap() without some code} );
32isnt( exception { Class::MOP::Method->wrap( [] ) }, undef, q{... can't call wrap() without some code} );
33isnt( exception { Class::MOP::Method->wrap( bless {} => 'Fail' ) }, undef, q{... can't call wrap() without some code} );
34
35isnt( exception { Class::MOP::Method->name }, undef, q{... can't call name() as a class method} );
36isnt( exception { Class::MOP::Method->body }, undef, q{... can't call body() as a class method} );
37isnt( exception { Class::MOP::Method->package_name }, undef, q{... can't call package_name() as a class method} );
38isnt( exception { Class::MOP::Method->fully_qualified_name }, undef, q{... can't call fully_qualified_name() as a class method} );
8048fe76 39
cbd9f942 40my $meta = Class::MOP::Method->meta;
49ca2e97 41isa_ok( $meta, 'Class::MOP::Class' );
cbd9f942 42
49ca2e97 43foreach my $method_name (
44 qw(
a4258ffd 45 wrap
49ca2e97 46 package_name
47 name
48 )
49 ) {
50 ok( $meta->has_method($method_name),
51 '... Class::MOP::Method->has_method(' . $method_name . ')' );
52 my $method = $meta->get_method($method_name);
53 is( $method->package_name, 'Class::MOP::Method',
54 '... our package is Class::MOP::Method' );
55 is( $method->name, $method_name,
56 '... our sub name is "' . $method_name . '"' );
cbd9f942 57}
58
871e9eb5 59isnt( exception {
49ca2e97 60 Class::MOP::Method->wrap();
871e9eb5 61}, undef, '... bad args for &wrap' );
cbd9f942 62
871e9eb5 63isnt( exception {
49ca2e97 64 Class::MOP::Method->wrap('Fail');
871e9eb5 65}, undef, '... bad args for &wrap' );
cbd9f942 66
871e9eb5 67isnt( exception {
49ca2e97 68 Class::MOP::Method->wrap( [] );
871e9eb5 69}, undef, '... bad args for &wrap' );
b38f3848 70
871e9eb5 71isnt( exception {
49ca2e97 72 Class::MOP::Method->wrap( sub {'FAIL'} );
871e9eb5 73}, undef, '... bad args for &wrap' );
b38f3848 74
871e9eb5 75isnt( exception {
49ca2e97 76 Class::MOP::Method->wrap( sub {'FAIL'}, package_name => 'main' );
871e9eb5 77}, undef, '... bad args for &wrap' );
b38f3848 78
871e9eb5 79isnt( exception {
49ca2e97 80 Class::MOP::Method->wrap( sub {'FAIL'}, name => '__ANON__' );
871e9eb5 81}, undef, '... bad args for &wrap' );
b38f3848 82
871e9eb5 83is( exception {
49ca2e97 84 Class::MOP::Method->wrap( bless( sub {'FAIL'}, "Foo" ),
85 name => '__ANON__', package_name => 'Foo::Bar' );
871e9eb5 86}, undef, '... blessed coderef to &wrap' );
3ea3a033 87
2226a8b0 88my $clone = $method->clone(
89 package_name => 'NewPackage',
90 name => 'new_name',
91);
b38f3848 92
49ca2e97 93isa_ok( $clone, 'Class::MOP::Method' );
94is( $clone->package_name, 'NewPackage',
95 '... cloned method has new package name' );
96is( $clone->name, 'new_name', '... cloned method has new sub name' );
97is( $clone->fully_qualified_name, 'NewPackage::new_name',
98 '... cloned method has new fq name' );
99is( $clone->original_method, $method,
100 '... cloned method has correct original_method' );
101is( $clone->original_package_name, 'main',
102 '... cloned method has correct original_package_name' );
103is( $clone->original_name, '__ANON__',
104 '... cloned method has correct original_name' );
105is( $clone->original_fully_qualified_name, 'main::__ANON__',
106 '... cloned method has correct original_fully_qualified_name' );
55228454 107
108my $clone2 = $clone->clone(
109 package_name => 'NewerPackage',
110 name => 'newer_name',
111);
112
49ca2e97 113is( $clone2->package_name, 'NewerPackage',
114 '... clone of clone has new package name' );
115is( $clone2->name, 'newer_name', '... clone of clone has new sub name' );
116is( $clone2->fully_qualified_name, 'NewerPackage::newer_name',
117 '... clone of clone new fq name' );
118is( $clone2->original_method, $clone,
119 '... cloned method has correct original_method' );
120is( $clone2->original_package_name, 'main',
121 '... original_package_name follows clone chain' );
122is( $clone2->original_name, '__ANON__',
123 '... original_name follows clone chain' );
124is( $clone2->original_fully_qualified_name, 'main::__ANON__',
125 '... original_fully_qualified_name follows clone chain' );
e97359e7 126
127Class::MOP::Class->create(
128 'Method::Subclass',
129 superclasses => ['Class::MOP::Method'],
130 attributes => [
131 Class::MOP::Attribute->new(
132 foo => (
133 accessor => 'foo',
134 )
135 ),
136 ],
137);
138
139my $wrapped = Method::Subclass->wrap($method, foo => 'bar');
140isa_ok($wrapped, 'Method::Subclass');
141isa_ok($wrapped, 'Class::MOP::Method');
142is($wrapped->foo, 'bar', 'attribute set properly');
143is($wrapped->package_name, 'main', 'package_name copied properly');
144is($wrapped->name, '__ANON__', 'method name copied properly');
145
146my $wrapped2 = Method::Subclass->wrap($method, foo => 'baz', name => 'FOO');
147is($wrapped2->name, 'FOO', 'got a new method name');
86a4d873 148
149done_testing;