1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
use strict;
use warnings;
use Test::More;
use Test::Fatal;
{ package Local::Dummy; use Test::Requires { 'Moo' => '1.006' } };
{
package Local::Class;
use Moo;
use Sub::HandlesVia;
has collection => (
is => 'ro',
handles_via => 'Array',
handles => {
front => 'head',
back => 'tail',
},
);
}
my $collection = Local::Class->new(
collection => [qw/ a b c d e f /],
);
# head
is_deeply [$collection->front(0)], [], 'head(0)';
is_deeply [$collection->front(3)], [qw{a b c}], 'head(3)';
is_deeply [$collection->front(30)], [qw{a b c d e f}], 'head(30)';
is_deeply [$collection->front(-2)], [qw{a b c d}], 'head(-2)'
or diag explain[ $collection->front(-2) ];
is_deeply [$collection->front(-30)], [], 'head(-30)'
or diag explain[ $collection->front(-30) ];
# tail
is_deeply [$collection->back(0)], [], 'tail(0)';
is_deeply [$collection->back(3)], [qw{d e f}], 'tail(3)';
is_deeply [$collection->back(30)], [qw{a b c d e f}], 'tail(30)';
is_deeply [$collection->back(-2)], [qw{c d e f}], 'tail(-2)';
is_deeply [$collection->back(-30)], [], 'tail(-30)';
# exception
like(
exception { $collection->front(3, 4, 5) },
qr/Wrong number of parameters( to Local::Class::front)?; got 4; expected 2 at front=Array:head/,
'Correct exception',
);
done_testing;
|