Commit | Line | Data |
2bab2be6 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
7d28758b |
6 | use Test::More tests => 47; |
2bab2be6 |
7 | use Test::Exception; |
8 | |
5582521c |
9 | use Scalar::Util qw/isweak reftype/; |
10 | |
2bab2be6 |
11 | BEGIN { |
12 | use_ok('Class::MOP::Instance'); |
13 | } |
14 | |
de943e6a |
15 | can_ok( "Class::MOP::Instance", $_ ) for qw/ |
49c93440 |
16 | new |
17 | |
de943e6a |
18 | create_instance |
19 | bless_instance_structure |
20 | |
49c93440 |
21 | get_all_slots |
ee7c0467 |
22 | |
23 | initialize_all_slots |
7d28758b |
24 | deinitialize_all_slots |
de943e6a |
25 | |
26 | get_slot_value |
27 | set_slot_value |
c174112e |
28 | initialize_slot |
7d28758b |
29 | deinitialize_slot |
c174112e |
30 | is_slot_initialized |
ee7c0467 |
31 | weaken_slot_value |
32 | strengthen_slot_value |
33 | |
34 | inline_get_slot_value |
35 | inline_set_slot_value |
36 | inline_initialize_slot |
7d28758b |
37 | inline_deinitialize_slot |
ee7c0467 |
38 | inline_is_slot_initialized |
39 | inline_weaken_slot_value |
40 | inline_strengthen_slot_value |
de943e6a |
41 | /; |
42 | |
43 | { |
44 | package Foo; |
45 | use metaclass; |
49c93440 |
46 | |
47 | Foo->meta->add_attribute('moosen'); |
de943e6a |
48 | |
49 | package Bar; |
50 | use metaclass; |
51 | use base qw/Foo/; |
de943e6a |
52 | |
49c93440 |
53 | Bar->meta->add_attribute('elken'); |
54 | } |
de943e6a |
55 | |
49c93440 |
56 | my $mi_foo = Foo->meta->get_meta_instance; |
57 | isa_ok($mi_foo, "Class::MOP::Instance"); |
de943e6a |
58 | |
49c93440 |
59 | is_deeply( |
60 | [ $mi_foo->get_all_slots ], |
61 | [ "moosen" ], |
62 | '... get all slots for Foo'); |
de943e6a |
63 | |
64 | my $mi_bar = Bar->meta->get_meta_instance; |
49c93440 |
65 | isa_ok($mi_bar, "Class::MOP::Instance"); |
de943e6a |
66 | |
49c93440 |
67 | isnt($mi_foo, $mi_bar, '... they are not the same instance'); |
de943e6a |
68 | |
49c93440 |
69 | is_deeply( |
70 | [ sort $mi_bar->get_all_slots ], |
71 | [ "elken", "moosen" ], |
72 | '... get all slots for Bar'); |
de943e6a |
73 | |
49c93440 |
74 | my $i_foo = $mi_foo->create_instance; |
75 | isa_ok($i_foo, "Foo"); |
de943e6a |
76 | |
49c93440 |
77 | { |
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'); |
82 | } |
de943e6a |
83 | |
7d28758b |
84 | ok(!$mi_foo->is_slot_initialized( $i_foo, "moosen" ), "slot not initialized"); |
85 | |
49c93440 |
86 | ok(!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 | |
90 | ok($mi_foo->is_slot_initialized( $i_foo, "moosen" ), "slot initialized"); |
91 | |
92 | ok(!defined($mi_foo->get_slot_value( $i_foo, "moosen" )), "... but no value for slot"); |
93 | |
de943e6a |
94 | $mi_foo->set_slot_value( $i_foo, "moosen", "the value" ); |
95 | |
49c93440 |
96 | is($mi_foo->get_slot_value( $i_foo, "moosen" ), "the value", "... get slot value"); |
49c93440 |
97 | ok(!$i_foo->can('moosen'), '... Foo cant moosen'); |
5582521c |
98 | |
5582521c |
99 | my $ref = []; |
5582521c |
100 | |
ee7c0467 |
101 | $mi_foo->set_slot_value( $i_foo, "moosen", $ref ); |
102 | $mi_foo->weaken_slot_value( $i_foo, "moosen" ); |
5582521c |
103 | |
ee7c0467 |
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" ); |
5582521c |
106 | ok( !isweak($mi_foo->get_slot_value( $i_foo, "moosen" )), "return value not weak" ); |
107 | |
108 | undef $ref; |
109 | |
110 | is( $mi_foo->get_slot_value( $i_foo, "moosen" ), undef, "weak value destroyed" ); |
111 | |
112 | $ref = []; |
113 | |
114 | $mi_foo->set_slot_value( $i_foo, "moosen", $ref ); |
115 | |
116 | undef $ref; |
117 | |
118 | is( reftype( $mi_foo->get_slot_value( $i_foo, "moosen" ) ), "ARRAY", "value not weak yet" ); |
119 | |
120 | $mi_foo->weaken_slot_value( $i_foo, "moosen" ); |
121 | |
122 | is( $mi_foo->get_slot_value( $i_foo, "moosen" ), undef, "weak value destroyed" ); |
123 | |
5582521c |
124 | $ref = []; |
125 | |
126 | $mi_foo->set_slot_value( $i_foo, "moosen", $ref ); |
5582521c |
127 | $mi_foo->weaken_slot_value( $i_foo, "moosen" ); |
ee7c0467 |
128 | ok( isweak($i_foo->{moosen}), '... white box test of weaken' ); |
5582521c |
129 | $mi_foo->strengthen_slot_value( $i_foo, "moosen" ); |
ee7c0467 |
130 | ok( !isweak($i_foo->{moosen}), '... white box test of weaken' ); |
5582521c |
131 | |
132 | undef $ref; |
133 | |
134 | is( reftype( $mi_foo->get_slot_value( $i_foo, "moosen" ) ), "ARRAY", "weak value can be strengthened" ); |
135 | |
7d28758b |
136 | $mi_foo->deinitialize_slot( $i_foo, "moosen" ); |
137 | |
138 | ok(!$mi_foo->is_slot_initialized( $i_foo, "moosen" ), "slot deinitialized"); |
139 | |
140 | ok(!defined($mi_foo->get_slot_value( $i_foo, "moosen" )), "... no value for slot"); |
141 | |