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