1 | ##
|
---|
2 | # IO Test
|
---|
3 |
|
---|
4 | MRubyIOTestUtil.io_test_setup
|
---|
5 | $cr, $crlf, $cmd = MRubyIOTestUtil.win? ? [1, "\r\n", "cmd /c "] : [0, "\n", ""]
|
---|
6 |
|
---|
7 | def assert_io_open(meth)
|
---|
8 | assert "assert_io_open" do
|
---|
9 | fd = IO.sysopen($mrbtest_io_rfname)
|
---|
10 | assert_equal Fixnum, fd.class
|
---|
11 | io1 = IO.__send__(meth, fd)
|
---|
12 | begin
|
---|
13 | assert_equal IO, io1.class
|
---|
14 | assert_equal $mrbtest_io_msg, io1.read
|
---|
15 | ensure
|
---|
16 | io1.close
|
---|
17 | end
|
---|
18 |
|
---|
19 | io2 = IO.__send__(meth, IO.sysopen($mrbtest_io_rfname))do |io|
|
---|
20 | if meth == :open
|
---|
21 | assert_equal $mrbtest_io_msg, io.read
|
---|
22 | else
|
---|
23 | flunk "IO.#{meth} does not take block"
|
---|
24 | end
|
---|
25 | end
|
---|
26 | io2.close unless meth == :open
|
---|
27 | end
|
---|
28 | end
|
---|
29 |
|
---|
30 | assert('IO.class', '15.2.20') do
|
---|
31 | assert_equal(Class, IO.class)
|
---|
32 | end
|
---|
33 |
|
---|
34 | assert('IO.superclass', '15.2.20.2') do
|
---|
35 | assert_equal(Object, IO.superclass)
|
---|
36 | end
|
---|
37 |
|
---|
38 | assert('IO.ancestors', '15.2.20.3') do
|
---|
39 | assert_include(IO.ancestors, Enumerable)
|
---|
40 | end
|
---|
41 |
|
---|
42 | assert('IO.open', '15.2.20.4.1') do
|
---|
43 | assert_io_open(:open)
|
---|
44 | end
|
---|
45 |
|
---|
46 | assert('IO#close', '15.2.20.5.1') do
|
---|
47 | io = IO.new(IO.sysopen($mrbtest_io_rfname))
|
---|
48 | assert_nil io.close
|
---|
49 | end
|
---|
50 |
|
---|
51 | assert('IO#closed?', '15.2.20.5.2') do
|
---|
52 | io = IO.new(IO.sysopen($mrbtest_io_rfname))
|
---|
53 | assert_false io.closed?
|
---|
54 | io.close
|
---|
55 | assert_true io.closed?
|
---|
56 | end
|
---|
57 |
|
---|
58 | #assert('IO#each', '15.2.20.5.3') do
|
---|
59 | #assert('IO#each_byte', '15.2.20.5.4') do
|
---|
60 | #assert('IO#each_line', '15.2.20.5.5') do
|
---|
61 |
|
---|
62 | assert('IO#eof?', '15.2.20.5.6') do
|
---|
63 | io = IO.new(IO.sysopen($mrbtest_io_wfname, 'w'), 'w')
|
---|
64 | assert_raise(IOError) do
|
---|
65 | io.eof?
|
---|
66 | end
|
---|
67 | io.close
|
---|
68 |
|
---|
69 | # empty file
|
---|
70 | io = IO.open(IO.sysopen($mrbtest_io_wfname, 'w'), 'w')
|
---|
71 | io.close
|
---|
72 | io = IO.open(IO.sysopen($mrbtest_io_wfname, 'r'), 'r')
|
---|
73 | assert_true io.eof?
|
---|
74 | io.close
|
---|
75 |
|
---|
76 | # nonempty file
|
---|
77 | io = IO.new(IO.sysopen($mrbtest_io_rfname))
|
---|
78 | assert_false io.eof?
|
---|
79 | io.readchar
|
---|
80 | assert_false io.eof?
|
---|
81 | io.read
|
---|
82 | assert_true io.eof?
|
---|
83 | io.close
|
---|
84 | end
|
---|
85 |
|
---|
86 | assert('IO#flush', '15.2.20.5.7') do
|
---|
87 | # Note: mruby-io does not have any buffer to be flushed now.
|
---|
88 | io = IO.new(IO.sysopen($mrbtest_io_wfname))
|
---|
89 | assert_equal io, io.flush
|
---|
90 | io.close
|
---|
91 | assert_raise(IOError) do
|
---|
92 | io.flush
|
---|
93 | end
|
---|
94 | end
|
---|
95 |
|
---|
96 | assert('IO#getc', '15.2.20.5.8') do
|
---|
97 | io = IO.new(IO.sysopen($mrbtest_io_rfname))
|
---|
98 | $mrbtest_io_msg.split("").each { |ch|
|
---|
99 | assert_equal ch, io.getc
|
---|
100 | }
|
---|
101 | assert_equal nil, io.getc
|
---|
102 | io.close
|
---|
103 | end
|
---|
104 |
|
---|
105 | #assert('IO#gets', '15.2.20.5.9') do
|
---|
106 | #assert('IO#initialize_copy', '15.2.20.5.10') do
|
---|
107 | #assert('IO#print', '15.2.20.5.11') do
|
---|
108 | #assert('IO#putc', '15.2.20.5.12') do
|
---|
109 | #assert('IO#puts', '15.2.20.5.13') do
|
---|
110 |
|
---|
111 | assert('IO#read', '15.2.20.5.14') do
|
---|
112 | IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
|
---|
113 | assert_raise(ArgumentError) { io.read(-5) }
|
---|
114 | assert_raise(TypeError) { io.read("str") }
|
---|
115 |
|
---|
116 | len = $mrbtest_io_msg.length
|
---|
117 | assert_equal '', io.read(0)
|
---|
118 | assert_equal 'mruby', io.read(5)
|
---|
119 | assert_equal $mrbtest_io_msg[5,len], io.read(len)
|
---|
120 |
|
---|
121 | assert_equal "", io.read
|
---|
122 | assert_nil io.read(1)
|
---|
123 | end
|
---|
124 |
|
---|
125 | IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
|
---|
126 | assert_equal $mrbtest_io_msg, io.read
|
---|
127 | end
|
---|
128 | end
|
---|
129 |
|
---|
130 | assert "IO#read(n) with n > IO::BUF_SIZE" do
|
---|
131 | skip "pipe is not supported on this platform" if MRubyIOTestUtil.win?
|
---|
132 | IO.pipe do |r,w|
|
---|
133 | n = IO::BUF_SIZE+1
|
---|
134 | w.write 'a'*n
|
---|
135 | assert_equal 'a'*n, r.read(n)
|
---|
136 | end
|
---|
137 | end
|
---|
138 |
|
---|
139 | assert('IO#readchar', '15.2.20.5.15') do
|
---|
140 | # almost same as IO#getc
|
---|
141 | IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
|
---|
142 | $mrbtest_io_msg.split("").each { |ch|
|
---|
143 | assert_equal ch, io.readchar
|
---|
144 | }
|
---|
145 | assert_raise(EOFError) do
|
---|
146 | io.readchar
|
---|
147 | end
|
---|
148 | end
|
---|
149 | end
|
---|
150 |
|
---|
151 | #assert('IO#readline', '15.2.20.5.16') do
|
---|
152 | #assert('IO#readlines', '15.2.20.5.17') do
|
---|
153 |
|
---|
154 | assert('IO#sync', '15.2.20.5.18') do
|
---|
155 | io = IO.new(IO.sysopen($mrbtest_io_rfname))
|
---|
156 | s = io.sync
|
---|
157 | assert_true(s == true || s == false)
|
---|
158 | io.close
|
---|
159 | assert_raise(IOError) do
|
---|
160 | io.sync
|
---|
161 | end
|
---|
162 | end
|
---|
163 |
|
---|
164 | assert('IO#sync=', '15.2.20.5.19') do
|
---|
165 | io = IO.new(IO.sysopen($mrbtest_io_rfname))
|
---|
166 | io.sync = true
|
---|
167 | assert_true io.sync
|
---|
168 | io.sync = false
|
---|
169 | assert_false io.sync
|
---|
170 | io.close
|
---|
171 | assert_raise(IOError) do
|
---|
172 | io.sync = true
|
---|
173 | end
|
---|
174 | end
|
---|
175 |
|
---|
176 | assert('IO#write', '15.2.20.5.20') do
|
---|
177 | io = IO.open(IO.sysopen($mrbtest_io_wfname))
|
---|
178 | assert_equal 0, io.write("")
|
---|
179 | io.close
|
---|
180 |
|
---|
181 | io = IO.open(IO.sysopen($mrbtest_io_wfname, "r+"), "r+")
|
---|
182 | assert_equal 7, io.write("abcdefg")
|
---|
183 | io.rewind
|
---|
184 | assert_equal "ab", io.read(2)
|
---|
185 | assert_equal 3, io.write("123")
|
---|
186 | io.rewind
|
---|
187 | assert_equal "ab123fg", io.read
|
---|
188 | io.close
|
---|
189 | end
|
---|
190 |
|
---|
191 | assert('IO#<<') do
|
---|
192 | io = IO.open(IO.sysopen($mrbtest_io_wfname))
|
---|
193 | io << "" << ""
|
---|
194 | assert_equal 0, io.pos
|
---|
195 | io.close
|
---|
196 | end
|
---|
197 |
|
---|
198 | assert('IO#dup for readable') do
|
---|
199 | io = IO.new(IO.sysopen($mrbtest_io_rfname))
|
---|
200 | dup = io.dup
|
---|
201 | assert_true io != dup
|
---|
202 | assert_true io.fileno != dup.fileno
|
---|
203 | begin
|
---|
204 | assert_true dup.close_on_exec?
|
---|
205 | rescue NotImplementedError
|
---|
206 | end
|
---|
207 | assert_equal 'm', dup.sysread(1)
|
---|
208 | assert_equal 'r', io.sysread(1)
|
---|
209 | assert_equal 'u', dup.sysread(1)
|
---|
210 | assert_equal 'b', io.sysread(1)
|
---|
211 | assert_equal 'y', dup.sysread(1)
|
---|
212 | dup.close
|
---|
213 | assert_false io.closed?
|
---|
214 | io.close
|
---|
215 | end
|
---|
216 |
|
---|
217 | assert('IO#dup for writable') do
|
---|
218 | io = IO.open(IO.sysopen($mrbtest_io_wfname, 'w+'), 'w+')
|
---|
219 | dup = io.dup
|
---|
220 | io.syswrite "mruby"
|
---|
221 | assert_equal 5, dup.sysseek(0, IO::SEEK_CUR)
|
---|
222 | io.sysseek 0, IO::SEEK_SET
|
---|
223 | assert_equal 0, dup.sysseek(0, IO::SEEK_CUR)
|
---|
224 | assert_equal "mruby", dup.sysread(5)
|
---|
225 | dup.close
|
---|
226 | io.close
|
---|
227 | end
|
---|
228 |
|
---|
229 | assert('IO.for_fd') do
|
---|
230 | assert_io_open(:for_fd)
|
---|
231 | end
|
---|
232 |
|
---|
233 | assert('IO.new') do
|
---|
234 | assert_io_open(:new)
|
---|
235 | end
|
---|
236 |
|
---|
237 | assert('IO gc check') do
|
---|
238 | assert_nothing_raised { 100.times { IO.new(0) } }
|
---|
239 | end
|
---|
240 |
|
---|
241 | assert('IO.sysopen("./nonexistent")') do
|
---|
242 | if Object.const_defined? :Errno
|
---|
243 | eclass = Errno::ENOENT
|
---|
244 | else
|
---|
245 | eclass = RuntimeError
|
---|
246 | end
|
---|
247 | assert_raise eclass do
|
---|
248 | fd = IO.sysopen "./nonexistent"
|
---|
249 | IO._sysclose fd
|
---|
250 | end
|
---|
251 | end
|
---|
252 |
|
---|
253 | assert('IO.sysopen, IO#sysread') do
|
---|
254 | fd = IO.sysopen $mrbtest_io_rfname
|
---|
255 | io = IO.new fd
|
---|
256 | str1 = " "
|
---|
257 | str2 = io.sysread(5, str1)
|
---|
258 | assert_equal $mrbtest_io_msg[0,5], str1
|
---|
259 | assert_equal $mrbtest_io_msg[0,5], str2
|
---|
260 | assert_raise EOFError do
|
---|
261 | io.sysread(10000)
|
---|
262 | io.sysread(10000)
|
---|
263 | end
|
---|
264 |
|
---|
265 | assert_raise RuntimeError do
|
---|
266 | io.sysread(5, "abcde".freeze)
|
---|
267 | end
|
---|
268 |
|
---|
269 | io.close
|
---|
270 | assert_equal "", io.sysread(0)
|
---|
271 | assert_raise(IOError) { io.sysread(1) }
|
---|
272 | assert_raise(ArgumentError) { io.sysread(-1) }
|
---|
273 | io.closed?
|
---|
274 |
|
---|
275 | fd = IO.sysopen $mrbtest_io_wfname, "w"
|
---|
276 | io = IO.new fd, "w"
|
---|
277 | assert_raise(IOError) { io.sysread(1) }
|
---|
278 | io.close
|
---|
279 | end
|
---|
280 |
|
---|
281 | assert('IO.sysopen, IO#syswrite') do
|
---|
282 | fd = IO.sysopen $mrbtest_io_wfname, "w"
|
---|
283 | io = IO.new fd, "w"
|
---|
284 | str = "abcdefg"
|
---|
285 | len = io.syswrite(str)
|
---|
286 | assert_equal str.size, len
|
---|
287 | io.close
|
---|
288 |
|
---|
289 | io = IO.new(IO.sysopen($mrbtest_io_rfname), "r")
|
---|
290 | assert_raise(IOError) { io.syswrite("a") }
|
---|
291 | io.close
|
---|
292 | end
|
---|
293 |
|
---|
294 | assert('IO#_read_buf') do
|
---|
295 | fd = IO.sysopen $mrbtest_io_rfname
|
---|
296 | io = IO.new fd
|
---|
297 | def io._buf
|
---|
298 | @buf
|
---|
299 | end
|
---|
300 | msg_len = $mrbtest_io_msg.size
|
---|
301 | assert_equal '', io._buf
|
---|
302 | assert_equal $mrbtest_io_msg, io._read_buf
|
---|
303 | assert_equal $mrbtest_io_msg, io._buf
|
---|
304 | assert_equal 'mruby', io.read(5)
|
---|
305 | assert_equal 5, io.pos
|
---|
306 | assert_equal msg_len - 5, io._buf.size
|
---|
307 | assert_equal $mrbtest_io_msg[5,100], io.read
|
---|
308 | assert_equal 0, io._buf.size
|
---|
309 | assert_raise EOFError do
|
---|
310 | io._read_buf
|
---|
311 | end
|
---|
312 | assert_equal true, io.eof
|
---|
313 | assert_equal true, io.eof?
|
---|
314 | io.close
|
---|
315 | end
|
---|
316 |
|
---|
317 | assert('IO#isatty') do
|
---|
318 | skip "isatty is not supported on this platform" if MRubyIOTestUtil.win?
|
---|
319 | begin
|
---|
320 | f = File.open("/dev/tty")
|
---|
321 | rescue RuntimeError => e
|
---|
322 | skip e.message
|
---|
323 | else
|
---|
324 | assert_true f.isatty
|
---|
325 | ensure
|
---|
326 | f&.close
|
---|
327 | end
|
---|
328 | begin
|
---|
329 | f = File.open($mrbtest_io_rfname)
|
---|
330 | assert_false f.isatty
|
---|
331 | ensure
|
---|
332 | f&.close
|
---|
333 | end
|
---|
334 | end
|
---|
335 |
|
---|
336 | assert('IO#pos=, IO#seek') do
|
---|
337 | fd = IO.sysopen $mrbtest_io_rfname
|
---|
338 | io = IO.new fd
|
---|
339 | def io._buf
|
---|
340 | @buf
|
---|
341 | end
|
---|
342 | assert_equal 'm', io.getc
|
---|
343 | assert_equal 1, io.pos
|
---|
344 | assert_equal 0, io.seek(0)
|
---|
345 | assert_equal 0, io.pos
|
---|
346 | io.close
|
---|
347 | end
|
---|
348 |
|
---|
349 | assert('IO#rewind') do
|
---|
350 | fd = IO.sysopen $mrbtest_io_rfname
|
---|
351 | io = IO.new fd
|
---|
352 | assert_equal 'm', io.getc
|
---|
353 | assert_equal 1, io.pos
|
---|
354 | assert_equal 0, io.rewind
|
---|
355 | assert_equal 0, io.pos
|
---|
356 | io.close
|
---|
357 | end
|
---|
358 |
|
---|
359 | assert('IO#gets') do
|
---|
360 | fd = IO.sysopen $mrbtest_io_rfname
|
---|
361 | io = IO.new fd
|
---|
362 |
|
---|
363 | # gets without arguments
|
---|
364 | assert_equal $mrbtest_io_msg, io.gets, "gets without arguments"
|
---|
365 | assert_equal nil, io.gets, "gets returns nil, when EOF"
|
---|
366 |
|
---|
367 | # gets with limit
|
---|
368 | io.pos = 0
|
---|
369 | assert_equal $mrbtest_io_msg[0, 5], io.gets(5), "gets with limit"
|
---|
370 |
|
---|
371 | # gets with rs
|
---|
372 | io.pos = 0
|
---|
373 | assert_equal $mrbtest_io_msg[0, 6], io.gets(' '), "gets with rs"
|
---|
374 |
|
---|
375 | # gets with rs, limit
|
---|
376 | io.pos = 0
|
---|
377 | assert_equal $mrbtest_io_msg[0, 5], io.gets(' ', 5), "gets with rs, limit"
|
---|
378 | io.close
|
---|
379 | assert_equal true, io.closed?, "close success"
|
---|
380 |
|
---|
381 | # reading many-lines file.
|
---|
382 | fd = IO.sysopen $mrbtest_io_wfname, "w"
|
---|
383 | io = IO.new fd, "w"
|
---|
384 | io.write "0123456789" * 2 + "\na"
|
---|
385 | assert_equal 22 + $cr, io.pos
|
---|
386 | io.close
|
---|
387 | assert_equal true, io.closed?
|
---|
388 |
|
---|
389 | fd = IO.sysopen $mrbtest_io_wfname
|
---|
390 | io = IO.new fd
|
---|
391 | line = io.gets
|
---|
392 |
|
---|
393 | # gets first line
|
---|
394 | assert_equal "0123456789" * 2 + "\n", line, "gets first line"
|
---|
395 | assert_equal 21, line.size
|
---|
396 | assert_equal 21 + $cr, io.pos
|
---|
397 |
|
---|
398 | # gets second line
|
---|
399 | assert_equal "a", io.gets, "gets second line"
|
---|
400 |
|
---|
401 | # gets third line
|
---|
402 | assert_equal nil, io.gets, "gets third line; returns nil"
|
---|
403 |
|
---|
404 | io.close
|
---|
405 | end
|
---|
406 |
|
---|
407 | assert('IO#gets - paragraph mode') do
|
---|
408 | fd = IO.sysopen $mrbtest_io_wfname, "w"
|
---|
409 | io = IO.new fd, "w"
|
---|
410 | io.write "0" * 10 + "\n"
|
---|
411 | io.write "1" * 10 + "\n\n"
|
---|
412 | io.write "2" * 10 + "\n"
|
---|
413 | assert_equal 34 + $cr * 4, io.pos
|
---|
414 | io.close
|
---|
415 |
|
---|
416 | fd = IO.sysopen $mrbtest_io_wfname
|
---|
417 | io = IO.new fd
|
---|
418 | para1 = "#{'0' * 10}\n#{'1' * 10}\n\n"
|
---|
419 | text1 = io.gets("")
|
---|
420 | assert_equal para1, text1
|
---|
421 | para2 = "#{'2' * 10}\n"
|
---|
422 | text2 = io.gets("")
|
---|
423 | assert_equal para2, text2
|
---|
424 | io.close
|
---|
425 | end
|
---|
426 |
|
---|
427 | assert('IO.popen') do
|
---|
428 | begin
|
---|
429 | $? = nil
|
---|
430 | io = IO.popen("#{$cmd}echo mruby-io")
|
---|
431 | assert_true io.close_on_exec?
|
---|
432 | assert_equal Fixnum, io.pid.class
|
---|
433 |
|
---|
434 | out = io.read
|
---|
435 | assert_equal out.class, String
|
---|
436 | assert_include out, 'mruby-io'
|
---|
437 |
|
---|
438 | io.close
|
---|
439 | if Object.const_defined? :Process
|
---|
440 | assert_true $?.success?
|
---|
441 | else
|
---|
442 | assert_equal 0, $?
|
---|
443 | end
|
---|
444 |
|
---|
445 | assert_true io.closed?
|
---|
446 | rescue NotImplementedError => e
|
---|
447 | skip e.message
|
---|
448 | end
|
---|
449 | end
|
---|
450 |
|
---|
451 | assert('IO.popen with in option') do
|
---|
452 | begin
|
---|
453 | IO.pipe do |r, w|
|
---|
454 | w.write 'hello'
|
---|
455 | w.close
|
---|
456 | assert_equal "hello", IO.popen("cat", "r", in: r) { |i| i.read }
|
---|
457 | assert_equal "", r.read
|
---|
458 | end
|
---|
459 | assert_raise(ArgumentError) { IO.popen("hello", "r", in: Object.new) }
|
---|
460 | rescue NotImplementedError => e
|
---|
461 | skip e.message
|
---|
462 | end
|
---|
463 | end
|
---|
464 |
|
---|
465 | assert('IO.popen with out option') do
|
---|
466 | begin
|
---|
467 | IO.pipe do |r, w|
|
---|
468 | IO.popen("echo 'hello'", "w", out: w) {}
|
---|
469 | w.close
|
---|
470 | assert_equal "hello\n", r.read
|
---|
471 | end
|
---|
472 | rescue NotImplementedError => e
|
---|
473 | skip e.message
|
---|
474 | end
|
---|
475 | end
|
---|
476 |
|
---|
477 | assert('IO.popen with err option') do
|
---|
478 | begin
|
---|
479 | IO.pipe do |r, w|
|
---|
480 | assert_equal "", IO.popen("echo 'hello' 1>&2", "r", err: w) { |i| i.read }
|
---|
481 | w.close
|
---|
482 | assert_equal "hello\n", r.read
|
---|
483 | end
|
---|
484 | rescue NotImplementedError => e
|
---|
485 | skip e.message
|
---|
486 | end
|
---|
487 | end
|
---|
488 |
|
---|
489 | assert('IO.read') do
|
---|
490 | # empty file
|
---|
491 | fd = IO.sysopen $mrbtest_io_wfname, "w"
|
---|
492 | io = IO.new fd, "w"
|
---|
493 | io.close
|
---|
494 | assert_equal "", IO.read($mrbtest_io_wfname)
|
---|
495 | assert_equal nil, IO.read($mrbtest_io_wfname, 1)
|
---|
496 |
|
---|
497 | # one byte file
|
---|
498 | fd = IO.sysopen $mrbtest_io_wfname, "w"
|
---|
499 | io = IO.new fd, "w"
|
---|
500 | io.write "123"
|
---|
501 | io.close
|
---|
502 | assert_equal "123", IO.read($mrbtest_io_wfname)
|
---|
503 | assert_equal "", IO.read($mrbtest_io_wfname, 0)
|
---|
504 | assert_equal "1", IO.read($mrbtest_io_wfname, 1)
|
---|
505 | assert_equal "", IO.read($mrbtest_io_wfname, 0, 10)
|
---|
506 | assert_equal "23", IO.read($mrbtest_io_wfname, 2, 1)
|
---|
507 | assert_equal "23", IO.read($mrbtest_io_wfname, 10, 1)
|
---|
508 | assert_equal "", IO.read($mrbtest_io_wfname, nil, 10)
|
---|
509 | assert_equal nil, IO.read($mrbtest_io_wfname, 1, 10)
|
---|
510 | end
|
---|
511 |
|
---|
512 | assert('IO#fileno') do
|
---|
513 | fd = IO.sysopen $mrbtest_io_rfname
|
---|
514 | io = IO.new fd
|
---|
515 | assert_equal io.fileno, fd
|
---|
516 | assert_equal io.to_i, fd
|
---|
517 | io.close
|
---|
518 | end
|
---|
519 |
|
---|
520 | assert('IO#close_on_exec') do
|
---|
521 | fd = IO.sysopen $mrbtest_io_wfname, "w"
|
---|
522 | io = IO.new fd, "w"
|
---|
523 | begin
|
---|
524 | # IO.sysopen opens a file descripter with O_CLOEXEC flag.
|
---|
525 | assert_true io.close_on_exec?
|
---|
526 | rescue ScriptError
|
---|
527 | io.close
|
---|
528 | skip "IO\#close_on_exec is not implemented."
|
---|
529 | end
|
---|
530 |
|
---|
531 | io.close_on_exec = false
|
---|
532 | assert_equal(false, io.close_on_exec?)
|
---|
533 | io.close_on_exec = true
|
---|
534 | assert_equal(true, io.close_on_exec?)
|
---|
535 | io.close_on_exec = false
|
---|
536 | assert_equal(false, io.close_on_exec?)
|
---|
537 |
|
---|
538 | io.close
|
---|
539 |
|
---|
540 | begin
|
---|
541 | r, w = IO.pipe
|
---|
542 | assert_equal(true, r.close_on_exec?)
|
---|
543 | r.close_on_exec = false
|
---|
544 | assert_equal(false, r.close_on_exec?)
|
---|
545 | r.close_on_exec = true
|
---|
546 | assert_equal(true, r.close_on_exec?)
|
---|
547 |
|
---|
548 | assert_equal(true, w.close_on_exec?)
|
---|
549 | w.close_on_exec = false
|
---|
550 | assert_equal(false, w.close_on_exec?)
|
---|
551 | w.close_on_exec = true
|
---|
552 | assert_equal(true, w.close_on_exec?)
|
---|
553 | ensure
|
---|
554 | r.close unless r.closed?
|
---|
555 | w.close unless w.closed?
|
---|
556 | end
|
---|
557 | end
|
---|
558 |
|
---|
559 | assert('IO#sysseek') do
|
---|
560 | IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
|
---|
561 | assert_equal 2, io.sysseek(2)
|
---|
562 | assert_equal 5, io.sysseek(3, IO::SEEK_CUR) # 2 + 3 => 5
|
---|
563 | assert_equal $mrbtest_io_msg.size - 4, io.sysseek(-4, IO::SEEK_END)
|
---|
564 | end
|
---|
565 | end
|
---|
566 |
|
---|
567 | assert('IO#pread') do
|
---|
568 | skip "IO#pread is not implemented on this configuration" unless MRubyIOTestUtil::MRB_WITH_IO_PREAD_PWRITE
|
---|
569 |
|
---|
570 | IO.open(IO.sysopen($mrbtest_io_rfname, 'r'), 'r') do |io|
|
---|
571 | assert_equal $mrbtest_io_msg.byteslice(5, 8), io.pread(8, 5)
|
---|
572 | assert_equal 0, io.pos
|
---|
573 | assert_equal $mrbtest_io_msg.byteslice(1, 5), io.pread(5, 1)
|
---|
574 | assert_equal 0, io.pos
|
---|
575 | assert_raise(RuntimeError) { io.pread(20, -9) }
|
---|
576 | end
|
---|
577 | end
|
---|
578 |
|
---|
579 | assert('IO#pwrite') do
|
---|
580 | skip "IO#pwrite is not implemented on this configuration" unless MRubyIOTestUtil::MRB_WITH_IO_PREAD_PWRITE
|
---|
581 |
|
---|
582 | IO.open(IO.sysopen($mrbtest_io_wfname, 'w+'), 'w+') do |io|
|
---|
583 | assert_equal 6, io.pwrite("Warld!", 7)
|
---|
584 | assert_equal 0, io.pos
|
---|
585 | assert_equal 7, io.pwrite("Hello, ", 0)
|
---|
586 | assert_equal 0, io.pos
|
---|
587 | assert_equal "Hello, Warld!", io.read
|
---|
588 | assert_equal 6, io.pwrite("world!", 7)
|
---|
589 | assert_equal 13, io.pos
|
---|
590 | io.pos = 0
|
---|
591 | assert_equal "Hello, world!", io.read
|
---|
592 | end
|
---|
593 | end
|
---|
594 |
|
---|
595 | assert('IO.pipe') do
|
---|
596 | begin
|
---|
597 | called = false
|
---|
598 | IO.pipe do |r, w|
|
---|
599 | assert_true r.kind_of?(IO)
|
---|
600 | assert_true w.kind_of?(IO)
|
---|
601 | assert_false r.closed?
|
---|
602 | assert_false w.closed?
|
---|
603 | assert_true FileTest.pipe?(r)
|
---|
604 | assert_true FileTest.pipe?(w)
|
---|
605 | assert_nil r.pid
|
---|
606 | assert_nil w.pid
|
---|
607 | assert_true 2 < r.fileno
|
---|
608 | assert_true 2 < w.fileno
|
---|
609 | assert_true r.fileno != w.fileno
|
---|
610 | assert_false r.sync
|
---|
611 | assert_true w.sync
|
---|
612 | assert_equal 8, w.write('test for')
|
---|
613 | assert_equal 'test', r.read(4)
|
---|
614 | assert_equal ' for', r.read(4)
|
---|
615 | assert_equal 5, w.write(' pipe')
|
---|
616 | assert_equal nil, w.close
|
---|
617 | assert_equal ' pipe', r.read
|
---|
618 | called = true
|
---|
619 | assert_raise(IOError) { r.write 'test' }
|
---|
620 | # TODO:
|
---|
621 | # This assert expect raise IOError but got RuntimeError
|
---|
622 | # Because mruby-io not have flag for I/O readable
|
---|
623 | # assert_raise(IOError) { w.read }
|
---|
624 | end
|
---|
625 | assert_true called
|
---|
626 |
|
---|
627 | assert_nothing_raised do
|
---|
628 | IO.pipe { |r, w| r.close; w.close }
|
---|
629 | end
|
---|
630 | rescue NotImplementedError => e
|
---|
631 | skip e.message
|
---|
632 | end
|
---|
633 | end
|
---|
634 |
|
---|
635 | assert('`cmd`') do
|
---|
636 | begin
|
---|
637 | assert_equal `#{$cmd}echo foo`, "foo#{$crlf}"
|
---|
638 | rescue NotImplementedError => e
|
---|
639 | skip e.message
|
---|
640 | end
|
---|
641 | end
|
---|
642 |
|
---|
643 | MRubyIOTestUtil.io_test_cleanup
|
---|