6 use Scalar::Util qw( isweak );
11 package HasClassAttribute;
14 use MooseX::ClassAttribute;
15 use MooseX::AttributeHelpers;
20 class_has 'ObjectCount' =>
26 class_has 'WeakAttribute' =>
32 class_has 'LazyAttribute' =>
36 # The side effect is used to test that this was called
38 default => sub { $Lazy = 1 },
41 class_has 'ReadOnlyAttribute' =>
47 class_has 'ManyNames' =>
56 class_has 'Delegatee' =>
59 handles => [ 'units', 'color' ],
60 # if it's not lazy it makes a new object before we define
61 # Delegatee's attributes.
63 default => sub { Delegatee->new() },
66 class_has 'Mapping' =>
67 ( metaclass => 'Collection::Hash',
69 isa => 'HashRef[Str]',
70 default => sub { {} },
72 { exists => 'ExistsInMapping',
73 keys => 'IdsInMapping',
81 builder => '_BuildIt',
84 class_has 'LazyBuilt' =>
87 builder => '_BuildIt',
90 class_has 'Triggerish' =>
92 trigger => sub { shift->_CallTrigger(@_) },
107 $self->ObjectCount( $self->ObjectCount() + 1 );
115 push @Triggered, [@_];
122 $class->meta()->make_immutable();
123 Delegatee->meta()->make_immutable();
149 use MooseX::ClassAttribute;
151 extends 'HasClassAttribute';
153 class_has '+ReadOnlyAttribute' =>
156 class_has 'YetAnotherAttribute' =>
168 local $Test::Builder::Level = $Test::Builder::Level + 1;
171 is( HasClassAttribute->ObjectCount(), 0,
172 'ObjectCount() is 0' );
174 my $hca1 = HasClassAttribute->new();
175 is( $hca1->size(), 5,
176 'size is 5 - object attribute works as expected' );
177 is( HasClassAttribute->ObjectCount(), 1,
178 'ObjectCount() is 1' );
180 my $hca2 = HasClassAttribute->new( size => 10 );
181 is( $hca2->size(), 10,
182 'size is 10 - object attribute can be set via constructor' );
183 is( HasClassAttribute->ObjectCount(), 2,
184 'ObjectCount() is 2' );
185 is( $hca2->ObjectCount(), 2,
186 'ObjectCount() is 2 - can call class attribute accessor on object' );
190 my $hca3 = HasClassAttribute->new( ObjectCount => 20 );
191 is( $hca3->ObjectCount(), 3,
192 'class attributes passed to the constructor do not get set in the object' );
193 is( HasClassAttribute->ObjectCount(), 3,
194 'class attributes are not affected by constructor params' );
198 my $object = bless {}, 'Thing';
200 HasClassAttribute->WeakAttribute($object);
204 ok( ! defined HasClassAttribute->WeakAttribute(),
205 'weak class attributes are weak' );
209 is( $HasClassAttribute::Lazy, 0,
210 '$HasClassAttribute::Lazy is 0' );
212 is( HasClassAttribute->LazyAttribute(), 1,
213 'HasClassAttribute->LazyAttribute() is 1' );
215 is( $HasClassAttribute::Lazy, 1,
216 '$HasClassAttribute::Lazy is 1 after calling LazyAttribute' );
220 eval { HasClassAttribute->ReadOnlyAttribute(20) };
221 like( $@, qr/\QCannot assign a value to a read-only accessor/,
222 'cannot set read-only class attribute' );
226 is( Child->ReadOnlyAttribute(), 30,
227 q{Child class can extend parent's class attribute} );
231 ok( ! HasClassAttribute->HasM(),
232 'HasM() returns false before M is set' );
234 HasClassAttribute->SetM(22);
236 ok( HasClassAttribute->HasM(),
237 'HasM() returns true after M is set' );
238 is( HasClassAttribute->M(), 22,
241 HasClassAttribute->ClearM();
243 ok( ! HasClassAttribute->HasM(),
244 'HasM() returns false after M is cleared' );
248 isa_ok( HasClassAttribute->Delegatee(), 'Delegatee',
249 'has a Delegetee object' );
250 is( HasClassAttribute->units(), 5,
251 'units() delegates to Delegatee and returns 5' );
255 my @ids = HasClassAttribute->IdsInMapping();
257 'there are no keys in the mapping yet' );
259 ok( ! HasClassAttribute->ExistsInMapping('a'),
260 'key does not exist in mapping' );
262 HasClassAttribute->SetMapping( a => 20 );
264 ok( HasClassAttribute->ExistsInMapping('a'),
265 'key does exist in mapping' );
267 is( HasClassAttribute->GetMapping('a'), 20,
268 'value for a in mapping is 20' );
272 is( HasClassAttribute->Built(), 42,
273 'attribute with builder works' );
275 is( HasClassAttribute->LazyBuilt(), 42,
276 'attribute with lazy builder works' );
280 HasClassAttribute->Triggerish(42);
281 is( scalar @HasClassAttribute::Triggered, 1, 'trigger was called' );
282 is( HasClassAttribute->Triggerish(), 42, 'Triggerish is now 42' );
284 HasClassAttribute->Triggerish(84);
285 is( HasClassAttribute->Triggerish(), 84, 'Triggerish is now 84' );
287 is_deeply( \@HasClassAttribute::Triggered,
288 [ [ qw( HasClassAttribute 42 ) ],
289 [ qw( HasClassAttribute 84 42 ) ],
291 'trigger passes old value correctly' );