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