/
grep-p.t
77 lines (62 loc) 路 1.91 KB
/
grep-p.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use v6;
use Test;
# L<S32::Containers/"List"/"=item grep">
plan 17;
my @list = (1 .. 10);
is grep( { ($_ % 2) }, @list, :p ).grep(Pair), [0=>1,2=>3,4=>5,6=>7,8=>9],
'do we get Pairs';
is grep( { ($_ % 2) }, @list, :p ), [0=>1,2=>3,4=>5,6=>7,8=>9],
'simple direct test of sub';
is @list.grep( { ($_ % 2) }, :p ), [0=>1,2=>3,4=>5,6=>7,8=>9],
'simple direct test of method';
{
is 42.grep({ 1 }, :p), [0=>42],
"method form of grep works on numbers";
is 'str'.grep({ 1 }, :p), [0=>'str'],
"method form of grep works on strings";
}
#
# Grep with mutating block
#
# L<S02/Names/"$_, $!, and $/ are context<rw> by default">
{
my @array = <a b c d>;
is @array.grep({ $_ ~= "c"; 1 }, :p), [0=>'ac',1=>'bc',2=>'cc',3=>'dc'],
'mutating $_ in grep works (1)';
is @array, [<ac bc cc dc>],
'mutating $_ in grep works (2)';
}
# grep with last, next etc.
{
is (1..16).grep({last if $_ % 5 == 0; $_ % 2 == 0}, :p),
[1=>2,3=>4], 'last works in grep';
is (1..12).grep({next if $_ % 5 == 0; $_ % 2 == 0}, :p),
[1=>2,3=>4,5=>6,7=>8,11=>12], 'next works in grep';
}
# since the test argument to .grep is a Matcher, we can also
# check type constraints:
{
is (2, [], 4, [], 5).grep(Int, :p),
[0=>2,2=>4,4=>5], ".grep with non-Code matcher";
is grep(Int, 2, [], 4, [], 5, :p),
[0=>2,2=>4,4=>5], "grep with non-Code matcher";
}
{
my @a = <a b c>;
my @b = <b c d>;
is @a.grep(any(@b), :p), [1=>'b',2=>'c'], 'Junction matcher';
}
# Bool handling
{
throws-like { grep $_ == 1, 1,2,3, :p }, X::Match::Bool;
throws-like { (1,2,3).grep: $_== 1, :p }, X::Match::Bool;
is grep( Bool,True,False,Int, :p ), [0=>True,1=>False],
'can we match on Bool as type';
is (True,False,Int).grep(Bool, :p), [0=>True,1=>False],
'can we match on Bool as type';
}
# :!p handling
{
is (^10).grep(Int, :!p), [^10], 'is :!p the same as no attribute';
}
# vim: ft=perl6