X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F002_basic_array.t;h=a3362764dbf5ecc492dbee41e45a9e93be5ddc7e;hb=6c321470afd71bf53767618279e3b7bd7dd8cb10;hp=fbd71762edd0a1c63a35e69d345b5e79a121f993;hpb=77d02b8b55108cc6819c456152fc2d504c5cd069;p=gitmo%2FMooseX-AttributeHelpers.git diff --git a/t/002_basic_array.t b/t/002_basic_array.t index fbd7176..a336276 100644 --- a/t/002_basic_array.t +++ b/t/002_basic_array.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More no_plan => 1; +use Test::More tests => 69; use Test::Exception; BEGIN { @@ -17,17 +17,31 @@ BEGIN { has 'options' => ( metaclass => 'Collection::Array', is => 'ro', - isa => 'ArrayRef[Int]', + isa => 'ArrayRef[Str]', default => sub { [] }, - provides => { - 'push' => 'add_options', - 'pop' => 'remove_last_option', - 'shift' => 'remove_first_option', - 'unshift' => 'insert_options', - 'get' => 'get_option_at', - 'set' => 'set_option_at', - 'count' => 'num_options', - 'empty' => 'has_options', + provides => { + 'push' => 'add_options', + 'pop' => 'remove_last_option', + 'shift' => 'remove_first_option', + 'unshift' => 'insert_options', + 'get' => 'get_option_at', + 'set' => 'set_option_at', + 'count' => 'num_options', + 'empty' => 'has_options', + 'clear' => 'clear_options', + 'splice' => 'splice_options', + 'sort_in_place' => 'sort_options_in_place', + 'accessor' => 'option_accessor', + }, + curries => { + 'push' => { + add_options_with_speed => ['funrolls', 'funbuns'] + }, + 'unshift' => { + prepend_prerequisites_along_with => ['first', 'second'] + }, + 'sort_in_place' => { descending_options => [ sub { $_[1] <=> $_[0] } ], + }, } ); } @@ -43,7 +57,10 @@ can_ok($stuff, $_) for qw[ get_option_at set_option_at num_options + clear_options has_options + sort_options_in_place + option_accessor ]; is_deeply($stuff->options, [10, 12], '... got options'); @@ -107,23 +124,102 @@ is($stuff->remove_first_option, 10, '... getting the first option'); is($stuff->num_options, 5, '... got 5 options'); is($stuff->get_option_at(0), 20, '... get option at index 0'); +$stuff->clear_options; +is_deeply( $stuff->options, [], "... clear options" ); + +$stuff->add_options(5, 1, 2, 3); +$stuff->sort_options_in_place; +is_deeply( $stuff->options, [1, 2, 3, 5], "... sort options in place (default sort order)" ); + +$stuff->sort_options_in_place( sub { $_[1] <=> $_[0] } ); +is_deeply( $stuff->options, [5, 3, 2, 1], "... sort options in place (descending order)" ); + +$stuff->clear_options(); +$stuff->add_options(5, 1, 2, 3); +lives_ok { + $stuff->descending_options(); +} '... curried sort in place lives ok'; + +is_deeply( $stuff->options, [5, 3, 2, 1], "... sort currying" ); + +throws_ok { $stuff->sort_options_in_place('foo') } qr/Argument must be a code reference/, + 'error when sort_in_place receives a non-coderef argument'; + +$stuff->clear_options; + +lives_ok { + $stuff->add_options('tree'); +} '... set the options okay'; + +lives_ok { + $stuff->add_options_with_speed('compatible', 'safe'); +} '... add options with speed okay'; + +is_deeply($stuff->options, [qw/tree funrolls funbuns compatible safe/], + 'check options after add_options_with_speed'); + +lives_ok { + $stuff->prepend_prerequisites_along_with(); +} '... add prerequisite options okay'; + +$stuff->clear_options; +$stuff->add_options( 1, 2 ); + +lives_ok { + $stuff->splice_options( 1, 0, 'foo' ); +} '... splice_options works'; + +is_deeply( + $stuff->options, [ 1, 'foo', 2 ], + 'splice added expected option' +); + +is($stuff->option_accessor(1 => 'foo++'), 'foo++'); +is($stuff->option_accessor(1), 'foo++'); + ## check some errors +#dies_ok { +# $stuff->insert_options(undef); +#} '... could not add an undef where a string is expected'; +# +#dies_ok { +# $stuff->set_option(5, {}); +#} '... could not add a hash ref where a string is expected'; + +dies_ok { + Stuff->new(options => [ undef, 10, undef, 20 ]); +} '... bad constructor params'; + dies_ok { - $stuff->add_options([]); -} '... could not add an array ref where an int is expected'; + my $stuff = Stuff->new(); + $stuff->add_options(undef); +} '... rejects push of an invalid type'; dies_ok { + my $stuff = Stuff->new(); $stuff->insert_options(undef); -} '... could not add an undef where an int is expected'; +} '... rejects unshift of an invalid type'; dies_ok { - $stuff->set_option(5, {}); -} '... could not add a hash ref where an int is expected'; + my $stuff = Stuff->new(); + $stuff->set_option_at( 0, undef ); +} '... rejects set of an invalid type'; dies_ok { - Stuff->new(options => [ 'Foo', 10, 'Bar', 20 ]); -} '... bad constructor params'; + my $stuff = Stuff->new(); + $stuff->sort_in_place_options( undef ); +} '... sort rejects arg of invalid type'; + +dies_ok { + my $stuff = Stuff->new(); + $stuff->option_accessor(); +} '... accessor rejects 0 args'; + +dies_ok { + my $stuff = Stuff->new(); + $stuff->option_accessor(1, 2, 3); +} '... accessor rejects 3 args'; ## test the meta @@ -139,6 +235,10 @@ is_deeply($options->provides, { 'set' => 'set_option_at', 'count' => 'num_options', 'empty' => 'has_options', -}, '... got the right provies mapping'); + 'clear' => 'clear_options', + 'splice' => 'splice_options', + 'sort_in_place' => 'sort_options_in_place', + 'accessor' => 'option_accessor', +}, '... got the right provides mapping'); -is($options->container_type, 'Int', '... got the right container type'); +is($options->type_constraint->type_parameter, 'Str', '... got the right container type');