6 use Test::More tests => 54;
13 # the canonical form of of the 'handles'
14 # option is the hash ref mapping a
15 # method name to the delegated method name
21 has 'bar' => (is => 'rw', default => 10);
28 default => sub { Foo->new },
29 handles => { 'foo_bar' => 'bar' }
36 ok($bar->foo, '... we have something in bar->foo');
37 isa_ok($bar->foo, 'Foo');
39 is($bar->foo->bar, 10, '... bar->foo->bar returned the right default');
41 can_ok($bar, 'foo_bar');
42 is($bar->foo_bar, 10, '... bar->foo_bar delegated correctly');
44 my $foo = Foo->new(bar => 25);
47 is($foo->bar, 25, '... got the right foo->bar');
51 } '... assigned the new Foo to Bar->foo';
53 is($bar->foo, $foo, '... assigned bar->foo with the new Foo');
55 is($bar->foo->bar, 25, '... bar->foo->bar returned the right result');
56 is($bar->foo_bar, 25, '... and bar->foo_bar delegated correctly again');
58 # we also support an array based format
59 # which assumes that the name is the same
66 sub go { 'Engine::go' }
67 sub stop { 'Engine::stop' }
74 default => sub { Engine->new },
75 handles => [ 'go', 'stop' ]
82 isa_ok($car->engine, 'Engine');
83 can_ok($car->engine, 'go');
84 can_ok($car->engine, 'stop');
86 is($car->engine->go, 'Engine::go', '... got the right value from ->engine->go');
87 is($car->engine->stop, 'Engine::stop', '... got the right value from ->engine->stop');
92 is($car->go, 'Engine::go', '... got the right value from ->go');
93 is($car->stop, 'Engine::stop', '... got the right value from ->stop');
95 # and we support regexp delegation
101 sub foo { 'Baz::foo' }
102 sub bar { 'Baz::bar' }
103 sub boo { 'Baz::boo' }
111 default => sub { Baz->new },
121 default => sub { Baz->new },
131 default => sub { Baz->new },
137 my $baz_proxy = Baz::Proxy1->new;
138 isa_ok($baz_proxy, 'Baz::Proxy1');
140 can_ok($baz_proxy, 'baz');
141 isa_ok($baz_proxy->baz, 'Baz');
143 can_ok($baz_proxy, 'foo');
144 can_ok($baz_proxy, 'bar');
145 can_ok($baz_proxy, 'boo');
147 is($baz_proxy->foo, 'Baz::foo', '... got the right proxied return value');
148 is($baz_proxy->bar, 'Baz::bar', '... got the right proxied return value');
149 is($baz_proxy->boo, 'Baz::boo', '... got the right proxied return value');
152 my $baz_proxy = Baz::Proxy2->new;
153 isa_ok($baz_proxy, 'Baz::Proxy2');
155 can_ok($baz_proxy, 'baz');
156 isa_ok($baz_proxy->baz, 'Baz');
158 can_ok($baz_proxy, 'foo');
159 can_ok($baz_proxy, 'boo');
161 is($baz_proxy->foo, 'Baz::foo', '... got the right proxied return value');
162 is($baz_proxy->boo, 'Baz::boo', '... got the right proxied return value');
165 my $baz_proxy = Baz::Proxy3->new;
166 isa_ok($baz_proxy, 'Baz::Proxy3');
168 can_ok($baz_proxy, 'baz');
169 isa_ok($baz_proxy->baz, 'Baz');
171 can_ok($baz_proxy, 'bar');
172 can_ok($baz_proxy, 'boo');
174 is($baz_proxy->bar, 'Baz::bar', '... got the right proxied return value');
175 is($baz_proxy->boo, 'Baz::boo', '... got the right proxied return value');
188 sub foo { 'Foo::Baz::FOO' }
189 sub bar { 'Foo::Baz::BAR' }
190 sub baz { 'Foo::Baz::BAZ' }
198 handles => 'Foo::Bar',
204 my $foo = Foo::Thing->new(thing => Foo::Baz->new);
205 isa_ok($foo, 'Foo::Thing');
206 isa_ok($foo->thing, 'Foo::Baz');
208 ok($foo->meta->has_method('foo'), '... we have the method we expect');
209 ok($foo->meta->has_method('bar'), '... we have the method we expect');
210 ok(!$foo->meta->has_method('baz'), '... we dont have the method we expect');
212 is($foo->foo, 'Foo::Baz::FOO', '... got the right value');
213 is($foo->bar, 'Foo::Baz::BAR', '... got the right value');
214 is($foo->thing->baz, 'Foo::Baz::BAZ', '... got the right value');