4 use Test::More tests => 45;
7 use Scalar::Util qw/isweak reftype/;
9 use Class::MOP::Instance;
11 can_ok( "Class::MOP::Instance", $_ ) for qw/
15 bless_instance_structure
20 deinitialize_all_slots
32 inline_initialize_slot
33 inline_deinitialize_slot
34 inline_is_slot_initialized
35 inline_weaken_slot_value
36 inline_strengthen_slot_value
43 Foo->meta->add_attribute('moosen');
49 Bar->meta->add_attribute('elken');
52 my $mi_foo = Foo->meta->get_meta_instance;
53 isa_ok($mi_foo, "Class::MOP::Instance");
56 [ $mi_foo->get_all_slots ],
58 '... get all slots for Foo');
60 my $mi_bar = Bar->meta->get_meta_instance;
61 isa_ok($mi_bar, "Class::MOP::Instance");
63 isnt($mi_foo, $mi_bar, '... they are not the same instance');
66 [ sort $mi_bar->get_all_slots ],
67 [ "elken", "moosen" ],
68 '... get all slots for Bar');
70 my $i_foo = $mi_foo->create_instance;
71 isa_ok($i_foo, "Foo");
74 my $i_foo_2 = $mi_foo->create_instance;
75 isa_ok($i_foo_2, "Foo");
76 isnt($i_foo_2, $i_foo, '... not the same instance');
77 is_deeply($i_foo, $i_foo_2, '... but the same structure');
80 ok(!$mi_foo->is_slot_initialized( $i_foo, "moosen" ), "slot not initialized");
82 ok(!defined($mi_foo->get_slot_value( $i_foo, "moosen" )), "... no value for slot");
84 $mi_foo->initialize_slot( $i_foo, "moosen" );
86 #Removed becayse slot initialization works differently now (groditi)
87 #ok($mi_foo->is_slot_initialized( $i_foo, "moosen" ), "slot initialized");
89 ok(!defined($mi_foo->get_slot_value( $i_foo, "moosen" )), "... but no value for slot");
91 $mi_foo->set_slot_value( $i_foo, "moosen", "the value" );
93 is($mi_foo->get_slot_value( $i_foo, "moosen" ), "the value", "... get slot value");
94 ok(!$i_foo->can('moosen'), '... Foo cant moosen');
98 $mi_foo->set_slot_value( $i_foo, "moosen", $ref );
99 $mi_foo->weaken_slot_value( $i_foo, "moosen" );
101 ok( isweak($i_foo->{moosen}), '... white box test of weaken' );
102 is( $mi_foo->get_slot_value( $i_foo, "moosen" ), $ref, "weak value is fetchable" );
103 ok( !isweak($mi_foo->get_slot_value( $i_foo, "moosen" )), "return value not weak" );
107 is( $mi_foo->get_slot_value( $i_foo, "moosen" ), undef, "weak value destroyed" );
111 $mi_foo->set_slot_value( $i_foo, "moosen", $ref );
115 is( reftype( $mi_foo->get_slot_value( $i_foo, "moosen" ) ), "ARRAY", "value not weak yet" );
117 $mi_foo->weaken_slot_value( $i_foo, "moosen" );
119 is( $mi_foo->get_slot_value( $i_foo, "moosen" ), undef, "weak value destroyed" );
123 $mi_foo->set_slot_value( $i_foo, "moosen", $ref );
124 $mi_foo->weaken_slot_value( $i_foo, "moosen" );
125 ok( isweak($i_foo->{moosen}), '... white box test of weaken' );
126 $mi_foo->strengthen_slot_value( $i_foo, "moosen" );
127 ok( !isweak($i_foo->{moosen}), '... white box test of weaken' );
131 is( reftype( $mi_foo->get_slot_value( $i_foo, "moosen" ) ), "ARRAY", "weak value can be strengthened" );
133 $mi_foo->deinitialize_slot( $i_foo, "moosen" );
135 ok(!$mi_foo->is_slot_initialized( $i_foo, "moosen" ), "slot deinitialized");
137 ok(!defined($mi_foo->get_slot_value( $i_foo, "moosen" )), "... no value for slot");