File: 101_InstanceCountingClass_test.t

package info (click to toggle)
libclass-mop-perl 0.36-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 552 kB
  • ctags: 209
  • sloc: perl: 6,157; makefile: 46
file content (59 lines) | stat: -rw-r--r-- 1,257 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 12;
use File::Spec;

BEGIN { 
    use_ok('Class::MOP');    
    require_ok(File::Spec->catdir('examples', 'InstanceCountingClass.pod'));
}

=pod

This is a trivial and contrived example of how to 
make a metaclass which will count all the instances
created. It is not meant to be anything more than 
a simple demonstration of how to make a metaclass.

=cut

{
    package Foo;
    
    use metaclass 'InstanceCountingClass';
    
    sub new  {
        my $class = shift;
        $class->meta->new_object(@_);
    }
    
    package Bar;
    
    our @ISA = ('Foo');
}

is(Foo->meta->get_count(), 0, '... our Foo count is 0');
is(Bar->meta->get_count(), 0, '... our Bar count is 0');

my $foo = Foo->new();
isa_ok($foo, 'Foo');

is(Foo->meta->get_count(), 1, '... our Foo count is now 1');
is(Bar->meta->get_count(), 0, '... our Bar count is still 0');

my $bar = Bar->new();
isa_ok($bar, 'Bar');

is(Foo->meta->get_count(), 1, '... our Foo count is still 1');
is(Bar->meta->get_count(), 1, '... our Bar count is now 1');

for (2 .. 10) {
    Foo->new();
}

is(Foo->meta->get_count(), 10, '... our Foo count is now 10');    
is(Bar->meta->get_count(), 1, '... our Bar count is still 1');