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