/
split.t
137 lines (108 loc) 路 3.83 KB
/
split.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use v6;
use Test;
# L<S29/Str/"=item split">
# XXX - this needs to be updated when Str.split(Str) works again
# this test really wants is_deeply()
# and got it, except for a couple of cases that fail because of Match objects
# being returned -- Aankhen
plan 27;
# split on an empty string
my %ords = (
1 => 'first',
2 => 'second',
3 => 'third',
4 => 'fourth',
5 => 'fifth',
6 => 'sixth',
7 => 'seventh',
8 => 'eighth',
9 => 'ninth',
);
sub split_test(@splitted, @expected, Str $desc) {
is +@splitted, +@expected,
"split created the correct value amount for: $desc";
is @splitted[$_], @expected[$_],
"the %ords{$_ + 1} value matched for: $desc"
for 0 .. @splitted.end;
is_deeply [~<< @splitted], [~<< @expected], "values match";
}
is_deeply split("", "forty-two"),
qw/f o r t y - t w o/,
q{split "", Str};
# split on a space
is_deeply split(' ', 'split this string'),
qw/split this string/,
q{split ' ', Str};
# split on a single character delimiter
is_deeply split('$', 'try$this$string'),
qw/try this string/,
q{split '$', Str};
# split on a multi-character delimiter
is_deeply split(', ', "comma, separated, values"),
qw/comma separated values/,
q{split ', ', Str};
# split on a variable delimiter
my $delimiter = '::';
is_deeply split($delimiter, "Perl6::Pugs::Test"),
qw/Perl6 Pugs Test/,
q{split $delimiter, Str};
# split with a reg-exp
is_deeply split(rx:Perl5 {,}, "split,me"),
qw/split me/,
q/split rx:Perl5 {,}, Str/;
# split on multiple space characters
is_deeply split(rx:Perl5 {\s+}, "Hello World Goodbye Mars"),
qw/Hello World Goodbye Mars/,
q/split rx:Perl5 {\s+}, Str/;
is_deeply split(rx:Perl5 {(\s+)}, "Hello test"),
('Hello', ("Hello test" ~~ rx:Perl5 {(\s+)}), 'test'),
q/split rx:Perl5 {(\s+)}, Str/;
is_deeply "to be || ! to be".split(' '),
qw/to be || ! to be/,
q/Str.split(' ')/;
is_deeply "this will be split".split(rx:Perl5 { }),
qw/this will be split/,
q/Str.split(rx:Perl5 { })/;
# split on multiple space characters
diag "here";
is_deeply split(rx:Perl5 {\s+}, "Hello World Goodbye Mars", 3),
( qw/Hello World/, "Goodbye Mars" ),
q/split rx:Perl5 {\s+}, Str, limit/;
is_deeply split(" ", "Hello World Goodbye Mars", 3),
( qw/Hello World/, " Goodbye Mars" ),
q/split " ", Str, limit/;
is_deeply "Hello World Goodbye Mars".split(rx:Perl5 {\s+}, 3),
( qw/Hello World/, "Goodbye Mars" ),
q/Str.split(rx:Perl5 {\s+}, limit)/;
is_deeply "Hello World Goodbye Mars".split(" ", 3),
( qw/Hello World/, " Goodbye Mars" ),
q/Str.split(" ", limit)/;
is_deeply "Word".split("", 3), qw/W o rd/,
q/Str.split("", limit)/;
# XXX: Here Pugs emulates p5 default awk field splitting behaviour.
is_deeply " abc def ".split(), qw/abc def/,
q/Str.split()/;
# ... yet how do you do this with p6 function form of split?
# Note that split(' ', $x) special casing of ' ' pattern (a la p5)
# is not implemented in Pugs. Should it be?
# This one returns an empty list
is_deeply "".split(), (),
q/"".split()/;
# ... yet this one does not (different to p5).
# blessed by $Larry at Message-ID: <20060118191046.GB32562@wall.org>
split_test "".split(':'), (""),
q/"".split(':')/;
# using /.../
is_deeply "a.b".split(/\./), <a b>,
q{"a.b".split(/\./)};
is_deeply "abcd".split(/<null>/), <a b c d>,
q{"abcd".split(/<null>/)};
{
' ' ~~ /(\s)/;
if $0 eq ' ' {
is_deeply "foo bar baz".split(/<prior>/), <foo bar baz>,
q{"foo bar baz".split(/<prior>/)};
} else {
skip q{' ' ~~ /\s/ did not result in ' '};
}
}