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