X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F005_basic_list.t;h=0bfbfb529f512d6336df359c44d2a7d03318faf9;hb=b3b218736e15bab2107be3c8f6db139dc09fcb0a;hp=016c83b26524edf1f169839eeef01e8a00fb27b3;hpb=b77cfe61151b25aecf807cbd755fdd7b44d78d5f;p=gitmo%2FMooseX-AttributeHelpers.git diff --git a/t/005_basic_list.t b/t/005_basic_list.t index 016c83b..0bfbfb5 100644 --- a/t/005_basic_list.t +++ b/t/005_basic_list.t @@ -3,10 +3,8 @@ use strict; use warnings; -use Test::More tests => 29; +use Test::More tests => 35; use Test::Exception; -use DateTime; -use DateTime::Format::Strptime; BEGIN { use_ok('MooseX::AttributeHelpers'); @@ -33,27 +31,30 @@ BEGIN { 'get' => 'get_option_at', 'first' => 'get_first_option', 'last' => 'get_last_option', + 'sort' => 'sorted_options', }, curries => { 'grep' => {less_than_five => [ sub { $_ < 5 } ]}, 'map' => {up_by_one => [ sub { $_ + 1 } ]}, - 'join' => {dashify => [ '-' ]} + 'join' => {dashify => [ '-' ]}, + 'sort' => {descending => [ sub { $_[1] <=> $_[0] } ]}, } ); - has datetimes => ( + has animals => ( + is => 'rw', + isa => 'ArrayRef[Str]', metaclass => 'Collection::List', - is => 'rw', - isa => 'ArrayRef[DateTime]', curries => { - grep => { - times_with_day => sub { - my ($self, $body, $datetime) = @_; - $body->($self, sub { $_->ymd eq $datetime->ymd }); - }, - }, - }, - ); + grep => { + double_length_of => sub { + my ($self, $body, $arg) = @_; + + $body->($self, sub { length($_) == length($arg) * 2 }); + } + } + } + ) } my $stuff = Stuff->new(options => [ 1 .. 10 ]); @@ -69,6 +70,7 @@ can_ok($stuff, $_) for qw[ options join_options get_option_at + sorted_options ]; is_deeply($stuff->_options, [1 .. 10], '... got options'); @@ -77,7 +79,7 @@ ok($stuff->has_options, '... we have options'); is($stuff->num_options, 10, '... got 2 options'); cmp_ok($stuff->get_option_at(0), '==', 1, '... get option 0'); cmp_ok($stuff->get_first_option, '==', 1, '... get first'); -cmp_ok($stuff->get_last_option, '==', 10, '... get first'); +cmp_ok($stuff->get_last_option, '==', 10, '... get last'); is_deeply( [ $stuff->filter_options(sub { $_[0] % 2 == 0 }) ], @@ -97,6 +99,14 @@ is_deeply([ $stuff->options ], [1 .. 10], '... got the list of options'); is($stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10', '... joined the list of options by :'); +is_deeply([ $stuff->sorted_options ], [sort (1..10)], + '... got sorted options (default sort order)'); +is_deeply([ $stuff->sorted_options( sub { $_[1] <=> $_[0] } ) ], [sort { $b <=> $a } (1..10)], + '... got sorted options (descending sort order) '); + +throws_ok { $stuff->sorted_options('foo') } qr/Argument must be a code reference/, + 'error when sort receives a non-coderef argument'; + # test the currying is_deeply([ $stuff->less_than_five() ], [1 .. 4]); @@ -104,16 +114,16 @@ is_deeply([ $stuff->up_by_one() ], [2 .. 11]); is($stuff->dashify, '1-2-3-4-5-6-7-8-9-10'); -$stuff->datetimes([ - DateTime->now->subtract(days => 1), - DateTime->now->subtract(days => 1), - DateTime->now, - DateTime->now, -]); +$stuff->animals([ qw/cat duck horse cattle gorilla elephant flamingo kangaroo/ ]); -my $my_time = DateTime->now; +# 4 * 2 = 8 +is_deeply( + [ sort $stuff->double_length_of('fish') ], + [ sort qw/elephant flamingo kangaroo/ ], + 'returns all elements with double length of string "fish"' +); -is($stuff->times_with_day($my_time), 2, 'check for currying with a coderef'); +is_deeply([$stuff->descending], [reverse 1 .. 10]); ## test the meta @@ -130,7 +140,13 @@ is_deeply($options->provides, { 'join' => 'join_options', 'get' => 'get_option_at', 'first' => 'get_first_option', - 'last' => 'get_last_option' -}, '... got the right provies mapping'); + 'last' => 'get_last_option', + 'sort' => 'sorted_options', +}, '... got the right provides mapping'); is($options->type_constraint->type_parameter, 'Int', '... got the right container type'); + +dies_ok { + $stuff->sort_in_place_options( undef ); +} '... sort rejects arg of invalid type'; +