6 use Scalar::Util qw( isweak );
27 # The side effect is used to test that this was called
29 default => sub { $Lazy = 1 },
31 ReadOnlyAttribute => {
47 handles => [ 'units', 'color' ],
48 # if it's not lazy it makes a new object before we define
49 # Delegatee's attributes.
51 default => sub { Delegatee->new() },
56 isa => 'HashRef[Str]',
57 default => sub { {} },
59 'ExistsInMapping' => 'exists',
60 'IdsInMapping' => 'keys',
61 'GetMapping' => 'get',
62 'SetMapping' => 'set',
67 builder => '_BuildIt',
72 builder => '_BuildIt',
76 trigger => sub { shift->_CallTrigger(@_) },
81 package HasClassAttribute;
84 use MooseX::ClassAttribute;
86 while ( my ( $name, $def ) = each %SharedTests::Attrs ) {
87 class_has $name => %{$def};
101 $self->ObjectCount( $self->ObjectCount() + 1 );
109 push @Triggered, [@_];
115 $class->meta()->make_immutable();
116 Delegatee->meta()->make_immutable();
142 use MooseX::ClassAttribute;
144 extends 'HasClassAttribute';
146 class_has '+ReadOnlyAttribute' => ( default => 30 );
148 class_has 'YetAnotherAttribute' => (
157 my $class = shift || 'HasClassAttribute';
159 local $Test::Builder::Level = $Test::Builder::Level + 1;
163 $class->ObjectCount(), 0,
167 my $hca1 = $class->new();
170 'size is 5 - object attribute works as expected'
173 $class->ObjectCount(), 1,
177 my $hca2 = $class->new( size => 10 );
180 'size is 10 - object attribute can be set via constructor'
183 $class->ObjectCount(), 2,
187 $hca2->ObjectCount(), 2,
188 'ObjectCount() is 2 - can call class attribute accessor on object'
193 my $hca3 = $class->new( ObjectCount => 20 );
195 $hca3->ObjectCount(), 3,
196 'class attributes passed to the constructor do not get set in the object'
199 $class->ObjectCount(), 3,
200 'class attributes are not affected by constructor params'
205 my $object = bless {}, 'Thing';
207 $class->WeakAttribute($object);
212 !defined $class->WeakAttribute(),
213 'weak class attributes are weak'
219 $SharedTests::Lazy, 0,
220 '$SharedTests::Lazy is 0'
224 $class->LazyAttribute(), 1,
225 '$class->LazyAttribute() is 1'
229 $SharedTests::Lazy, 1,
230 '$SharedTests::Lazy is 1 after calling LazyAttribute'
235 eval { $class->ReadOnlyAttribute(20) };
237 $@, qr/\QCannot assign a value to a read-only accessor/,
238 'cannot set read-only class attribute'
244 Child->ReadOnlyAttribute(), 30,
245 q{Child class can extend parent's class attribute}
252 'HasM() returns false before M is set'
259 'HasM() returns true after M is set'
270 'HasM() returns false after M is cleared'
276 $class->Delegatee(), 'Delegatee',
277 'has a Delegetee object'
281 'units() delegates to Delegatee and returns 5'
286 my @ids = $class->IdsInMapping();
289 'there are no keys in the mapping yet'
293 !$class->ExistsInMapping('a'),
294 'key does not exist in mapping'
297 $class->SetMapping( a => 20 );
300 $class->ExistsInMapping('a'),
301 'key does exist in mapping'
305 $class->GetMapping('a'), 20,
306 'value for a in mapping is 20'
313 'attribute with builder works'
317 $class->LazyBuilt(), 42,
318 'attribute with lazy builder works'
323 $class->Triggerish(42);
324 my $triggered = do { no strict 'refs'; \@{ $class . '::Triggered' } };
325 is( scalar @{$triggered}, 1, 'trigger was called' );
326 is( $class->Triggerish(), 42, 'Triggerish is now 42' );
328 $class->Triggerish(84);
329 is( $class->Triggerish(), 84, 'Triggerish is now 84' );
334 [ $class, qw( 42 ) ],
335 [ $class, qw( 84 42 ) ],
337 'trigger passes old value correctly'