Commit | Line | Data |
f892c0f0 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More tests => 65; |
7 | use File::Spec; |
8 | |
9 | BEGIN { |
10 | use_ok('Class::MOP'); |
11 | require_ok(File::Spec->catdir('examples', 'ArrayBasedStorage.pod')); |
12 | } |
13 | |
14 | { |
15 | package Foo; |
16 | |
17 | use strict; |
18 | use warnings; |
1becdfcc |
19 | use metaclass ( |
c54756cd |
20 | ':attribute_metaclass' => 'ArrayBasedStorage::Attribute', |
f892c0f0 |
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; |
1becdfcc |
56 | use metaclass ( |
c54756cd |
57 | ':attribute_metaclass' => 'ArrayBasedStorage::Attribute', |
f892c0f0 |
58 | ':instance_metaclass' => 'ArrayBasedStorage::Instance', |
59 | ); |
60 | |
61 | Baz->meta->add_attribute('bling' => ( |
62 | accessor => 'bling', |
63 | default => 'Baz::bling' |
64 | )); |
65 | |
66 | package Bar::Baz; |
67 | |
68 | use strict; |
69 | use warnings; |
70 | |
71 | use base 'Bar', 'Baz'; |
72 | } |
73 | |
74 | my $foo = Foo->new(); |
75 | isa_ok($foo, 'Foo'); |
76 | |
77 | can_ok($foo, 'foo'); |
78 | can_ok($foo, 'has_foo'); |
79 | can_ok($foo, 'get_bar'); |
80 | can_ok($foo, 'set_bar'); |
81 | |
82 | ok(!$foo->has_foo, '... Foo::foo is not defined yet'); |
83 | is($foo->foo(), undef, '... Foo::foo is not defined yet'); |
84 | is($foo->get_bar(), 'FOO is BAR', '... Foo::bar has been initialized'); |
85 | |
86 | $foo->foo('This is Foo'); |
87 | |
88 | ok($foo->has_foo, '... Foo::foo is defined now'); |
89 | is($foo->foo(), 'This is Foo', '... Foo::foo == "This is Foo"'); |
90 | |
91 | $foo->set_bar(42); |
92 | is($foo->get_bar(), 42, '... Foo::bar == 42'); |
93 | |
94 | my $foo2 = Foo->new(); |
95 | isa_ok($foo2, 'Foo'); |
96 | |
97 | ok(!$foo2->has_foo, '... Foo2::foo is not defined yet'); |
98 | is($foo2->foo(), undef, '... Foo2::foo is not defined yet'); |
99 | is($foo2->get_bar(), 'FOO is BAR', '... Foo2::bar has been initialized'); |
100 | |
101 | $foo2->set_bar('DONT PANIC'); |
102 | is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC'); |
103 | |
104 | is($foo->get_bar(), 42, '... Foo::bar == 42'); |
105 | |
106 | # now Bar ... |
107 | |
108 | my $bar = Bar->new(); |
109 | isa_ok($bar, 'Bar'); |
110 | isa_ok($bar, 'Foo'); |
111 | |
112 | can_ok($bar, 'foo'); |
113 | can_ok($bar, 'has_foo'); |
114 | can_ok($bar, 'get_bar'); |
115 | can_ok($bar, 'set_bar'); |
116 | can_ok($bar, 'baz'); |
117 | can_ok($bar, 'has_baz'); |
118 | |
119 | ok(!$bar->has_foo, '... Bar::foo is not defined yet'); |
120 | is($bar->foo(), undef, '... Bar::foo is not defined yet'); |
121 | is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized'); |
122 | ok(!$bar->has_baz, '... Bar::baz is not defined yet'); |
123 | is($bar->baz(), undef, '... Bar::baz is not defined yet'); |
124 | |
125 | $bar->foo('This is Bar::foo'); |
126 | |
127 | ok($bar->has_foo, '... Bar::foo is defined now'); |
128 | is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"'); |
129 | is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized'); |
130 | |
131 | $bar->baz('This is Bar::baz'); |
132 | |
133 | ok($bar->has_baz, '... Bar::baz is defined now'); |
134 | is($bar->baz(), 'This is Bar::baz', '... Bar::foo == "This is Bar"'); |
135 | is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"'); |
136 | is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized'); |
137 | |
138 | # now Baz ... |
139 | |
140 | my $baz = Bar::Baz->new(); |
141 | isa_ok($baz, 'Bar::Baz'); |
142 | isa_ok($baz, 'Bar'); |
143 | isa_ok($baz, 'Foo'); |
144 | isa_ok($baz, 'Baz'); |
145 | |
146 | can_ok($baz, 'foo'); |
147 | can_ok($baz, 'has_foo'); |
148 | can_ok($baz, 'get_bar'); |
149 | can_ok($baz, 'set_bar'); |
150 | can_ok($baz, 'baz'); |
151 | can_ok($baz, 'has_baz'); |
152 | can_ok($baz, 'bling'); |
153 | |
154 | is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized'); |
155 | is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized'); |
156 | |
157 | ok(!$baz->has_foo, '... Bar::Baz::foo is not defined yet'); |
158 | is($baz->foo(), undef, '... Bar::Baz::foo is not defined yet'); |
159 | ok(!$baz->has_baz, '... Bar::Baz::baz is not defined yet'); |
160 | is($baz->baz(), undef, '... Bar::Baz::baz is not defined yet'); |
161 | |
162 | $baz->foo('This is Bar::Baz::foo'); |
163 | |
164 | ok($baz->has_foo, '... Bar::Baz::foo is defined now'); |
165 | is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"'); |
166 | is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized'); |
167 | is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized'); |
168 | |
169 | $baz->baz('This is Bar::Baz::baz'); |
170 | |
171 | ok($baz->has_baz, '... Bar::Baz::baz is defined now'); |
172 | is($baz->baz(), 'This is Bar::Baz::baz', '... Bar::Baz::foo == "This is Bar"'); |
173 | is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"'); |
174 | is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized'); |
175 | is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized'); |
176 | |
177 | |