adding in the metaclass pragma
[gitmo/Class-MOP.git] / t / 101_InstanceCountingClass_test.t
CommitLineData
1a7ebbb3 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 12;
9ec169fe 7use File::Spec;
1a7ebbb3 8
9BEGIN {
10 use_ok('Class::MOP');
9ec169fe 11 require_ok(File::Spec->catdir('examples', 'InstanceCountingClass.pod'));
1a7ebbb3 12}
13
14=pod
15
16This is a trivial and contrived example of how to
17make a metaclass which will count all the instances
18created. It is not meant to be anything more than
19a 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;
d6fbcd05 30 bless $class->meta->construct_instance(@_) => $class;
1a7ebbb3 31 }
32
33 package Bar;
34
35 our @ISA = ('Foo');
36}
37
38is(Foo->meta->get_count(), 0, '... our Foo count is 0');
39is(Bar->meta->get_count(), 0, '... our Bar count is 0');
40
41my $foo = Foo->new();
42isa_ok($foo, 'Foo');
43
44is(Foo->meta->get_count(), 1, '... our Foo count is now 1');
45is(Bar->meta->get_count(), 0, '... our Bar count is still 0');
46
47my $bar = Bar->new();
48isa_ok($bar, 'Bar');
49
50is(Foo->meta->get_count(), 1, '... our Foo count is still 1');
51is(Bar->meta->get_count(), 1, '... our Bar count is now 1');
52
53for (2 .. 10) {
54 Foo->new();
55}
56
57is(Foo->meta->get_count(), 10, '... our Foo count is now 10');
58is(Bar->meta->get_count(), 1, '... our Bar count is still 1');
59