6 use Test::More tests => 47;
9 use Scalar::Util qw/isweak reftype/;
12 use_ok('Class::MOP::Instance');
15 can_ok( "Class::MOP::Instance", $_ ) for qw/
19 bless_instance_structure
24 deinitialize_all_slots
36 inline_initialize_slot
37 inline_deinitialize_slot
38 inline_is_slot_initialized
39 inline_weaken_slot_value
40 inline_strengthen_slot_value
47 Foo->meta->add_attribute('moosen');
53 Bar->meta->add_attribute('elken');
56 my $mi_foo = Foo->meta->get_meta_instance;
57 isa_ok($mi_foo, "Class::MOP::Instance");
60 [ $mi_foo->get_all_slots ],
62 '... get all slots for Foo');
64 my $mi_bar = Bar->meta->get_meta_instance;
65 isa_ok($mi_bar, "Class::MOP::Instance");
67 isnt($mi_foo, $mi_bar, '... they are not the same instance');
70 [ sort $mi_bar->get_all_slots ],
71 [ "elken", "moosen" ],
72 '... get all slots for Bar');
74 my $i_foo = $mi_foo->create_instance;
75 isa_ok($i_foo, "Foo");
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');
84 ok(!$mi_foo->is_slot_initialized( $i_foo, "moosen" ), "slot not initialized");
86 ok(!defined($mi_foo->get_slot_value( $i_foo, "moosen" )), "... no value for slot");
88 $mi_foo->initialize_slot( $i_foo, "moosen" );
90 ok($mi_foo->is_slot_initialized( $i_foo, "moosen" ), "slot initialized");
92 ok(!defined($mi_foo->get_slot_value( $i_foo, "moosen" )), "... but no value for slot");
94 $mi_foo->set_slot_value( $i_foo, "moosen", "the value" );
96 is($mi_foo->get_slot_value( $i_foo, "moosen" ), "the value", "... get slot value");
97 ok(!$i_foo->can('moosen'), '... Foo cant moosen');
101 $mi_foo->set_slot_value( $i_foo, "moosen", $ref );
102 $mi_foo->weaken_slot_value( $i_foo, "moosen" );
104 ok( isweak($i_foo->{moosen}), '... white box test of weaken' );
105 is( $mi_foo->get_slot_value( $i_foo, "moosen" ), $ref, "weak value is fetchable" );
106 ok( !isweak($mi_foo->get_slot_value( $i_foo, "moosen" )), "return value not weak" );
110 is( $mi_foo->get_slot_value( $i_foo, "moosen" ), undef, "weak value destroyed" );
114 $mi_foo->set_slot_value( $i_foo, "moosen", $ref );
118 is( reftype( $mi_foo->get_slot_value( $i_foo, "moosen" ) ), "ARRAY", "value not weak yet" );
120 $mi_foo->weaken_slot_value( $i_foo, "moosen" );
122 is( $mi_foo->get_slot_value( $i_foo, "moosen" ), undef, "weak value destroyed" );
126 $mi_foo->set_slot_value( $i_foo, "moosen", $ref );
127 $mi_foo->weaken_slot_value( $i_foo, "moosen" );
128 ok( isweak($i_foo->{moosen}), '... white box test of weaken' );
129 $mi_foo->strengthen_slot_value( $i_foo, "moosen" );
130 ok( !isweak($i_foo->{moosen}), '... white box test of weaken' );
134 is( reftype( $mi_foo->get_slot_value( $i_foo, "moosen" ) ), "ARRAY", "weak value can be strengthened" );
136 $mi_foo->deinitialize_slot( $i_foo, "moosen" );
138 ok(!$mi_foo->is_slot_initialized( $i_foo, "moosen" ), "slot deinitialized");
140 ok(!defined($mi_foo->get_slot_value( $i_foo, "moosen" )), "... no value for slot");