Skip to content

Commit a00ed4d

Browse files
committed
re-wrote spurt tests, DRY
1 parent a95a3c3 commit a00ed4d

File tree

1 file changed

+45
-57
lines changed

1 file changed

+45
-57
lines changed

S32-io/spurt.t

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,67 @@
11
use v6;
22
use Test;
3-
plan 12;
3+
#plan 22;
4+
plan *;
5+
46
# L<S32::IO/Functions/spurt>
57

68
my $path = "tempfile-spurt-test";
79

8-
# Tests for:
9-
# multi spurt (Str $filename,
10-
# Buf $contents,
11-
# Bool :append = False,
12-
# Bool :$createonly = False)
13-
#
14-
# and
15-
#
16-
# multi spurt (Str $filename,
17-
# Str $contents,
18-
# Str :$enc = $?ENC,
19-
# Bool :append = False,
20-
# Bool :$createonly = False,
21-
#?niecza skip "does not like Buf.new"
22-
{
23-
my $buf = Buf.new(0xBE, 0xEF, 0xC0, 0xDE);
24-
25-
spurt($path, $buf);
26-
is slurp($path, :bin), $buf, "spurting Buf ok";
27-
28-
spurt $path, "24", :enc("ASCII");
29-
is slurp($path), "24", "spurt to IO with enc";
30-
31-
spurt $path, $buf;
32-
spurt $path, $buf, :append;
33-
is slurp($path, :bin), ($buf ~ $buf), "spurting Buf with append";
34-
unlink $path;
10+
# filename as str tests
11+
all-tests({ $path });
3512

36-
lives_ok { spurt $path, $buf, :createonly }, "createonly creates file";
37-
ok $path.IO.e, "file was created";
38-
dies_ok { spurt $path, :createonly }, "createonly fails if file exists";
39-
unlink $path;
13+
# filename as IO tests
14+
#?rakudo skip "does not support IO::Handle"
15+
{
16+
all-tests({ $path.IO });
4017
}
4118

42-
# Tests for:
43-
# multi spurt (IO $fh,
44-
# Str $contents,
45-
# Str :$enc = $?ENC,
46-
# Bool :append = False,
47-
# Bool :$createonly = False)
48-
# and
49-
#
50-
# multi spurt (IO $fh,
51-
# Buf $contents,
52-
# Bool :append = False,
53-
# Bool :$createonly = False)
54-
#?rakudo skip "No matching signature"
55-
{
56-
spurt $path.IO, "42";
57-
is slurp($path), "42", "spurt to IO";
19+
sub all-tests(Callable $handle) {
20+
my $buf = Buf.new(0xBE, 0xEF, 0xC0, 0xDE);
21+
my $txt = "42";
22+
23+
#?niecza skip ":bin option for slurp fails"
24+
{
25+
spurt $handle(), $buf;
26+
is slurp($path, :bin), $buf, "spurting Buf ok";
27+
}
28+
spurt $handle(), $txt;
29+
is slurp($path), $txt, "spurting txt ok";
5830

5931
#?niecza skip "Excess arguments to spurt, unused named enc"
6032
{
61-
spurt $path.IO, "24", :enc("ASCII");
62-
is slurp($path), "24", "spurt to IO with enc";
33+
spurt $handle(), $txt, :enc("ASCII");
34+
is slurp($path), $txt, "spurt with enc";
6335
}
64-
36+
37+
#?niecza skip "Excess arguments to spurt, unused named append"
38+
{
39+
spurt $handle(), $buf;
40+
spurt $handle(), $buf, :append;
41+
is slurp($path, :bin), ($buf ~ $buf), "spurting Buf with append";
42+
}
43+
6544
#?niecza skip "Excess arguments to spurt, unused named append"
6645
{
67-
spurt $path.IO, "42";
68-
spurt $path.IO, "24", :append;
69-
is slurp($path), "4224", "spurt to IO with append";
46+
spurt $handle(), $txt;
47+
spurt $handle(), $txt, :append;
48+
is slurp($path), ($txt ~ $txt), "spurting txt with append";
7049
}
50+
51+
unlink $path;
52+
53+
#?niecza skip "Excess arguments to spurt, unused named createonly"
54+
lives_ok { spurt $handle(), $buf, :createonly }, "createonly creates file with Buf";
55+
#?niecza todo ""
56+
ok $path.IO.e, "file was created";
57+
dies_ok { spurt $handle(), $txt, :createonly }, "createonly with Buf fails if file exists";
7158
unlink $path;
7259

73-
#?niecza 2 skip "Excess arguments to spurt, unused named createonly"
74-
lives_ok { spurt $path.IO, "", :createonly }
60+
#?niecza skip "Excess arguments to spurt, unused named createonly"
61+
lives_ok { spurt $handle(), $txt, :createonly }, "createonly with text creates file";
62+
#?niecza todo ""
7563
ok $path.IO.e, "file was created";
76-
dies_ok { spurt $path.IO, "", :createonly }
64+
dies_ok { spurt $handle(), $txt, :createonly }, "createonly with text fails if file exists";
7765
unlink $path;
7866
}
7967

0 commit comments

Comments
 (0)