xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/Class/Struct.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl -w
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateBEGIN {
4*0Sstevel@tonic-gate	chdir 't' if -d 't';
5*0Sstevel@tonic-gate	@INC = '../lib';
6*0Sstevel@tonic-gate}
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gate#
9*0Sstevel@tonic-gate# A couple of simple classes to use as struct elements.
10*0Sstevel@tonic-gate#
11*0Sstevel@tonic-gatepackage aClass;
12*0Sstevel@tonic-gatesub new { bless {}, shift }
13*0Sstevel@tonic-gatesub meth { 42 }
14*0Sstevel@tonic-gate
15*0Sstevel@tonic-gatepackage RecClass;
16*0Sstevel@tonic-gatesub new { bless {}, shift }
17*0Sstevel@tonic-gate
18*0Sstevel@tonic-gate#
19*0Sstevel@tonic-gate# The first of our Class::Struct based objects.
20*0Sstevel@tonic-gate#
21*0Sstevel@tonic-gatepackage MyObj;
22*0Sstevel@tonic-gateuse Class::Struct;
23*0Sstevel@tonic-gateuse Class::Struct 'struct'; # test out both forms
24*0Sstevel@tonic-gateuse Class::Struct SomeClass => { SomeElem => '$' };
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gatestruct( s => '$', a => '@', h => '%', c => 'aClass' );
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate#
29*0Sstevel@tonic-gate# The second Class::Struct objects:
30*0Sstevel@tonic-gate# test the 'compile-time without package name' feature.
31*0Sstevel@tonic-gate#
32*0Sstevel@tonic-gatepackage MyOther;
33*0Sstevel@tonic-gateuse Class::Struct s => '$', a => '@', h => '%', c => 'aClass';
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate#
36*0Sstevel@tonic-gate# back to main...
37*0Sstevel@tonic-gate#
38*0Sstevel@tonic-gatepackage main;
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gateuse Test::More tests => 24;
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gatemy $obj = MyObj->new;
43*0Sstevel@tonic-gateisa_ok $obj, 'MyObj';
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate$obj->s('foo');
46*0Sstevel@tonic-gateis $obj->s(), 'foo';
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gateisa_ok $obj->a, 'ARRAY';
49*0Sstevel@tonic-gate$obj->a(2, 'secundus');
50*0Sstevel@tonic-gateis $obj->a(2), 'secundus';
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gate$obj->a([4,5,6]);
53*0Sstevel@tonic-gateis $obj->a(1), 5;
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gateisa_ok $obj->h, 'HASH';
56*0Sstevel@tonic-gate$obj->h('x', 10);
57*0Sstevel@tonic-gateis $obj->h('x'), 10;
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate$obj->h({h=>7,r=>8,f=>9});
60*0Sstevel@tonic-gateis $obj->h('r'), 8;
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gateis $obj->c, undef;
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate$obj = MyObj->new( c => aClass->new );
65*0Sstevel@tonic-gateisa_ok $obj->c, 'aClass';
66*0Sstevel@tonic-gateis $obj->c->meth(), 42;
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate$obj = MyOther->new;
70*0Sstevel@tonic-gateisa_ok $obj, 'MyOther';
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate$obj->s('foo');
73*0Sstevel@tonic-gateis $obj->s(), 'foo';
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gateisa_ok $obj->a, 'ARRAY';
76*0Sstevel@tonic-gate$obj->a(2, 'secundus');
77*0Sstevel@tonic-gateis $obj->a(2), 'secundus';
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate$obj->a([4,5,6]);
80*0Sstevel@tonic-gateis $obj->a(1), 5;
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gateisa_ok $obj->h, 'HASH';
83*0Sstevel@tonic-gate$obj->h('x', 10);
84*0Sstevel@tonic-gateis $obj->h('x'), 10;
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate$obj->h({h=>7,r=>8,f=>9});
87*0Sstevel@tonic-gateis $obj->h('r'), 8;
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gateis $obj->c, undef;
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate$obj = MyOther->new( c => aClass->new );
92*0Sstevel@tonic-gateisa_ok $obj->c, 'aClass';
93*0Sstevel@tonic-gateis $obj->c->meth(), 42;
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gatemy $obk = SomeClass->new();
98*0Sstevel@tonic-gate$obk->SomeElem(123);
99*0Sstevel@tonic-gateis $obk->SomeElem(), 123;
100*0Sstevel@tonic-gate
101*0Sstevel@tonic-gatemy $recobj = RecClass->new();
102*0Sstevel@tonic-gateisa_ok $recobj, 'RecClass';
103*0Sstevel@tonic-gate
104