6 use Test::More tests => 46;
13 # the canonical form of of the 'handles'
14 # option is the hash ref mapping a
15 # method name to the delegated method name
23 has 'bar' => (is => 'rw', default => 10);
32 default => sub { Foo->new },
33 handles => { 'foo_bar' => 'bar' }
40 ok($bar->foo, '... we have something in bar->foo');
41 isa_ok($bar->foo, 'Foo');
43 is($bar->foo->bar, 10, '... bar->foo->bar returned the right default');
45 can_ok($bar, 'foo_bar');
46 is($bar->foo_bar, 10, '... bar->foo_bar delegated correctly');
48 my $foo = Foo->new(bar => 25);
51 is($foo->bar, 25, '... got the right foo->bar');
55 } '... assigned the new Foo to Bar->foo';
57 is($bar->foo, $foo, '... assigned bar->foo with the new Foo');
59 is($bar->foo->bar, 25, '... bar->foo->bar returned the right result');
60 is($bar->foo_bar, 25, '... and bar->foo_bar delegated correctly again');
62 # we also support an array based format
63 # which assumes that the name is the same
72 sub go { 'Engine::go' }
73 sub stop { 'Engine::stop' }
82 default => sub { Engine->new },
83 handles => [ 'go', 'stop' ]
90 isa_ok($car->engine, 'Engine');
91 can_ok($car->engine, 'go');
92 can_ok($car->engine, 'stop');
94 is($car->engine->go, 'Engine::go', '... got the right value from ->engine->go');
95 is($car->engine->stop, 'Engine::stop', '... got the right value from ->engine->stop');
100 is($car->go, 'Engine::go', '... got the right value from ->go');
101 is($car->stop, 'Engine::stop', '... got the right value from ->stop');
103 # and we support regexp delegation
111 sub foo { 'Baz::foo' }
112 sub bar { 'Baz::bar' }
113 sub boo { 'Baz::boo' }
123 default => sub { Baz->new },
135 default => sub { Baz->new },
147 default => sub { Baz->new },
153 my $baz_proxy = Baz::Proxy1->new;
154 isa_ok($baz_proxy, 'Baz::Proxy1');
156 can_ok($baz_proxy, 'baz');
157 isa_ok($baz_proxy->baz, 'Baz');
159 can_ok($baz_proxy, 'foo');
160 can_ok($baz_proxy, 'bar');
161 can_ok($baz_proxy, 'boo');
163 is($baz_proxy->foo, 'Baz::foo', '... got the right proxied return value');
164 is($baz_proxy->bar, 'Baz::bar', '... got the right proxied return value');
165 is($baz_proxy->boo, 'Baz::boo', '... got the right proxied return value');
168 my $baz_proxy = Baz::Proxy2->new;
169 isa_ok($baz_proxy, 'Baz::Proxy2');
171 can_ok($baz_proxy, 'baz');
172 isa_ok($baz_proxy->baz, 'Baz');
174 can_ok($baz_proxy, 'foo');
175 can_ok($baz_proxy, 'boo');
177 is($baz_proxy->foo, 'Baz::foo', '... got the right proxied return value');
178 is($baz_proxy->boo, 'Baz::boo', '... got the right proxied return value');
181 my $baz_proxy = Baz::Proxy3->new;
182 isa_ok($baz_proxy, 'Baz::Proxy3');
184 can_ok($baz_proxy, 'baz');
185 isa_ok($baz_proxy->baz, 'Baz');
187 can_ok($baz_proxy, 'bar');
188 can_ok($baz_proxy, 'boo');
190 is($baz_proxy->bar, 'Baz::bar', '... got the right proxied return value');
191 is($baz_proxy->boo, 'Baz::boo', '... got the right proxied return value');