6 use Test::More tests => 45;
9 use Scalar::Util qw/isweak reftype/;
11 use Class::MOP::Instance;
13 can_ok( "Class::MOP::Instance", $_ ) for qw/
17 bless_instance_structure
22 deinitialize_all_slots
34 inline_initialize_slot
35 inline_deinitialize_slot
36 inline_is_slot_initialized
37 inline_weaken_slot_value
38 inline_strengthen_slot_value
45 Foo->meta->add_attribute('moosen');
51 Bar->meta->add_attribute('elken');
54 my $mi_foo = Foo->meta->get_meta_instance;
55 isa_ok($mi_foo, "Class::MOP::Instance");
58 [ $mi_foo->get_all_slots ],
60 '... get all slots for Foo');
62 my $mi_bar = Bar->meta->get_meta_instance;
63 isa_ok($mi_bar, "Class::MOP::Instance");
65 isnt($mi_foo, $mi_bar, '... they are not the same instance');
68 [ sort $mi_bar->get_all_slots ],
69 [ "elken", "moosen" ],
70 '... get all slots for Bar');
72 my $i_foo = $mi_foo->create_instance;
73 isa_ok($i_foo, "Foo");
76 my $i_foo_2 = $mi_foo->create_instance;
77 isa_ok($i_foo_2, "Foo");
78 isnt($i_foo_2, $i_foo, '... not the same instance');
79 is_deeply($i_foo, $i_foo_2, '... but the same structure');
82 ok(!$mi_foo->is_slot_initialized( $i_foo, "moosen" ), "slot not initialized");
84 ok(!defined($mi_foo->get_slot_value( $i_foo, "moosen" )), "... no value for slot");
86 $mi_foo->initialize_slot( $i_foo, "moosen" );
88 #Removed becayse slot initialization works differently now (groditi)
89 #ok($mi_foo->is_slot_initialized( $i_foo, "moosen" ), "slot initialized");
91 ok(!defined($mi_foo->get_slot_value( $i_foo, "moosen" )), "... but no value for slot");
93 $mi_foo->set_slot_value( $i_foo, "moosen", "the value" );
95 is($mi_foo->get_slot_value( $i_foo, "moosen" ), "the value", "... get slot value");
96 ok(!$i_foo->can('moosen'), '... Foo cant moosen');
100 $mi_foo->set_slot_value( $i_foo, "moosen", $ref );
101 $mi_foo->weaken_slot_value( $i_foo, "moosen" );
103 ok( isweak($i_foo->{moosen}), '... white box test of weaken' );
104 is( $mi_foo->get_slot_value( $i_foo, "moosen" ), $ref, "weak value is fetchable" );
105 ok( !isweak($mi_foo->get_slot_value( $i_foo, "moosen" )), "return value not weak" );
109 is( $mi_foo->get_slot_value( $i_foo, "moosen" ), undef, "weak value destroyed" );
113 $mi_foo->set_slot_value( $i_foo, "moosen", $ref );
117 is( reftype( $mi_foo->get_slot_value( $i_foo, "moosen" ) ), "ARRAY", "value not weak yet" );
119 $mi_foo->weaken_slot_value( $i_foo, "moosen" );
121 is( $mi_foo->get_slot_value( $i_foo, "moosen" ), undef, "weak value destroyed" );
125 $mi_foo->set_slot_value( $i_foo, "moosen", $ref );
126 $mi_foo->weaken_slot_value( $i_foo, "moosen" );
127 ok( isweak($i_foo->{moosen}), '... white box test of weaken' );
128 $mi_foo->strengthen_slot_value( $i_foo, "moosen" );
129 ok( !isweak($i_foo->{moosen}), '... white box test of weaken' );
133 is( reftype( $mi_foo->get_slot_value( $i_foo, "moosen" ) ), "ARRAY", "weak value can be strengthened" );
135 $mi_foo->deinitialize_slot( $i_foo, "moosen" );
137 ok(!$mi_foo->is_slot_initialized( $i_foo, "moosen" ), "slot deinitialized");
139 ok(!defined($mi_foo->get_slot_value( $i_foo, "moosen" )), "... no value for slot");