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