-
Notifications
You must be signed in to change notification settings - Fork 135
/
grep.t
82 lines (67 loc) · 2.41 KB
/
grep.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
78
79
80
81
use v6-alpha;
use Test;
# L<S29/"List"/"=item grep">
=begin pod
built-in grep tests
=end pod
plan 29;
my @list = (1 .. 10);
{
my @result = grep { ($_ % 2) }, @list;
is(+@result, 5, 'we got a list back');
is(@result[0], 1, 'got the value we expected');
is(@result[1], 3, 'got the value we expected');
is(@result[2], 5, 'got the value we expected');
is(@result[3], 7, 'got the value we expected');
is(@result[4], 9, 'got the value we expected');
}
{
my @result = @list.grep():{ ($_ % 2) };
is(+@result, 5, 'we got a list back');
is(@result[0], 1, 'got the value we expected');
is(@result[1], 3, 'got the value we expected');
is(@result[2], 5, 'got the value we expected');
is(@result[3], 7, 'got the value we expected');
is(@result[4], 9, 'got the value we expected');
}
{
my @result = @list.grep:{ ($_ % 2) };
is(+@result, 5, 'we got a list back');
is(@result[0], 1, 'got the value we expected');
is(@result[1], 3, 'got the value we expected');
is(@result[2], 5, 'got the value we expected');
is(@result[3], 7, 'got the value we expected');
is(@result[4], 9, 'got the value we expected');
}
{
my @result = grep { ($_ % 2) }: @list;
is(+@result, 5, 'we got a list back');
is(@result[0], 1, 'got the value we expected');
is(@result[1], 3, 'got the value we expected');
is(@result[2], 5, 'got the value we expected');
is(@result[3], 7, 'got the value we expected');
is(@result[4], 9, 'got the value we expected');
}
# .grep shouldn't work on non-arrays
{
#?pugs 2 todo 'bug'
dies_ok { 42.grep:{ $_ } }, "method form of grep should not work on numbers";
dies_ok { "str".grep:{ $_ } }, "method form of grep should not work on strings";
is ~(42,).grep:{ 1 }, "42", "method form of grep should work on arrays";
}
#
# Grep with mutating block
#
# Dubious: According to S29's definition, neither map nor grep allows for
# mutation. On the other hand, it seems useful to preserve the bugward
# behaviour. Marking :todo<unspecced>, pending p6l discussion (see thread
# "[S29] Mutating map and grep" on p6l started by Ingo Blechschmidt
# L<"http://www.nntp.perl.org/group/perl.perl6.language/22553">) and S29 patch.
#?rakudo todo 'unspecced'
{
my @array = <a b c d>;
is ~(try { @array.grep:{ $_ ~= "c"; 1 } }), "ac bc cc dc",
'mutating $_ in grep works (1)';
is ~@array, "ac bc cc dc",
'mutating $_ in grep works (2)';
}