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