File: inherit.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 (317 lines) | stat: -rw-r--r-- 13,295 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
#!perl -w

# $Id: inherit.t 801 2004-10-28 22:33:20Z theory $

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

use strict;
use Test::More tests => 129;

##############################################################################
# Create a simple class.
##############################################################################

package Test::One;

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     => 'one',
                                  package => __PACKAGE__,
                                  name    => 'One Class',
                                  desc    => 'Test One Class.'),
        "Create One's Class::Meta" );

    # Add a constructor.
    ok( $c->add_constructor( name => 'new',
                             create  => 1 ),
        "Create One's construtor" );

    # Add a couple of attributes with created methods.
    ok( $c->add_attribute( name     => 'id',
                           view     => Class::Meta::PUBLIC,
                           authz    => Class::Meta::READ,
                           create   => Class::Meta::GET,
                           type     => 'integer',
                           label    => 'ID',
                           desc     => "The object's ID.",
                           required => 1,
                           default  => 12,
                       ),
        "Create One's ID attribute" );

    ok( $c->add_attribute( name     => 'name',
                           view     => Class::Meta::PUBLIC,
                           authz    => Class::Meta::RDWR,
                           create   => Class::Meta::GETSET,
                           type     => 'string',
                           label    => 'Name',
                           desc     => "The object's name.",
                           required => 1,
                           default  => 'foo',
                       ),
        "Create One's name attribute" );

    ok( $c->add_attribute( name     => 'count',
                           view     => Class::Meta::PUBLIC,
                           authz    => Class::Meta::RDWR,
                           create   => Class::Meta::GETSET,
                           context  => Class::Meta::CLASS,
                           type     => 'integer',
                           label    => 'Count',
                           desc     => "The object count.",
                           default  => 0,
                       ),
        "Create One's count attribute" );

    ok( $c->add_method(name => 'foo'), "Add foo method to One" );
    ok( $c->add_method(name => 'bar'), "Add bar method to One" );
    ok( $c->build, "Build Test::One" );
}
sub foo { __PACKAGE__ }
sub bar { __PACKAGE__ }

package Test::Two;
use base 'Test::One';

BEGIN {
    Test::More->import;
    main::use_ok( 'Class::Meta');
}

BEGIN {
    ok( my $c = Class::Meta->new( key     => 'two',
                                  package => __PACKAGE__,
                                  name    => 'Two Class',
                                  desc    => 'Test Two Class.'),
        "Create Two's Class::Meta" );

    # Add another constructor.
    ok( $c->add_constructor(name => 'two_new'), "Create Two's ctor" );

    # Add an attribute.
    ok( $c->add_attribute( name     => 'description',
                           view     => Class::Meta::PUBLIC,
                           authz    => Class::Meta::RDWR,
                           create   => Class::Meta::GETSET,
                           type     => 'string',
                           label    => 'Description',
                           desc     => "The object's description.",
                           required => 1,
                           default  => '',
                       ),
        "Create Two's description attribute" );

    # Make sure that adding an attribute with the same name as in a parent class
    # causes an exception.
    eval {
        $c->add_attribute( name     => 'name',
                           view     => Class::Meta::PUBLIC,
                           authz    => Class::Meta::RDWR,
                           create   => Class::Meta::GETSET,
                           type     => 'string',
                           label    => 'Name',
                           desc     => "The object's name.",
                           required => 1,
                           default  => '',
                       )
    };

    ok( my $err = $@, "Catch duplicate attribute exception" );
    like( $err, qr/Attribute 'name' already exists in class 'Test::One'/,
          "Check error message" );

    # But allow an attribute with the same name to be added using the override
    # parameter.
    $c->add_attribute( name     => 'name',
                       view     => Class::Meta::PUBLIC,
                       authz    => Class::Meta::RDWR,
                       create   => Class::Meta::GETSET,
                       type     => 'string',
                       label    => 'Overridden Name',
                       desc     => "The object's name.",
                       required => 1,
                       default  => '',
                       override => 1,
                   );

    # Add a method.
    ok( $c->add_method(name => 'woah'), "Add woah method to One" );
    # Add an overriding method.
    ok( $c->add_method(name => 'bar'), "Add bar method to Two" );

    ok( $c->build, "Build Test::Two" );
}

sub woah { __PACKAGE__ }
sub bar { __PACKAGE__ }

package main;

# Check out Test::One's class object.
ok( my $one_class = Test::One->my_class, "Get One's Class object" );
isa_ok( $one_class, 'Class::Meta::Class' );
ok( $one_class->is_a('Test::One'), "Check it's for Test::One" );
ok( ! $one_class->is_a('Test::Two'), "Check it's not for Test::Two" );
ok( ! $one_class->parents, "Check that One has no parents" );

# Check One's attributes.
ok( my @one_attributes = $one_class->attributes, "Get attributes" );
is( scalar @one_attributes, 3, "Check for three attributes" );
is( $one_attributes[0]->name, 'id', "Check for id attribute" );
is( $one_attributes[1]->name, 'name', "Check for name attribute" );
is( $one_attributes[2]->name, 'count', "Check for count attribute" );

# Check out Test::Two's class object.
ok( my $two_class = Test::Two->my_class, "Get Two's Class object" );
isa_ok( $two_class, 'Class::Meta::Class' );
ok( $two_class->is_a('Test::One'), "Check it's for Test::One" );
ok( $two_class->is_a('Test::Two'), "Check it's for Test::Two" );
is( ($two_class->parents)[0], $one_class, "Check that Two has One for a parent" );

# Check Two's attributes.
ok( my @two_attributes = $two_class->attributes, "Get attributes" );
is( scalar @two_attributes, 4, "Check for four attributes" );
is( $two_attributes[0]->name, 'id', "Check for id attribute" );
is( $one_attributes[0], $two_attributes[0], "Check for same id as One" );
is( $two_attributes[1]->name, 'name', "Check for name attribute" );
isnt( $one_attributes[1], $two_attributes[1], "Check for different name than One" );
is( $two_attributes[1]->label, 'Overridden Name', 'Check for overridden name' );
is( $two_attributes[2]->name, 'count', "Check for count attribute" );
is( $one_attributes[2], $two_attributes[2], "Check for same count as One" );
is( $two_attributes[3]->name, 'description', "Check for description attribute" );

# Make sure that One's new() constructor works.
ok( my $one = Test::One->new( name => 'foo'), "Construct One object" );
isa_ok( $one, 'Test::One' );
eval { Test::One->new(name => 'foo',  description => 'bar') };
ok( my $err = $@, 'Catch bad One parameter exception' );
like( $err, qr/No such attribute 'description' in Test::One/,
      'Check bad One exception' );

# Make sure that One's new constructor object works.
ok( my $one_new = $one_class->constructors('new'), "Get one's new object" );
ok( $one = $one_new->call('Test::One'), "Create new one indirectly" );
isa_ok( $one, 'Test::One' );

# Check One's attribute accessors.
is( $one->get_name, 'foo', "Check One's name" );
ok( $one->set_name('hello'), "Set One's name" );
is( $one->get_name, 'hello', "Check One's new name" );
is( $one->get_id, 12, "Check One's id" );
eval { $one->set_id(1) };
ok( $err = $@, "Check for set_id exception" );

# Check One's attribute object accessors.
is( $one_attributes[0]->get($one), 12, "Check attr call id" );
ok( $one_attributes[1]->set($one, 'howdy'), "Call set on One" );
is( $one_attributes[1]->get($one), 'howdy', "Call get on One" );

# Check One's methods.
is( $one->foo, 'Test::One', "Check One->foo" );
is( $one->bar, 'Test::One', "Check One->bar" );
eval { $one->woah };
ok( $err = $@, "Catch One->woah exception" );

# Check One's method objects.
ok( my $foo = $one_class->methods('foo'), "Get foo method object" );
is( $foo->package, 'Test::One', "Check One foo's package" );
is( $foo->call($one), 'Test::One', "Check One foo's call" );
ok( my $bar = $one_class->methods('bar'), "Get bar method object" );
is( $bar->package, 'Test::One', "Check One bar's package" );
is( $bar->call($one), 'Test::One', "Check One bar's call" );

# Make sure that Two inherits new() and works with its attributes.
ok( my $two = Test::Two->new( name => 'foo'), "Construct Two object" );
isa_ok( $two, 'Test::Two' );
ok( $two = Test::Two->new(name => 'foo',  description => 'bar'),
    "Construct another Two object" );
isa_ok( $two, 'Test::Two' );

# Make sure that One's new constructor object works.
ok( my $two_new = $two_class->constructors('new'), "Get two's new object" );
is( $two_new, $one_new, 'Check for the same new as in one' );
ok( $two = $one_new->call('Test::Two'), "Create new two indirectly" );
isa_ok( $two, 'Test::Two' );

# make sure that Two's own constructor works, too.
ok( $two = Test::Two->two_new(name => 'Larry'),
    "Construct another Two object" );
isa_ok( $two, 'Test::Two' );

# Check Two's attribute accessors.
is( $two->get_id, 12, "Check Two's id" );
eval { $two->set_id(1) };
ok( $err = $@, "Check for set_id exception" );
is( $two->get_name, 'Larry', "Check Two's name" );
ok( $two->set_name('hello'), "Set Two's name" );
is( $two->get_name, 'hello', "Check Two's new name" );

is( $two->get_count, 0, "Check Two's count" );
ok( $two->set_count(12), "Set Two's count" );
is( $two->get_count, 12, "Check Two's new count" );

is( $two->get_description, '', "Check Two's description" );
ok( $two->set_description('yello'), "Set Two's description" );
is( $two->get_description, 'yello', "Check Two's new description" );

# Check Two's attribute object accessors.
is( $two_attributes[0]->get($two), 12, "Check attr call id" );

is( $two_attributes[1]->get($two), 'hello', "Call get name on Two" );
ok( $two_attributes[1]->set($two, 'howdy'), "Call set name on Two" );
is( $two_attributes[1]->get($two), 'howdy', "Call get name on Two again" );

is( $two_attributes[2]->get($two), 12, "Call get count on Two" );
ok( $two_attributes[2]->set($two, 10), "Call set count on Two" );
is( $two_attributes[2]->get($two), 10, "Call get count on Two again" );

is( $two_attributes[3]->get($two), 'yello', "Call get on Two" );
ok( $two_attributes[3]->set($two, 'rowdy'), "Call set on Two" );
is( $two_attributes[3]->get($two), 'rowdy', "Call get on Two again" );

# Make sure that the count class attribute accessors work as expected.
is( $one->get_count, 10, 'Check one get_count' );
is( $two->get_count, 10, 'Check two get_count' );
is( Test::One->get_count, 10, 'Check Test::One get_count' );
is( Test::Two->get_count, 10, 'Check Test::Two get_count' );

ok( Test::One->set_count(22), 'Set One count' );
is( $one->get_count, 22, 'Check one get_count again' );
is( $two->get_count, 22, 'Check two get_count again' );
is( Test::One->get_count, 22, 'Check Test::One get_count again' );
is( Test::Two->get_count, 22, 'Check Test::Two get_count again' );

ok( $one->set_count(35), 'Set $one count' );
is( $one->get_count, 35, 'Check one get_count three' );
is( $two->get_count, 35, 'Check two get_count three' );
is( Test::One->get_count, 35, 'Check Test::One get_count three' );
is( Test::Two->get_count, 35, 'Check Test::Two get_count three' );

# Check Two's methods.
is( $two->foo, 'Test::One', 'Check Two->foo' );
is( $two->bar, 'Test::Two', 'Check Two->bar' );
is( $two->woah, 'Test::Two', 'Check Two->woah' );

# Check Two's methods.
is( $two->foo, 'Test::One', "Check Two->foo" );
is( $two->bar, 'Test::Two', "Check Two->bar" );
is( $two->woah, 'Test::Two', "Check Two->woah" );

# Check Two's method objects.
ok( $foo = $two_class->methods('foo'), "Get foo method object" );
is( $foo->package, 'Test::One', "Check Two foo's package" );
is( $foo->call($two), 'Test::One', "Check Two foo's call" );
ok( $bar = $two_class->methods('bar'), "Get bar method object" );
is( $bar->package, 'Test::Two', "Check Two bar's package" );
is( $bar->call($two), 'Test::Two', "Check Two bar's call" );
ok( my $woah = $two_class->methods('woah'), "Get woah method object" );
is( $woah->package, 'Test::Two', "Check Two woah's package" );
is( $woah->call($two), 'Test::Two', "Check Two woah's call" );