|
1 | 1 | use v6;
|
| 2 | +use lib <t/spec/packages/>; |
2 | 3 | use Test;
|
| 4 | +use Test::Util; |
3 | 5 |
|
4 |
| -plan 36; |
| 6 | +plan 60; |
5 | 7 |
|
6 | 8 | # L<S32::IO/Functions/spurt>
|
7 | 9 |
|
@@ -139,3 +141,76 @@ if $path.IO.e {
|
139 | 141 | is .e, True, 'for non-existent file after spurting, .e says it exists';
|
140 | 142 | }
|
141 | 143 | }
|
| 144 | + |
| 145 | +{ # 2017 IO Grant |
| 146 | + sub test-spurt ($file, $data, $expected = $data, :$senc, :$meth, |args) { |
| 147 | + my &SPURT = $meth ?? IO::Path.^lookup('spurt') !! &spurt; |
| 148 | + subtest "spurt {$data.^name} {args.perl} {$senc if $senc}" => { |
| 149 | + plan 2; |
| 150 | + my $form = "[{&SPURT.^name.lc} form]"; |
| 151 | + |
| 152 | + ok SPURT($file, $data, |args), "spurt is successful $form"; |
| 153 | + is-deeply |( |
| 154 | + $expected ~~ Blob |
| 155 | + ?? ($file.slurp(:bin), (Buf[uint8].new: $expected)) |
| 156 | + !! ($file.slurp(:enc($senc || args<enc> || 'utf8')), |
| 157 | + $expected) |
| 158 | + ), "wrote correct data $form"; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + sub test-spurt-fails ($file, $data, $expected = $data, :$meth, |args) { |
| 163 | + my &SPURT = $meth ?? IO::Path.^lookup('spurt') !! &spurt; |
| 164 | + |
| 165 | + subtest "spurt {$data.^name} {args.perl} fails" => { |
| 166 | + plan 1; |
| 167 | + my $form = "[{&SPURT.^name.lc} form]"; |
| 168 | + |
| 169 | + my $res = SPURT($file, $data, |args); |
| 170 | + isa-ok $res, Failure, 'failed spurt returns a Failure'; |
| 171 | + $res.so; # handle Failure |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + constant $str = "I ♥ Perl 6"; |
| 176 | + constant $bin = $str.encode; |
| 177 | + |
| 178 | + test-spurt make-temp-file(), $str; |
| 179 | + test-spurt make-temp-file(), $str, :meth; |
| 180 | + test-spurt make-temp-file(), $bin; |
| 181 | + test-spurt make-temp-file(), $bin, :meth; |
| 182 | + |
| 183 | + with make-temp-file() { |
| 184 | + with .open(:w) { .print: "Hi!"; .close } |
| 185 | + test-spurt-fails $_, $str, :createonly; |
| 186 | + test-spurt-fails $_, $str, :createonly, :meth; |
| 187 | + test-spurt-fails $_, $bin, :createonly; |
| 188 | + test-spurt-fails $_, $bin, :createonly, :meth; |
| 189 | + |
| 190 | + test-spurt $_, $str, "Hi!" ~ $str, :append; |
| 191 | + test-spurt $_, $str, "Hi!" ~ $str x 2, :append, :meth; |
| 192 | + test-spurt $_, $bin, "Hi!" ~ $str x 3, :append; |
| 193 | + test-spurt $_, $bin, "Hi!" ~ $str x 4, :append, :meth; |
| 194 | + } |
| 195 | + |
| 196 | + constant $lstr = Buf.new(200).decode('Latin-1'); |
| 197 | + constant $lbin = Buf.new(200); |
| 198 | + |
| 199 | + test-spurt make-temp-file(), $lstr, :enc<Latin-1>; |
| 200 | + test-spurt make-temp-file(), $lstr, :enc<Latin-1>, :meth; |
| 201 | + test-spurt make-temp-file(), $lbin; |
| 202 | + test-spurt make-temp-file(), $lbin, :meth; |
| 203 | + |
| 204 | + with make-temp-file() { |
| 205 | + with .open(:w) { .print: "Hi!"; .close } |
| 206 | + test-spurt-fails $_, $lstr, :enc<Latin-1>, :createonly; |
| 207 | + test-spurt-fails $_, $lstr, :enc<Latin-1>, :createonly, :meth; |
| 208 | + test-spurt-fails $_, $lbin, :createonly; |
| 209 | + test-spurt-fails $_, $lbin, :createonly, :meth; |
| 210 | + |
| 211 | + test-spurt $_, $lstr, "Hi!" ~ $lstr, :append, :enc<Latin-1>; |
| 212 | + test-spurt $_, $lstr, "Hi!" ~ $lstr x 2, :append, :enc<Latin-1>, :meth; |
| 213 | + test-spurt $_, $lbin, "Hi!" ~ $lstr x 3, :append, :senc<Latin-1>; |
| 214 | + test-spurt $_, $lbin, "Hi!" ~ $lstr x 4, :append, :senc<Latin-1>, :meth; |
| 215 | + } |
| 216 | +} |
0 commit comments