3
# $Id: constraints.t 1492 2005-04-07 19:19:38Z theory $
5
##############################################################################
7
##############################################################################
10
use Test::More tests => 24;
12
##############################################################################
13
# Create a simple class.
14
##############################################################################
16
package Class::Meta::Testing123;
20
main::use_ok('Class::Meta');
21
main::use_ok('Class::Meta::Types::String');
25
# Import Test::More functions into this package.
27
ok( my $cm = Class::Meta->new, "Create new Class::Meta object" );
30
ok( $cm->add_constructor( name => 'new',
34
# Add a required attribute with a default
35
ok( $cm->add_attribute( name => 'req_def',
40
"Add required attribute with a default" );
42
# Add a once attribute.
43
ok( $cm->add_attribute( name => 'once',
47
"Add a once attribute" );
49
# Add a once attribute with a default.
50
ok( $cm->add_attribute( name => 'once_def',
55
"Add a once attribute" );
57
# Add a required once attribute with a default.
58
ok( $cm->add_attribute( name => 'once_req',
64
"Add a required once attribute" );
67
ok( $cm->build, "Build class" );
72
ok( my $obj = Class::Meta::Testing123->new, 'Create new object' );
74
# Check required attribute.
75
is( $obj->req_def, 'hello', 'Check required attribute' );
76
ok( $obj->req_def('foo'), 'Set required attribute' );
77
is( $obj->req_def, 'foo', 'Check required attribute new value' );
78
eval { $obj->req_def(undef) };
79
ok( $@, 'Catch required exception' );
81
# Check once attribute.
82
is( $obj->once, undef, "Once is undefined" );
83
ok( $obj->once('hee'), "set once attribute" );
84
is( $obj->once, 'hee', "Check new once value" );
85
eval { $obj->once('ha') };
86
ok( $@, 'Catch once exception' );
88
# Check once with a default.
89
is( $obj->once_def, 'hola', 'Check once_def' );
90
eval { $obj->once_def('ha') };
91
ok( $@, 'Catch once_def exception' );
92
is( $obj->once_def, 'hola', "Check once_def hasn't changed" );
94
# Check required once with a default.
95
is( $obj->once_req, 'bonjour', 'Check once_req' );
96
eval { $obj->once_req('ha') };
97
ok( $@, 'Catch once_req exception' );
98
is( $obj->once_req, 'bonjour', "Check once_req hasn't changed" );