anon-classes
[gitmo/Class-MOP.git] / t / 018_anon_class.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 7;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Class::MOP');
11 }
12
13 my $anon_class = Class::MOP::Class->create_anon_class();
14 isa_ok($anon_class, 'Class::MOP::Class');
15
16 like($anon_class->name, qr/Class::MOP::Class::__ANON__::SERIAL::[0-9]+/, '... got an anon class package name');
17
18 lives_ok {
19     $anon_class->add_method('foo' => sub { "__ANON__::foo" });
20 } '... added a method to my anon-class';
21
22 my $instance = $anon_class->new_object();
23 isa_ok($instance, $anon_class->name);
24
25 is($instance->foo, '__ANON__::foo', '... got the right return value of our foo method');
26
27 # NOTE:
28 # I bumped this test up to 100_000 instances, and 
29 # still got not conflicts. If your application needs
30 # more than that, your probably mst
31
32 my %conflicts;
33 foreach my $i (1 .. 1000) {
34     $conflicts{ Class::MOP::Class->create_anon_class()->name } = undef;
35 }
36 is(scalar(keys %conflicts), 1000, '... got as many classes as I would expect');
37