Allow metaclasses to be reinitialized from an existing metaclass, instead of only...
[gitmo/Class-MOP.git] / t / 101_InstanceCountingClass_test.t
CommitLineData
1a7ebbb3 1use strict;
2use warnings;
3
efd3d14c 4use Test::More tests => 11;
9ec169fe 5use File::Spec;
1a7ebbb3 6
efd3d14c 7BEGIN {use Class::MOP;
10dd437b 8 require_ok(File::Spec->catfile('examples', 'InstanceCountingClass.pod'));
1a7ebbb3 9}
10
11=pod
12
13This is a trivial and contrived example of how to
14make a metaclass which will count all the instances
15created. It is not meant to be anything more than
16a simple demonstration of how to make a metaclass.
17
18=cut
19
20{
21 package Foo;
22
677eb158 23 use metaclass 'InstanceCountingClass';
24
1a7ebbb3 25 sub new {
26 my $class = shift;
5659d76e 27 $class->meta->new_object(@_);
1a7ebbb3 28 }
29
30 package Bar;
31
32 our @ISA = ('Foo');
33}
34
35is(Foo->meta->get_count(), 0, '... our Foo count is 0');
36is(Bar->meta->get_count(), 0, '... our Bar count is 0');
37
38my $foo = Foo->new();
39isa_ok($foo, 'Foo');
40
41is(Foo->meta->get_count(), 1, '... our Foo count is now 1');
42is(Bar->meta->get_count(), 0, '... our Bar count is still 0');
43
44my $bar = Bar->new();
45isa_ok($bar, 'Bar');
46
47is(Foo->meta->get_count(), 1, '... our Foo count is still 1');
48is(Bar->meta->get_count(), 1, '... our Bar count is now 1');
49
50for (2 .. 10) {
51 Foo->new();
52}
53
54is(Foo->meta->get_count(), 10, '... our Foo count is now 10');
55is(Bar->meta->get_count(), 1, '... our Bar count is still 1');
56