Commit | Line | Data |
1a7ebbb3 |
1 | use strict; |
2 | use warnings; |
3 | |
efd3d14c |
4 | use Test::More tests => 11; |
9ec169fe |
5 | use File::Spec; |
1a7ebbb3 |
6 | |
efd3d14c |
7 | BEGIN {use Class::MOP; |
10dd437b |
8 | require_ok(File::Spec->catfile('examples', 'InstanceCountingClass.pod')); |
1a7ebbb3 |
9 | } |
10 | |
11 | =pod |
12 | |
13 | This is a trivial and contrived example of how to |
14 | make a metaclass which will count all the instances |
15 | created. It is not meant to be anything more than |
16 | a 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 | |
35 | is(Foo->meta->get_count(), 0, '... our Foo count is 0'); |
36 | is(Bar->meta->get_count(), 0, '... our Bar count is 0'); |
37 | |
38 | my $foo = Foo->new(); |
39 | isa_ok($foo, 'Foo'); |
40 | |
41 | is(Foo->meta->get_count(), 1, '... our Foo count is now 1'); |
42 | is(Bar->meta->get_count(), 0, '... our Bar count is still 0'); |
43 | |
44 | my $bar = Bar->new(); |
45 | isa_ok($bar, 'Bar'); |
46 | |
47 | is(Foo->meta->get_count(), 1, '... our Foo count is still 1'); |
48 | is(Bar->meta->get_count(), 1, '... our Bar count is now 1'); |
49 | |
50 | for (2 .. 10) { |
51 | Foo->new(); |
52 | } |
53 | |
54 | is(Foo->meta->get_count(), 10, '... our Foo count is now 10'); |
55 | is(Bar->meta->get_count(), 1, '... our Bar count is still 1'); |
56 | |