File: chk_types_affordance.t

package info (click to toggle)
libclass-meta-perl 0.53-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 544 kB
  • ctags: 124
  • sloc: perl: 5,571; makefile: 44
file content (487 lines) | stat: -rw-r--r-- 19,144 bytes parent folder | download
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
#!/usr/bin/perl -w

# $Id: chk_types_affordance.t 682 2004-09-28 05:59:10Z theory $

##############################################################################
# Set up the tests.
##############################################################################

package Class::Meta::Testing;

use strict;
use Test::More tests => 208;
BEGIN {
    $SIG{__DIE__} = \&Carp::confess;
    use_ok( 'Class::Meta');
    use_ok( 'Class::Meta::Type');
    use_ok( 'Class::Meta::Types::Numeric', 'affordance');
    use_ok( 'Class::Meta::Types::Perl', 'affordance');
    use_ok( 'Class::Meta::Types::String', 'affordance');
    use_ok( 'Class::Meta::Types::Boolean', 'affordance');
    our @ISA = qw(Class::Meta::Attribute);
}

my $obj = bless {};
my $aname = 'foo';
my $i = 0;
my $attr;

##############################################################################
# Create a Class::Meta object. We'll use it to create attributes for testing
# the creation of accessors.
ok( my $cm = Class::Meta->new, "Create Class::Meta object" );

##############################################################################
# Check string data type.
ok( my $type = Class::Meta::Type->new('string'), 'Get string' );
is( $type, Class::Meta::Type->new('STRING'), 'Check lc conversion on key' );
is( $type->key, 'string', "Check string key" );
is( $type->name, 'String', "Check string name" );
is( ref $type->check, 'ARRAY', "Check string check" );

foreach my $chk (@{ $type->check }) {
    is( ref $chk, 'CODE', 'Check string code');
}

# Check to make sure that the accessor is created properly. Start with a
# simple set_ method.
ok( $attr = $cm->add_attribute( name => $aname . ++$i, type => 'string'),
    "Create $aname$i attribute" );
ok( $type->build(__PACKAGE__, $attr, Class::Meta::GETSET),
    "Make simple string set" );
ok( my $mut = UNIVERSAL::can(__PACKAGE__, "set_$aname$i"),
    "String mutator exists");
ok( my $acc = UNIVERSAL::can(__PACKAGE__, "get_$aname$i"),
    "String getter exists");

# Test it.
ok( $obj->$mut('test'), "Set string value" );
is( $obj->$acc, 'test', "Check string value" );

# Make it fail the checks.
eval { $obj->$mut([]) };
ok( my $err = $@, "Got invalid string error" );
like( $err, qr/^Value .* is not a valid string/, 'correct string exception' );

# Check to make sure that the Attribute class accessor coderefs are getting
# created.
ok( my $set = $type->make_attr_set($attr), "Check string attr_set" );
ok( my $get = $type->make_attr_get($attr), "Check string attr_get" );

# Make sure they get and set values correctly.
is( $get->($obj), 'test', "Check string getter" );
ok( $set->($obj, 'bar'), "Check string setter" );
is( $get->($obj), 'bar', "Check string getter again" );

##############################################################################
# Check boolean data type.
ok( $type = Class::Meta::Type->new('boolean'), 'Get boolean' );
is( $type, Class::Meta::Type->new('bool'), 'Check bool alias' );
is( $type->key, 'boolean', "Check boolean key" );
is( $type->name, 'Boolean', "Check boolean name" );
# Boolean is special -- it has no checkers.
ok( ! defined $type->check, "Check boolean check" );

# Check to make sure that the accessor is created properly. Start with a
# simple set_ method.
ok( $attr = $cm->add_attribute( name => $aname . ++$i, type => 'string'),
    "Create $aname$i attribute" );
ok( $type->build(__PACKAGE__, $attr, Class::Meta::GETSET),
    "Make simple boolean set" );
ok( $mut = UNIVERSAL::can(__PACKAGE__, "set_$aname$i\_on"),
    "Boolean on mutator exists");
ok( my $off = UNIVERSAL::can(__PACKAGE__, "set_$aname$i\_off"),
    "Boolean off mutator exists");
ok( $acc = UNIVERSAL::can(__PACKAGE__, "is_$aname$i"),
    "Boolean mutator exists");

# Test it.
ok( $obj->$mut, "Set boolean value on" );
is( $obj->$acc, 1, "Check boolean value on" );
$obj->$off; # Set boolean value off.
is( $obj->$acc, 0, "Check boolean value off" );

# And finally, check to make sure that the Attribute class accessor coderefs
# are getting created.
ok( $set = $type->make_attr_set($attr), "Check boolean attr_set" );
ok( $get = $type->make_attr_get($attr), "Check boolean attr_get" );

# Make sure they get and set values correctly.
is( $get->($obj), 0, "Check boolean getter" );
$set->($obj, 12);
is( $get->($obj), 1, "Check boolean getter again" );

##############################################################################
# Check whole data type.
ok( $type = Class::Meta::Type->new('whole'), 'Get whole' );
is( $type->key, 'whole', "Check whole key" );
is( $type->name, 'Whole Number', "Check whole name" );
is( ref $type->check, 'ARRAY', "Check whole check" );
foreach my $chk (@{ $type->check }) {
    is( ref $chk, 'CODE', 'Check whole code');
}

# Check to make sure that the accessor is created properly. Start with a
# simple set_ method.
ok( $attr = $cm->add_attribute( name => $aname . ++$i, type => 'string'),
    "Create $aname$i attribute" );
ok( $type->build(__PACKAGE__, $attr, Class::Meta::GETSET),
    "Make simple whole set" );
ok( $mut = UNIVERSAL::can(__PACKAGE__, "set_$aname$i"),
    "Whole mutator exists");
ok( $acc = UNIVERSAL::can(__PACKAGE__, "get_$aname$i"),
    "Whole getter exists");

# Test it.
ok( $obj->$mut(12), "Set whole value" );
is( $obj->$acc, 12, "Check whole value" );

# Make it fail the checks.
eval { $obj->$mut(-12) };
ok( $err = $@, "Got invalid whole error" );
like( $err, qr/^Value .* is not a valid whole number/,
      'correct whole exception' );

# Check to make sure that the Attribute class accessor coderefs are getting
# created.
ok( $set = $type->make_attr_set($attr), "Check whole attr_set" );
ok( $get = $type->make_attr_get($attr), "Check whole attr_get" );

# Make sure they get and set values correctly.
is( $get->($obj), 12, "Check whole getter" );
ok( $set->($obj, 100), "Check whole setter" );
is( $get->($obj), 100, "Check whole getter again" );

##############################################################################
# Check integer data type.
ok( $type = Class::Meta::Type->new('integer'), 'Get integer' );
is( $type, Class::Meta::Type->new('int'), 'Check int alias' );
is( $type->key, 'integer', "Check integer key" );
is( $type->name, 'Integer', "Check integer name" );
is( ref $type->check, 'ARRAY', "Check integer check" );
foreach my $chk (@{ $type->check }) {
    is( ref $chk, 'CODE', 'Check integer code');
}

# Check to make sure that the accessor is created properly. Start with a
# simple set_ method.
ok( $attr = $cm->add_attribute( name => $aname . ++$i, type => 'string'),
    "Create $aname$i attribute" );
ok( $type->build(__PACKAGE__, $attr, Class::Meta::GETSET),
    "Make simple integer set" );
ok( $mut = UNIVERSAL::can(__PACKAGE__, "set_$aname$i"),
    "Integer mutator exists");
ok( $acc = UNIVERSAL::can(__PACKAGE__, "get_$aname$i"),
    "Integer getter exists");

# Test it.
ok( $obj->$mut(12), "Set integer value" );
is( $obj->$acc, 12, "Check integer value" );

# Make it fail the checks.
eval { $obj->$mut(12.2) };
ok( $err = $@, "Got invalid integer error" );
like( $err, qr/^Value .* is not a valid integer/,
      'correct integer exception' );

# Check to make sure that the Attribute class accessor coderefs are getting
# created.
ok( $set = $type->make_attr_set($attr), "Check integer attr_set" );
ok( $get = $type->make_attr_get($attr), "Check integer attr_get" );

# Make sure they get and set values correctly.
is( $get->($obj), 12, "Check integer getter" );
ok( $set->($obj, -100), "Check integer setter" );
is( $get->($obj), -100, "Check integer getter again" );

##############################################################################
# Check decimal data type.
ok( $type = Class::Meta::Type->new('decimal'), 'Get decimal' );
is( $type, Class::Meta::Type->new('dec'), 'Check dec alias' );
is( $type->key, 'decimal', "Check decimal key" );
is( $type->name, 'Decimal Number', "Check decimal name" );
is( ref $type->check, 'ARRAY', "Check decimal check" );
foreach my $chk (@{ $type->check }) {
    is( ref $chk, 'CODE', 'Check decimal code');
}

# Check to make sure that the accessor is created properly. Start with a
# simple set_ method.
ok( $attr = $cm->add_attribute( name => $aname . ++$i, type => 'string'),
    "Create $aname$i attribute" );
ok( $type->build(__PACKAGE__, $attr, Class::Meta::GETSET),
    "Make simple decimal set" );
ok( $mut = UNIVERSAL::can(__PACKAGE__, "set_$aname$i"),
    "Decimal mutator exists");
ok( $acc = UNIVERSAL::can(__PACKAGE__, "get_$aname$i"),
    "Decimal getter exists");

# Test it.
ok( $obj->$mut(12.2), "Set decimal value" );
is( $obj->$acc, 12.2, "Check decimal value" );

# Make it fail the checks.
eval { $obj->$mut('foo') };
ok( $err = $@, "Got invalid decimal error" );
like( $err, qr/^Value .* is not a valid decimal/,
      'correct decimal exception' );

# Check to make sure that the Attribute class accessor coderefs are getting
# created.
ok( $set = $type->make_attr_set($attr), "Check decimal attr_set" );
ok( $get = $type->make_attr_get($attr), "Check decimal attr_get" );

# Make sure they get and set values correctly.
is( $get->($obj), 12.2, "Check decimal getter" );
ok( $set->($obj, +100.23), "Check decimal setter" );
is( $get->($obj), +100.23, "Check decimal getter again" );

##############################################################################
# Check float data type.
ok( $type = Class::Meta::Type->new('float'), 'Get float' );
is( $type->key, 'float', "Check float key" );
is( $type->name, 'Floating Point Number', "Check float name" );
is( ref $type->check, 'ARRAY', "Check float check" );
foreach my $chk (@{ $type->check }) {
    is( ref $chk, 'CODE', 'Check float code');
}

# Check to make sure that the accessor is created properly. Start with a
# simple set_ method.
ok( $attr = $cm->add_attribute( name => $aname . ++$i, type => 'string'),
    "Create $aname$i attribute" );
ok( $type->build(__PACKAGE__, $attr, Class::Meta::GETSET),
    "Make simple float set" );
ok( $mut = UNIVERSAL::can(__PACKAGE__, "set_$aname$i"),
    "Float mutator exists");
ok( $acc = UNIVERSAL::can(__PACKAGE__, "get_$aname$i"),
    "Float getter exists");

# Test it.
ok( $obj->$mut(1.23e99), "Set float value" );
is( $obj->$acc, 1.23e99, "Check float value" );

# Make it fail the checks.
eval { $obj->$mut('foo') };
ok( $err = $@, "Got invalid float error" );
like( $err, qr/^Value .* is not a valid float/,
      'correct float exception' );

# Check to make sure that the Attribute class accessor coderefs are getting
# created.
ok( $set = $type->make_attr_set($attr), "Check float attr_set" );
ok( $get = $type->make_attr_get($attr), "Check float attr_get" );

# Make sure they get and set values correctly.
is( $get->($obj), 1.23e99, "Check float getter" );
ok( $set->($obj, -100.23543), "Check float setter" );
is( $get->($obj), -100.23543, "Check float getter again" );

##############################################################################
# Check scalar data type.
ok( $type = Class::Meta::Type->new('scalar'), 'Get scalar' );
is( $type->key, 'scalar', "Check scalar key" );
is( $type->name, 'Scalar', "Check scalar name" );
# Scalars aren't validated or convted.
ok( ! defined $type->check, "Check scalar check" );

# Check to make sure that the accessor is created properly. Start with a
# simple set_ method.
ok( $attr = $cm->add_attribute( name => $aname . ++$i, type => 'string'),
    "Create $aname$i attribute" );
ok( $type->build(__PACKAGE__, $attr, Class::Meta::GETSET),
    "Make simple scalar set" );
ok( $mut = UNIVERSAL::can(__PACKAGE__, "set_$aname$i"),
    "Scalar mutator exists");
ok( $acc = UNIVERSAL::can(__PACKAGE__, "get_$aname$i"),
    "Scalar getter exists");

# Test it.
ok( $obj->$mut('foo'), "Set scalar value" );
is( $obj->$acc, 'foo', "Check scalar value" );

# Check to make sure that the Attribute class accessor coderefs are getting
# created.
ok( $set = $type->make_attr_set($attr), "Check scalar attr_set" );
ok( $get = $type->make_attr_get($attr), "Check scalar attr_get" );

# Make sure they get and set values correctly.
is( $get->($obj), 'foo', "Check scalar getter" );
ok( $set->($obj, []), "Check scalar setter" );
is( ref $get->($obj), 'ARRAY', "Check scalar getter again" );

##############################################################################
# Check scalar reference data type.
ok( $type = Class::Meta::Type->new('scalarref'), 'Get scalar ref' );
is( $type->key, 'scalarref', "Check scalar ref key" );
is( $type->name, 'Scalar Reference', "Check scalar ref name" );
is( ref $type->check, 'ARRAY', "Check scalar ref check" );
foreach my $chk (@{ $type->check }) {
    is( ref $chk, 'CODE', 'Check scalar ref code');
}

# Check to make sure that the accessor is created properly. Start with a
# simple set_ method.
ok( $attr = $cm->add_attribute( name => $aname . ++$i, type => 'string'),
    "Create $aname$i attribute" );
ok( $type->build(__PACKAGE__, $attr, Class::Meta::GETSET),
    "Make simple scalarref set" );
ok( $mut = UNIVERSAL::can(__PACKAGE__, "set_$aname$i"),
    "Scalarref mutator exists");
ok( $acc = UNIVERSAL::can(__PACKAGE__, "get_$aname$i"),
    "Scalarref getter exists");

# Test it.
my $sref = \"foo";
ok( $obj->$mut($sref), "Set scalarref value" );
is( $obj->$acc, $sref, "Check scalarref value" );

# Make it fail the checks.
eval { $obj->$mut('foo') };
ok( $err = $@, "Got invalid scalarref error" );
like( $err, qr/^Value .* is not a valid Scalar Reference/,
      'correct scalarref exception' );

# Check to make sure that the Attribute class accessor coderefs are getting
# created.
ok( $set = $type->make_attr_set($attr), "Check scalarref attr_set" );
ok( $get = $type->make_attr_get($attr), "Check scalarref attr_get" );

# Make sure they get and set values correctly.
is( $get->($obj), $sref, "Check scalarref getter" );
$sref = \"bar";
ok( $set->($obj, $sref), "Check scalarref setter" );
is( $get->($obj), $sref, "Check scalarref getter again" );

##############################################################################
# Check array data type.
ok( $type = Class::Meta::Type->new('array'), 'Get array' );
is( $type, Class::Meta::Type->new('arrayref'), 'Check arrayref alias' );
is( $type->key, 'array', "Check array key" );
is( $type->name, 'Array Reference', "Check array name" );
is( ref $type->check, 'ARRAY', "Check array check" );
foreach my $chk (@{ $type->check }) {
    is( ref $chk, 'CODE', 'Check array code');
}

# Check to make sure that the accessor is created properly. Start with a
# simple set_ method.
ok( $attr = $cm->add_attribute( name => $aname . ++$i, type => 'string'),
    "Create $aname$i attribute" );
ok( $type->build(__PACKAGE__, $attr, Class::Meta::GETSET),
    "Make simple arrayref set" );
ok( $mut = UNIVERSAL::can(__PACKAGE__, "set_$aname$i"),
    "Arrayref mutator exists");
ok( $acc = UNIVERSAL::can(__PACKAGE__, "get_$aname$i"),
    "Arrayref getter exists");

# Test it.
my $aref = [1,2,3];
ok( $obj->$mut($aref), "Set arrayref value" );
is( $obj->$acc, $aref, "Check arrayref value" );

# Make it fail the checks.
eval { $obj->$mut('foo') };
ok( $err = $@, "Got invalid arrayref error" );
like( $err, qr/^Value .* is not a valid Array Reference/,
      'correct arrayref exception' );

# Check to make sure that the Attribute class accessor coderefs are getting
# created.
ok( $set = $type->make_attr_set($attr), "Check arrayref attr_set" );
ok( $get = $type->make_attr_get($attr), "Check arrayref attr_get" );

# Make sure they get and set values correctly.
is( $get->($obj), $aref, "Check arrayref getter" );
$aref = [4,5,6];
ok( $set->($obj, $aref), "Check arrayref setter" );
is( $get->($obj), $aref, "Check arrayref getter again" );

##############################################################################
# Check hash data type.
ok( $type = Class::Meta::Type->new('hash'), 'Get hash' );
is( $type, Class::Meta::Type->new('hashref'), 'Check hashref alias' );
is( $type->key, 'hash', "Check hash key" );
is( $type->name, 'Hash Reference', "Check hash name" );
is( ref $type->check, 'ARRAY', "Check hash check" );
foreach my $chk (@{ $type->check }) {
    is( ref $chk, 'CODE', 'Check hash code');
}

# Check to make sure that the accessor is created properly. Start with a
# simple set_ method.
ok( $attr = $cm->add_attribute( name => $aname . ++$i, type => 'string'),
    "Create $aname$i attribute" );
ok( $type->build(__PACKAGE__, $attr, Class::Meta::GETSET),
    "Make simple hashref set" );
ok( $mut = UNIVERSAL::can(__PACKAGE__, "set_$aname$i"),
    "Hashref mutator exists");
ok( $acc = UNIVERSAL::can(__PACKAGE__, "get_$aname$i"),
    "Hashref getter exists");

# Test it.
my $href = {};
ok( $obj->$mut($href), "Set hashref value" );
is( $obj->$acc, $href, "Check hashref value" );

# Make it fail the checks.
eval { $obj->$mut('foo') };
ok( $err = $@, "Got invalid hashref error" );
like( $err, qr/^Value .* is not a valid Hash Reference/,
      'correct hashref exception' );

# Check to make sure that the Attribute class accessor coderefs are getting
# created.
ok( $set = $type->make_attr_set($attr), "Check hashref attr_set" );
ok( $get = $type->make_attr_get($attr), "Check hashref attr_get" );

# Make sure they get and set values correctly.
is( $get->($obj), $href, "Check hashref getter" );
$href = { foo => 'bar' };
ok( $set->($obj, $href), "Check hashref setter" );
is( $get->($obj), $href, "Check hashref getter again" );

##############################################################################
# Check code data type.
ok( $type = Class::Meta::Type->new('code'), 'Get code' );
is( $type, Class::Meta::Type->new('coderef'), 'Check coderef alias' );
is( $type, Class::Meta::Type->new('closure'), 'Check closure alias' );
is( $type->key, 'code', "Check code key" );
is( $type->name, 'Code Reference', "Check code name" );
is( ref $type->check, 'ARRAY', "Check code check" );
foreach my $chk (@{ $type->check }) {
    is( ref $chk, 'CODE', 'Check code code');
}

# Check to make sure that the accessor is created properly. Start with a
# simple set_ method.
ok( $attr = $cm->add_attribute( name => $aname . ++$i, type => 'string'),
    "Create $aname$i attribute" );
ok( $type->build(__PACKAGE__, $attr, Class::Meta::GETSET),
    "Make simple coderef set" );
ok( $mut = UNIVERSAL::can(__PACKAGE__, "set_$aname$i"),
    "Coderef mutator exists");
ok( $acc = UNIVERSAL::can(__PACKAGE__, "get_$aname$i"),
    "Coderef getter exists");

# Test it.
my $cref = sub {};
ok( $obj->$mut($cref), "Set coderef value" );
is( $obj->$acc, $cref, "Check coderef value" );

# Make it fail the checks.
eval { $obj->$mut('foo') };
ok( $err = $@, "Got invalid coderef error" );
like( $err, qr/^Value .* is not a valid Code Reference/,
      'correct coderef exception' );

# Check to make sure that the Attribute class accessor coderefs are getting
# created.
ok( $set = $type->make_attr_set($attr), "Check coderef attr_set" );
ok( $get = $type->make_attr_get($attr), "Check coderef attr_get" );

# Make sure they get and set values correctly.
is( $get->($obj), $cref, "Check coderef getter" );
$cref = sub { 'foo' };
ok( $set->($obj, $cref), "Check coderef setter" );
is( $get->($obj), $cref, "Check coderef getter again" );