fixed all the attribute name to be more Perl6ish and then removed the : in the init_a...
[gitmo/Class-MOP.git] / t / 108_ArrayBasedStorage_test.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 69;
7 use File::Spec;
8 use Scalar::Util 'reftype';
9
10 BEGIN { 
11     use_ok('Class::MOP');    
12     require_ok(File::Spec->catdir('examples', 'ArrayBasedStorage.pod'));
13 }
14
15 {
16     package Foo;
17     
18     use strict;
19     use warnings;    
20     use metaclass (
21         'instance_metaclass'  => 'ArrayBasedStorage::Instance',
22     );
23     
24     Foo->meta->add_attribute('foo' => (
25         accessor  => 'foo',
26         predicate => 'has_foo',
27     ));
28     
29     Foo->meta->add_attribute('bar' => (
30         reader  => 'get_bar',
31         writer  => 'set_bar',
32         default => 'FOO is BAR'            
33     ));
34     
35     sub new  {
36         my $class = shift;
37         $class->meta->new_object(@_);
38     }
39     
40     package Bar;
41     
42     use strict;
43     use warnings;
44     
45     use base 'Foo';
46     
47     Bar->meta->add_attribute('baz' => (
48         accessor  => 'baz',
49         predicate => 'has_baz',
50     ));   
51     
52     package Baz;
53     
54     use strict;
55     use warnings;
56     use metaclass (        
57         'instance_metaclass'  => 'ArrayBasedStorage::Instance',
58     );
59     
60     Baz->meta->add_attribute('bling' => (
61         accessor  => 'bling',
62         default   => 'Baz::bling'
63     ));     
64     
65     package Bar::Baz;
66     
67     use strict;
68     use warnings;
69     
70     use base 'Bar', 'Baz'; 
71 }
72
73 my $foo = Foo->new();
74 isa_ok($foo, 'Foo');
75
76 is(reftype($foo), 'ARRAY', '... Foo is made with ARRAY');
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 is(reftype($foo2), 'ARRAY', '... Foo is made with ARRAY');
99
100 ok(!$foo2->has_foo, '... Foo2::foo is not defined yet');
101 is($foo2->foo(), undef, '... Foo2::foo is not defined yet');
102 is($foo2->get_bar(), 'FOO is BAR', '... Foo2::bar has been initialized');
103
104 $foo2->set_bar('DONT PANIC');
105 is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC');
106
107 is($foo->get_bar(), 42, '... Foo::bar == 42');
108
109 # now Bar ...
110
111 my $bar = Bar->new();
112 isa_ok($bar, 'Bar');
113 isa_ok($bar, 'Foo');
114
115 is(reftype($bar), 'ARRAY', '... Bar is made with ARRAY');
116
117 can_ok($bar, 'foo');
118 can_ok($bar, 'has_foo');
119 can_ok($bar, 'get_bar');
120 can_ok($bar, 'set_bar');
121 can_ok($bar, 'baz');
122 can_ok($bar, 'has_baz');
123
124 ok(!$bar->has_foo, '... Bar::foo is not defined yet');
125 is($bar->foo(), undef, '... Bar::foo is not defined yet');
126 is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
127 ok(!$bar->has_baz, '... Bar::baz is not defined yet');
128 is($bar->baz(), undef, '... Bar::baz is not defined yet');
129
130 $bar->foo('This is Bar::foo');
131
132 ok($bar->has_foo, '... Bar::foo is defined now');
133 is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"');
134 is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
135
136 $bar->baz('This is Bar::baz');
137
138 ok($bar->has_baz, '... Bar::baz is defined now');
139 is($bar->baz(), 'This is Bar::baz', '... Bar::foo == "This is Bar"');
140 is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"');
141 is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
142
143 # now Baz ...
144
145 my $baz = Bar::Baz->new();
146 isa_ok($baz, 'Bar::Baz');
147 isa_ok($baz, 'Bar');
148 isa_ok($baz, 'Foo');
149 isa_ok($baz, 'Baz');
150
151 is(reftype($baz), 'ARRAY', '... Bar::Baz is made with ARRAY');
152
153 can_ok($baz, 'foo');
154 can_ok($baz, 'has_foo');
155 can_ok($baz, 'get_bar');
156 can_ok($baz, 'set_bar');
157 can_ok($baz, 'baz');
158 can_ok($baz, 'has_baz');
159 can_ok($baz, 'bling');
160
161 is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
162 is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
163
164 ok(!$baz->has_foo, '... Bar::Baz::foo is not defined yet');
165 is($baz->foo(), undef, '... Bar::Baz::foo is not defined yet');
166 ok(!$baz->has_baz, '... Bar::Baz::baz is not defined yet');
167 is($baz->baz(), undef, '... Bar::Baz::baz is not defined yet');
168
169 $baz->foo('This is Bar::Baz::foo');
170
171 ok($baz->has_foo, '... Bar::Baz::foo is defined now');
172 is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"');
173 is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
174 is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
175
176 $baz->baz('This is Bar::Baz::baz');
177
178 ok($baz->has_baz, '... Bar::Baz::baz is defined now');
179 is($baz->baz(), 'This is Bar::Baz::baz', '... Bar::Baz::foo == "This is Bar"');
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