3
# $Id: view.t 2384 2005-12-14 04:27:23Z theory $
5
##############################################################################
7
##############################################################################
10
use Test::More $] < 5.008
11
? (skip_all => 'Older Carp lacks @CARP_NOT support')
14
my $fn = File::Spec->catfile('t', 'view.t');
16
##############################################################################
17
# Create a simple class.
18
##############################################################################
20
package Class::Meta::Test;
25
use_ok('Class::Meta');
26
use_ok('Class::Meta::Types::Numeric');
27
use_ok('Class::Meta::Types::String');
31
ok( my $c = Class::Meta->new(
33
package => __PACKAGE__,
34
name => 'Class::Meta::TestPerson Class',
35
trust => 'Class::Meta::TrustMe',
36
desc => 'Special person class just for testing Class::Meta.',
37
), "Create Class::Meta object" );
40
ok( $c->add_constructor( name => 'new',
42
"Add new constructor" );
44
# Add a protected constructor.
45
ok( $c->add_constructor( name => 'prot_new',
46
view => Class::Meta::PROTECTED,
48
"Add protected constructor" );
50
# Add a private constructor.
51
ok( $c->add_constructor( name => 'priv_new',
52
view => Class::Meta::PRIVATE,
54
"Add private constructor" );
56
# Add a trusted constructor.
57
ok( $c->add_constructor( name => 'trust_new',
58
view => Class::Meta::TRUSTED,
60
"Add trusted constructor" );
62
# Add a couple of attributes with created methods.
63
ok( $c->add_attribute( name => 'id',
64
view => Class::Meta::PUBLIC,
71
ok( $c->add_attribute( name => 'name',
72
view => Class::Meta::PROTECTED,
78
"Add protected name attribute" );
79
ok( $c->add_attribute( name => 'age',
80
view => Class::Meta::PRIVATE,
83
desc => "The person's age.",
87
"Add private age attribute" );
88
ok( $c->add_attribute( name => 'sn',
89
view => Class::Meta::TRUSTED,
92
desc => "The person's serial number.",
96
"Add trusted sn attribute" );
100
##############################################################################
101
# From within the package, the all attributes should just work.
102
##############################################################################
104
ok( my $obj = __PACKAGE__->new, "Create new object" );
105
ok( my $class = __PACKAGE__->my_class, "Get class object" );
107
[map { $_->name } $class->attributes],
108
[qw(id name age sn)],
109
'Call to attributes() should return all attributes'
113
[map { $_->name } $class->constructors],
114
[qw(new prot_new priv_new trust_new)],
115
'Call to constructors() should return all constructors'
118
# Check id public attribute.
119
is( $obj->id, 22, 'Check default ID' );
120
ok( $obj->id(12), "Set ID" );
121
is( $obj->id, 12, 'Check 12 ID' );
122
ok( my $attr = $class->attributes('id'), 'Get "id" attribute object' );
123
is( $attr->get($obj), 12, "Check indirect 12 ID" );
124
ok( $attr->set($obj, 15), "Indirectly set ID" );
125
is( $attr->get($obj), 15, "Check indirect 15 ID" );
127
# Check name protected attribute succeeds.
128
is( $obj->name, '', 'Check empty name' );
129
ok( $obj->name('Larry'), "Set name" );
130
is( $obj->name, 'Larry', 'Check "Larry" name' );
131
ok( $attr = $class->attributes('name'), 'Get "name" attribute object' );
132
is( $attr->get($obj), 'Larry', 'Check indirect "Larry" name' );
133
ok( $attr->set($obj, 'Chip'), "Indirectly set name" );
134
is( $attr->get($obj), 'Chip', 'Check indirect "chip" name' );
136
# Check age private attribute succeeds.
137
is( $obj->age, 0, 'Check default age' );
138
ok( $obj->age(42), "Set age" );
139
is( $obj->age, 42, 'Check 42 age' );
140
ok( $attr = $class->attributes('age'), 'Get "age" attribute object' );
141
is( $attr->get($obj), 42, "Check indirect 12 age" );
142
ok( $attr->set($obj, 15), "Indirectly set age" );
143
is( $attr->get($obj), 15, "Check indirect 15 age" );
145
# Check sn trusted attribute succeeds.
146
is( $obj->sn, '', 'Check empty sn' );
147
ok( $obj->sn('123456789'), "Set sn" );
148
is( $obj->sn, '123456789', 'Check "123456789" sn' );
149
ok( $attr = $class->attributes('sn'), 'Get "sn" attribute object' );
150
is( $attr->get($obj), '123456789', 'Check indirect "123456789" sn' );
151
ok( $attr->set($obj, '987654321'), "Indirectly set sn" );
152
is( $attr->get($obj), '987654321', 'Check indirect "987654321" sn' );
154
# Make sure that we can set all of the attributes via new().
155
ok( $obj = __PACKAGE__->new( id => 10,
159
"Create another new object" );
161
is( $obj->id, 10, 'Check 10 ID' );
162
is( $obj->name, 'Damian', 'Check Damian name' );
163
is( $obj->age, 35, 'Check 35 age' );
164
is( $obj->sn, 'au', 'Check sn is "au"');
166
# Do the same with the constructor object.
167
ok( my $ctor = $class->constructors('new'), 'Get "new" constructor object' );
168
ok( $obj = $ctor->call(__PACKAGE__,
173
"Create another new object" );
175
is( $obj->id, 10, 'Check 10 ID' );
176
is( $obj->name, 'Damian', 'Check Damian name' );
177
is( $obj->age, 35, 'Check 35 age' );
178
is( $obj->sn, 'au', 'Check sn is "au"');
180
# Make sure that we can set all of the attributes via prot_new().
181
ok( $obj = __PACKAGE__->prot_new( id => 10,
185
"Create another prot_new object" );
187
is( $obj->id, 10, 'Check 10 ID' );
188
is( $obj->name, 'Damian', 'Check Damian name' );
189
is( $obj->age, 35, 'Check 35 age' )
190
;is( $obj->sn, 'au', 'Check sn is "au"');
192
# Do the same with the constructor object.
193
ok( $ctor = $class->constructors('prot_new'),
194
'Get "prot_new" constructor object' );
195
ok( $obj = $ctor->call(__PACKAGE__,
200
"Create another prot_new object" );
202
is( $obj->id, 10, 'Check 10 ID' );
203
is( $obj->name, 'Damian', 'Check Damian name' );
204
is( $obj->age, 35, 'Check 35 age' );
205
is( $obj->sn, 'au', 'Check sn is "au"');
207
# Make sure that we can set all of the attributes via priv_new().
208
ok( $obj = __PACKAGE__->priv_new( id => 10,
212
"Create another priv_new object" );
214
is( $obj->id, 10, 'Check 10 ID' );
215
is( $obj->name, 'Damian', 'Check Damian name' );
216
is( $obj->age, 35, 'Check 35 age' );
217
is( $obj->sn, 'au', 'Check sn is "au"');
219
# Do the same with the constructor object.
220
ok( $ctor = $class->constructors('priv_new'),
221
'Get "priv_new" constructor object' );
222
ok( $obj = $ctor->call(__PACKAGE__,
227
"Create another priv_new object" );
229
is( $obj->id, 10, 'Check 10 ID' );
230
is( $obj->name, 'Damian', 'Check Damian name' );
231
is( $obj->age, 35, 'Check 35 age' );
232
is( $obj->sn, 'au', 'Check sn is "au"');
234
# Make sure that we can set all of the attributes via trust_new().
235
ok( $obj = __PACKAGE__->trust_new( id => 10,
239
"Create another trust_new object" );
241
is( $obj->id, 10, 'Check 10 ID' );
242
is( $obj->name, 'Damian', 'Check Damian name' );
243
is( $obj->age, 35, 'Check 35 age' );
244
is( $obj->sn, 'au', 'Check sn is "au"');
246
# Do the same with the constructor object.
247
ok( $ctor = $class->constructors('trust_new'),
248
'Get "trust_new" constructor object' );
249
ok( $obj = $ctor->call(__PACKAGE__,
254
"Create another priv_new object" );
256
is( $obj->id, 10, 'Check 10 ID' );
257
is( $obj->name, 'Damian', 'Check Damian name' );
258
is( $obj->age, 35, 'Check 35 age' );
259
is( $obj->sn, 'au', 'Check sn is "au"');
261
##############################################################################
262
# Set up an inherited package.
263
##############################################################################
264
package Class::Meta::Testarama;
266
use base 'Class::Meta::Test';
270
Class::Meta->new(key => 'testarama')->build;
273
ok( $obj = __PACKAGE__->new, "Create new Testarama object" );
274
ok( $class = __PACKAGE__->my_class, "Get Testarama class object" );
275
is_deeply( [map { $_->name } $class->attributes], [qw(id name)],
276
"Call to attributes() should return public and protected attrs" );
277
is_deeply( [map { $_->name } $class->constructors], [qw(new prot_new)],
278
"Call to constructors() should return public and protected ctors" );
280
# Check id public attribute.
281
is( $obj->id, 22, 'Check default ID' );
282
ok( $obj->id(12), "Set ID" );
283
is( $obj->id, 12, 'Check 12 ID' );
284
ok( $attr = $class->attributes('id'), 'Get "id" attribute object' );
285
is( $attr->get($obj), 12, "Check indirect 12 ID" );
286
ok( $attr->set($obj, 15), "Indirectly set ID" );
287
is( $attr->get($obj), 15, "Check indirect 15 ID" );
289
# Check name protected attribute succeeds.
290
is( $obj->name, '', 'Check empty name' );
291
ok( $obj->name('Larry'), "Set name" );
292
is( $obj->name, 'Larry', 'Check Larry name' );
293
ok( $attr = $class->attributes('name'), 'Get "name" attribute object' );
294
is( $attr->get($obj), 'Larry', 'Check indirect "Larry" name' );
295
ok( $attr->set($obj, 'Chip'), "Indirectly set name" );
296
is( $attr->get($obj), 'Chip', 'Check indirect "chip" name' );
298
# Check age private attribute
299
eval { $obj->age(12) };
300
main::chk( 'private exception',
301
qr/age is a private attribute of Class::Meta::Test/);
303
main::chk( 'private exception again',
304
qr/age is a private attribute of Class::Meta::Test/);
306
# Check that age fails when accessed indirectly, too.
307
ok( $attr = $class->attributes('age'), 'Get "age" attribute object' );
308
eval { $attr->set($obj, 12) };
309
main::chk('indirect private exception',
310
qr/age is a private attribute of Class::Meta::Test/);
311
eval { $attr->get($obj) };
312
main::chk('another indirect private exception',
313
qr/age is a private attribute of Class::Meta::Test/);
315
# Check sn trusted attribute fails.
316
eval { $obj->sn('foo') };
317
main::chk( 'trusted exception',
318
qr/sn is a trusted attribute of Class::Meta::Test/);
320
main::chk( 'trusted exception again',
321
qr/sn is a trusted attribute of Class::Meta::Test/);
323
# Check that sn fails when accessed indirectly, too.
324
ok( $attr = $class->attributes('sn'), 'Get "sn" attribute object' );
325
eval { $attr->set($obj, 'foo') };
326
main::chk('indirect trusted exception',
327
qr/sn is a trusted attribute of Class::Meta::Test/);
328
eval { $attr->get($obj) };
329
main::chk('another indirect trusted exception',
330
qr/sn is a trusted attribute of Class::Meta::Test/);
332
# Make sure that we can set protected attributes via new().
333
ok( $obj = __PACKAGE__->new( id => 10,
335
"Create another new object" );
337
is( $obj->id, 10, 'Check 10 ID' );
338
is( $obj->name, 'Damian', 'Check Damian name' );
340
# Make sure that the private attribute fails.
342
eval { __PACKAGE__->new( age => 44 ) };
344
main::chk('constructor private exception',
345
qr/age is a private attribute of Class::Meta::Test/);
347
# Do the same with the new constructor object.
348
ok( $ctor = $class->constructors('new'), 'Get "new" constructor object' );
349
ok( $obj = $ctor->call(__PACKAGE__,
352
"Create another new object" );
354
is( $obj->id, 10, 'Check 10 ID' );
355
is( $obj->name, 'Damian', 'Check Damian name' );
357
# Make sure that the private attribute fails.
358
eval { $ctor->call(__PACKAGE__, age => 44 ) };
359
main::chk('indirect constructor private exception',
360
qr/age is a private attribute of Class::Meta::Test/);
362
# Make sure that we can set protected attributes via prot_new().
363
ok( $obj = __PACKAGE__->prot_new( id => 10,
365
"Create another prot_new object" );
367
is( $obj->id, 10, 'Check 10 ID' );
368
is( $obj->name, 'Damian', 'Check Damian name' );
370
# Make sure that the private attribute fails.
371
eval { __PACKAGE__->prot_new( age => 44 ) };
372
main::chk('constructor private exception',
373
qr/age is a private attribute of Class::Meta::Test/);
375
# Do the same with the prot_new constructor object.
376
ok( $ctor = $class->constructors('prot_new'),
377
'Get "prot_new" constructor object' );
378
ok( $obj = $ctor->call(__PACKAGE__,
381
"Create another prot_new object" );
383
is( $obj->id, 10, 'Check 10 ID' );
384
is( $obj->name, 'Damian', 'Check Damian name' );
386
# Make sure that the private attribute fails.
387
eval { $ctor->call(__PACKAGE__, age => 44 ) };
388
main::chk('indirect constructor private exception',
389
qr/age is a private attribute of Class::Meta::Test/);
391
# Make sure that the private constructor fails.
392
eval { __PACKAGE__->priv_new };
393
main::chk('priv_new exeption',
394
qr/priv_new is a private constructor of Class::Meta::Test/);
396
# Make sure the same is true of the priv_new constructor object.
397
ok( $ctor = $class->constructors('priv_new'),
398
'Get "priv_new" constructor object' );
399
eval { $ctor->call(__PACKAGE__) };
400
main::chk('indirect priv_new exeption',
401
qr/priv_new is a private constructor of Class::Meta::Test/);
403
##############################################################################
404
# Set up a trusted package.
405
##############################################################################
406
package Class::Meta::TrustMe;
409
BEGIN { Test::More->import }
411
ok( $obj = Class::Meta::Test->new, "Create new Test object" );
412
ok( $class = Class::Meta::Test->my_class, "Get Test class object" );
413
is_deeply( [map { $_->name } $class->attributes], [qw(id sn)],
414
"Call to attributes() should return public and trusted attrs" );
416
[map { $_->name } Class::Meta::Testarama->my_class->attributes],
418
'Call to attributes() should return public and trusted attrs',
422
[map { $_->name } Class::Meta::Testarama->my_class->constructors],
424
'Call to constructors() should return public and trusted ctors',
427
# Check id public attribute.
428
is( $obj->id, 22, 'Check default ID' );
429
ok( $obj->id(12), "Set ID" );
430
is( $obj->id, 12, 'Check 12 ID' );
431
ok( $attr = $class->attributes('id'), 'Get "id" attribute object' );
432
is( $attr->get($obj), 12, "Check indirect 12 ID" );
433
ok( $attr->set($obj, 15), "Indirectly set ID" );
434
is( $attr->get($obj), 15, "Check indirect 15 ID" );
436
# Check name protected attribute
437
eval { $obj->name('foo') };
438
main::chk('protected exception',
439
qr/name is a protected attribute of Class::Meta::Test/);
441
main::chk('another protected exception',
442
qr/name is a protected attribute of Class::Meta::Test/);
444
# Check that name fails when accessed indirectly, too.
445
ok( $attr = $class->attributes('name'), 'Get "name" attribute object' );
446
eval { $attr->set($obj, 'foo') };
447
main::chk('indirect protected exception',
448
qr/name is a protected attribute of Class::Meta::Test/);
449
eval { $attr->get($obj) };
450
main::chk('another indirect protected exception',
451
qr/name is a protected attribute of Class::Meta::Test/);
453
# Check age private attribute
454
eval { $obj->age(12) };
455
main::chk( 'private exception',
456
qr/age is a private attribute of Class::Meta::Test/);
458
main::chk( 'private exception again',
459
qr/age is a private attribute of Class::Meta::Test/);
461
# Check that age fails when accessed indirectly, too.
462
ok( $attr = $class->attributes('age'), 'Get "age" attribute object' );
463
eval { $attr->set($obj, 12) };
464
main::chk('indirect private exception',
465
qr/age is a private attribute of Class::Meta::Test/);
466
eval { $attr->get($obj) };
467
main::chk('another indirect private exception',
468
qr/age is a private attribute of Class::Meta::Test/);
470
# Check sn trusted attribute succeeds.
471
is( $obj->sn, '', 'Check empty sn' );
472
ok( $obj->sn('123456789'), "Set sn" );
473
is( $obj->sn, '123456789', 'Check "123456789" sn' );
474
ok( $attr = $class->attributes('sn'), 'Get "sn" attribute object' );
475
is( $attr->get($obj), '123456789', 'Check indirect "123456789" sn' );
476
ok( $attr->set($obj, '987654321'), "Indirectly set sn" );
477
is( $attr->get($obj), '987654321', 'Check indirect "987654321" sn' );
479
# Make sure that sn trusted attribute works for subclasses, too.
480
ok( $obj = Class::Meta::Testarama->new, "Create new Testarama object" );
481
is( $obj->sn, '', 'Check empty sn' );
482
ok( $obj->sn('123456789'), "Set sn" );
483
is( $obj->sn, '123456789', 'Check "123456789" sn' );
484
ok( $attr = $class->attributes('sn'), 'Get "sn" attribute object' );
485
is( $attr->get($obj), '123456789', 'Check indirect "123456789" sn' );
486
ok( $attr->set($obj, '987654321'), "Indirectly set sn" );
487
is( $attr->get($obj), '987654321', 'Check indirect "987654321" sn' );
489
# Make sure that we can set trusted attributes via new().
490
ok( $obj = Class::Meta::Test->new( id => 10,
492
"Create another new object" );
494
is( $obj->id, 10, 'Check 10 ID' );
495
is( $obj->sn, 'foo', 'Check foo sn' );
497
# Make sure that the private attribute fails.
498
eval { Class::Meta::Test->new( age => 44 ) };
499
main::chk('constructor private exception',
500
qr/age is a private attribute of Class::Meta::Test/);
502
# Make sure that the protected attribute fails.
503
eval { Class::Meta::Test->new( name => 'Damian' ) };
504
main::chk('constructor protected exception',
505
qr/name is a protected attribute of Class::Meta::Test/);
507
# Do the same with the new constructor object.
508
ok( $ctor = $class->constructors('new'), 'Get "new" constructor object' );
509
ok( $obj = $ctor->call('Class::Meta::Test',
512
"Create another new object" );
514
is( $obj->id, 10, 'Check 10 ID' );
515
is( $obj->sn, 'foo', 'Check foo sn' );
517
# Make sure that the private attribute fails.
518
eval { $ctor->call('Class::Meta::Test', age => 44 ) };
519
main::chk('indirect constructor private exception',
520
qr/age is a private attribute of Class::Meta::Test/);
522
# Make sure that the protected attribute fails.
523
eval { $ctor->call('Class::Meta::Test', name => 'Damian' ) };
524
main::chk('indirect constructor protected exception',
525
qr/name is a protected attribute of Class::Meta::Test/);
527
# Make sure that we can set trusted attributes via trust_new().
528
ok( $obj = Class::Meta::Test->trust_new( id => 10,
530
"Create another trust_new object" );
532
is( $obj->id, 10, 'Check 10 ID' );
533
is( $obj->sn, 'foo', 'Check foo name' );
535
# Make sure that the private attribute fails.
536
eval { Class::Meta::Test->trust_new( age => 44 ) };
537
main::chk('constructor private exception',
538
qr/age is a private attribute of Class::Meta::Test/);
540
# Make sure that the protected attribute fails.
541
eval { Class::Meta::Test->trust_new( name => 'Damian' ) };
542
main::chk('constructor protected exception',
543
qr/name is a protected attribute of Class::Meta::Test/);
545
# Do the same with the trust_new constructor object.
546
ok( $ctor = $class->constructors('trust_new'),
547
'Get "trust_new" constructor object' );
548
ok( $obj = $ctor->call('Class::Meta::Test',
551
"Create another trust_new object" );
553
is( $obj->id, 10, 'Check 10 ID' );
554
is( $obj->sn, 'foo', 'Check foo name' );
556
# Make sure that the private attribute fails.
557
eval { $ctor->call('Class::Meta::Test', age => 44 ) };
558
main::chk('indirect constructor private exception',
559
qr/age is a private attribute of Class::Meta::Test/);
561
# Make sure that the private attribute fails.
562
eval { $ctor->call('Class::Meta::Test', age => 44 ) };
563
main::chk('indirect constructor private exception',
564
qr/age is a private attribute of Class::Meta::Test/);
566
# Make sure that the protected constructor fails.
567
eval { Class::Meta::Test->prot_new };
568
main::chk('prot_new exeption',
569
qr/prot_new is a protected constrctor of Class::Meta::Test/);
571
# Make sure the same is true of the priv_new constructor object.
572
ok( $ctor = $class->constructors('priv_new'),
573
'Get "priv_new" constructor object' );
574
eval { $ctor->call('Class::Meta::Test') };
575
main::chk('indirect priv_new exeption',
576
qr/priv_new is a private constructor of Class::Meta::Test/);
578
##############################################################################
579
# Now do test in a completely independent package.
580
##############################################################################
583
ok( $obj = Class::Meta::Test->new, "Create new object in main" );
584
ok( $class = Class::Meta::Test->my_class, "Get class object in main" );
586
# Make sure we can access id.
587
is( $obj->id, 22, 'Check default ID' );
588
ok( $obj->id(12), "Set ID" );
589
is( $obj->id, 12, 'Check 12 ID' );
590
ok( $attr = $class->attributes('id'), 'Get "id" attribute object' );
591
is( $attr->get($obj), 12, "Check indirect 12 ID" );
592
ok( $attr->set($obj, 15), "Indirectly set ID" );
593
is( $attr->get($obj), 15, "Check indirect 15 ID" );
595
# Check name protected attribute
596
eval { $obj->name('foo') };
597
chk('protected exception',
598
qr/name is a protected attribute of Class::Meta::Test/);
600
chk('another protected exception',
601
qr/name is a protected attribute of Class::Meta::Test/);
603
# Check that name fails when accessed indirectly, too.
604
ok( $attr = $class->attributes('name'), 'Get "name" attribute object' );
605
eval { $attr->set($obj, 'foo') };
606
chk('indirect protected exception',
607
qr/name is a protected attribute of Class::Meta::Test/);
608
eval { $attr->get($obj) };
609
chk('another indirect protected exception',
610
qr/name is a protected attribute of Class::Meta::Test/);
612
# Check sn trusted attribute, which can't be accessed by subclasses.
613
eval { $obj->sn('foo') };
614
main::chk( 'trusted exception',
615
qr/sn is a trusted attribute of Class::Meta::Test/);
617
main::chk( 'trusted exception again',
618
qr/sn is a trusted attribute of Class::Meta::Test/);
620
# Check that sn fails when accessed indirectly, too.
621
ok( $attr = $class->attributes('sn'), 'Get "sn" attribute object' );
622
eval { $attr->set($obj, 'foo') };
623
main::chk('indirect trusted exception',
624
qr/sn is a trusted attribute of Class::Meta::Test/);
625
eval { $attr->get($obj) };
626
main::chk('another indirect trusted exception',
627
qr/sn is a trusted attribute of Class::Meta::Test/);
629
# Check age private attribute
630
eval { $obj->age(12) };
631
chk( 'private exception',
632
qr/age is a private attribute of Class::Meta::Test/ );
634
chk( 'another private exception',
635
qr/age is a private attribute of Class::Meta::Test/);
637
# Check that age fails when accessed indirectly, too.
638
ok( $attr = $class->attributes('age'), 'Get "age" attribute object' );
639
eval { $attr->set($obj, 12) };
640
chk( 'indirect private exception',
641
qr/age is a private attribute of Class::Meta::Test/);
642
eval { $attr->get($obj) };
643
chk( 'another indirect private exception',
644
qr/age is a private attribute of Class::Meta::Test/);
646
# Try the constructor with parameters.
647
ok( $obj = Class::Meta::Test->new( id => 1 ), "Create new object with id" );
648
is( $obj->id, 1, 'Check 1 ID' );
649
ok( $ctor = $class->constructors('new'), "Get new constructor" );
650
ok( $obj = $ctor->call('Class::Meta::Test', id => 52 ),
651
"Indirectly create new object with id" );
652
is( $obj->id, 52, 'Check 52 ID' );
654
# Make sure that the protected attribute fails.
655
eval { Class::Meta::Test->new( name => 'foo' ) };
656
chk( 'constructor protected exception',
657
qr/name is a protected attribute of Class::Meta::Test/ );
658
eval { $ctor->call('Class::Meta::Test', name => 'foo' ) };
659
chk( 'indirect constructor protected exception',
660
qr/name is a protected attribute of Class::Meta::Test/);
662
# Make sure that the private attribute fails.
663
eval { Class::Meta::Test->new( age => 44 ) };
664
chk('constructor private exception',
665
qr/age is a private attribute of Class::Meta::Test/);
666
eval { $ctor->call('Class::Meta::Test', age => 44 ) };
667
chk( 'indirect constructor private exception',
668
qr/age is a private attribute of Class::Meta::Test/);
670
# Make sure that the protected constructor fails.
671
eval { Class::Meta::Test->prot_new };
672
chk( 'prot_new exeption',
673
qr/prot_new is a protected constrctor of Class::Meta::Test/ );
675
# Make sure the same is true of the prot_new constructor object.
676
ok( $ctor = $class->constructors('prot_new'),
677
'Get "prot_new" constructor object' );
678
eval { $ctor->call(__PACKAGE__) };
679
chk( 'indirect prot_new exeption',
680
qr/prot_new is a protected constrctor of Class::Meta::Test/ );
682
# Make sure that the private constructor fails.
683
eval { Class::Meta::Test->priv_new };
684
chk( 'priv_new exeption',
685
qr/priv_new is a private constructor of Class::Meta::Test/ );
687
# Make sure the same is true of the priv_new constructor object.
688
ok( $ctor = $class->constructors('priv_new'),
689
'Get "priv_new" constructor object' );
690
eval { $ctor->call(__PACKAGE__) };
691
chk( 'indirect priv_new exeption',
692
qr/priv_new is a private constructor of Class::Meta::Test/ );
695
my ($name, $qr) = @_;
696
# Catch the exception.
697
ok( my $err = $@, "Caught $name error" );
699
like( $err, $qr, "Correct error" );
700
# Make sure it refers to this file.
701
like( $err, qr/(?:at\s+\Q$fn\E|\Q$fn\E\s+at)\s+line/, 'Correct context' );
702
# Make sure it doesn't refer to other Class::Meta files.
703
unlike( $err, qr|lib/Class/Meta|, 'Not incorrect context')