3
# $Id: meth.t 2404 2005-12-17 03:40:23Z theory $
5
##############################################################################
7
##############################################################################
10
use Test::More tests => 54;
12
##############################################################################
13
# Create a simple class.
14
##############################################################################
16
package Class::Meta::TestPerson;
19
# Make sure we can load Class::Meta.
20
BEGIN { main::use_ok( 'Class::Meta' ) }
23
# Import Test::More functions into this package.
26
# Create a new Class::Meta object.
27
ok( my $c = Class::Meta->new(key => 'person',
28
package => __PACKAGE__),
31
isa_ok($c, 'Class::Meta');
33
# Create a new method with all of the parameters set.
34
sub foo_meth { 'foo' }
35
ok( my $meth = $c->add_method(
37
desc => 'The foo method',
38
label => 'Foo method',
39
context => Class::Meta::CLASS,
40
view => Class::Meta::PUBLIC
41
), 'Create foo_meth' );
43
isa_ok($meth, 'Class::Meta::Method');
46
is( $meth->name, "foo_meth", "Check foo_meth name" );
47
is( $meth->desc, "The foo method", "Check foo_meth desc" );
48
is( $meth->label, "Foo method", "Check foo_meth label" );
49
ok( $meth->view == Class::Meta::PUBLIC, "Check foo_meth view" );
50
ok( $meth->context == Class::Meta::CLASS, "Check foo_meth context" );
51
is ($meth->call(__PACKAGE__), 'foo', 'Call the foo_meth method' );
53
# Okay, now test to make sure that an attempt to create a method directly
55
eval { my $meth = Class::Meta::Method->new };
56
ok( my $err = $@, "Get method construction exception");
57
like( $err, qr/Package 'Class::Meta::TestPerson' cannot create/,
58
"Caught proper exception");
60
# Now try it without a name.
61
eval{ $c->add_method() };
62
ok( $err = $@, "Caught no name exception");
63
like( $err, qr/Parameter 'name' is required in call to new/,
64
"Caught proper no name exception");
66
# Try a duplicately-named method.
67
eval{ $c->add_method(name => 'foo_meth') };
68
ok( $err = $@, "Caught dupe name exception");
69
like( $err, qr/Method 'foo_meth' already exists in class/,
70
"Caught proper dupe name exception");
72
# Try a of bogus visibility.
73
eval { $c->add_method( name => 'new_meth',
75
ok( $err = $@, "Caught another bogus view exception");
76
like( $err, qr/Not a valid view parameter: '10'/,
77
"Caught another proper bogus view exception");
79
# Try a of bogus context.
80
eval { $c->add_method( name => 'new_meth',
82
ok( $err = $@, "Caught another bogus context exception");
83
like( $err, qr/Not a valid context parameter: '10'/,
84
"Caught another proper bogus context exception");
87
eval { $c->add_method( name => 'new_meth',
89
ok( $err = $@, "Caught bogus caller exception");
90
like( $err, qr/Parameter caller must be a code reference/,
91
"Caught proper bogus caller exception");
93
# Now test all of the defaults.
95
ok( $meth = $c->add_method( name => 'new_meth' ), "Create 'new_meth'" );
96
isa_ok($meth, 'Class::Meta::Method');
99
is( $meth->name, "new_meth", "Check new_meth name" );
100
ok( ! defined $meth->desc, "Check new_meth desc" );
101
ok( ! defined $meth->label, "Check new_meth label" );
102
ok( $meth->view == Class::Meta::PUBLIC, "Check new_meth view" );
103
ok( $meth->context == Class::Meta::OBJECT, "Check new_meth context" );
104
is( $meth->call(__PACKAGE__), '22', 'Call the new_meth method' );
106
# Now install a method.
107
ok( $meth = $c->add_method(
109
code => sub { return 'implicitly' },
110
), 'Define a method');
111
isa_ok($meth, 'Class::Meta::Method');
113
ok( $c->build, 'Build the class' );
114
can_ok( __PACKAGE__, 'implicit' );
115
is( __PACKAGE__->implicit, 'implicitly',
116
'It should be the method we installed' );
117
is( $meth->call(__PACKAGE__), 'implicitly',
118
'and we should be able to call it indirectly' );
121
# Now try subclassing Class::Meta.
123
package Class::Meta::SubClass;
124
use base 'Class::Meta';
126
Class::Meta::Method->new( shift->SUPER::class, @_);
129
package Class::Meta::AnotherTest;
133
# Import Test::More functions into this package.
136
# Create a new Class::Meta object.
137
ok( my $c = Class::Meta::SubClass->new(
139
package => __PACKAGE__
140
), "Create subclassed CM object" );
142
isa_ok($c, 'Class::Meta');
143
isa_ok($c, 'Class::Meta::SubClass');
145
ok( my $meth = $c->add_method( name => 'foo_meth'),
146
'Create subclassed foo_meth' );
148
isa_ok($meth, 'Class::Meta::Method');
150
# Test its accessors.
151
is( $meth->name, "foo_meth", "Check new foo_meth name" );
152
ok( ! defined $meth->desc, "Check new foo_meth desc" );
153
ok( ! defined $meth->label, "Check new foo_meth label" );
154
ok( $meth->view == Class::Meta::PUBLIC, "Check new foo_meth view" );
155
ok( $meth->context == Class::Meta::OBJECT, "Check new foo_meth context" );
156
is( $meth->call(__PACKAGE__), '100', 'Call the new foo_meth method' );
159
##############################################################################
160
# Now try subclassing Class::Meta::Method.
161
package Class::Meta::Method::Sub;
162
use base 'Class::Meta::Method';
164
# Make sure we can override new and build.
165
sub new { shift->SUPER::new(@_) }
166
sub build { shift->SUPER::build(@_) }
168
sub foo { shift->{foo} }
171
ok( my $cm = Class::Meta->new( method_class => 'Class::Meta::Method::Sub'),
173
ok( my $meth = $cm->add_method(name => 'foo', foo => 'bar'),
175
isa_ok($meth, 'Class::Meta::Method::Sub');
176
isa_ok($meth, 'Class::Meta::Method');
177
is( $meth->name, 'foo', "Check an attibute");
178
is( $meth->foo, 'bar', "Check added attibute");