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 | |
ce87e388 |
26 | sub meta { InstanceCountingClass->initialize($_[0]) } |
1a7ebbb3 |
27 | sub new { |
28 | my $class = shift; |
29 | bless $class->meta->construct_instance() => $class; |
30 | } |
31 | |
32 | package Bar; |
33 | |
34 | our @ISA = ('Foo'); |
35 | } |
36 | |
37 | is(Foo->meta->get_count(), 0, '... our Foo count is 0'); |
38 | is(Bar->meta->get_count(), 0, '... our Bar count is 0'); |
39 | |
40 | my $foo = Foo->new(); |
41 | isa_ok($foo, 'Foo'); |
42 | |
43 | is(Foo->meta->get_count(), 1, '... our Foo count is now 1'); |
44 | is(Bar->meta->get_count(), 0, '... our Bar count is still 0'); |
45 | |
46 | my $bar = Bar->new(); |
47 | isa_ok($bar, 'Bar'); |
48 | |
49 | is(Foo->meta->get_count(), 1, '... our Foo count is still 1'); |
50 | is(Bar->meta->get_count(), 1, '... our Bar count is now 1'); |
51 | |
52 | for (2 .. 10) { |
53 | Foo->new(); |
54 | } |
55 | |
56 | is(Foo->meta->get_count(), 10, '... our Foo count is now 10'); |
57 | is(Bar->meta->get_count(), 1, '... our Bar count is still 1'); |
58 | |