Part 1 of the great clone plan.
[gitmo/Class-MOP.git] / t / 030_method.t
CommitLineData
cbd9f942 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
2226a8b0 6use Test::More tests => 39;
cbd9f942 7use Test::Exception;
8
da88f307 9use Class::MOP;
10use Class::MOP::Method;
11
cbd9f942 12
b38f3848 13my $method = Class::MOP::Method->wrap(
14 sub { 1 },
15 package_name => 'main',
16 name => '__ANON__',
17);
de19f115 18is($method->meta, Class::MOP::Method->meta, '... instance and class both lead to the same meta');
19
20is($method->package_name, 'main', '... our package is main::');
21is($method->name, '__ANON__', '... our sub name is __ANON__');
d41e86f2 22is($method->fully_qualified_name, 'main::__ANON__', '... our subs full name is main::__ANON__');
2226a8b0 23is($method->original_method, undef, '... no original_method ');
24is($method->original_package_name, 'main', '... the original_package_name is the same as package_name');
25is($method->original_name, '__ANON__', '... the original_name is the same as name');
26is($method->original_fully_qualified_name, 'main::__ANON__', '... the original_fully_qualified_name is the same as fully_qualified_name');
22286063 27
bf3db0fb 28dies_ok { Class::MOP::Method->wrap } q{... can't call wrap() without some code};
29dies_ok { Class::MOP::Method->wrap([]) } q{... can't call wrap() without some code};
30dies_ok { Class::MOP::Method->wrap(bless {} => 'Fail') } q{... can't call wrap() without some code};
8048fe76 31
da88f307 32dies_ok { Class::MOP::Method->name } q{... can't call name() as a class method};
33dies_ok { Class::MOP::Method->body } q{... can't call body() as a class method};
34dies_ok { Class::MOP::Method->package_name } q{... can't call package_name() as a class method};
35dies_ok { Class::MOP::Method->fully_qualified_name } q{... can't call fully_qualified_name() as a class method};
8048fe76 36
cbd9f942 37my $meta = Class::MOP::Method->meta;
38isa_ok($meta, 'Class::MOP::Class');
39
de19f115 40foreach my $method_name (qw(
a4258ffd 41 wrap
de19f115 42 package_name
43 name
44 )) {
45 ok($meta->has_method($method_name), '... Class::MOP::Method->has_method(' . $method_name . ')');
46 my $method = $meta->get_method($method_name);
47 is($method->package_name, 'Class::MOP::Method', '... our package is Class::MOP::Method');
48 is($method->name, $method_name, '... our sub name is "' . $method_name . '"');
cbd9f942 49}
50
51dies_ok {
a4258ffd 52 Class::MOP::Method->wrap()
cbd9f942 53} '... bad args for &wrap';
54
55dies_ok {
a4258ffd 56 Class::MOP::Method->wrap('Fail')
cbd9f942 57} '... bad args for &wrap';
58
59dies_ok {
a4258ffd 60 Class::MOP::Method->wrap([])
b38f3848 61} '... bad args for &wrap';
62
63dies_ok {
64 Class::MOP::Method->wrap(sub { 'FAIL' })
65} '... bad args for &wrap';
66
67dies_ok {
68 Class::MOP::Method->wrap(sub { 'FAIL' }, package_name => 'main')
69} '... bad args for &wrap';
70
71dies_ok {
72 Class::MOP::Method->wrap(sub { 'FAIL' }, name => '__ANON__')
73} '... bad args for &wrap';
74
2226a8b0 75my $clone = $method->clone(
76 package_name => 'NewPackage',
77 name => 'new_name',
78);
b38f3848 79
2226a8b0 80isa_ok($clone, 'Class::MOP::Method');
81is($clone->package_name, 'NewPackage', '... cloned method has new pckage name');
82is($clone->name, 'new_name', '... cloned method has new sub name');
83is($clone->fully_qualified_name, 'NewPackage::new_name', '... cloned method has new fq name');
84is($clone->original_method, $method, '... cloned method has correct original_method');
85is($clone->original_package_name, 'main', '... cloned method has correct original_package_name');
86is($clone->original_name, '__ANON__', '... cloned method has correct original_name');
87is($clone->original_fully_qualified_name, 'main::__ANON__', '... cloned method has correct original_fully_qualified_name');