Include method name in immutable methods (fixes #49680)
[gitmo/Class-MOP.git] / t / 102_InsideOutClass_test.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 88;
5 use File::Spec;
6 use Scalar::Util 'reftype';
7
8 BEGIN {use Class::MOP;    
9     require_ok(File::Spec->catfile('examples', 'InsideOutClass.pod'));
10 }
11
12 {
13     package Foo;
14     
15     use strict;
16     use warnings;    
17     
18     use metaclass (
19         'attribute_metaclass' => 'InsideOutClass::Attribute',
20         'instance_metaclass'  => 'InsideOutClass::Instance'
21     );
22     
23     Foo->meta->add_attribute('foo' => (
24         accessor  => 'foo',
25         predicate => 'has_foo',
26     ));
27     
28     Foo->meta->add_attribute('bar' => (
29         reader  => 'get_bar',
30         writer  => 'set_bar',
31         default => 'FOO is BAR'            
32     ));
33     
34     sub new  {
35         my $class = shift;
36         $class->meta->new_object(@_);
37     }
38     
39     package Bar;
40     use metaclass (
41         'attribute_metaclass' => 'InsideOutClass::Attribute',
42         'instance_metaclass'  => 'InsideOutClass::Instance'
43     );
44     
45     use strict;
46     use warnings;
47     
48     use base 'Foo';
49     
50     Bar->meta->add_attribute('baz' => (
51         accessor  => 'baz',
52         predicate => 'has_baz',
53     ));   
54     
55     package Baz;
56     
57     use strict;
58     use warnings;
59     use metaclass (     
60         'attribute_metaclass' => 'InsideOutClass::Attribute',
61         'instance_metaclass'  => 'InsideOutClass::Instance'
62     );
63     
64     Baz->meta->add_attribute('bling' => (
65         accessor  => 'bling',
66         default   => 'Baz::bling'
67     ));     
68     
69     package Bar::Baz;
70     use metaclass (
71         'attribute_metaclass' => 'InsideOutClass::Attribute',
72         'instance_metaclass'  => 'InsideOutClass::Instance'
73     );
74     
75     use strict;
76     use warnings;
77     
78     use base 'Bar', 'Baz';    
79 }
80
81 my $foo = Foo->new();
82 isa_ok($foo, 'Foo');
83
84 is(reftype($foo), 'SCALAR', '... Foo is made with SCALAR');
85
86 can_ok($foo, 'foo');
87 can_ok($foo, 'has_foo');
88 can_ok($foo, 'get_bar');
89 can_ok($foo, 'set_bar');
90
91 ok(!$foo->has_foo, '... Foo::foo is not defined yet');
92 is($foo->foo(), undef, '... Foo::foo is not defined yet');
93 is($foo->get_bar(), 'FOO is BAR', '... Foo::bar has been initialized');
94
95 $foo->foo('This is Foo');
96
97 ok($foo->has_foo, '... Foo::foo is defined now');
98 is($foo->foo(), 'This is Foo', '... Foo::foo == "This is Foo"');
99
100 $foo->set_bar(42);
101 is($foo->get_bar(), 42, '... Foo::bar == 42');
102
103 my $foo2 = Foo->new();
104 isa_ok($foo2, 'Foo');
105
106 is(reftype($foo2), 'SCALAR', '... Foo is made with SCALAR');
107
108 ok(!$foo2->has_foo, '... Foo2::foo is not defined yet');
109 is($foo2->foo(), undef, '... Foo2::foo is not defined yet');
110 is($foo2->get_bar(), 'FOO is BAR', '... Foo2::bar has been initialized');
111
112 $foo2->set_bar('DONT PANIC');
113 is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC');
114
115 is($foo->get_bar(), 42, '... Foo::bar == 42');
116
117 # now Bar ...
118
119 my $bar = Bar->new();
120 isa_ok($bar, 'Bar');
121 isa_ok($bar, 'Foo');
122
123 is(reftype($bar), 'SCALAR', '... Bar is made with SCALAR');
124
125 can_ok($bar, 'foo');
126 can_ok($bar, 'has_foo');
127 can_ok($bar, 'get_bar');
128 can_ok($bar, 'set_bar');
129 can_ok($bar, 'baz');
130 can_ok($bar, 'has_baz');
131
132 ok(!$bar->has_foo, '... Bar::foo is not defined yet');
133 is($bar->foo(), undef, '... Bar::foo is not defined yet');
134 is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
135 ok(!$bar->has_baz, '... Bar::baz is not defined yet');
136 is($bar->baz(), undef, '... Bar::baz is not defined yet');
137
138 $bar->foo('This is Bar::foo');
139
140 ok($bar->has_foo, '... Bar::foo is defined now');
141 is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"');
142 is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
143
144 $bar->baz('This is Bar::baz');
145
146 ok($bar->has_baz, '... Bar::baz is defined now');
147 is($bar->baz(), 'This is Bar::baz', '... Bar::foo == "This is Bar"');
148 is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"');
149 is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
150
151 # now Baz ...
152
153 my $baz = Bar::Baz->new();
154 isa_ok($baz, 'Bar::Baz');
155 isa_ok($baz, 'Bar');
156 isa_ok($baz, 'Foo');
157 isa_ok($baz, 'Baz');
158
159 is(reftype($baz), 'SCALAR', '... Bar::Baz is made with SCALAR');
160
161 can_ok($baz, 'foo');
162 can_ok($baz, 'has_foo');
163 can_ok($baz, 'get_bar');
164 can_ok($baz, 'set_bar');
165 can_ok($baz, 'baz');
166 can_ok($baz, 'has_baz');
167 can_ok($baz, 'bling');
168
169 is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
170 is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
171
172 ok(!$baz->has_foo, '... Bar::Baz::foo is not defined yet');
173 is($baz->foo(), undef, '... Bar::Baz::foo is not defined yet');
174 ok(!$baz->has_baz, '... Bar::Baz::baz is not defined yet');
175 is($baz->baz(), undef, '... Bar::Baz::baz is not defined yet');
176
177 $baz->foo('This is Bar::Baz::foo');
178
179 ok($baz->has_foo, '... Bar::Baz::foo is defined now');
180 is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"');
181 is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
182 is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
183
184 $baz->baz('This is Bar::Baz::baz');
185
186 ok($baz->has_baz, '... Bar::Baz::baz is defined now');
187 is($baz->baz(), 'This is Bar::Baz::baz', '... Bar::Baz::foo == "This is Bar"');
188 is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"');
189 is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
190 is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
191
192 {
193     no strict 'refs';
194     
195     ok(*{'Foo::foo'}{HASH}, '... there is a foo package variable in Foo');
196     ok(*{'Foo::bar'}{HASH}, '... there is a bar package variable in Foo');
197
198     is(scalar(keys(%{'Foo::foo'})), 4, '... got the right number of entries for Foo::foo');
199     is(scalar(keys(%{'Foo::bar'})), 4, '... got the right number of entries for Foo::bar');    
200
201     ok(!*{'Bar::foo'}{HASH}, '... no foo package variable in Bar');
202     ok(!*{'Bar::bar'}{HASH}, '... no bar package variable in Bar');
203     ok(*{'Bar::baz'}{HASH}, '... there is a baz package variable in Bar');
204
205     is(scalar(keys(%{'Bar::foo'})), 0, '... got the right number of entries for Bar::foo');
206     is(scalar(keys(%{'Bar::bar'})), 0, '... got the right number of entries for Bar::bar');
207     is(scalar(keys(%{'Bar::baz'})), 2, '... got the right number of entries for Bar::baz');
208     
209     ok(*{'Baz::bling'}{HASH}, '... there is a bar package variable in Baz');
210
211     is(scalar(keys(%{'Baz::bling'})), 1, '... got the right number of entries for Baz::bling');        
212     
213     ok(!*{'Bar::Baz::foo'}{HASH}, '... no foo package variable in Bar::Baz');
214     ok(!*{'Bar::Baz::bar'}{HASH}, '... no bar package variable in Bar::Baz');
215     ok(!*{'Bar::Baz::baz'}{HASH}, '... no baz package variable in Bar::Baz');
216     ok(!*{'Bar::Baz::bling'}{HASH}, '... no bar package variable in Baz::Baz');
217
218     is(scalar(keys(%{'Bar::Baz::foo'})), 0, '... got the right number of entries for Bar::Baz::foo');
219     is(scalar(keys(%{'Bar::Baz::bar'})), 0, '... got the right number of entries for Bar::Baz::bar');
220     is(scalar(keys(%{'Bar::Baz::baz'})), 0, '... got the right number of entries for Bar::Baz::baz');    
221     is(scalar(keys(%{'Bar::Baz::bling'})), 0, '... got the right number of entries for Bar::Baz::bling');        
222 }