| 1 | #!/usr/bin/env perl |
| 2 | |
| 3 | use Test::More 'no_plan'; |
| 4 | use strict; |
| 5 | use warnings; |
| 6 | |
| 7 | use autobox::Core; |
| 8 | |
| 9 | my @array = qw(1 2 3); |
| 10 | |
| 11 | my @odd = @array->grep(sub { $_ % 2 }); |
| 12 | |
| 13 | is_deeply \@odd, [qw(1 3)], "Expected coderef grep results"; |
| 14 | |
| 15 | my $arrayref = @array->grep( sub { 'foo' } ); |
| 16 | |
| 17 | is ref $arrayref, 'ARRAY', "Returns arrayref in scalar context"; |
| 18 | |
| 19 | @array = qw( foo bar baz ); |
| 20 | my $d; |
| 21 | |
| 22 | ok ( eval { @array->grep( sub { 42 } || 1) }, "Should accept code refs" ); |
| 23 | ok ( eval { @array->grep( qr/foo/ ) || 1 }, "Should accept Regexps" ); |
| 24 | |
| 25 | is_deeply( $d = @array->grep('foo'), [qw( foo )], "Works with SCALAR" ); |
| 26 | is_deeply( $d = @array->grep('zar'), [], "Works with SCALAR" ); |
| 27 | is_deeply( $d = @array->grep(qr/^ba/), [qw( bar baz )], "Works with Regexp" ); |
| 28 | is_deeply( $d = @array->grep(sub { /^ba/ }), [qw( bar baz )], "... as with Code refs" ); |
| 29 | |
| 30 | # context |
| 31 | my @d = @array->grep(qr/^ba/); |
| 32 | |
| 33 | is scalar @d, 2, "Returns an array in list context"; |