X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F070_native_traits%2F013_array_coerce.t;h=81451576977e4db87ad83d9410934d87f3524861;hb=53a4d826caec4b82f5b23e0bc0a4e8e2f44243b9;hp=88f2845d0f5606f828cce74dc644ad63a28d7168;hpb=c7fb753e6b64ef921536b0028c74b91120a9f562;p=gitmo%2FMoose.git diff --git a/t/070_native_traits/013_array_coerce.t b/t/070_native_traits/013_array_coerce.t index 88f2845..8145157 100644 --- a/t/070_native_traits/013_array_coerce.t +++ b/t/070_native_traits/013_array_coerce.t @@ -100,7 +100,8 @@ my $foo = Foo->new; use Moose; has thing => ( - is => 'ro', isa => 'Str', + is => 'ro', + isa => 'Int', ); } @@ -112,23 +113,19 @@ my $foo = Foo->new; class_type 'Thing'; coerce 'Thing' - => from 'Str' - => via { Thing->new(thing => $_) }; + => from 'Int' + => via { Thing->new( thing => $_ ) }; subtype 'ArrayRefOfThings' => as 'ArrayRef[Thing]'; coerce 'ArrayRefOfThings' - => from 'ArrayRef[Str]' - => via { [ map { Thing->new(thing => $_) } @$_ ] }; + => from 'ArrayRef[Int]' + => via { [ map { Thing->new( thing => $_ ) } @{$_} ] }; coerce 'ArrayRefOfThings' - => from 'Str' - => via { [ Thing->new(thing => $_) ] }; - - coerce 'ArrayRefOfThings' - => from 'Thing' - => via { [ $_ ] }; + => from 'Int' + => via { [ Thing->new( thing => $_ ) ] }; has array => ( traits => ['Array'], @@ -136,21 +133,104 @@ my $foo = Foo->new; isa => 'ArrayRefOfThings', coerce => 1, handles => { - push_array => 'push', - set_array => 'set', - get_array => 'get', + push_array => 'push', + unshift_array => 'unshift', + set_array => 'set', + insert_array => 'insert', }, ); } -TODO: { - my $bar = Bar->new( array => [qw( a b c )] ); +{ + my $bar = Bar->new( array => [ 1, 2, 3 ] ); + + $bar->push_array( 4, 5 ); + + is_deeply( + [ map { $_->thing } @{ $bar->array } ], + [ 1, 2, 3, 4, 5 ], + 'push coerces new members' + ); + + $bar->unshift_array( -1, 0 ); + + is_deeply( + [ map { $_->thing } @{ $bar->array } ], + [ -1, 0, 1, 2, 3, 4, 5 ], + 'unshift coerces new members' + ); + + $bar->set_array( 3 => 9 ); + + is_deeply( + [ map { $_->thing } @{ $bar->array } ], + [ -1, 0, 1, 9, 3, 4, 5 ], + 'set coerces new members' + ); + + $bar->insert_array( 3 => 42 ); + + is_deeply( + [ map { $_->thing } @{ $bar->array } ], + [ -1, 0, 1, 42, 9, 3, 4, 5 ], + 'insert coerces new members' + ); +} + +{ + package Baz; + use Moose; + use Moose::Util::TypeConstraints; - todo_skip 'coercion in push dies here!', 1; + subtype 'SmallArrayRef' + => as 'ArrayRef' + => where { @{$_} <= 2 }; - $bar->push_array('d'); + coerce 'SmallArrayRef' + => from 'ArrayRef' + => via { [ @{$_}[ -2, -1 ] ] }; - is( $bar->get_array(3)->thing, 'd', 'push coerces the array' ); + has array => ( + traits => ['Array'], + is => 'rw', + isa => 'SmallArrayRef', + coerce => 1, + handles => { + push_array => 'push', + set_array => 'set', + insert_array => 'insert', + }, + ); +} + +{ + my $baz = Baz->new( array => [ 1, 2, 3 ] ); + + is_deeply( + $baz->array, [ 2, 3 ], + 'coercion truncates array ref in constructor' + ); + + $baz->push_array(4); + + is_deeply( + $baz->array, [ 3, 4 ], + 'coercion truncates array ref on push' + ); + + $baz->insert_array( 1 => 5 ); + + is_deeply( + $baz->array, [ 5, 4 ], + 'coercion truncates array ref on insert' + ); + + $baz->push_array( 7, 8, 9 ); + + is_deeply( + $baz->array, [ 8, 9 ], + 'coercion truncates array ref on push' + ); } done_testing;