6 use Scalar::Util qw( isweak );
10 package HasClassAttribute;
13 use MooseX::ClassAttribute;
14 use MooseX::AttributeHelpers;
19 class_has 'ObjectCount' => (
25 class_has 'WeakAttribute' => (
31 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' ],
61 # if it's not lazy it makes a new object before we define
62 # Delegatee's attributes.
64 default => sub { Delegatee->new() },
67 class_has 'Mapping' => (
68 metaclass => 'Collection::Hash',
70 isa => 'HashRef[Str]',
71 default => sub { {} },
73 exists => 'ExistsInMapping',
74 keys => 'IdsInMapping',
80 class_has 'Built' => (
82 builder => '_BuildIt',
85 class_has 'LazyBuilt' => (
88 builder => '_BuildIt',
91 class_has 'Triggerish' => (
93 trigger => sub { shift->_CallTrigger(@_) },
107 $self->ObjectCount( $self->ObjectCount() + 1 );
115 push @Triggered, [@_];
121 $class->meta()->make_immutable();
122 Delegatee->meta()->make_immutable();
148 use MooseX::ClassAttribute;
150 extends 'HasClassAttribute';
152 class_has '+ReadOnlyAttribute' => ( default => 30 );
154 class_has 'YetAnotherAttribute' => (
163 local $Test::Builder::Level = $Test::Builder::Level + 1;
167 HasClassAttribute->ObjectCount(), 0,
171 my $hca1 = HasClassAttribute->new();
174 'size is 5 - object attribute works as expected'
177 HasClassAttribute->ObjectCount(), 1,
181 my $hca2 = HasClassAttribute->new( size => 10 );
184 'size is 10 - object attribute can be set via constructor'
187 HasClassAttribute->ObjectCount(), 2,
191 $hca2->ObjectCount(), 2,
192 'ObjectCount() is 2 - can call class attribute accessor on object'
197 my $hca3 = HasClassAttribute->new( ObjectCount => 20 );
199 $hca3->ObjectCount(), 3,
200 'class attributes passed to the constructor do not get set in the object'
203 HasClassAttribute->ObjectCount(), 3,
204 'class attributes are not affected by constructor params'
209 my $object = bless {}, 'Thing';
211 HasClassAttribute->WeakAttribute($object);
216 !defined HasClassAttribute->WeakAttribute(),
217 'weak class attributes are weak'
223 $HasClassAttribute::Lazy, 0,
224 '$HasClassAttribute::Lazy is 0'
228 HasClassAttribute->LazyAttribute(), 1,
229 'HasClassAttribute->LazyAttribute() is 1'
233 $HasClassAttribute::Lazy, 1,
234 '$HasClassAttribute::Lazy is 1 after calling LazyAttribute'
239 eval { HasClassAttribute->ReadOnlyAttribute(20) };
241 $@, qr/\QCannot assign a value to a read-only accessor/,
242 'cannot set read-only class attribute'
248 Child->ReadOnlyAttribute(), 30,
249 q{Child class can extend parent's class attribute}
255 !HasClassAttribute->HasM(),
256 'HasM() returns false before M is set'
259 HasClassAttribute->SetM(22);
262 HasClassAttribute->HasM(),
263 'HasM() returns true after M is set'
266 HasClassAttribute->M(), 22,
270 HasClassAttribute->ClearM();
273 !HasClassAttribute->HasM(),
274 'HasM() returns false after M is cleared'
280 HasClassAttribute->Delegatee(), 'Delegatee',
281 'has a Delegetee object'
284 HasClassAttribute->units(), 5,
285 'units() delegates to Delegatee and returns 5'
290 my @ids = HasClassAttribute->IdsInMapping();
293 'there are no keys in the mapping yet'
297 !HasClassAttribute->ExistsInMapping('a'),
298 'key does not exist in mapping'
301 HasClassAttribute->SetMapping( a => 20 );
304 HasClassAttribute->ExistsInMapping('a'),
305 'key does exist in mapping'
309 HasClassAttribute->GetMapping('a'), 20,
310 'value for a in mapping is 20'
316 HasClassAttribute->Built(), 42,
317 'attribute with builder works'
321 HasClassAttribute->LazyBuilt(), 42,
322 'attribute with lazy builder works'
327 HasClassAttribute->Triggerish(42);
328 is( scalar @HasClassAttribute::Triggered, 1, 'trigger was called' );
329 is( HasClassAttribute->Triggerish(), 42, 'Triggerish is now 42' );
331 HasClassAttribute->Triggerish(84);
332 is( HasClassAttribute->Triggerish(), 84, 'Triggerish is now 84' );
335 \@HasClassAttribute::Triggered,
337 [qw( HasClassAttribute 42 )],
338 [qw( HasClassAttribute 84 42 )],
340 'trigger passes old value correctly'