Add support for weak references to Class::MOP::Instance
[gitmo/Class-MOP.git] / t / 108_ArrayBasedInstance_test.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 19;
7 use File::Spec;
8
9 BEGIN { 
10     use_ok('Class::MOP');    
11     require_ok(File::Spec->catdir('examples', 'ArrayBasedInstance.pod'));
12 }
13
14 {
15     package Foo;
16     
17     use metaclass 'Class::MOP::Class' => (
18         ':attribute_metaclass' => 'ArrayBasedInstance::Attribute',
19         ':instance_metaclass'  => 'ArrayBasedInstance::Instance',
20     );
21     
22     Foo->meta->add_attribute('foo' => (
23         accessor  => 'foo',
24         predicate => 'has_foo',
25     ));
26     
27     Foo->meta->add_attribute('bar' => (
28         reader  => 'get_bar',
29         writer  => 'set_bar',
30         default => 'FOO is BAR'            
31     ));
32     
33     sub new  {
34         my $class = shift;
35         $class->meta->new_object(@_);
36     }
37 }
38
39 my $foo = Foo->new();
40 isa_ok($foo, 'Foo');
41
42 can_ok($foo, 'foo');
43 can_ok($foo, 'has_foo');
44 can_ok($foo, 'get_bar');
45 can_ok($foo, 'set_bar');
46
47 ok(!$foo->has_foo, '... Foo::foo is not defined yet');
48 is($foo->foo(), undef, '... Foo::foo is not defined yet');
49 is($foo->get_bar(), 'FOO is BAR', '... Foo::bar has been initialized');
50
51 $foo->foo('This is Foo');
52
53 ok($foo->has_foo, '... Foo::foo is defined now');
54 is($foo->foo(), 'This is Foo', '... Foo::foo == "This is Foo"');
55
56 $foo->set_bar(42);
57 is($foo->get_bar(), 42, '... Foo::bar == 42');
58
59 my $foo2 = Foo->new();
60 isa_ok($foo2, 'Foo');
61
62 ok(!$foo2->has_foo, '... Foo2::foo is not defined yet');
63 is($foo2->foo(), undef, '... Foo2::foo is not defined yet');
64 is($foo2->get_bar(), 'FOO is BAR', '... Foo2::bar has been initialized');
65
66 $foo2->set_bar('DONT PANIC');
67 is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC');
68
69 is($foo->get_bar(), 42, '... Foo::bar == 42');