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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
|
#!perl -w
# $Id: view_affordance.t 2384 2005-12-14 04:27:23Z david $
##############################################################################
# Set up the tests.
##############################################################################
use strict;
use Test::More tests => 209;
##############################################################################
# Create a simple class.
##############################################################################
package Class::Meta::Test;
use strict;
BEGIN {
Test::More->import;
use_ok('Class::Meta');
use_ok('Class::Meta::Types::Numeric', 'affordance');
use_ok('Class::Meta::Types::String', 'affordance');
}
BEGIN {
ok( my $c = Class::Meta->new(
key => 'person',
package => __PACKAGE__,
trust => 'Class::Meta::TrustMe',
name => 'Class::Meta::TestPerson Class',
desc => 'Special person class just for testing Class::Meta.',
), "Create Class::Meta object" );
# Add a constructor.
ok( $c->add_constructor( name => 'new',
create => 1 ),
"Add new constructor" );
# Add a couple of attributes with created methods.
ok( $c->add_attribute( name => 'id',
view => Class::Meta::PUBLIC,
type => 'integer',
label => 'ID',
required => 1,
default => 22,
),
"Add id attribute" );
ok( $c->add_attribute( name => 'name',
view => Class::Meta::PROTECTED,
type => 'string',
label => 'Name',
required => 1,
default => '',
),
"Add protected name attribute" );
ok( $c->add_attribute( name => 'age',
view => Class::Meta::PRIVATE,
type => 'integer',
label => 'Age',
desc => "The person's age.",
required => 0,
default => 0,
),
"Add private age attribute" );
ok( $c->add_attribute( name => 'sn',
view => Class::Meta::TRUSTED,
type => 'string',
label => 'SN',
desc => "The person's serial number.",
required => 0,
default => '',
),
"Add trusted sn attribute" );
$c->build;
}
##############################################################################
# From within the package, the private and public attributes should just work.
##############################################################################
ok( my $obj = __PACKAGE__->new, "Create new object" );
ok( my $class = __PACKAGE__->my_class, "Get class object" );
is_deeply( [map { $_->name } $class->attributes], [qw(id name age sn)],
'Call to attributes() should return all attributes' );
# Check id public attribute.
is( $obj->get_id, 22, 'Check default ID' );
ok( $obj->set_id(12), "Set ID" );
is( $obj->get_id, 12, 'Check 12 ID' );
ok( my $attr = $class->attributes('id'), 'Get "id" attribute object' );
is( $attr->get($obj), 12, "Check indirect 12 ID" );
ok( $attr->set($obj, 15), "Indirectly set ID" );
is( $attr->get($obj), 15, "Check indirect 15 ID" );
# Check name protected attribute succeeds.
is( $obj->get_name, '', 'Check empty name' );
ok( $obj->set_name('Larry'), "Set name" );
is( $obj->get_name, 'Larry', 'Check "Larry" name' );
ok( $attr = $class->attributes('name'), 'Get "name" attribute object' );
is( $attr->get($obj), 'Larry', 'Check indirect "Larry" name' );
ok( $attr->set($obj, 'Chip'), "Indirectly set name" );
is( $attr->get($obj), 'Chip', 'Check indirect "chip" name' );
# Check age private attribute succeeds.
is( $obj->get_age, 0, 'Check default age' );
ok( $obj->set_age(42), "Set age" );
is( $obj->get_age, 42, 'Check 42 age' );
ok( $attr = $class->attributes('age'), 'Get "age" attribute object' );
is( $attr->get($obj), 42, "Check indirect 12 age" );
ok( $attr->set($obj, 15), "Indirectly set age" );
is( $attr->get($obj), 15, "Check indirect 15 age" );
# Check sn trusted attribute succeeds.
is( $obj->get_sn, '', 'Check empty sn' );
ok( $obj->set_sn('123456789'), "Set sn" );
is( $obj->get_sn, '123456789', 'Check "123456789" sn' );
ok( $attr = $class->attributes('sn'), 'Get "sn" attribute object' );
is( $attr->get($obj), '123456789', 'Check indirect "123456789" sn' );
ok( $attr->set($obj, '987654321'), "Indirectly set sn" );
is( $attr->get($obj), '987654321', 'Check indirect "987654321" sn' );
# Make sure that we can set all of the attributes via new().
ok( $obj = __PACKAGE__->new( id => 10,
name => 'Damian',
sn => 'au',
age => 35),
"Create another new object" );
is( $obj->get_id, 10, 'Check 10 ID' );
is( $obj->get_name, 'Damian', 'Check Damian name' );
is( $obj->get_age, 35, 'Check 35 age' );
is( $obj->get_sn, 'au', 'Check sn is "au"');
# Do the same with the constructor object.
ok( my $ctor = $class->constructors('new'), 'Get "new" constructor object' );
ok( $obj = $ctor->call(__PACKAGE__,
id => 10,
name => 'Damian',
sn => 'au',
age => 35),
"Create another new object" );
is( $obj->get_id, 10, 'Check 10 ID' );
is( $obj->get_name, 'Damian', 'Check Damian name' );
is( $obj->get_age, 35, 'Check 35 age' );
is( $obj->get_sn, 'au', 'Check sn is "au"');
##############################################################################
# Set up an inherited package.
##############################################################################
package Class::Meta::Testarama;
use strict;
use base 'Class::Meta::Test';
BEGIN {
Test::More->import;
Class::Meta->new(key => 'testarama')->build;
}
ok( $obj = __PACKAGE__->new, "Create new Testarama object" );
ok( $class = __PACKAGE__->my_class, "Get Testarama class object" );
is_deeply( [map { $_->name } $class->attributes], [qw(id name)],
"Call to attributes() should return public and protected attrs" );
# Check id public attribute.
is( $obj->get_id, 22, 'Check default ID' );
ok( $obj->set_id(12), "Set ID" );
is( $obj->get_id, 12, 'Check 12 ID' );
ok( $attr = $class->attributes('id'), 'Get "id" attribute object' );
is( $attr->get($obj), 12, "Check indirect 12 ID" );
ok( $attr->set($obj, 15), "Indirectly set ID" );
is( $attr->get($obj), 15, "Check indirect 15 ID" );
# Check name protected attribute succeeds.
is( $obj->get_name, '', 'Check empty name' );
ok( $obj->set_name('Larry'), "Set name" );
is( $obj->get_name, 'Larry', 'Check Larry name' );
ok( $attr = $class->attributes('name'), 'Get "name" attribute object' );
is( $attr->get($obj), 'Larry', 'Check indirect "Larry" name' );
ok( $attr->set($obj, 'Chip'), "Indirectly set name" );
is( $attr->get($obj), 'Chip', 'Check indirect "chip" name' );
# Check age private attribute
eval { $obj->set_age(12) };
ok( my $err = $@, 'Catch private exception');
like( $err, qr/age is a private attribute of Class::Meta::Test/,
'Correct private exception');
eval { $obj->get_age };
ok( $err = $@, 'Catch another private exception');
like( $err, qr/age is a private attribute of Class::Meta::Test/,
'Correct private exception again');
# Check that age fails when accessed indirectly, too.
ok( $attr = $class->attributes('age'), 'Get "age" attribute object' );
eval { $attr->set($obj, 12) };
ok( $err = $@, 'Catch indirect private exception');
like( $err, qr/age is a private attribute of Class::Meta::Test/,
'Correct indirectprivate exception');
eval { $attr->get($obj) };
ok( $err = $@, 'Catch another indirect private exception');
like( $err, qr/age is a private attribute of Class::Meta::Test/,
'Correct indirect private exception again');
# Check fail sn trusted attribute
eval { $obj->set_sn('foo') };
ok( $err = $@, 'Catch private exception');
like( $err, qr/sn is a trusted attribute of Class::Meta::Test/,
'Correct private exception');
eval { $obj->get_sn };
ok( $err = $@, 'Catch another private exception');
like( $err, qr/sn is a trusted attribute of Class::Meta::Test/,
'Correct private exception again');
# Check that sn fails when accessed indirectly, too.
ok( $attr = $class->attributes('sn'), 'Get "sn" attribute object' );
eval { $attr->set($obj, 'foo') };
ok( $err = $@, 'Catch indirect private exception');
like( $err, qr/sn is a trusted attribute of Class::Meta::Test/,
'Correct indirectprivate exception');
eval { $attr->get($obj) };
ok( $err = $@, 'Catch another indirect private exception');
like( $err, qr/sn is a trusted attribute of Class::Meta::Test/,
'Correct indirect private exception again');
# Make sure that we can set protected attributes via new().
ok( $obj = __PACKAGE__->new( id => 10,
name => 'Damian'),
"Create another new object" );
is( $obj->get_id, 10, 'Check 10 ID' );
is( $obj->get_name, 'Damian', 'Check Damian name' );
# Make sure that the private attribute fails.
eval { __PACKAGE__->new( age => 44 ) };
ok( $err = $@, 'Catch constructor private exception');
like( $err, qr/age is a private attribute of Class::Meta::Test/,
'Correct private constructor exception');
# Make sure that the trusted attribute fails.
eval { __PACKAGE__->new( sn => 'foo' ) };
ok( $err = $@, 'Catch constructor trusted exception');
like( $err, qr/sn is a trusted attribute of Class::Meta::Test/,
'Correct trusted constructor exception');
# Do the same with the constructor object.
ok( $ctor = $class->constructors('new'), 'Get "new" constructor object' );
ok( $obj = $ctor->call(__PACKAGE__,
id => 10,
name => 'Damian'),
"Create another new object" );
is( $obj->get_id, 10, 'Check 10 ID' );
is( $obj->get_name, 'Damian', 'Check Damian name' );
# Make sure that the private attribute fails.
eval { $ctor->call(__PACKAGE__, age => 44 ) };
ok( $err = $@, 'Catch indirect constructor private exception');
like( $err, qr/age is a private attribute of Class::Meta::Test/,
'Correct indirect private constructor exception');
# Make sure that the private attribute fails.
eval { $ctor->call(__PACKAGE__, sn => 'foo' ) };
ok( $err = $@, 'Catch indirect constructor trusted exception');
like( $err, qr/sn is a trusted attribute of Class::Meta::Test/,
'Correct indirect trusted constructor exception');
##############################################################################
# Set up a trusted package.
##############################################################################
package Class::Meta::TrustMe;
use strict;
BEGIN { Test::More->import }
ok( $obj = Class::Meta::Test->new, "Create new Test object" );
ok( $class = Class::Meta::Test->my_class, "Get Test class object" );
is_deeply( [map { $_->name } $class->attributes], [qw(id sn)],
"Call to attributes() should return public and trusted attrs" );
is_deeply( [map { $_->name } Class::Meta::Testarama->my_class->attributes],
[qw(id sn)],
"Call to inherited attributes() should also return public and protected attrs" );
# Check id public attribute.
is( $obj->get_id, 22, 'Check default ID' );
ok( $obj->set_id(12), "Set ID" );
is( $obj->get_id, 12, 'Check 12 ID' );
ok( $attr = $class->attributes('id'), 'Get "id" attribute object' );
is( $attr->get($obj), 12, "Check indirect 12 ID" );
ok( $attr->set($obj, 15), "Indirectly set ID" );
is( $attr->get($obj), 15, "Check indirect 15 ID" );
# Check name protected attribute
eval { $obj->set_name('foo') };
ok( $err = $@, "Catch protected exception");
like( $err, qr/name is a protected attribute of Class::Meta::Test/,
"Correct protected exception" );
eval { $obj->get_name };
ok( $err = $@, "Catch another protected exception");
like( $err, qr/name is a protected attribute of Class::Meta::Test/,
"Another correct protected exception" );
# Check that name fails when accessed indirectly, too.
ok( $attr = $class->attributes('name'), 'Get "name" attribute object' );
eval { $attr->set($obj, 'foo') };
ok( $err = $@, "Catch indirect protected exception");
like( $err, qr/name is a protected attribute of Class::Meta::Test/,
"Correct indirect protected exception" );
eval { $attr->get($obj, 'foo') };
ok( $err = $@, "Catch another indirect protected exception");
like( $err, qr/name is a protected attribute of Class::Meta::Test/,
"Another correct indirect protected exception" );
# Check age private attribute
eval { $obj->set_age(12) };
ok( $err = $@, 'Catch private exception');
like( $err, qr/age is a private attribute of Class::Meta::Test/,
'Correct private exception');
eval { $obj->get_age };
ok( $err = $@, 'Catch another private exception');
like( $err, qr/age is a private attribute of Class::Meta::Test/,
'Correct private exception again');
# Check that age fails when accessed indirectly, too.
ok( $attr = $class->attributes('age'), 'Get "age" attribute object' );
eval { $attr->set($obj, 12) };
ok( $err = $@, 'Catch indirect private exception');
like( $err, qr/age is a private attribute of Class::Meta::Test/,
'Correct indirectprivate exception');
eval { $attr->get($obj) };
ok( $err = $@, 'Catch another indirect private exception');
like( $err, qr/age is a private attribute of Class::Meta::Test/,
'Correct indirect private exception again');
# Check sn trusted attribute succeeds.
is( $obj->get_sn, '', 'Check empty sn' );
ok( $obj->set_sn('123456789'), "Set sn" );
is( $obj->get_sn, '123456789', 'Check "123456789" sn' );
ok( $attr = $class->attributes('sn'), 'Get "sn" attribute object' );
is( $attr->get($obj), '123456789', 'Check indirect "123456789" sn' );
ok( $attr->set($obj, '987654321'), "Indirectly set sn" );
is( $attr->get($obj), '987654321', 'Check indirect "987654321" sn' );
# Make sure that sn trusted attribute works for subclasses, too.
ok( $obj = Class::Meta::Testarama->new, "Create new Testarama object" );
is( $obj->get_sn, '', 'Check empty sn' );
ok( $obj->set_sn('123456789'), "Set sn" );
is( $obj->get_sn, '123456789', 'Check "123456789" sn' );
ok( $attr = $class->attributes('sn'), 'Get "sn" attribute object' );
is( $attr->get($obj), '123456789', 'Check indirect "123456789" sn' );
ok( $attr->set($obj, '987654321'), "Indirectly set sn" );
is( $attr->get($obj), '987654321', 'Check indirect "987654321" sn' );
# Make sure that we can set trusted attributes via new().
ok( $obj = Class::Meta::Test->new( id => 10,
sn => 'foo'),
"Create another new object" );
is( $obj->get_id, 10, 'Check 10 ID' );
is( $obj->get_sn, 'foo', 'Check foo sn' );
# Make sure that the private attribute fails.
eval { Class::Meta::Test->new( age => 44 ) };
ok( $err = $@, "Catch constructor private exception");
like( $err, qr/age is a private attribute of Class::Meta::Test/,
"Got the right constructor private exception");
# Make sure that the protected attribute fails.
eval { Class::Meta::Test->new( name => 'Damian' ) };
ok( $err = $@, "Catch constructor protected exception");
like( $err, qr/name is a protected attribute of Class::Meta::Test/,
"Got the right constructor protected exception");
# Do the same with the new constructor object.
ok( $ctor = $class->constructors('new'), 'Get "new" constructor object' );
ok( $obj = $ctor->call('Class::Meta::Test',
id => 10,
sn => 'foo'),
"Create another new object" );
is( $obj->get_id, 10, 'Check 10 ID' );
is( $obj->get_sn, 'foo', 'Check foo sn' );
# Make sure that the private attribute fails.
eval { $ctor->call('Class::Meta::Test', age => 44 ) };
ok( $err = $@, "Catch indirect constructor private exception");
like( $err, qr/age is a private attribute of Class::Meta::Test/,
"Got the right indirect constructor private exception");
# Make sure that the protected attribute fails.
eval { $ctor->call('Class::Meta::Test', name => 'Damian' ) };
ok( $err = $@, "Catch indirect constructor protected exception");
like( $err, qr/name is a protected attribute of Class::Meta::Test/,
"Got the right indirect constructor protected exception");
##############################################################################
# Now do test in a completely independent package.
##############################################################################
package main;
ok( $obj = Class::Meta::Test->new, "Create new object in main" );
ok( $class = Class::Meta::Test->my_class, "Get class object in main" );
# Make sure we can access id.
is( $obj->get_id, 22, 'Check default ID' );
ok( $obj->set_id(12), "Set ID" );
is( $obj->get_id, 12, 'Check 12 ID' );
ok( $attr = $class->attributes('id'), 'Get "id" attribute object' );
is( $attr->get($obj), 12, "Check indirect 12 ID" );
ok( $attr->set($obj, 15), "Indirectly set ID" );
is( $attr->get($obj), 15, "Check indirect 15 ID" );
# Check name protected attribute
eval { $obj->set_name('foo') };
ok( $err = $@, 'Catch protected exception');
like( $err, qr/name is a protected attribute of Class::Meta::Test/,
'Correct protected exception');
eval { $obj->get_name };
ok( $err = $@, 'Catch another protected exception');
like( $err, qr/name is a protected attribute of Class::Meta::Test/,
'Correct protected exception again');
# Check that name fails when accessed indirectly, too.
ok( $attr = $class->attributes('name'), 'Get "name" attribute object' );
eval { $attr->set($obj, 'foo') };
ok( $err = $@, 'Catch indirect protected exception');
like( $err, qr/name is a protected attribute of Class::Meta::Test/,
'Correct indirectprotected exception');
eval { $attr->get($obj) };
ok( $err = $@, 'Catch another indirect protected exception');
like( $err, qr/name is a protected attribute of Class::Meta::Test/,
'Correct indirect protected exception again');
# Check age private attribute
eval { $obj->set_age(12) };
ok( $err = $@, 'Catch private exception');
like( $err, qr/age is a private attribute of Class::Meta::Test/,
'Correct private exception');
eval { $obj->get_age };
ok( $err = $@, 'Catch another private exception');
like( $err, qr/age is a private attribute of Class::Meta::Test/,
'Correct private exception again');
# Check that age fails when accessed indirectly, too.
ok( $attr = $class->attributes('age'), 'Get "age" attribute object' );
eval { $attr->set($obj, 12) };
ok( $err = $@, 'Catch indirect private exception');
like( $err, qr/age is a private attribute of Class::Meta::Test/,
'Correct indirectprivate exception');
eval { $attr->get($obj) };
ok( $err = $@, 'Catch another indirect private exception');
like( $err, qr/age is a private attribute of Class::Meta::Test/,
'Correct indirect private exception again');
# Check sn trusted attribute
eval { $obj->set_sn('foo') };
ok( $err = $@, 'Catch private exception');
like( $err, qr/sn is a trusted attribute of Class::Meta::Test/,
'Correct private exception');
eval { $obj->get_sn };
ok( $err = $@, 'Catch another private exception');
like( $err, qr/sn is a trusted attribute of Class::Meta::Test/,
'Correct private exception again');
# Check that sn fails when accessed indirectly, too.
ok( $attr = $class->attributes('sn'), 'Get "sn" attribute object' );
eval { $attr->set($obj, 'foo') };
ok( $err = $@, 'Catch indirect private exception');
like( $err, qr/sn is a trusted attribute of Class::Meta::Test/,
'Correct indirectprivate exception');
eval { $attr->get($obj) };
ok( $err = $@, 'Catch another indirect private exception');
like( $err, qr/sn is a trusted attribute of Class::Meta::Test/,
'Correct indirect private exception again');
# Try the constructor with parameters.
ok( $obj = Class::Meta::Test->new( id => 1 ), "Create new object with id" );
is( $obj->get_id, 1, 'Check 1 ID' );
ok( $ctor = $class->constructors('new'), "Get new constructor" );
ok( $obj = $ctor->call('Class::Meta::Test', id => 52 ),
"Indirectly create new object with id" );
is( $obj->get_id, 52, 'Check 52 ID' );
# Make sure that the protected attribute fails.
eval { Class::Meta::Test->new( name => 'foo' ) };
ok( $err = $@, 'Catch constructor protected exception');
like( $err, qr/name is a protected attribute of Class::Meta::Test/,
'Correct protected constructor exception');
eval { $ctor->call('Class::Meta::Test', name => 'foo' ) };
ok( $err = $@, 'Catch indirect constructor protected exception');
like( $err, qr/name is a protected attribute of Class::Meta::Test/,
'Correct indirect protected constructor exception');
# Make sure that the private attribute fails.
eval { Class::Meta::Test->new( age => 44 ) };
ok( $err = $@, 'Catch constructor private exception');
like( $err, qr/age is a private attribute of Class::Meta::Test/,
'Correct private constructor exception');
eval { $ctor->call('Class::Meta::Test', age => 44 ) };
ok( $err = $@, 'Catch indirect constructor private exception');
like( $err, qr/age is a private attribute of Class::Meta::Test/,
'Correct indirect private constructor exception');
|