9 # A couple of simple classes to use as struct elements.
12 sub new { bless {}, shift }
16 sub new { bless {}, shift }
19 # The first of our Class::Struct based objects.
23 use Class::Struct 'struct'; # test out both forms
24 use Class::Struct SomeClass => { SomeElem => '$' };
26 struct( s => '$', a => '@', h => '%', c => 'aClass' );
29 # The second Class::Struct objects:
30 # test the 'compile-time without package name' feature.
33 use Class::Struct s => '$', a => '@', h => '%', c => 'aClass';
36 # test overriden accessors
38 package OverrideAccessor;
42 no warnings qw(Class::Struct);
43 struct( 'OverrideAccessor', { count => '$' } );
47 my ($self,$count) = @_;
50 $self->{'OverrideAccessor::count'} = $count + 9;
53 return $self->{'OverrideAccessor::count'};
69 isa_ok $obj->a, 'ARRAY';
70 $obj->a(2, 'secundus');
71 is $obj->a(2), 'secundus';
76 isa_ok $obj->h, 'HASH';
80 $obj->h({h=>7,r=>8,f=>9});
85 $obj = MyObj->new( c => aClass->new );
86 isa_ok $obj->c, 'aClass';
87 is $obj->c->meth(), 42;
91 isa_ok $obj, 'MyOther';
96 isa_ok $obj->a, 'ARRAY';
97 $obj->a(2, 'secundus');
98 is $obj->a(2), 'secundus';
103 isa_ok $obj->h, 'HASH';
107 $obj->h({h=>7,r=>8,f=>9});
112 $obj = MyOther->new( c => aClass->new );
113 isa_ok $obj->c, 'aClass';
114 is $obj->c->meth(), 42;
118 my $obk = SomeClass->new();
120 is $obk->SomeElem(), 123;
122 my $recobj = RecClass->new();
123 isa_ok $recobj, 'RecClass';
125 my $override_obj = OverrideAccessor->new( count => 3 );
126 is $override_obj->count, 12;
128 $override_obj->count( 1 );
129 is $override_obj->count, 10;
132 use Class::Struct Kapow => { z_zwap => 'Regexp', sploosh => 'MyObj' };
134 is eval { main->new(); }, undef,
135 'No new method injected into current package';
137 my $obj3 = Kapow->new();
139 isa_ok $obj3, 'Kapow';
140 is $obj3->z_zwap, undef, 'No z_zwap member by default';
141 is $obj3->sploosh, undef, 'No sploosh member by default';
143 isa_ok $obj3->z_zwap, 'Regexp', 'Can set z_zwap member';
144 $obj3->sploosh(MyObj->new(s => 'pie'));
145 isa_ok $obj3->sploosh, 'MyObj',
146 'Can set sploosh member to object of correct class';
147 is $obj3->sploosh->s, 'pie', 'Can set sploosh member to correct object';
149 my $obj4 = Kapow->new( z_zwap => qr//, sploosh => MyObj->new(a => ['Good']) );
151 isa_ok $obj4, 'Kapow';
152 isa_ok $obj4->z_zwap, 'Regexp', 'Initialised z_zwap member';
153 isa_ok $obj4->sploosh, 'MyObj', 'Initialised sploosh member';
154 is_deeply $obj4->sploosh->a, ['Good'], 'with correct object';
156 my $obj5 = Kapow->new( sploosh => { h => {perl => 'rules'} } );
158 isa_ok $obj5, 'Kapow';
159 is $obj5->z_zwap, undef, 'No z_zwap member by default';
160 isa_ok $obj5->sploosh, 'MyObj', 'Initialised sploosh member from hash';
161 is_deeply $obj5->sploosh->h, { perl => 'rules'} , 'with correct object';
165 struct( s => '$', a => '@', h => '%', c => 'aClass' );
166 }, undef, 'Calling struct a second time fails';
168 like $@, qr/^function 'new' already defined in package MyObj/,
169 'fails with the expected error';
171 is eval { MyObj->new( a => {} ) }, undef,
172 'Using a hash where an array reference is expected';
173 like $@, qr/^Initializer for a must be array reference/,
174 'fails with the expected error';
176 is eval { MyObj->new( h => [] ) }, undef,
177 'Using an array where a hash reference is expected';
178 like $@, qr/^Initializer for h must be hash reference/,
179 'fails with the expected error';
181 is eval { Kapow->new( sploosh => { h => [perl => 'rules'] } ); }, undef,
182 'Using an array where a hash reference is expected in an initialiser list';
183 like $@, qr/^Initializer for h must be hash reference/,
184 'fails with the expected error';
186 is eval { Kapow->new( sploosh => [ h => {perl => 'rules'} ] ); }, undef,
187 "Using an array for a member object's initialiser list";
188 like $@, qr/^Initializer for sploosh must be hash or MyObj reference/,
189 'fails with the expected error';
193 use Class::Struct 'struct';
194 struct( 'pow' => '@$%!' );
195 }, undef, 'Bad type fails';
196 like $@, qr/^'\@\$\%\!' is not a valid struct element type/,
197 'with the expected error';
200 $obj3->sploosh(MyOther->new(s => 3.14));
201 }, undef, 'Setting member to the wrong class of object fails';
202 like $@, qr/^sploosh argument is wrong class/,
203 'with the expected error';
204 is $obj3->sploosh->s, 'pie', 'Object is unchanged';
207 $obj3->sploosh(MyObj->new(s => 3.14), 'plop');
208 }, undef, 'Too many arguments to setter fails';
209 like $@, qr/^Too many args to sploosh/,
210 'with the expected error';
211 is $obj3->sploosh->s, 'pie', 'Object is unchanged';
215 use Class::Struct 'struct';
216 struct( Blurp => {}, 'Bonus!' );
217 }, undef, 'hash based class with extra argument fails';
218 like $@, qr/\Astruct usage error.*\n.*\n/,
219 'with the expected confession';
223 use Class::Struct 'struct';
224 struct( Zamm => [], 'Bonus!' );
225 }, undef, 'array based class with extra argument fails';
226 like $@, qr/\Astruct usage error.*\n.*\n/,
227 'with the expected confession';
231 use Class::Struct 'struct';
232 struct( Thwapp => ['Bonus!'] );
233 }, undef, 'array based class with extra constructor argument fails';
234 like $@, qr/\Astruct usage error.*\n.*\n/,
235 'with the expected confession';
239 use Class::Struct 'struct';
240 struct( z_zwap => 'Regexp', sploosh => 'MyObj', 'Bonus' );
241 }, undef, 'default array based class with extra constructor argument fails';
242 like $@, qr/\Astruct usage error.*\n.*\n/,
243 'with the expected confession';
247 use parent -norequire, 'Urkkk';
248 use Class::Struct 'struct';
249 struct( beer => 'foamy' );
250 }, undef, '@ISA is not allowed';
251 like $@, qr/^struct class cannot be a subclass \(\@ISA not allowed\)/,
252 'with the expected error';