upload
[gitmo/Class-MOP.git] / t / 060_instance.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 39;
7 use Test::Exception;
8
9 use Scalar::Util qw/isweak reftype/;
10
11 BEGIN {
12     use_ok('Class::MOP::Instance');    
13 }
14
15 can_ok( "Class::MOP::Instance", $_ ) for qw/
16     new 
17     
18         create_instance
19         bless_instance_structure
20
21     get_all_slots
22     
23         initialize_all_slots    
24
25         get_slot_value
26         set_slot_value
27         initialize_slot
28         is_slot_initialized     
29         weaken_slot_value
30         strengthen_slot_value   
31         
32         inline_get_slot_value
33         inline_set_slot_value
34         inline_initialize_slot
35         inline_is_slot_initialized      
36         inline_weaken_slot_value
37         inline_strengthen_slot_value    
38 /;
39
40 {
41         package Foo;
42         use metaclass;
43         
44         Foo->meta->add_attribute('moosen');
45
46         package Bar;
47         use metaclass;
48         use base qw/Foo/;
49
50         Bar->meta->add_attribute('elken');
51 }
52
53 my $mi_foo = Foo->meta->get_meta_instance;
54 isa_ok($mi_foo, "Class::MOP::Instance");
55
56 is_deeply(
57     [ $mi_foo->get_all_slots ], 
58     [ "moosen" ], 
59     '... get all slots for Foo');
60
61 my $mi_bar = Bar->meta->get_meta_instance;
62 isa_ok($mi_bar, "Class::MOP::Instance");
63
64 isnt($mi_foo, $mi_bar, '... they are not the same instance');
65
66 is_deeply(
67     [ sort $mi_bar->get_all_slots ], 
68     [ "elken", "moosen" ], 
69     '... get all slots for Bar');
70
71 my $i_foo = $mi_foo->create_instance;
72 isa_ok($i_foo, "Foo");
73
74 {
75     my $i_foo_2 = $mi_foo->create_instance;
76     isa_ok($i_foo_2, "Foo");    
77     isnt($i_foo_2, $i_foo, '... not the same instance');
78     is_deeply($i_foo, $i_foo_2, '... but the same structure');
79 }
80
81 ok(!defined($mi_foo->get_slot_value( $i_foo, "moosen" )), "... no value for slot");
82
83 $mi_foo->set_slot_value( $i_foo, "moosen", "the value" );
84
85 is($mi_foo->get_slot_value( $i_foo, "moosen" ), "the value", "... get slot value");
86 ok(!$i_foo->can('moosen'), '... Foo cant moosen');
87
88 my $ref = [];
89
90 $mi_foo->set_slot_value( $i_foo, "moosen", $ref );
91 $mi_foo->weaken_slot_value( $i_foo, "moosen" );
92
93 ok( isweak($i_foo->{moosen}), '... white box test of weaken' );
94 is( $mi_foo->get_slot_value( $i_foo, "moosen" ), $ref, "weak value is fetchable" );
95 ok( !isweak($mi_foo->get_slot_value( $i_foo, "moosen" )), "return value not weak" );
96
97 undef $ref;
98
99 is( $mi_foo->get_slot_value( $i_foo, "moosen" ), undef, "weak value destroyed" );
100
101 $ref = [];
102
103 $mi_foo->set_slot_value( $i_foo, "moosen", $ref );
104
105 undef $ref;
106
107 is( reftype( $mi_foo->get_slot_value( $i_foo, "moosen" ) ), "ARRAY", "value not weak yet" );
108
109 $mi_foo->weaken_slot_value( $i_foo, "moosen" );
110
111 is( $mi_foo->get_slot_value( $i_foo, "moosen" ), undef, "weak value destroyed" );
112
113 $ref = [];
114
115 $mi_foo->set_slot_value( $i_foo, "moosen", $ref );
116 $mi_foo->weaken_slot_value( $i_foo, "moosen" );
117 ok( isweak($i_foo->{moosen}), '... white box test of weaken' );
118 $mi_foo->strengthen_slot_value( $i_foo, "moosen" );
119 ok( !isweak($i_foo->{moosen}), '... white box test of weaken' );
120
121 undef $ref;
122
123 is( reftype( $mi_foo->get_slot_value( $i_foo, "moosen" ) ), "ARRAY", "weak value can be strengthened" );
124