X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F070_native_traits%2F205_trait_list.t;h=0501ee71f32aac8a7a7eeeecd9906b586eededcb;hb=f7fd22b6d12dedd3c0666e30c05b2e02b3e8acb3;hp=8f0be38e1a92317851134876744637a2065b103b;hpb=1853a27e1534e9b5fd73c83a636cda8603eec63f;p=gitmo%2FMoose.git diff --git a/t/070_native_traits/205_trait_list.t b/t/070_native_traits/205_trait_list.t index 8f0be38..0501ee7 100644 --- a/t/070_native_traits/205_trait_list.t +++ b/t/070_native_traits/205_trait_list.t @@ -3,13 +3,14 @@ use strict; use warnings; -use Test::More tests => 34; +use Test::More; use Test::Exception; use Test::Moose 'does_ok'; my $sort; my $less; my $up; +my $prod; { package Stuff; use Moose; @@ -29,14 +30,15 @@ my $up; 'options' => 'elements', 'join_options' => 'join', 'get_option_at' => 'get', - 'get_first_option' => 'head', - 'all_but_first_option' => 'tail', - 'get_last_option' => 'last', 'sorted_options' => 'sort', - 'less_than_five' => [ grep => [ $less = sub { $_ < 5 } ] ], - 'up_by_one' => [ map => [ $up = sub { $_ + 1 } ] ], - 'dashify' => [ join => ['-'] ], - 'descending' => [ sort => [ $sort = sub { $_[1] <=> $_[0] } ] ], + 'randomized_options' => 'shuffle', + 'unique_options' => 'uniq', + 'less_than_five' => [ grep => ($less = sub { $_ < 5 }) ], + 'up_by_one' => [ map => ($up = sub { $_ + 1 }) ], + 'pairwise_options' => [ natatime => 2 ], + 'dashify' => [ join => '-' ], + 'descending' => [ sort => ($sort = sub { $_[1] <=> $_[0] }) ], + 'product' => [ reduce => ($prod = sub { $_[0] * $_[1] }) ], }, ); @@ -56,6 +58,14 @@ can_ok( $stuff, $_ ) for qw[ join_options get_option_at sorted_options + randomized_options + unique_options + less_than_five + up_by_one + pairwise_options + dashify + descending + product ]; is_deeply( $stuff->_options, [ 1 .. 10 ], '... got options' ); @@ -63,9 +73,6 @@ is_deeply( $stuff->_options, [ 1 .. 10 ], '... got options' ); ok( !$stuff->has_no_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 head' ); -is_deeply( [ $stuff->all_but_first_option ], [ 2 .. 10 ], '... get tail' ); -cmp_ok( $stuff->get_last_option, '==', 10, '... get last' ); is_deeply( [ $stuff->filter_options( sub { $_ % 2 == 0 } ) ], @@ -101,6 +108,12 @@ throws_ok { $stuff->sorted_options('foo') } qr/Argument must be a code reference/, 'error when sort receives a non-coderef argument'; +is_deeply( [ sort { $a <=> $b } $stuff->randomized_options ], [ 1 .. 10 ] ); + +my @pairs; +$stuff->pairwise_options(sub { push @pairs, [@_] }); +is_deeply( \@pairs, [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9, 10 ] ] ); + # test the currying is_deeply( [ $stuff->less_than_five() ], [ 1 .. 4 ] ); @@ -110,6 +123,11 @@ is( $stuff->dashify, '1-2-3-4-5-6-7-8-9-10' ); is_deeply( [ $stuff->descending ], [ reverse 1 .. 10 ] ); +is( $stuff->product, 3628800 ); + +my $other_stuff = Stuff->new( options => [ 1, 1, 2, 3, 5 ] ); +is_deeply( [ $other_stuff->unique_options ], [1, 2, 3, 5] ); + ## test the meta my $options = $stuff->meta->get_attribute('_options'); @@ -126,14 +144,15 @@ is_deeply( 'options' => 'elements', 'join_options' => 'join', 'get_option_at' => 'get', - 'get_first_option' => 'head', - 'all_but_first_option' => 'tail', - 'get_last_option' => 'last', 'sorted_options' => 'sort', - 'less_than_five' => [ grep => [$less] ], - 'up_by_one' => [ map => [$up] ], - 'dashify' => [ join => ['-'] ], - 'descending' => [ sort => [$sort] ], + 'randomized_options' => 'shuffle', + 'unique_options' => 'uniq', + 'less_than_five' => [ grep => $less ], + 'up_by_one' => [ map => $up ], + 'pairwise_options' => [ natatime => 2 ], + 'dashify' => [ join => '-' ], + 'descending' => [ sort => $sort ], + 'product' => [ reduce => $prod ], }, '... got the right handles mapping' ); @@ -146,3 +165,4 @@ dies_ok { } '... sort rejects arg of invalid type'; +done_testing;