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