|
| 1 | +use v6; |
| 2 | +use Test; |
| 3 | + |
| 4 | +my @endings = |
| 5 | + "\n" => "LF", |
| 6 | + "\r\n" => "CRLF", |
| 7 | +# "\r" => "CR", # CR later |
| 8 | + # other line endings later still |
| 9 | +; |
| 10 | + |
| 11 | +plan @endings * (1 + 3 * ( (3 * 7) + 7)); |
| 12 | + |
| 13 | +my $filename = 't/spec/S16-io/lines.testing'; |
| 14 | +my @text = <zero one two three four>; |
| 15 | + |
| 16 | +for @endings -> (:key($eol), :value($EOL)) { |
| 17 | + unlink $filename; # make sure spurt will work |
| 18 | + |
| 19 | + my $text = @text.join($eol); |
| 20 | + ok $filename.IO.spurt($text), "could we spurt a file with $EOL"; |
| 21 | + |
| 22 | + for (), '', :chomp, '', :!chomp, $eol -> $chomp, $end { |
| 23 | + my $status = "{$chomp.gist} / $EOL"; |
| 24 | + |
| 25 | + for (), True, :close, False, :!close, True -> $closing, $open { |
| 26 | + my $handle = open($filename, |$chomp); |
| 27 | + isa-ok $handle, IO::Handle; |
| 28 | + |
| 29 | + my $status = OUTER::<$status> ~ " / {$open ?? 'open' !! 'close'}"; |
| 30 | + |
| 31 | + my $first; |
| 32 | + for $handle.lines -> $line { |
| 33 | + is $line,"@text[0]$end", "read first line: $status"; |
| 34 | + $first = $line; |
| 35 | + last; |
| 36 | + } |
| 37 | + |
| 38 | + is $handle.ins, 1, "read one line: $status"; |
| 39 | + my @lines = $first, |$handle.lines(|$closing); |
| 40 | + is @lines.join, @text.join($end), "rest of file: $status"; |
| 41 | + |
| 42 | + is $handle.ins, ($open ?? 5 !! 1), "read five lines: $status"; |
| 43 | + is $handle.opened, $open, "handle still open: $status"; |
| 44 | + ok $handle.close, "closed handle: $status"; |
| 45 | + } |
| 46 | + |
| 47 | + # slicing |
| 48 | + my $handle = open($filename, |$chomp); |
| 49 | + isa-ok $handle, IO::Handle; |
| 50 | + |
| 51 | + my @lines = $handle.lines[1,2]; |
| 52 | + is @lines.join, @text[1,2].join($end) ~ $end, "handle 1,2: $status"; |
| 53 | + is $handle.ins, 3, "handle read three lines: $status"; |
| 54 | + |
| 55 | + is $handle.lines[*-1], @text[*-1], "handle last line: $status"; |
| 56 | + |
| 57 | + # slicing on IO::Path |
| 58 | + #?rakudo skip "Reading from filehandle failed: bad file descriptor" |
| 59 | + is $filename.IO.lines(|$chomp)[1,2].join($end), |
| 60 | + @text[1,2].join($end) ~ $end, |
| 61 | + "path 1,2: $status"; |
| 62 | + |
| 63 | + is $filename.IO.lines(|$chomp)[*-1], |
| 64 | + @text[*-1], |
| 65 | + "path last line: $status"; |
| 66 | + |
| 67 | + is $filename.IO.lines(|$chomp).elems, +@text, "path elems: $status"; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +unlink $filename; # cleanup |
| 72 | + |
| 73 | +# vim: ft=perl6 |
0 commit comments