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',
91 $self->ObjectCount( $self->ObjectCount() + 1 );
98 $class->meta()->make_immutable();
99 Delegatee->meta()->make_immutable();
125 use MooseX::ClassAttribute;
127 extends 'HasClassAttribute';
129 class_has '+ReadOnlyAttribute' =>
132 class_has 'YetAnotherAttribute' =>
144 local $Test::Builder::Level = $Test::Builder::Level + 1;
147 is( HasClassAttribute->ObjectCount(), 0,
148 'ObjectCount() is 0' );
150 my $hca1 = HasClassAttribute->new();
151 is( $hca1->size(), 5,
152 'size is 5 - object attribute works as expected' );
153 is( HasClassAttribute->ObjectCount(), 1,
154 'ObjectCount() is 1' );
156 my $hca2 = HasClassAttribute->new( size => 10 );
157 is( $hca2->size(), 10,
158 'size is 10 - object attribute can be set via constructor' );
159 is( HasClassAttribute->ObjectCount(), 2,
160 'ObjectCount() is 2' );
161 is( $hca2->ObjectCount(), 2,
162 'ObjectCount() is 2 - can call class attribute accessor on object' );
166 my $hca3 = HasClassAttribute->new( ObjectCount => 20 );
167 is( $hca3->ObjectCount(), 3,
168 'class attributes passed to the constructor do not get set in the object' );
169 is( HasClassAttribute->ObjectCount(), 3,
170 'class attributes are not affected by constructor params' );
174 my $object = bless {}, 'Thing';
176 HasClassAttribute->WeakAttribute($object);
180 ok( ! defined HasClassAttribute->WeakAttribute(),
181 'weak class attributes are weak' );
185 is( $HasClassAttribute::Lazy, 0,
186 '$HasClassAttribute::Lazy is 0' );
188 is( HasClassAttribute->LazyAttribute(), 1,
189 'HasClassAttribute->LazyAttribute() is 1' );
191 is( $HasClassAttribute::Lazy, 1,
192 '$HasClassAttribute::Lazy is 1 after calling LazyAttribute' );
196 eval { HasClassAttribute->ReadOnlyAttribute(20) };
197 like( $@, qr/\QCannot assign a value to a read-only accessor/,
198 'cannot set read-only class attribute' );
202 is( Child->ReadOnlyAttribute(), 30,
203 q{Child class can extend parent's class attribute} );
207 ok( ! HasClassAttribute->HasM(),
208 'HasM() returns false before M is set' );
210 HasClassAttribute->SetM(22);
212 ok( HasClassAttribute->HasM(),
213 'HasM() returns true after M is set' );
214 is( HasClassAttribute->M(), 22,
217 HasClassAttribute->ClearM();
219 ok( ! HasClassAttribute->HasM(),
220 'HasM() returns false after M is cleared' );
224 isa_ok( HasClassAttribute->Delegatee(), 'Delegatee',
225 'has a Delegetee object' );
226 is( HasClassAttribute->units(), 5,
227 'units() delegates to Delegatee and returns 5' );
231 my @ids = HasClassAttribute->IdsInMapping();
233 'there are no keys in the mapping yet' );
235 ok( ! HasClassAttribute->ExistsInMapping('a'),
236 'key does not exist in mapping' );
238 HasClassAttribute->SetMapping( a => 20 );
240 ok( HasClassAttribute->ExistsInMapping('a'),
241 'key does exist in mapping' );
243 is( HasClassAttribute->GetMapping('a'), 20,
244 'value for a in mapping is 20' );