6 use Scalar::Util qw( isweak );
12 if ( eval 'use MooseX::AttributeHelpers 0.13; 1;' )
20 package HasClassAttribute;
23 use MooseX::ClassAttribute;
28 class_has 'ObjectCount' =>
34 class_has 'WeakAttribute' =>
40 class_has 'LazyAttribute' =>
44 # The side effect is used to test that this was called
46 default => sub { $Lazy = 1 },
49 class_has 'ReadOnlyAttribute' =>
55 class_has 'ManyNames' =>
64 class_has 'Delegatee' =>
67 handles => [ 'units', 'color' ],
68 # if it's not lazy it makes a new object before we define
69 # Delegatee's attributes.
71 default => sub { Delegatee->new() },
76 class_has 'Mapping' =>
77 ( metaclass => 'Collection::Hash',
79 isa => 'HashRef[Str]',
80 default => sub { {} },
82 { exists => 'ExistsInMapping',
83 keys => 'IdsInMapping',
102 $self->ObjectCount( $self->ObjectCount() + 1 );
109 $class->meta()->make_immutable();
110 Delegatee->meta()->make_immutable();
136 use MooseX::ClassAttribute;
138 extends 'HasClassAttribute';
140 class_has '+ReadOnlyAttribute' =>
150 local $Test::Builder::Level = $Test::Builder::Level + 1;
153 is( HasClassAttribute->ObjectCount(), 0,
154 'ObjectCount() is 0' );
156 my $hca1 = HasClassAttribute->new();
157 is( $hca1->size(), 5,
158 'size is 5 - object attribute works as expected' );
159 is( HasClassAttribute->ObjectCount(), 1,
160 'ObjectCount() is 1' );
162 my $hca2 = HasClassAttribute->new( size => 10 );
163 is( $hca2->size(), 10,
164 'size is 10 - object attribute can be set via constructor' );
165 is( HasClassAttribute->ObjectCount(), 2,
166 'ObjectCount() is 2' );
167 is( $hca2->ObjectCount(), 2,
168 'ObjectCount() is 2 - can call class attribute accessor on object' );
172 my $hca3 = HasClassAttribute->new( ObjectCount => 20 );
173 is( $hca3->ObjectCount(), 3,
174 'class attributes passed to the constructor do not get set in the object' );
175 is( HasClassAttribute->ObjectCount(), 3,
176 'class attributes are not affected by constructor params' );
180 my $object = bless {}, 'Thing';
182 HasClassAttribute->WeakAttribute($object);
186 ok( ! defined HasClassAttribute->WeakAttribute(),
187 'weak class attributes are weak' );
191 is( $HasClassAttribute::Lazy, 0,
192 '$HasClassAttribute::Lazy is 0' );
194 is( HasClassAttribute->LazyAttribute(), 1,
195 'HasClassAttribute->LazyAttribute() is 1' );
197 is( $HasClassAttribute::Lazy, 1,
198 '$HasClassAttribute::Lazy is 1 after calling LazyAttribute' );
202 eval { HasClassAttribute->ReadOnlyAttribute(20) };
203 like( $@, qr/\QCannot assign a value to a read-only accessor/,
204 'cannot set read-only class attribute' );
208 is( Child->ReadOnlyAttribute(), 30,
209 q{Child class can extend parent's class attribute} );
213 ok( ! HasClassAttribute->HasM(),
214 'HasM() returns false before M is set' );
216 HasClassAttribute->SetM(22);
218 ok( HasClassAttribute->HasM(),
219 'HasM() returns true after M is set' );
220 is( HasClassAttribute->M(), 22,
223 HasClassAttribute->ClearM();
225 ok( ! HasClassAttribute->HasM(),
226 'HasM() returns false after M is cleared' );
230 isa_ok( HasClassAttribute->Delegatee(), 'Delegatee',
231 'has a Delegetee object' );
232 is( HasClassAttribute->units(), 5,
233 'units() delegates to Delegatee and returns 5' );
238 skip 'These tests require MooseX::AttributeHelpers', 4
241 my @ids = HasClassAttribute->IdsInMapping();
243 'there are no keys in the mapping yet' );
245 ok( ! HasClassAttribute->ExistsInMapping('a'),
246 'key does not exist in mapping' );
248 HasClassAttribute->SetMapping( a => 20 );
250 ok( HasClassAttribute->ExistsInMapping('a'),
251 'key does exist in mapping' );
253 is( HasClassAttribute->GetMapping('a'), 20,
254 'value for a in mapping is 20' );