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