|
| 1 | +use v6; |
| 2 | +use Test; |
| 3 | + |
| 4 | +# L<S32::Containers/"List"/"=item first"> |
| 5 | + |
| 6 | +plan 26; |
| 7 | + |
| 8 | +my @list = (1 ... 10); |
| 9 | + |
| 10 | +{ |
| 11 | + my $result = first { $^a % 2 }, @list, :end, :p; |
| 12 | + ok $result ~~ Pair, "first(, :end, :p) returns an Pair"; |
| 13 | + is $result, 8=>9, "returned value by first() is correct"; |
| 14 | +} |
| 15 | + |
| 16 | +{ |
| 17 | + my $result = first { $^a % 2 }, 1, 2, 3, 4, 5, 6, 7, 8, :end, :p; |
| 18 | + ok $result ~~ Pair, "first(, :end, :p) returns an Pair"; |
| 19 | + is $result, 6=>7, "returned value by first(, :end, :p) is correct"; |
| 20 | +} |
| 21 | + |
| 22 | +{ |
| 23 | + my $result = @list.first( { $^a == 4}, :end, :p ); |
| 24 | + ok $result ~~ Pair, "method form of first, :end, :p returns an Pair"; |
| 25 | + is $result, 3=>4, "method form of first, :end, :p returns expected item"; |
| 26 | +} |
| 27 | + |
| 28 | +{ |
| 29 | + ok @list.first( { $^a == 11 }, :end, :p ) =:= Nil, 'first, :end, :p returns Nil on unsuccessful match'; |
| 30 | +} |
| 31 | + |
| 32 | +{ |
| 33 | + my $count = 0; |
| 34 | + my $matcher = sub (Int $x) { $count++; $x % 2 }; |
| 35 | + is @list.first($matcher, :end, :p), 8=>9, |
| 36 | + 'first(, :end, :p) search for odd elements successful'; |
| 37 | + is $count, 2, |
| 38 | + 'Matching closure in first(, :end, :p) is only executed twice'; |
| 39 | +} |
| 40 | + |
| 41 | +{ |
| 42 | + is @list.first(4..6, :end, :p), 5=>6, |
| 43 | + "method form of first, :end, :p with range returns the expected item"; |
| 44 | + is @list.first(4..^6, :end, :p), 4=>5, |
| 45 | + "method form of first, :end, :p with range returns the expected item"; |
| 46 | +} |
| 47 | + |
| 48 | +{ |
| 49 | + my @fancy_list = (1, 2, "Hello", 3/4, 4.Num); |
| 50 | + is @fancy_list.first(Str, :end, :p), 2=>"Hello", "first by type Str works"; |
| 51 | + is @fancy_list.first(Int, :end, :p), 1=>2, "first by type Int works"; |
| 52 | + is @fancy_list.first(Rat, :end, :p), 3=>3/4, "first by type Rat works"; |
| 53 | +} |
| 54 | + |
| 55 | +{ |
| 56 | + my @fancy_list = <Philosopher Goblet Prince>; |
| 57 | + is @fancy_list.first(/o/, :end, :p), 1=>'Goblet', |
| 58 | + "first by regex /o/"; |
| 59 | + is @fancy_list.first(/ob/, :end, :p), 1=>'Goblet', |
| 60 | + "first by regex /ob/"; |
| 61 | + is @fancy_list.first(/l.*o/, :end, :p), 0=>'Philosopher', |
| 62 | + "first by regex /l.*o/"; |
| 63 | +} |
| 64 | + |
| 65 | +{ |
| 66 | + is <a b c b a>.first('c' | 'b', :end, :p), |
| 67 | + 3=>'b', '.first, :end, :p also takes a junction as matcher'; |
| 68 | + |
| 69 | + is (first 'c'|'b', <a b c b a>, :end, :p), |
| 70 | + 3=>'b', '.first, :end, :p also takes a junction as matcher (sub form)'; |
| 71 | +} |
| 72 | + |
| 73 | +# Bool handling |
| 74 | +{ |
| 75 | + throws-like { first $_ == 1, 1,2,3, :end, :p }, X::Match::Bool; |
| 76 | + throws-like { (1,2,3).first: $_== 1, :end, :p }, X::Match::Bool; |
| 77 | + is first( Bool,True,False,Int, :end, :p ), 1=>False, |
| 78 | + 'can we match on Bool as type'; |
| 79 | + is (True,False,Int).first(Bool, :end, :p), 1=>False, |
| 80 | + 'can we match on Bool as type'; |
| 81 | +} |
| 82 | + |
| 83 | +# :!p handling |
| 84 | +{ |
| 85 | + is (^10).first(Int, :end, :!p), 9, 'is :!p the same as no attribute'; |
| 86 | +} |
| 87 | + |
| 88 | +#vim: ft=perl6 |
0 commit comments