7 use Scalar::Util qw/isweak reftype/;
9 use Class::MOP::Instance;
11 can_ok( "Class::MOP::Instance", $_ ) for qw/
19 deinitialize_all_slots
31 inline_initialize_slot
32 inline_deinitialize_slot
33 inline_is_slot_initialized
34 inline_weaken_slot_value
35 inline_strengthen_slot_value
42 Foo->meta->add_attribute('moosen');
48 Bar->meta->add_attribute('elken');
51 my $mi_foo = Foo->meta->get_meta_instance;
52 isa_ok($mi_foo, "Class::MOP::Instance");
55 [ $mi_foo->get_all_slots ],
57 '... get all slots for Foo');
59 my $mi_bar = Bar->meta->get_meta_instance;
60 isa_ok($mi_bar, "Class::MOP::Instance");
62 isnt($mi_foo, $mi_bar, '... they are not the same instance');
65 [ sort $mi_bar->get_all_slots ],
66 [ "elken", "moosen" ],
67 '... get all slots for Bar');
69 my $i_foo = $mi_foo->create_instance;
70 isa_ok($i_foo, "Foo");
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');
79 ok(!$mi_foo->is_slot_initialized( $i_foo, "moosen" ), "slot not initialized");
81 ok(!defined($mi_foo->get_slot_value( $i_foo, "moosen" )), "... no value for slot");
83 $mi_foo->initialize_slot( $i_foo, "moosen" );
85 #Removed becayse slot initialization works differently now (groditi)
86 #ok($mi_foo->is_slot_initialized( $i_foo, "moosen" ), "slot initialized");
88 ok(!defined($mi_foo->get_slot_value( $i_foo, "moosen" )), "... but no value for slot");
90 $mi_foo->set_slot_value( $i_foo, "moosen", "the value" );
92 is($mi_foo->get_slot_value( $i_foo, "moosen" ), "the value", "... get slot value");
93 ok(!$i_foo->can('moosen'), '... Foo cant moosen');
97 $mi_foo->set_slot_value( $i_foo, "moosen", $ref );
98 $mi_foo->weaken_slot_value( $i_foo, "moosen" );
100 ok( isweak($i_foo->{moosen}), '... white box test of weaken' );
101 is( $mi_foo->get_slot_value( $i_foo, "moosen" ), $ref, "weak value is fetchable" );
102 ok( !isweak($mi_foo->get_slot_value( $i_foo, "moosen" )), "return value not weak" );
106 is( $mi_foo->get_slot_value( $i_foo, "moosen" ), undef, "weak value destroyed" );
110 $mi_foo->set_slot_value( $i_foo, "moosen", $ref );
114 is( reftype( $mi_foo->get_slot_value( $i_foo, "moosen" ) ), "ARRAY", "value not weak yet" );
116 $mi_foo->weaken_slot_value( $i_foo, "moosen" );
118 is( $mi_foo->get_slot_value( $i_foo, "moosen" ), undef, "weak value destroyed" );
122 $mi_foo->set_slot_value( $i_foo, "moosen", $ref );
123 $mi_foo->weaken_slot_value( $i_foo, "moosen" );
124 ok( isweak($i_foo->{moosen}), '... white box test of weaken' );
125 $mi_foo->strengthen_slot_value( $i_foo, "moosen" );
126 ok( !isweak($i_foo->{moosen}), '... white box test of weaken' );
130 is( reftype( $mi_foo->get_slot_value( $i_foo, "moosen" ) ), "ARRAY", "weak value can be strengthened" );
132 $mi_foo->deinitialize_slot( $i_foo, "moosen" );
134 ok(!$mi_foo->is_slot_initialized( $i_foo, "moosen" ), "slot deinitialized");
136 ok(!defined($mi_foo->get_slot_value( $i_foo, "moosen" )), "... no value for slot");