none of this use_ok nonsense
[gitmo/Class-MOP.git] / t / 060_instance.t
CommitLineData
2bab2be6 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
f2fd2f40 6use Test::More tests => 46;
2bab2be6 7use Test::Exception;
8
5582521c 9use Scalar::Util qw/isweak reftype/;
10
2bab2be6 11BEGIN {
f2fd2f40 12 use_ok('Class::MOP::Instance');
2bab2be6 13}
14
de943e6a 15can_ok( "Class::MOP::Instance", $_ ) for qw/
f2fd2f40 16 new
17
18 create_instance
19 bless_instance_structure
de943e6a 20
49c93440 21 get_all_slots
f2fd2f40 22
23 initialize_all_slots
24 deinitialize_all_slots
25
26 get_slot_value
27 set_slot_value
28 initialize_slot
29 deinitialize_slot
30 is_slot_initialized
31 weaken_slot_value
32 strengthen_slot_value
33
34 inline_get_slot_value
35 inline_set_slot_value
36 inline_initialize_slot
37 inline_deinitialize_slot
38 inline_is_slot_initialized
39 inline_weaken_slot_value
40 inline_strengthen_slot_value
de943e6a 41/;
42
43{
f2fd2f40 44 package Foo;
45 use metaclass;
46
47 Foo->meta->add_attribute('moosen');
de943e6a 48
f2fd2f40 49 package Bar;
50 use metaclass;
51 use base qw/Foo/;
de943e6a 52
f2fd2f40 53 Bar->meta->add_attribute('elken');
49c93440 54}
de943e6a 55
49c93440 56my $mi_foo = Foo->meta->get_meta_instance;
57isa_ok($mi_foo, "Class::MOP::Instance");
de943e6a 58
49c93440 59is_deeply(
f2fd2f40 60 [ $mi_foo->get_all_slots ],
61 [ "moosen" ],
49c93440 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(
f2fd2f40 70 [ sort $mi_bar->get_all_slots ],
71 [ "elken", "moosen" ],
49c93440 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;
f2fd2f40 79 isa_ok($i_foo_2, "Foo");
49c93440 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
f2fd2f40 90#Removed becayse slot initialization works differently now (groditi)
91#ok($mi_foo->is_slot_initialized( $i_foo, "moosen" ), "slot initialized");
7d28758b 92
93ok(!defined($mi_foo->get_slot_value( $i_foo, "moosen" )), "... but no value for slot");
94
de943e6a 95$mi_foo->set_slot_value( $i_foo, "moosen", "the value" );
96
49c93440 97is($mi_foo->get_slot_value( $i_foo, "moosen" ), "the value", "... get slot value");
49c93440 98ok(!$i_foo->can('moosen'), '... Foo cant moosen');
5582521c 99
5582521c 100my $ref = [];
5582521c 101
ee7c0467 102$mi_foo->set_slot_value( $i_foo, "moosen", $ref );
103$mi_foo->weaken_slot_value( $i_foo, "moosen" );
5582521c 104
ee7c0467 105ok( isweak($i_foo->{moosen}), '... white box test of weaken' );
106is( $mi_foo->get_slot_value( $i_foo, "moosen" ), $ref, "weak value is fetchable" );
5582521c 107ok( !isweak($mi_foo->get_slot_value( $i_foo, "moosen" )), "return value not weak" );
108
109undef $ref;
110
111is( $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
117undef $ref;
118
119is( reftype( $mi_foo->get_slot_value( $i_foo, "moosen" ) ), "ARRAY", "value not weak yet" );
120
121$mi_foo->weaken_slot_value( $i_foo, "moosen" );
122
123is( $mi_foo->get_slot_value( $i_foo, "moosen" ), undef, "weak value destroyed" );
124
5582521c 125$ref = [];
126
127$mi_foo->set_slot_value( $i_foo, "moosen", $ref );
5582521c 128$mi_foo->weaken_slot_value( $i_foo, "moosen" );
ee7c0467 129ok( isweak($i_foo->{moosen}), '... white box test of weaken' );
5582521c 130$mi_foo->strengthen_slot_value( $i_foo, "moosen" );
ee7c0467 131ok( !isweak($i_foo->{moosen}), '... white box test of weaken' );
5582521c 132
133undef $ref;
134
135is( reftype( $mi_foo->get_slot_value( $i_foo, "moosen" ) ), "ARRAY", "weak value can be strengthened" );
136
7d28758b 137$mi_foo->deinitialize_slot( $i_foo, "moosen" );
138
139ok(!$mi_foo->is_slot_initialized( $i_foo, "moosen" ), "slot deinitialized");
140
141ok(!defined($mi_foo->get_slot_value( $i_foo, "moosen" )), "... no value for slot");
142