~ubuntu-branches/ubuntu/jaunty/libclass-meta-perl/jaunty

« back to all changes in this revision

Viewing changes to t/types.t

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Krzyzaniak (eloy)
  • Date: 2006-01-03 17:29:20 UTC
  • Revision ID: james.westby@ubuntu.com-20060103172920-h94p8qrrav90bzq0
Tags: upstream-0.52
ImportĀ upstreamĀ versionĀ 0.52

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -w
 
2
 
 
3
# $Id: types.t 802 2004-10-28 23:21:10Z theory $
 
4
 
 
5
##############################################################################
 
6
# Set up the tests.
 
7
##############################################################################
 
8
 
 
9
use strict;
 
10
use Test::More tests => 58;
 
11
 
 
12
##############################################################################
 
13
# Create a simple class.
 
14
##############################################################################
 
15
 
 
16
package Class::Meta::TestTypes;
 
17
use strict;
 
18
 
 
19
BEGIN {
 
20
    $SIG{__DIE__} = \&Carp::confess;
 
21
    main::use_ok( 'Class::Meta');
 
22
    main::use_ok( 'Class::Meta::Type');
 
23
    main::use_ok( 'Class::Meta::Types::Numeric');
 
24
    main::use_ok( 'Class::Meta::Types::Perl');
 
25
    main::use_ok( 'Class::Meta::Types::String');
 
26
    main::use_ok( 'Class::Meta::Types::Boolean');
 
27
    @Bart::ISA = qw(Simpson);
 
28
}
 
29
 
 
30
BEGIN {
 
31
    # Add the new data type.
 
32
    Class::Meta::Type->add( key       => 'simpson',
 
33
                            name      => 'Simpson',
 
34
                            desc      => 'An Simpson object.',
 
35
                            check     => 'Simpson',
 
36
                        );
 
37
 
 
38
    my $c = Class::Meta->new(package => __PACKAGE__,
 
39
                             key     => 'types',
 
40
                             name    => 'Class::Meta::TestTypes Class',
 
41
                             desc    => 'Just for testing Class::Meta.'
 
42
                         );
 
43
    $c->add_constructor(name => 'new');
 
44
 
 
45
    $c->add_attribute( name  => 'name',
 
46
                  view   => Class::Meta::PUBLIC,
 
47
                  type  => 'string',
 
48
                  length   => 256,
 
49
                  label => 'Name',
 
50
                  field => 'text',
 
51
                  desc  => "The person's name.",
 
52
                  required   => 0,
 
53
                  default   => undef,
 
54
                  create   => Class::Meta::GETSET
 
55
              );
 
56
    $c->add_attribute( name  => 'age',
 
57
                  view   => Class::Meta::PUBLIC,
 
58
                  type  => 'integer',
 
59
                  label => 'Age',
 
60
                  field => 'text',
 
61
                  desc  => "The person's age.",
 
62
                  required   => 0,
 
63
                  default   => undef,
 
64
                  create   => Class::Meta::GETSET
 
65
              );
 
66
    $c->add_attribute( name  => 'alive',
 
67
                  view   => Class::Meta::PUBLIC,
 
68
                  type  => 'bool',
 
69
                  label => 'Living',
 
70
                  field => 'checkbox',
 
71
                  desc  => "Is the person alive?",
 
72
                  required   => 0,
 
73
                  default   => 1,
 
74
              );
 
75
    $c->add_attribute( name  => 'whole',
 
76
                  view   => Class::Meta::PUBLIC,
 
77
                  type  => 'whole',
 
78
                  label => 'A whole number.',
 
79
                  field => 'text',
 
80
                  desc  => "A whole number.",
 
81
                  required   => 0,
 
82
                  default   => undef,
 
83
                  create   => Class::Meta::GETSET
 
84
              );
 
85
    $c->add_attribute( name  => 'dec',
 
86
                  view   => Class::Meta::PUBLIC,
 
87
                  type  => 'decimal',
 
88
                  label => 'A decimal number.',
 
89
                  field => 'text',
 
90
                  desc  => "A decimal number.",
 
91
                  required   => 0,
 
92
                  default   => undef,
 
93
                  create   => Class::Meta::GETSET
 
94
              );
 
95
    $c->add_attribute( name  => 'real',
 
96
                  view   => Class::Meta::PUBLIC,
 
97
                  type  => 'real',
 
98
                  label => 'A real number.',
 
99
                  field => 'text',
 
100
                  desc  => "A real number.",
 
101
                  required   => 0,
 
102
                  default   => undef,
 
103
                  create   => Class::Meta::GETSET
 
104
              );
 
105
    $c->add_attribute( name  => 'float',
 
106
                  view   => Class::Meta::PUBLIC,
 
107
                  type  => 'float',
 
108
                  label => 'A float.',
 
109
                  field => 'text',
 
110
                  desc  => "A floating point number.",
 
111
                  required   => 0,
 
112
                  default   => undef,
 
113
                  create   => Class::Meta::GETSET
 
114
              );
 
115
    $c->add_attribute( name  => 'scalar',
 
116
                  view   => Class::Meta::PUBLIC,
 
117
                  type  => 'scalarref',
 
118
                  label => 'A scalar.',
 
119
                  field => 'text',
 
120
                  desc  => "A scalar reference.",
 
121
                  required   => 0,
 
122
                  default   => undef,
 
123
                  create   => Class::Meta::GETSET
 
124
              );
 
125
    $c->add_attribute( name  => 'array',
 
126
                  view   => Class::Meta::PUBLIC,
 
127
                  type  => 'array',
 
128
                  label => 'A array.',
 
129
                  field => 'text',
 
130
                  desc  => "A array reference.",
 
131
                  required   => 0,
 
132
                  default   => undef,
 
133
                  create   => Class::Meta::GETSET
 
134
              );
 
135
    $c->add_attribute( name  => 'hash',
 
136
                  view   => Class::Meta::PUBLIC,
 
137
                  type  => 'hash',
 
138
                  label => 'A hash.',
 
139
                  field => 'text',
 
140
                  desc  => "A hash reference.",
 
141
                  required   => 0,
 
142
                  default   => undef,
 
143
                  create   => Class::Meta::GETSET
 
144
              );
 
145
    $c->add_attribute( name  => 'simpson',
 
146
                  view   => Class::Meta::PUBLIC,
 
147
                  type  => 'simpson',
 
148
                  label => 'A Simpson Object',
 
149
                  field => 'text',
 
150
                  desc  => 'A Simpson object.',
 
151
                  required   => 0,
 
152
                  default => sub { bless {}, 'Simpson' },
 
153
                  create   => Class::Meta::GETSET
 
154
              );
 
155
 
 
156
    $c->build;
 
157
}
 
158
 
 
159
 
 
160
##############################################################################
 
161
# Do the tests.
 
162
##############################################################################
 
163
 
 
164
package main;
 
165
# Instantiate a base class object and test its accessors.
 
166
ok( my $t = Class::Meta::TestTypes->new, 'Class::Meta::TestTypes->new');
 
167
 
 
168
# Grab its metadata object.
 
169
ok( my $class = $t->my_class, "Get the Class::Meta::Class object" );
 
170
 
 
171
# Test the is_a() method.
 
172
ok( $class->is_a('Class::Meta::TestTypes'), 'Class isa TestTypes');
 
173
 
 
174
# Test the key methods.
 
175
is( $class->key, 'types', 'Key is correct');
 
176
 
 
177
# Test the name method.
 
178
is( $class->name, 'Class::Meta::TestTypes Class', "Name is correct");
 
179
 
 
180
# Test the description methods.
 
181
is( $class->desc, 'Just for testing Class::Meta.',
 
182
    "Description is correct");
 
183
 
 
184
# Test string.
 
185
ok( $t->name('David'), 'name to "David"' );
 
186
is( $t->name, 'David', 'name is "David"' );
 
187
eval { $t->name([]) };
 
188
ok( my $err = $@, 'name to array ref croaks' );
 
189
like( $err, qr/^Value .* is not a valid string/, 'correct string exception' );
 
190
 
 
191
# Test boolean.
 
192
ok( $t->alive, 'alive true');
 
193
is( $t->alive(0), 0, 'alive off');
 
194
ok( !$t->alive, 'alive false');
 
195
ok( $t->alive(1), 'alive on' );
 
196
ok( $t->alive, 'alive true again');
 
197
ok( my $alive = $class->attributes('alive'), "Get alive attribute object" );
 
198
is( $alive->type, 'boolean', "Check that the alias was converted" );
 
199
 
 
200
# Test whole number.
 
201
eval { $t->whole(0) };
 
202
ok( $err = $@, 'whole to 0 croaks' );
 
203
like( $err, qr/^Value '0' is not a valid whole number/,
 
204
     'correct whole number exception' );
 
205
ok( $t->whole(1), 'whole to 1.');
 
206
 
 
207
# Test integer.
 
208
eval { $t->age(0.5) };
 
209
ok( $err = $@, 'age to 0.5 croaks');
 
210
like( $err, qr/^Value '0\.5' is not a valid integer/,
 
211
     'correct integer exception' );
 
212
ok( $t->age(10), 'age to 10.');
 
213
 
 
214
# Test decimal.
 
215
eval { $t->dec('+') };
 
216
ok( $err = $@, 'dec to "+" croaks');
 
217
like( $err, qr/^Value '\+' is not a valid decimal number/,
 
218
     'correct decimal exception' );
 
219
ok( $t->dec(3.14), 'dec to 3.14.');
 
220
 
 
221
# Test real.
 
222
eval { $t->real('+') };
 
223
ok( $err = $@, 'real to "+" croaks');
 
224
like( $err, qr/^Value '\+' is not a valid real number/,
 
225
     'correct real exception' );
 
226
ok( $t->real(123.4567), 'real to 123.4567.');
 
227
ok( $t->real(-123.4567), 'real to -123.4567.');
 
228
 
 
229
# Test float.
 
230
eval { $t->float('+') };
 
231
ok( $err = $@, 'float to "+" croaks');
 
232
like( $err, qr/^Value '\+' is not a valid floating point number/,
 
233
     'correct float exception' );
 
234
ok( $t->float(1.23e99), 'float to 1.23e99.');
 
235
 
 
236
# Test OBJECT with default specifying object type.
 
237
ok( my $simpson = $t->simpson, 'simpson' );
 
238
isa_ok($simpson, 'Simpson');
 
239
eval { $t->simpson('foo') };
 
240
ok( $err = $@, 'simpson to "foo" croaks' );
 
241
like( $err, qr/^Value 'foo' is not a valid Simpson/,
 
242
     'correct object exception' );
 
243
 
 
244
# Try a wrong object.
 
245
eval { $t->simpson($t) };
 
246
ok( $err = $@, 'simpson to \$fh croaks' );
 
247
like( $err, qr/^Value '.*' is not a valid Simpson/,
 
248
     'correct object exception' );
 
249
ok( $t->simpson($simpson), 'simpson to \$simpson.');
 
250
 
 
251
# Try a subclass.
 
252
my $bart = bless {}, 'Bart';
 
253
ok( $t->simpson($bart), "Set simpson to a subclass." );
 
254
isa_ok($t->simpson, 'Bart', "Check subclass" );
 
255
ok( $t->simpson($simpson), 'simpson to \$simpson.');
 
256
 
 
257
# Test SCALAR.
 
258
eval { $t->scalar('foo') };
 
259
ok( $err = $@, 'scalar to "foo" croaks' );
 
260
like( $err, qr/^Value 'foo' is not a valid Scalar Reference/,
 
261
     'correct scalar exception' );
 
262
ok( $t->scalar(\"foo"), 'scalar to \\"foo".');
 
263
 
 
264
# Test ARRAY.
 
265
eval { $t->array('foo') };
 
266
ok( $err = $@, 'array to "foo" croaks' );
 
267
like( $err, qr/^Value 'foo' is not a valid Array Reference/,
 
268
     'correct array exception' );
 
269
ok( $t->array(["foo"]), 'array to ["foo"].');
 
270
 
 
271
# Test HASH.
 
272
eval { $t->hash('foo') };
 
273
ok( $err = $@, 'hash to "foo" croaks' );
 
274
like( $err, qr/^Value 'foo' is not a valid Hash Reference/,
 
275
     'correct hash exception' );
 
276
ok( $t->hash({ foo => 1 }), 'hash to { foo => 1 }.');