Skip to content

Commit bfcd27b

Browse files
committed
add S32-io/open.t that tests extended open modes
1 parent 0d2a5c0 commit bfcd27b

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

S32-io/open.t

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
use v6;
2+
use Test;
3+
4+
plan 23;
5+
6+
my \PATH = 't-S32-io-open.tmp';
7+
my \PATH-RX = rx/'t-S32-io-open.tmp'/;
8+
9+
LEAVE unlink PATH;
10+
11+
# cannot open nonexistent files without :create
12+
{ unlink PATH;
13+
14+
throws-like 'open PATH, :mode<ro>', Exception,
15+
'cannot open nonexistent file in mode ro', message => PATH-RX;
16+
17+
throws-like 'open PATH, :mode<wo>', Exception,
18+
'cannot open nonexistent file in mode wo', message => PATH-RX;
19+
20+
throws-like 'open PATH, :mode<rw>', Exception,
21+
'cannot open nonexistent file in mode rw', message => PATH-RX;
22+
}
23+
24+
# can create, write to and read from file
25+
{ unlink PATH;
26+
my $fh;
27+
28+
$fh = open PATH, :mode<wo>, :create;
29+
ok defined($fh), 'can create file in mode wo';
30+
31+
ok ?$fh.print('42'), 'can write to file in mode wo';
32+
33+
$fh.close;
34+
35+
$fh = open PATH, :mode<ro>;
36+
ok defined($fh), 'can open existing file in mode ro';
37+
38+
is $fh.get, '42', 'can read from file in mode ro';
39+
40+
$fh.close;
41+
42+
$fh = open PATH, :mode<wo>;
43+
ok defined($fh), 'can open existing file in mode wo';
44+
45+
throws-like '$fh.get', Exception, 'cannot read from file in mode wo';
46+
47+
$fh.close;
48+
}
49+
50+
# test :rw
51+
{ unlink PATH;
52+
53+
my $fh = open PATH, :rw;
54+
ok defined($fh), 'can use :rw to create file';
55+
56+
$fh.print('cthulhu fhtagn');
57+
$fh.seek(0, SeekFromBeginning);
58+
59+
is $fh.get, 'cthulhu fhtagn', 'can write to and read from :rw file';
60+
61+
$fh.close;
62+
}
63+
64+
# test :update
65+
{ unlink PATH;
66+
my $fh;
67+
68+
throws-like 'open PATH, :update', Exception,
69+
'cannot use :update on nonexistent file', message => PATH-RX;
70+
71+
$fh = open PATH, :w;
72+
$fh.print('12x45');
73+
$fh.close;
74+
75+
$fh = open PATH, :update;
76+
ok defined($fh), 'can use :update on existing file';
77+
78+
$fh.seek(2, SeekFromBeginning);
79+
is $fh.getc, 'x', 'can use :update to read from file';
80+
81+
$fh.seek(2, SeekFromBeginning);
82+
ok ?$fh.write('3'.encode), 'can use :update to write to file';
83+
84+
$fh.seek(0, SeekFromBeginning);
85+
is $fh.get, '12345', 'have used :update successfully';
86+
87+
$fh.close;
88+
}
89+
90+
# check that :rw does not truncate
91+
{ unlink PATH;
92+
my $fh;
93+
94+
$fh = open PATH, :w;
95+
$fh.print('camelia');
96+
$fh.close;
97+
98+
$fh = open PATH, :rw;
99+
is $fh.get, 'camelia', 'using :rw does not truncate';
100+
101+
$fh.close;
102+
}
103+
104+
# check that :w does truncate
105+
{ unlink PATH;
106+
my $fh;
107+
108+
$fh = open PATH, :w;
109+
$fh.print('camelia');
110+
$fh.close;
111+
112+
$fh = open PATH, :w;
113+
$fh.close;
114+
115+
$fh = open PATH, :r;
116+
ok !defined($fh.getc), 'using :w does truncate';
117+
118+
$fh.close;
119+
}
120+
121+
# test :x
122+
{ unlink PATH;
123+
124+
my $fh = open PATH, :x;
125+
ok defined($fh), 'can use :x to create file';
126+
127+
$fh.close;
128+
129+
throws-like 'open PATH, :x', Exception,
130+
'cannot use :x to open existing file', message => PATH-RX;
131+
}
132+
133+
# test :rx
134+
{ unlink PATH;
135+
136+
my $fh = open PATH, :rx;
137+
ok defined($fh), 'can use :rx to create file';
138+
139+
$fh.print('I <3 P6');
140+
$fh.seek(0, SeekFromBeginning);
141+
142+
is $fh.get, 'I <3 P6', 'can write to and read from :rx file';
143+
144+
$fh.close;
145+
146+
throws-like 'open PATH, :rx', Exception,
147+
'cannot use :rx to open existing file', message => PATH-RX;
148+
}
149+
150+
# vim: ft=perl6

0 commit comments

Comments
 (0)