Update to Scalar-List-Utils-1.15
[p5sagit/p5-mst-13.2.git] / ext / List / Util / t / first.t
CommitLineData
1bfb5477 1#!./perl
2
f4a2945e 3BEGIN {
1bfb5477 4 unless (-d 'blib') {
f4a2945e 5 chdir 't' if -d 't';
6 @INC = '../lib';
6b05f64e 7 require Config; import Config;
1bfb5477 8 keys %Config; # Silence warning
6b05f64e 9 if ($Config{extensions} !~ /\bList\/Util\b/) {
10 print "1..0 # Skip: List::Util was not built\n";
11 exit 0;
12 }
1bfb5477 13 }
f4a2945e 14}
15
cf083cf9 16use Test::More tests => 8;
f4a2945e 17use List::Util qw(first);
cf083cf9 18my $v;
f4a2945e 19
cf083cf9 20ok(defined &first, 'defined');
f4a2945e 21
cf083cf9 22$v = first { 8 == ($_ - 1) } 9,4,5,6;
23is($v, 9, 'one more than 8');
f4a2945e 24
cf083cf9 25$v = first { 0 } 1,2,3,4;
26is($v, undef, 'none match');
f4a2945e 27
cf083cf9 28$v = first { 0 };
29is($v, undef, 'no args');
f4a2945e 30
cf083cf9 31$v = first { $_->[1] le "e" and "e" le $_->[2] }
1bfb5477 32 [qw(a b c)], [qw(d e f)], [qw(g h i)];
cf083cf9 33is_deeply($v, [qw(d e f)], 'reference args');
1bfb5477 34
35# Check that eval{} inside the block works correctly
36my $i = 0;
cf083cf9 37$v = first { eval { die }; ($i == 5, $i = $_)[0] } 0,1,2,3,4,5,5;
38is($v, 5, 'use of eval');
60f3865b 39
cf083cf9 40$v = eval { first { die if $_ } 0,0,1 };
41is($v, undef, 'use of die');
60f3865b 42
43sub foobar { first { !defined(wantarray) || wantarray } "not ","not ","not " }
44
cf083cf9 45($v) = foobar();
46is($v, undef, 'wantarray');
47
48