4 assert('IO TEST SETUP') do
5 MRubyIOTestUtil.io_test_setup
6 $cr = MRubyIOTestUtil.win? ? 1 : 0 # "\n" include CR or not
9 assert('IO', '15.2.20') do
10 assert_equal(Class, IO.class)
13 assert('IO', '15.2.20.2') do
14 assert_equal(Object, IO.superclass)
17 assert('IO', '15.2.20.3') do
18 assert_include(IO.ancestors, Enumerable)
21 assert('IO.open', '15.2.20.4.1') do
22 fd = IO.sysopen $mrbtest_io_rfname
23 assert_equal Fixnum, fd.class
25 assert_equal IO, io.class
26 assert_equal $mrbtest_io_msg, io.read
29 fd = IO.sysopen $mrbtest_io_rfname
31 assert_equal $mrbtest_io_msg, io.read
35 assert('IO#close', '15.2.20.5.1') do
36 io = IO.new(IO.sysopen($mrbtest_io_rfname))
40 assert('IO#closed?', '15.2.20.5.2') do
41 io = IO.new(IO.sysopen($mrbtest_io_rfname))
42 assert_false io.closed?
44 assert_true io.closed?
47 #assert('IO#each', '15.2.20.5.3') do
48 #assert('IO#each_byte', '15.2.20.5.4') do
49 #assert('IO#each_line', '15.2.20.5.5') do
51 assert('IO#eof?', '15.2.20.5.6') do
52 io = IO.new(IO.sysopen($mrbtest_io_wfname, 'w'), 'w')
53 assert_raise(IOError) do
59 io = IO.open(IO.sysopen($mrbtest_io_wfname, 'w'), 'w')
61 io = IO.open(IO.sysopen($mrbtest_io_wfname, 'r'), 'r')
66 io = IO.new(IO.sysopen($mrbtest_io_rfname))
75 assert('IO#flush', '15.2.20.5.7') do
76 # Note: mruby-io does not have any buffer to be flushed now.
77 io = IO.new(IO.sysopen($mrbtest_io_wfname))
78 assert_equal io, io.flush
80 assert_raise(IOError) do
85 assert('IO#getc', '15.2.20.5.8') do
86 io = IO.new(IO.sysopen($mrbtest_io_rfname))
87 $mrbtest_io_msg.each_char { |ch|
88 assert_equal ch, io.getc
90 assert_equal nil, io.getc
94 #assert('IO#gets', '15.2.20.5.9') do
95 #assert('IO#initialize_copy', '15.2.20.5.10') do
96 #assert('IO#print', '15.2.20.5.11') do
97 #assert('IO#putc', '15.2.20.5.12') do
98 #assert('IO#puts', '15.2.20.5.13') do
100 assert('IO#read', '15.2.20.5.14') do
101 IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
102 assert_raise(ArgumentError) { io.read(-5) }
103 assert_raise(TypeError) { io.read("str") }
105 len = $mrbtest_io_msg.length
106 assert_equal '', io.read(0)
107 assert_equal 'mruby', io.read(5)
108 assert_equal $mrbtest_io_msg[5,len], io.read(len)
110 assert_equal "", io.read
111 assert_nil io.read(1)
114 IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
115 assert_equal $mrbtest_io_msg, io.read
119 assert "IO#read(n) with n > IO::BUF_SIZE" do
120 skip "pipe is not supported on this platform" if MRubyIOTestUtil.win?
124 assert_equal r.read(n), 'a'*n
127 assert('IO#readchar', '15.2.20.5.15') do
128 # almost same as IO#getc
129 IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
130 $mrbtest_io_msg.each_char { |ch|
131 assert_equal ch, io.readchar
133 assert_raise(EOFError) do
139 #assert('IO#readline', '15.2.20.5.16') do
140 #assert('IO#readlines', '15.2.20.5.17') do
142 assert('IO#sync', '15.2.20.5.18') do
143 io = IO.new(IO.sysopen($mrbtest_io_rfname))
145 assert_true(s == true || s == false)
147 assert_raise(IOError) do
152 assert('IO#sync=', '15.2.20.5.19') do
153 io = IO.new(IO.sysopen($mrbtest_io_rfname))
159 assert_raise(IOError) do
164 assert('IO#write', '15.2.20.5.20') do
165 io = IO.open(IO.sysopen($mrbtest_io_wfname))
166 assert_equal 0, io.write("")
169 io = IO.open(IO.sysopen($mrbtest_io_wfname, "r+"), "r+")
170 assert_equal 7, io.write("abcdefg")
172 assert_equal "ab", io.read(2)
173 assert_equal 3, io.write("123")
175 assert_equal "ab123fg", io.read
180 io = IO.open(IO.sysopen($mrbtest_io_wfname))
182 assert_equal 0, io.pos
186 assert('IO#dup for readable') do
187 io = IO.new(IO.sysopen($mrbtest_io_rfname))
189 assert_true io != dup
190 assert_true io.fileno != dup.fileno
192 assert_true dup.close_on_exec?
193 rescue NotImplementedError
195 assert_equal 'm', dup.sysread(1)
196 assert_equal 'r', io.sysread(1)
197 assert_equal 'u', dup.sysread(1)
198 assert_equal 'b', io.sysread(1)
199 assert_equal 'y', dup.sysread(1)
201 assert_false io.closed?
205 assert('IO#dup for writable') do
206 io = IO.open(IO.sysopen($mrbtest_io_wfname, 'w+'), 'w+')
209 assert_equal 5, dup.sysseek(0, IO::SEEK_CUR)
210 io.sysseek 0, IO::SEEK_SET
211 assert_equal 0, dup.sysseek(0, IO::SEEK_CUR)
212 assert_equal "mruby", dup.sysread(5)
217 assert('IO.for_fd') do
218 fd = IO.sysopen($mrbtest_io_rfname)
220 assert_equal $mrbtest_io_msg, io.read
229 assert('IO gc check') do
230 100.times { IO.new(0) }
233 assert('IO.sysopen("./nonexistent")') do
234 if Object.const_defined? :Errno
235 eclass = Errno::ENOENT
237 eclass = RuntimeError
239 assert_raise eclass do
240 fd = IO.sysopen "./nonexistent"
245 assert('IO.sysopen, IO#sysread') do
246 fd = IO.sysopen $mrbtest_io_rfname
249 str2 = io.sysread(5, str1)
250 assert_equal $mrbtest_io_msg[0,5], str1
251 assert_equal $mrbtest_io_msg[0,5], str2
252 assert_raise EOFError do
257 assert_raise RuntimeError do
258 io.sysread(5, "abcde".freeze)
262 assert_equal "", io.sysread(0)
263 assert_raise(IOError) { io.sysread(1) }
264 assert_raise(ArgumentError) { io.sysread(-1) }
267 fd = IO.sysopen $mrbtest_io_wfname, "w"
269 assert_raise(IOError) { io.sysread(1) }
273 assert('IO.sysopen, IO#syswrite') do
274 fd = IO.sysopen $mrbtest_io_wfname, "w"
277 len = io.syswrite(str)
278 assert_equal str.size, len
281 io = IO.new(IO.sysopen($mrbtest_io_rfname), "r")
282 assert_raise(IOError) { io.syswrite("a") }
286 assert('IO#_read_buf') do
287 fd = IO.sysopen $mrbtest_io_rfname
292 msg_len = $mrbtest_io_msg.size
293 assert_equal '', io._buf
294 assert_equal $mrbtest_io_msg, io._read_buf
295 assert_equal $mrbtest_io_msg, io._buf
296 assert_equal 'mruby', io.read(5)
297 assert_equal 5, io.pos
298 assert_equal msg_len - 5, io._buf.size
299 assert_equal $mrbtest_io_msg[5,100], io.read
300 assert_equal 0, io._buf.size
301 assert_raise EOFError do
304 assert_equal true, io.eof
305 assert_equal true, io.eof?
309 assert('IO#isatty') do
310 skip "isatty is not supported on this platform" if MRubyIOTestUtil.win?
312 f = File.open("/dev/tty")
313 rescue RuntimeError => e
321 f = File.open($mrbtest_io_rfname)
322 assert_false f.isatty
328 assert('IO#pos=, IO#seek') do
329 fd = IO.sysopen $mrbtest_io_rfname
334 assert_equal 'm', io.getc
335 assert_equal 1, io.pos
336 assert_equal 0, io.seek(0)
337 assert_equal 0, io.pos
341 assert('IO#rewind') do
342 fd = IO.sysopen $mrbtest_io_rfname
344 assert_equal 'm', io.getc
345 assert_equal 1, io.pos
346 assert_equal 0, io.rewind
347 assert_equal 0, io.pos
352 fd = IO.sysopen $mrbtest_io_rfname
355 # gets without arguments
356 assert_equal $mrbtest_io_msg, io.gets, "gets without arguments"
357 assert_equal nil, io.gets, "gets returns nil, when EOF"
361 assert_equal $mrbtest_io_msg[0, 5], io.gets(5), "gets with limit"
365 assert_equal $mrbtest_io_msg[0, 6], io.gets(' '), "gets with rs"
367 # gets with rs, limit
369 assert_equal $mrbtest_io_msg[0, 5], io.gets(' ', 5), "gets with rs, limit"
371 assert_equal true, io.closed?, "close success"
373 # reading many-lines file.
374 fd = IO.sysopen $mrbtest_io_wfname, "w"
376 io.write "0123456789" * 2 + "\na"
377 assert_equal 22 + $cr, io.pos
379 assert_equal true, io.closed?
381 fd = IO.sysopen $mrbtest_io_wfname
386 assert_equal "0123456789" * 2 + "\n", line, "gets first line"
387 assert_equal 21, line.size
388 assert_equal 21 + $cr, io.pos
391 assert_equal "a", io.gets, "gets second line"
394 assert_equal nil, io.gets, "gets third line; returns nil"
399 assert('IO#gets - paragraph mode') do
400 fd = IO.sysopen $mrbtest_io_wfname, "w"
402 io.write "0" * 10 + "\n"
403 io.write "1" * 10 + "\n\n"
404 io.write "2" * 10 + "\n"
405 assert_equal 34 + $cr * 4, io.pos
408 fd = IO.sysopen $mrbtest_io_wfname
410 para1 = "#{'0' * 10}\n#{'1' * 10}\n\n"
412 assert_equal para1, text1
413 para2 = "#{'2' * 10}\n"
415 assert_equal para2, text2
419 assert('IO.popen') do
422 io = IO.popen("echo mruby-io")
423 assert_true io.close_on_exec?
424 assert_equal Fixnum, io.pid.class
427 assert_equal out.class, String
428 assert_include out, 'mruby-io'
431 if Object.const_defined? :Process
432 assert_true $?.success?
437 assert_true io.closed?
438 rescue NotImplementedError => e
443 assert('IO.popen with in option') do
448 assert_equal "hello", IO.popen("cat", "r", in: r) { |i| i.read }
449 assert_equal "", r.read
451 assert_raise(ArgumentError) { IO.popen("hello", "r", in: Object.new) }
452 rescue NotImplementedError => e
457 assert('IO.popen with out option') do
460 IO.popen("echo 'hello'", "w", out: w) {}
462 assert_equal "hello\n", r.read
464 rescue NotImplementedError => e
469 assert('IO.popen with err option') do
472 assert_equal "", IO.popen("echo 'hello' 1>&2", "r", err: w) { |i| i.read }
474 assert_equal "hello\n", r.read
476 rescue NotImplementedError => e
483 fd = IO.sysopen $mrbtest_io_wfname, "w"
486 assert_equal "", IO.read($mrbtest_io_wfname)
487 assert_equal nil, IO.read($mrbtest_io_wfname, 1)
490 fd = IO.sysopen $mrbtest_io_wfname, "w"
494 assert_equal "123", IO.read($mrbtest_io_wfname)
495 assert_equal "", IO.read($mrbtest_io_wfname, 0)
496 assert_equal "1", IO.read($mrbtest_io_wfname, 1)
497 assert_equal "", IO.read($mrbtest_io_wfname, 0, 10)
498 assert_equal "23", IO.read($mrbtest_io_wfname, 2, 1)
499 assert_equal "23", IO.read($mrbtest_io_wfname, 10, 1)
500 assert_equal "", IO.read($mrbtest_io_wfname, nil, 10)
501 assert_equal nil, IO.read($mrbtest_io_wfname, 1, 10)
504 assert('IO#fileno') do
505 fd = IO.sysopen $mrbtest_io_rfname
507 assert_equal io.fileno, fd
508 assert_equal io.to_i, fd
512 assert('IO#close_on_exec') do
513 fd = IO.sysopen $mrbtest_io_wfname, "w"
516 # IO.sysopen opens a file descripter with O_CLOEXEC flag.
517 assert_true io.close_on_exec?
520 skip "IO\#close_on_exec is not implemented."
523 io.close_on_exec = false
524 assert_equal(false, io.close_on_exec?)
525 io.close_on_exec = true
526 assert_equal(true, io.close_on_exec?)
527 io.close_on_exec = false
528 assert_equal(false, io.close_on_exec?)
534 assert_equal(true, r.close_on_exec?)
535 r.close_on_exec = false
536 assert_equal(false, r.close_on_exec?)
537 r.close_on_exec = true
538 assert_equal(true, r.close_on_exec?)
540 assert_equal(true, w.close_on_exec?)
541 w.close_on_exec = false
542 assert_equal(false, w.close_on_exec?)
543 w.close_on_exec = true
544 assert_equal(true, w.close_on_exec?)
546 r.close unless r.closed?
547 w.close unless w.closed?
551 assert('IO#sysseek') do
552 IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
553 assert_equal 2, io.sysseek(2)
554 assert_equal 5, io.sysseek(3, IO::SEEK_CUR) # 2 + 3 => 5
555 assert_equal $mrbtest_io_msg.size - 4, io.sysseek(-4, IO::SEEK_END)
563 assert_true r.kind_of?(IO)
564 assert_true w.kind_of?(IO)
565 assert_false r.closed?
566 assert_false w.closed?
567 assert_true FileTest.pipe?(r)
568 assert_true FileTest.pipe?(w)
571 assert_true 2 < r.fileno
572 assert_true 2 < w.fileno
573 assert_true r.fileno != w.fileno
576 assert_equal 8, w.write('test for')
577 assert_equal 'test', r.read(4)
578 assert_equal ' for', r.read(4)
579 assert_equal 5, w.write(' pipe')
580 assert_equal nil, w.close
581 assert_equal ' pipe', r.read
583 assert_raise(IOError) { r.write 'test' }
585 # This assert expect raise IOError but got RuntimeError
586 # Because mruby-io not have flag for I/O readable
587 # assert_raise(IOError) { w.read }
591 assert_nothing_raised do
592 IO.pipe { |r, w| r.close; w.close }
594 rescue NotImplementedError => e
601 assert_equal `echo foo`, "foo\n"
602 rescue NotImplementedError => e
607 assert('IO TEST CLEANUP') do
608 assert_nil MRubyIOTestUtil.io_test_cleanup