Commit | Line | Data |
52e8a34c |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
f892c0f0 |
6 | use Test::More tests => 65; |
9ec169fe |
7 | use File::Spec; |
52e8a34c |
8 | |
9 | BEGIN { |
10 | use_ok('Class::MOP'); |
9ec169fe |
11 | require_ok(File::Spec->catdir('examples', 'InsideOutClass.pod')); |
52e8a34c |
12 | } |
13 | |
14 | { |
15 | package Foo; |
16 | |
f892c0f0 |
17 | use strict; |
18 | use warnings; |
19 | |
fed4cee7 |
20 | use metaclass 'Class::MOP::Class' => ( |
f892c0f0 |
21 | ':attribute_metaclass' => 'InsideOutClass::Attribute', |
22 | ':instance_metaclass' => 'InsideOutClass::Instance' |
677eb158 |
23 | ); |
52e8a34c |
24 | |
2e41896e |
25 | Foo->meta->add_attribute('foo' => ( |
26 | accessor => 'foo', |
27 | predicate => 'has_foo', |
28 | )); |
52e8a34c |
29 | |
2e41896e |
30 | Foo->meta->add_attribute('bar' => ( |
31 | reader => 'get_bar', |
32 | writer => 'set_bar', |
33 | default => 'FOO is BAR' |
34 | )); |
52e8a34c |
35 | |
36 | sub new { |
37 | my $class = shift; |
5659d76e |
38 | $class->meta->new_object(@_); |
52e8a34c |
39 | } |
f892c0f0 |
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 'Class::MOP::Class' => ( |
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'; |
52e8a34c |
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'); |
f892c0f0 |
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 | |