1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
#!/usr/bin/perl
# $Id: ctor.t 2872 2006-05-28 20:02:16Z theory $
##############################################################################
# Set up the tests.
##############################################################################
use strict;
use Test::More tests => 55;
##############################################################################
# Create a simple class.
##############################################################################
package Class::Meta::TestPerson;
use strict;
# Make sure we can load Class::Meta.
BEGIN { main::use_ok( 'Class::Meta' ) }
BEGIN {
# Import Test::More functions into this package.
Test::More->import;
# Create a new Class::Meta object.
ok( my $c = Class::Meta->new(package => __PACKAGE__,
key => 'person'),
"Create CM object" );
isa_ok($c, 'Class::Meta');
# Create a constructor.
sub inst { bless {} }
ok( my $ctor = $c->add_constructor( name => 'inst',
desc => 'The inst constructor',
label => 'inst Constructor',
create => 0,
view => Class::Meta::PUBLIC ),
"Create 'inst' ctor");
isa_ok($ctor, 'Class::Meta::Constructor');
# Test its accessors.
is( $ctor->name, "inst", "Check inst name" );
is( $ctor->desc, "The inst constructor", "Check inst desc" );
is( $ctor->label, "inst Constructor", "Check inst label" );
ok( $ctor->view == Class::Meta::PUBLIC, "Check inst view" );
isa_ok( $ctor->call(__PACKAGE__), __PACKAGE__);
# Okay, now test to make sure that an attempt to create a constructor
# directly fails.
eval { my $ctor = Class::Meta::Constructor->new };
ok( my $err = $@, "Get constructor construction exception");
like( $err, qr/Package 'Class::Meta::TestPerson' cannot create/,
"Caught proper exception");
# Now try it without a name.
eval{ $c->add_constructor() };
ok( $err = $@, "Caught no name exception");
like( $err, qr/Parameter 'name' is required in call to new/,
"Caught proper no name exception");
# Try a duplicately-named constructor.
eval{ $c->add_constructor(name => 'inst') };
ok( $err = $@, "Caught dupe name exception");
like( $err, qr/Method 'inst' already exists in class/,
"Caught proper dupe name exception");
# Try a couple of bogus visibilities.
eval { $c->add_constructor( name => 'new_ctor',
view => 25) };
ok( $err = $@, "Caught bogus view exception");
like( $err, qr/Not a valid view parameter: '25'/,
"Caught proper bogus view exception");
eval { $c->add_constructor( name => 'new_ctor',
view => 10) };
ok( $err = $@, "Caught another bogus view exception");
like( $err, qr/Not a valid view parameter: '10'/,
"Caught another proper bogus view exception");
# Try a bogus caller.
eval { $c->add_method( name => 'new_inst',
caller => 'foo' ) };
ok( $err = $@, "Caught bogus caller exception");
like( $err, qr/Parameter caller must be a code reference/,
"Caught proper bogus caller exception");
# Now test all of the defaults.
sub new_ctor { 22 }
ok( $ctor = $c->add_constructor( name => 'new_ctor',
create => 0 ), "Create 'new_ctor'" );
isa_ok($ctor, 'Class::Meta::Constructor');
# Test its accessors.
is( $ctor->name, "new_ctor", "Check new_ctor name" );
ok( ! defined $ctor->desc, "Check new_ctor desc" );
ok( ! defined $ctor->label, "Check new_ctor label" );
ok( $ctor->view == Class::Meta::PUBLIC, "Check new_ctor view" );
is ($ctor->call(__PACKAGE__), '22',
'Call the new_ctor constructor indirectly' );
}
# Now try subclassing Class::Meta.
package Class::Meta::SubClass;
use base 'Class::Meta';
sub add_constructor {
Class::Meta::Constructor->new( shift->SUPER::class, @_);
}
package Class::Meta::AnotherTest;
use strict;
BEGIN {
# Import Test::More functions into this package.
Test::More->import;
# Create a new Class::Meta object.
ok( my $c = Class::Meta::SubClass->new
(another => __PACKAGE__), "Create subclassed CM object" );
isa_ok($c, 'Class::Meta');
isa_ok($c, 'Class::Meta::SubClass');
sub foo_ctor { bless {} }
ok( my $ctor = $c->add_constructor( name => 'foo_ctor',
create => 0 ),
'Create subclassed foo_ctor' );
isa_ok($ctor, 'Class::Meta::Constructor');
# Test its accessors.
is( $ctor->name, "foo_ctor", "Check new foo_ctor name" );
ok( ! defined $ctor->desc, "Check new foo_ctor desc" );
ok( ! defined $ctor->label, "Check new foo_ctor label" );
ok( $ctor->view == Class::Meta::PUBLIC, "Check new foo_ctor view" );
isa_ok($ctor->call(__PACKAGE__), __PACKAGE__);
}
##############################################################################
# Now try subclassing Class::Meta::Constructor.
package Class::Meta::Constructor::Sub;
use base 'Class::Meta::Constructor';
# Make sure we can override new and build.
sub new { shift->SUPER::new(@_) }
sub build { shift->SUPER::build(@_) }
sub foo { shift->{foo} }
package main;
ok( my $cm = Class::Meta->new(
constructor_class => 'Class::Meta::Constructor::Sub'
), "Create Class" );
ok( my $ctor = $cm->add_constructor(name => 'foo', foo => 'bar'),
"Add foo constructor" );
isa_ok($ctor, 'Class::Meta::Constructor::Sub');
isa_ok($ctor, 'Class::Meta::Constructor');
is( $ctor->name, 'foo', "Check an attibute");
is( $ctor->foo, 'bar', "Check added attibute");
##############################################################################
# Now try mixing the setting of attributes.
package Try::Mixed::Constructor;
use Class::Meta::Types::Perl;
BEGIN { Test::More->import }
ok $cm = Class::Meta->new, 'Create new Class::Meta object';
ok $cm->add_constructor(name => 'new'), 'Add a constructor';
# Now write our own constructor.
ok(
$ctor = $cm->add_constructor(
name => 'implicit',
code => sub { ok 'Implicit constructor called' },
), 'Implicitly write constructor'
);
ok $cm->add_attribute(
name => 'foo',
type => 'scalar',
), 'Add "foo" attribute';
ok $cm->add_attribute(
name => 'bar',
type => 'scalar',
create => Class::Meta::NONE,
), 'Add "bar" attribute';
sub bar {
my $self = shift;
return $self->{bar} unless @_;
$self->foo(shift);
$self->{bar} = 'set';
}
ok $cm->build, 'Build the new class';
ok my $try = Try::Mixed::Constructor->new(bar => 'hey'),
'Construct an instance of the new class';
is $try->bar, 'set', '"bar" should be "set"';
is $try->foo, 'hey', '"foo" should be "hey"';
# Call implicit constructor and its test.
Try::Mixed::Constructor->implicit;
|