Skip to content

Commit 130fb95

Browse files
committed
Add tests for IO::Handle.split
1 parent e829bdc commit 130fb95

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

S16-io/split.t

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use v6;
2+
use Test;
3+
4+
plan 16;
5+
6+
my $filename = 't/spec/S16-io/split.testing';
7+
8+
sub test-split($text,@result,|c) {
9+
subtest {
10+
plan 4;
11+
unlink $filename;
12+
ok spurt($filename,$text), "could we spurt $filename";
13+
my @got = open($filename).split(|c,:close);
14+
is @got, @result, 'did we get expected result in array';
15+
16+
@got = ();
17+
@got.push($_) for open($filename).split(|c,:close);
18+
is @got, @result, 'did we get expected result in for loop';
19+
20+
is open($filename).split(|c,:close).elems, +@result, 'elems ok';
21+
}, "testing '$text.substr(^4)' with {c.list[0].^name}";
22+
}
23+
24+
# standard text file
25+
for "\r\n", /\r\n/ -> $sep {
26+
my $text = "zero\r\none\r\ntwo\r\nthree\r\nfour";
27+
my @clean = <zero one two three four>;
28+
test-split($text,@clean,$sep);
29+
}
30+
31+
# multi char string sep with part head/tail
32+
for "::", /\:\:/ -> $sep {
33+
my $text = ":zero::one::two::three::four:";
34+
my @clean = <:zero one two three four:>;
35+
test-split($text,@clean,$sep);
36+
}
37+
38+
# single star string sep
39+
for "o", /o/ -> $sep {
40+
my $text = "none\none\ntwo\nthree\nfour\n";
41+
my @clean = <<n "ne\n" "ne\ntw" "\nthree\nf" "ur\n">>;
42+
test-split($text,@clean,$sep);
43+
}
44+
45+
# nothing to split
46+
for "x",/x/ -> $sep {
47+
my $text = "";
48+
my @clean = "";
49+
test-split($text,@clean,$sep);
50+
}
51+
52+
# nothing between separators at all
53+
for "x",/x/ -> $sep {
54+
my $text = "x" x 10;
55+
my @clean = "" xx 11;
56+
test-split($text,@clean,$sep);
57+
}
58+
59+
# no separator found at all (small)
60+
for "x",/x/ -> $sep {
61+
my $text = "y" x 10;
62+
my @clean = "y" x 10;
63+
test-split($text,@clean,$sep);
64+
}
65+
66+
# no separator found at all (more than one chunk)
67+
for "x",/x/ -> $sep {
68+
my $text = "z" x 100000;
69+
my @clean = "z" x 100000;
70+
test-split($text,@clean,$sep);
71+
}
72+
73+
# no separator found in first chunk
74+
for "x",/x/ -> $sep {
75+
my $text = "Z" x 100000 ~ "xa";
76+
my @clean = "Z" x 100000, "a";
77+
test-split($text,@clean,$sep);
78+
}
79+
80+
unlink $filename; # cleanup
81+
82+
# vim: ft=perl6

0 commit comments

Comments
 (0)