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