Skip to content

Commit 79ff022

Browse files
committed
[io grant] Expand &spurt and IO::Path.spurt tests
Indirectly tests IO::Handle.spurt as well.
1 parent b929db4 commit 79ff022

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

S32-io/spurt.t

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use v6;
2+
use lib <t/spec/packages/>;
23
use Test;
4+
use Test::Util;
35

4-
plan 36;
6+
plan 60;
57

68
# L<S32::IO/Functions/spurt>
79

@@ -139,3 +141,76 @@ if $path.IO.e {
139141
is .e, True, 'for non-existent file after spurting, .e says it exists';
140142
}
141143
}
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

Comments
 (0)