Skip to content
Newer
Older
100644 236 lines (194 sloc) 6.96 KB
58135b3 [t/spec] changed 'use v6-alpha;' to 'use v6;'
moritz authored
1 use v6;
eb624f9 [t] Test suite refactoring.
cosimo authored
2
3 use Test;
4
98791c8 update some of the smartlinks
szabgab authored
5 # L<S32::IO/IO::File/open>
6 # old: L<S16/"Filehandles, files, and directories"/"open">
7 # old: L<S16/"Filehandles, files, and directories"/"close">
8 # old: L<S16/Unfiled/IO.get>
eb624f9 [t] Test suite refactoring.
cosimo authored
9
10 =begin pod
11
12 I/O tests
13
14 =end pod
15
fcbbbbe @moritz clean up behind masak; be more explicit in testing the retrieved line
moritz authored
16 plan 83;
eb624f9 [t] Test suite refactoring.
cosimo authored
17
a29ae0f [t/spec] better tempfile name
moritz authored
18 #?pugs emit if $*OS eq "browser" {
19 #?pugs emit skip_rest "Programs running in browsers don't have access to regular IO.";
20 #?pugs emit exit;
21 #?pugs emit }
eb624f9 [t] Test suite refactoring.
cosimo authored
22
23
25e2988 [t/spec] rakudo doesn't do Int() yet, so use a different way to gener…
moritz authored
24 sub nonce () { return ".{$*PID}." ~ (1..1000).pick() }
a29ae0f [t/spec] better tempfile name
moritz authored
25 my $filename = 'tempfile_filehandles_io' ~ nonce();
eb624f9 [t] Test suite refactoring.
cosimo authored
26
27 # create and write a file
28
29 my $out = open($filename, :w);
207f13f [t] and [t/spec]
moritz authored
30 isa_ok($out, IO);
eb624f9 [t] Test suite refactoring.
cosimo authored
31 $out.say("Hello World");
31329b9 [t/spec]: Fudge io.t for Rakudo.
pmichaud authored
32 $out.say("Foo Bar Baz");
eb624f9 [t] Test suite refactoring.
cosimo authored
33 $out.say("The End");
34 ok($out.close, 'file closed okay');
35
36 # read the file all possible ways
37
38 my $in1 = open($filename);
207f13f [t] and [t/spec]
moritz authored
39 isa_ok($in1, IO);
1751666 change readline to get or lines; test for line limit
lwall authored
40 my $line1a = get($in1);
41 is($line1a, "Hello World", 'get($in) worked (and autochomps)');
64cdf98 [t/spec] initial, very basic tests for IO.ins
moritz authored
42 is $in1.ins, 1, 'read one line (.ins)';
1751666 change readline to get or lines; test for line limit
lwall authored
43 my $line1b = get($in1);
44 is($line1b, "Foo Bar Baz", 'get($in) worked (and autochomps)');
64cdf98 [t/spec] initial, very basic tests for IO.ins
moritz authored
45 is $in1.ins, 2, 'read two lines (.ins)';
1751666 change readline to get or lines; test for line limit
lwall authored
46 my $line1c = get($in1);
47 is($line1c, "The End", 'get($in) worked');
48 is $in1.ins, 3, 'read three lines (.ins)';
eb624f9 [t] Test suite refactoring.
cosimo authored
49 ok($in1.close, 'file closed okay (1)');
50
51 my $in2 = open($filename);
207f13f [t] and [t/spec]
moritz authored
52 isa_ok($in2, IO);
1751666 change readline to get or lines; test for line limit
lwall authored
53 my $line2a = $in2.get();
54 is($line2a, "Hello World", '$in.get() worked');
55 my $line2b = $in2.get();
56 is($line2b, "Foo Bar Baz", '$in.get() worked');
57 my $line2c = $in2.get();
58 is($line2c, "The End", '$in.get() worked');
eb624f9 [t] Test suite refactoring.
cosimo authored
59 ok($in2.close, 'file closed okay (2)');
60
61 # L<S02/Files/you now write>
62 my $in3 = open($filename);
207f13f [t] and [t/spec]
moritz authored
63 isa_ok($in3, IO);
dfd2876 [t/spec] remove a few occurances of =$fh
moritz authored
64 {
65 my $line3a = $in3.get;
66 is($line3a, "Hello World", '$in.get worked(1)');
67 my $line3b = $in3.get;
68 is($line3b, "Foo Bar Baz", '$in.get worked(2)');
69 my $line3c = $in3.get;
70 is($line3c, "The End", '$in.get worked(3)');
71 }
eb624f9 [t] Test suite refactoring.
cosimo authored
72 ok($in3.close, 'file closed okay (3)');
73
74 # append to the file
75
76 my $append = open($filename, :a);
207f13f [t] and [t/spec]
moritz authored
77 isa_ok($append, IO);
eb624f9 [t] Test suite refactoring.
cosimo authored
78 $append.say("... Its not over yet!");
79 ok($append.close, 'file closed okay (append)');
80
81 # now read in in list context
82
83 my $in4 = open($filename);
207f13f [t] and [t/spec]
moritz authored
84 isa_ok($in4, IO);
dfd2876 [t/spec] remove a few occurances of =$fh
moritz authored
85 my @lines4 = lines($in4);
eb624f9 [t] Test suite refactoring.
cosimo authored
86 is(+@lines4, 4, 'we got four lines from the file');
dfd2876 [t/spec] remove a few occurances of =$fh
moritz authored
87 is $in4.ins, 4, 'same with .ins';
1751666 change readline to get or lines; test for line limit
lwall authored
88 is(@lines4[0], "Hello World", 'lines($in) worked in list context');
89 is(@lines4[1], "Foo Bar Baz", 'lines($in) worked in list context');
90 is(@lines4[2], "The End", 'lines($in) worked in list context');
91 is(@lines4[3], "... Its not over yet!", 'lines($in) worked in list context');
eb624f9 [t] Test suite refactoring.
cosimo authored
92 ok($in4.close, 'file closed okay (4)');
93
2d9bbd8 fixing the last commit
dimid authored
94 #?rakudo skip "prototype of sub lines(...) doesn't have limit yet"
2e66d4b fix: skip block while lines($filename, $limit) isn't implemented yet
dimid authored
95 {
eb624f9 [t] Test suite refactoring.
cosimo authored
96 my $in5 = open($filename);
207f13f [t] and [t/spec]
moritz authored
97 isa_ok($in5, IO);
b6878a0 add tests for lines($fh, $limit) and lines($filename[, $limit])
dimid authored
98 my @lines5 = lines($in5, 3);
99 is(+@lines5, 3, 'we got two lines from the file');
100 is $in5.ins, 3, 'same with .ins';
101 is(@lines5[0], "Hello World", 'lines($in) worked in list context');
102 is(@lines5[1], "Foo Bar Baz", 'lines($in) worked in list context');
103 is(@lines5[2], "The End", 'lines($in) worked in list context');
eb624f9 [t] Test suite refactoring.
cosimo authored
104 ok($in5.close, 'file closed okay (5)');
2e66d4b fix: skip block while lines($filename, $limit) isn't implemented yet
dimid authored
105 }
eb624f9 [t] Test suite refactoring.
cosimo authored
106
107 my $in6 = open($filename);
207f13f [t] and [t/spec]
moritz authored
108 isa_ok($in6, IO);
b6878a0 add tests for lines($fh, $limit) and lines($filename[, $limit])
dimid authored
109 my @lines6 = $in6.lines();
eb624f9 [t] Test suite refactoring.
cosimo authored
110 is(+@lines6, 4, 'we got four lines from the file');
b6878a0 add tests for lines($fh, $limit) and lines($filename[, $limit])
dimid authored
111 is(@lines6[0], "Hello World", '$in.lines() worked in list context');
112 is(@lines6[1], "Foo Bar Baz", '$in.lines() worked in list context');
113 is(@lines6[2], "The End", '$in.lines() worked in list context');
114 is(@lines6[3], "... Its not over yet!", '$in.lines() worked in list context');
eb624f9 [t] Test suite refactoring.
cosimo authored
115 ok($in6.close, 'file closed okay (6)');
116
117 my $in7 = open($filename);
207f13f [t] and [t/spec]
moritz authored
118 isa_ok($in7, IO);
b6878a0 add tests for lines($fh, $limit) and lines($filename[, $limit])
dimid authored
119 my @lines7 = $in7.lines;
120 is(+@lines7, 4, 'we got four lines from the file');
121 is(@lines7[0], "Hello World", '$in.lines worked in list context');
122 is(@lines7[1], "Foo Bar Baz", '$in.lines worked in list context');
123 is(@lines7[2], "The End", '$in.lines worked in list context');
124 is(@lines7[3], "... Its not over yet!", '$in.lines worked in list context');
eb624f9 [t] Test suite refactoring.
cosimo authored
125 ok($in7.close, 'file closed okay (7)');
126
b6878a0 add tests for lines($fh, $limit) and lines($filename[, $limit])
dimid authored
127 {
128 # test reading a file into an array and then closing before
129 # doing anything with the array (in other words, is pugs too lazy)
130 my $in8 = open($filename);
131 isa_ok($in8, IO);
132 my @lines8 = $in8.lines(3);
133 push @lines8, "and finally" ~ $in8.get;
134 ok($in8.close, 'file closed okay (8)');
135 is(+@lines8, 4, 'we got four lines from the file (lazily)');
136 is(@lines8[0], "Hello World", 'lines($in,3) worked in list context');
137 is(@lines8[1], "Foo Bar Baz", 'lines($in,3) worked in list context');
138 is(@lines8[2], "The End", 'lines($in,3) worked in list context');
e52f458 @moritz fix and fudge io.t
moritz authored
139 #?rakudo todo 'nom regression'
b6878a0 add tests for lines($fh, $limit) and lines($filename[, $limit])
dimid authored
140 is(@lines8[3], "and finally... Its not over yet!", 'get($in) worked after lines($in,$n)');
141 }
eb624f9 [t] Test suite refactoring.
cosimo authored
142
b6878a0 add tests for lines($fh, $limit) and lines($filename[, $limit])
dimid authored
143 #now be sure to delete the file as well
76534ab @moritz be less paranoid about the actual return value of unlink, just evalua…
moritz authored
144 ok(unlink($filename), 'file has been removed');
b6878a0 add tests for lines($fh, $limit) and lines($filename[, $limit])
dimid authored
145
eb624f9 [t] Test suite refactoring.
cosimo authored
146 # new file for testing other types of open() calls
147
b6878a0 add tests for lines($fh, $limit) and lines($filename[, $limit])
dimid authored
148
eb624f9 [t] Test suite refactoring.
cosimo authored
149 my $out8 = open($filename, :w);
207f13f [t] and [t/spec]
moritz authored
150 isa_ok($out8, IO);
eb624f9 [t] Test suite refactoring.
cosimo authored
151 $out8.say("Hello World");
152 ok($out8.close, 'file closed okay (out8)');
153
154 my $in8 = open($filename);
207f13f [t] and [t/spec]
moritz authored
155 isa_ok($in8, IO);
1751666 change readline to get or lines; test for line limit
lwall authored
156 my $line8_1 = get($in8);
157 is($line8_1, "Hello World", 'get($in) worked');
eb624f9 [t] Test suite refactoring.
cosimo authored
158 ok($in8.close, 'file closed okay (in8)');
159
160 my $fh9 = open($filename, :r, :w); # was "<+" ?
207f13f [t] and [t/spec]
moritz authored
161 isa_ok($fh9, IO);
1751666 change readline to get or lines; test for line limit
lwall authored
162 #my $line9_1 = get($fh9);
eb624f9 [t] Test suite refactoring.
cosimo authored
163 #is($line9_1, "Hello World");
164 #$fh9.say("Second line");
165 ok($fh9.close, 'file closed okay (9)');
166
167 #my $in9 = open($filename);
207f13f [t] and [t/spec]
moritz authored
168 #isa_ok($in9, IO);
1751666 change readline to get or lines; test for line limit
lwall authored
169 #my $line9_1 = get($in9);
170 #my $line9_2 = get($in9);
171 #is($line9_1, "Hello World", 'get($in) worked');
172 #is($line9_2, "Second line", 'get($in) worked');
eb624f9 [t] Test suite refactoring.
cosimo authored
173
31329b9 [t/spec]: Fudge io.t for Rakudo.
pmichaud authored
174 #?rakudo skip ':rw on open() unimplemented'
175 {
176 my $fh10 = open($filename, :rw); # was "<+" ?
207f13f [t] and [t/spec]
moritz authored
177 isa_ok($fh10, IO);
31329b9 [t/spec]: Fudge io.t for Rakudo.
pmichaud authored
178 #ok($fh10.close, 'file closed okay (10)');
179 }
eb624f9 [t] Test suite refactoring.
cosimo authored
180
52ff3e9 [t/spec] Test for RT #65348
kyle authored
181 # RT #65348
182 {
183 my $rt65348_out = open($filename, :w);
184 isa_ok $rt65348_out, IO;
185 $rt65348_out.say( 'RT #65348' );
186 $rt65348_out.say( '13.37' );
187 $rt65348_out.say( '42.17' );
188 ok $rt65348_out.close, 'close worked (rt65348 out)';
189
190 my $rt65348_in = open( $filename );
191 isa_ok $rt65348_in, IO;
192 my @list_context = ($rt65348_in.get);
193 is +@list_context, 1, '.get in list context reads only one line';
194 ok $rt65348_in.get.Int ~~ Int, '.get.Int gets int';
195 is $rt65348_in.get.Int, 42, '.get.Int gets the right int';
196 ok $rt65348_in.close, 'close worked (rt65348 in)';
197 }
198
fecfcdd [t/spec] Remove some Win32-specific logic on a test that saw us flunk…
jnthn authored
199 #?pugs todo 'buggy on Win32'
200 ok(unlink($filename), 'file has been removed');
20f1e14 [t/spec] switch to .IO form of file tests
moritz authored
201 nok $filename.IO ~~ :e, '... and the tempfile is gone, really';
734bc95 [t/spec]
moritz authored
202
e52f458 @moritz fix and fudge io.t
moritz authored
203 #?rakudo skip 'binary IO, encode, Buf'
abbfa28 [t/spec/S16-filehandles/io.t] a few :bin tests for &open
masak authored
204 {
205 my $binary_out_fh = open($filename, :w, :bin);
206 isa_ok($binary_out_fh, IO);
207 $binary_out_fh.write("föö".encode("ISO-8859-1"));
208 ok($binary_out_fh.close(), "file closed OK");
209 }
210
e52f458 @moritz fix and fudge io.t
moritz authored
211 #?rakudo skip 'binary IO, encode, Buf'
abbfa28 [t/spec/S16-filehandles/io.t] a few :bin tests for &open
masak authored
212 {
213 my $binary_in_fh = open($filename, :r, :bin);
214 isa_ok($binary_in_fh, IO);
215 my $buf = $binary_in_fh.read(4);
216 is $buf.elems, 3, "three bytes were read";
217 is $buf.decode("ISO-8859-1"), "föö", "the bytes decode into the right Str";
218 }
219
b0d6bcf @mberends remove S16-filehandles/io.t tempfile, and .gitignore *.niecza
mberends authored
220 unlink($filename);
221
ec2194c @masak [S16-filehandles/io.t] test for 2b13fe in Rakudo
masak authored
222 $out = open($filename, :w);
223 $out.say("Hello World");
224 $out.say("Foo Bar Baz");
225 $out.say("The End");
226 $out.close;
227
fcbbbbe @moritz clean up behind masak; be more explicit in testing the retrieved line
moritz authored
228 my $line;
229 lives_ok { $line = $filename.IO.get; }, "can read lines without explicitly opening IO";
230 is $line, 'Hello World', 'got the right line from .IO.get';
231 unlink($filename);
ec2194c @masak [S16-filehandles/io.t] test for 2b13fe in Rakudo
masak authored
232
e76dd4c @moritz s/done_testing/done/ as per recent S24 changes
moritz authored
233 done;
52ff3e9 [t/spec] Test for RT #65348
kyle authored
234
734bc95 [t/spec]
moritz authored
235 # vim: ft=perl6
Something went wrong with that request. Please try again.