This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make the Class::Struct import() wiser.
[perl5.git] / t / lib / class-struct.t
1 #!./perl -w
2
3 BEGIN {
4         chdir 't' if -d 't';
5         @INC = '../lib';
6 }
7
8 print "1..8\n";
9
10 package aClass;
11
12 sub new { bless {}, shift }
13
14 sub meth { 42 }
15
16 package MyObj;
17
18 use Class::Struct;
19 use Class::Struct 'struct'; # test out both forms
20
21 use Class::Struct SomeClass => { SomeElem => '$' };
22
23 struct( s => '$', a => '@', h => '%', c => 'aClass' );
24
25 my $obj = MyObj->new;
26
27 $obj->s('foo');
28
29 print "not " unless $obj->s() eq 'foo';
30 print "ok 1\n";
31
32 my $arf = $obj->a;
33
34 print "not " unless ref $arf eq 'ARRAY';
35 print "ok 2\n";
36
37 $obj->a(2, 'secundus');
38
39 print "not " unless $obj->a(2) eq 'secundus';
40 print "ok 3\n";
41
42 my $hrf = $obj->h;
43
44 print "not " unless ref $hrf eq 'HASH';
45 print "ok 4\n";
46
47 $obj->h('x', 10);
48
49 print "not " unless $obj->h('x') == 10;
50 print "ok 5\n";
51
52 my $orf = $obj->c;
53
54 print "not " unless ref $orf eq 'aClass';
55 print "ok 6\n";
56
57 print "not " unless $obj->c->meth() == 42;
58 print "ok 7\n";
59
60 my $obk = SomeClass->new();
61
62 $obk->SomeElem(123);
63
64 print "not " unless $obk->SomeElem() == 123;
65 print "ok 8\n";
66