4 MRubyIOTestUtil.io_test_setup
5 $cr, $crlf, $cmd = MRubyIOTestUtil.win? ? [1, "\r\n", "cmd /c "] : [0, "\n", ""]
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)
13 assert_equal IO, io1.class
14 assert_equal $mrbtest_io_msg, io1.read
19 io2 = IO.__send__(meth, IO.sysopen($mrbtest_io_rfname))do |io|
21 assert_equal $mrbtest_io_msg, io.read
23 flunk "IO.#{meth} does not take block"
26 io2.close unless meth == :open
28 assert_raise(RuntimeError) { IO.__send__(meth, 1023) } # For Windows
29 assert_raise(RuntimeError) { IO.__send__(meth, 1 << 26) }
30 assert_raise(RuntimeError) { IO.__send__(meth, 1 << 32) } if (1 << 32).kind_of?(Integer)
34 assert('IO.class', '15.2.20') do
35 assert_equal(Class, IO.class)
38 assert('IO.superclass', '15.2.20.2') do
39 assert_equal(Object, IO.superclass)
42 assert('IO.ancestors', '15.2.20.3') do
43 assert_include(IO.ancestors, Enumerable)
46 assert('IO.open', '15.2.20.4.1') do
50 assert('IO#close', '15.2.20.5.1') do
51 io = IO.new(IO.sysopen($mrbtest_io_rfname))
55 assert('IO#closed?', '15.2.20.5.2') do
56 io = IO.new(IO.sysopen($mrbtest_io_rfname))
57 assert_false io.closed?
59 assert_true io.closed?
62 #assert('IO#each', '15.2.20.5.3') do
63 #assert('IO#each_byte', '15.2.20.5.4') do
64 #assert('IO#each_line', '15.2.20.5.5') do
66 assert('IO#eof?', '15.2.20.5.6') do
67 io = IO.new(IO.sysopen($mrbtest_io_wfname, 'w'), 'w')
68 assert_raise(IOError) do
74 io = IO.open(IO.sysopen($mrbtest_io_wfname, 'w'), 'w')
76 io = IO.open(IO.sysopen($mrbtest_io_wfname, 'r'), 'r')
81 io = IO.new(IO.sysopen($mrbtest_io_rfname))
90 assert('IO#flush', '15.2.20.5.7') do
91 # Note: mruby-io does not have any buffer to be flushed now.
92 io = IO.new(IO.sysopen($mrbtest_io_wfname))
93 assert_equal io, io.flush
95 assert_raise(IOError) do
100 assert('IO#getc', '15.2.20.5.8') do
101 io = IO.new(IO.sysopen($mrbtest_io_rfname))
102 $mrbtest_io_msg.split("").each { |ch|
103 assert_equal ch, io.getc
105 assert_equal nil, io.getc
109 #assert('IO#gets', '15.2.20.5.9') do
110 #assert('IO#initialize_copy', '15.2.20.5.10') do
111 #assert('IO#print', '15.2.20.5.11') do
112 #assert('IO#putc', '15.2.20.5.12') do
113 #assert('IO#puts', '15.2.20.5.13') do
115 assert('IO#read', '15.2.20.5.14') do
116 IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
117 assert_raise(ArgumentError) { io.read(-5) }
118 assert_raise(TypeError) { io.read("str") }
120 len = $mrbtest_io_msg.length
121 assert_equal '', io.read(0)
122 assert_equal 'mruby', io.read(5)
123 assert_equal $mrbtest_io_msg[5,len], io.read(len)
125 assert_equal "", io.read
126 assert_nil io.read(1)
129 IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
130 assert_equal $mrbtest_io_msg, io.read
134 assert "IO#read(n) with n > IO::BUF_SIZE" do
135 skip "pipe is not supported on this platform" if MRubyIOTestUtil.win?
139 assert_equal 'a'*n, r.read(n)
143 assert('IO#readchar', '15.2.20.5.15') do
144 # almost same as IO#getc
145 IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
146 $mrbtest_io_msg.split("").each { |ch|
147 assert_equal ch, io.readchar
149 assert_raise(EOFError) do
155 #assert('IO#readline', '15.2.20.5.16') do
156 #assert('IO#readlines', '15.2.20.5.17') do
158 assert('IO#sync', '15.2.20.5.18') do
159 io = IO.new(IO.sysopen($mrbtest_io_rfname))
161 assert_true(s == true || s == false)
163 assert_raise(IOError) do
168 assert('IO#sync=', '15.2.20.5.19') do
169 io = IO.new(IO.sysopen($mrbtest_io_rfname))
175 assert_raise(IOError) do
180 assert('IO#write', '15.2.20.5.20') do
181 io = IO.open(IO.sysopen($mrbtest_io_wfname))
182 assert_equal 0, io.write("")
185 io = IO.open(IO.sysopen($mrbtest_io_wfname, "r+"), "r+")
186 assert_equal 7, io.write("abcdefg")
188 assert_equal "ab", io.read(2)
189 assert_equal 3, io.write("123")
191 assert_equal "ab123fg", io.read
196 io = IO.open(IO.sysopen($mrbtest_io_wfname))
198 assert_equal 0, io.pos
202 assert('IO#dup for readable') do
203 io = IO.new(IO.sysopen($mrbtest_io_rfname))
205 assert_true io != dup
206 assert_true io.fileno != dup.fileno
208 assert_true dup.close_on_exec?
209 rescue NotImplementedError
211 assert_equal 'm', dup.sysread(1)
212 assert_equal 'r', io.sysread(1)
213 assert_equal 'u', dup.sysread(1)
214 assert_equal 'b', io.sysread(1)
215 assert_equal 'y', dup.sysread(1)
217 assert_false io.closed?
221 assert('IO#dup for writable') do
222 io = IO.open(IO.sysopen($mrbtest_io_wfname, 'w+'), 'w+')
225 assert_equal 5, dup.sysseek(0, IO::SEEK_CUR)
226 io.sysseek 0, IO::SEEK_SET
227 assert_equal 0, dup.sysseek(0, IO::SEEK_CUR)
228 assert_equal "mruby", dup.sysread(5)
233 assert('IO.for_fd') do
234 assert_io_open(:for_fd)
241 assert('IO gc check') do
242 assert_nothing_raised { 100.times { IO.new(0) } }
245 assert('IO.sysopen("./nonexistent")') do
246 if Object.const_defined? :Errno
247 eclass = Errno::ENOENT
249 eclass = RuntimeError
251 assert_raise eclass do
252 fd = IO.sysopen "./nonexistent"
257 assert('IO.sysopen, IO#sysread') do
258 fd = IO.sysopen $mrbtest_io_rfname
261 str2 = io.sysread(5, str1)
262 assert_equal $mrbtest_io_msg[0,5], str1
263 assert_equal $mrbtest_io_msg[0,5], str2
264 assert_raise EOFError do
269 assert_raise RuntimeError do
270 io.sysread(5, "abcde".freeze)
274 assert_equal "", io.sysread(0)
275 assert_raise(IOError) { io.sysread(1) }
276 assert_raise(ArgumentError) { io.sysread(-1) }
279 fd = IO.sysopen $mrbtest_io_wfname, "w"
281 assert_raise(IOError) { io.sysread(1) }
285 assert('IO.sysopen, IO#syswrite') do
286 fd = IO.sysopen $mrbtest_io_wfname, "w"
289 len = io.syswrite(str)
290 assert_equal str.size, len
293 io = IO.new(IO.sysopen($mrbtest_io_rfname), "r")
294 assert_raise(IOError) { io.syswrite("a") }
298 assert('IO#_read_buf') do
299 fd = IO.sysopen $mrbtest_io_rfname
304 msg_len = $mrbtest_io_msg.size
305 assert_equal '', io._buf
306 assert_equal $mrbtest_io_msg, io._read_buf
307 assert_equal $mrbtest_io_msg, io._buf
308 assert_equal 'mruby', io.read(5)
309 assert_equal 5, io.pos
310 assert_equal msg_len - 5, io._buf.size
311 assert_equal $mrbtest_io_msg[5,100], io.read
312 assert_equal 0, io._buf.size
313 assert_raise EOFError do
316 assert_equal true, io.eof
317 assert_equal true, io.eof?
321 assert('IO#isatty') do
322 skip "isatty is not supported on this platform" if MRubyIOTestUtil.win?
324 f = File.open("/dev/tty")
325 rescue RuntimeError => e
333 f = File.open($mrbtest_io_rfname)
334 assert_false f.isatty
340 assert('IO#pos=, IO#seek') do
341 fd = IO.sysopen $mrbtest_io_rfname
346 assert_equal 'm', io.getc
347 assert_equal 1, io.pos
348 assert_equal 0, io.seek(0)
349 assert_equal 0, io.pos
353 assert('IO#rewind') do
354 fd = IO.sysopen $mrbtest_io_rfname
356 assert_equal 'm', io.getc
357 assert_equal 1, io.pos
358 assert_equal 0, io.rewind
359 assert_equal 0, io.pos
364 fd = IO.sysopen $mrbtest_io_rfname
367 # gets without arguments
368 assert_equal $mrbtest_io_msg, io.gets, "gets without arguments"
369 assert_equal nil, io.gets, "gets returns nil, when EOF"
373 assert_equal $mrbtest_io_msg[0, 5], io.gets(5), "gets with limit"
377 assert_equal $mrbtest_io_msg[0, 6], io.gets(' '), "gets with rs"
379 # gets with rs, limit
381 assert_equal $mrbtest_io_msg[0, 5], io.gets(' ', 5), "gets with rs, limit"
383 assert_equal true, io.closed?, "close success"
385 # reading many-lines file.
386 fd = IO.sysopen $mrbtest_io_wfname, "w"
388 io.write "0123456789" * 2 + "\na"
389 assert_equal 22 + $cr, io.pos
391 assert_equal true, io.closed?
393 fd = IO.sysopen $mrbtest_io_wfname
398 assert_equal "0123456789" * 2 + "\n", line, "gets first line"
399 assert_equal 21, line.size
400 assert_equal 21 + $cr, io.pos
403 assert_equal "a", io.gets, "gets second line"
406 assert_equal nil, io.gets, "gets third line; returns nil"
411 assert('IO#gets - paragraph mode') do
412 fd = IO.sysopen $mrbtest_io_wfname, "w"
414 io.write "0" * 10 + "\n"
415 io.write "1" * 10 + "\n\n"
416 io.write "2" * 10 + "\n"
417 assert_equal 34 + $cr * 4, io.pos
420 fd = IO.sysopen $mrbtest_io_wfname
422 para1 = "#{'0' * 10}\n#{'1' * 10}\n\n"
424 assert_equal para1, text1
425 para2 = "#{'2' * 10}\n"
427 assert_equal para2, text2
431 assert('IO.popen') do
434 io = IO.popen("#{$cmd}echo mruby-io")
435 assert_true io.close_on_exec?
436 assert_equal Fixnum, io.pid.class
439 assert_equal out.class, String
440 assert_include out, 'mruby-io'
443 if Object.const_defined? :Process
444 assert_true $?.success?
449 assert_true io.closed?
450 rescue NotImplementedError => e
455 assert('IO.popen with in option') do
460 assert_equal "hello", IO.popen("cat", "r", in: r) { |i| i.read }
461 assert_equal "", r.read
463 assert_raise(ArgumentError) { IO.popen("hello", "r", in: Object.new) }
464 rescue NotImplementedError => e
469 assert('IO.popen with out option') do
472 IO.popen("echo 'hello'", "w", out: w) {}
474 assert_equal "hello\n", r.read
476 rescue NotImplementedError => e
481 assert('IO.popen with err option') do
484 assert_equal "", IO.popen("echo 'hello' 1>&2", "r", err: w) { |i| i.read }
486 assert_equal "hello\n", r.read
488 rescue NotImplementedError => e
495 fd = IO.sysopen $mrbtest_io_wfname, "w"
498 assert_equal "", IO.read($mrbtest_io_wfname)
499 assert_equal nil, IO.read($mrbtest_io_wfname, 1)
502 fd = IO.sysopen $mrbtest_io_wfname, "w"
506 assert_equal "123", IO.read($mrbtest_io_wfname)
507 assert_equal "", IO.read($mrbtest_io_wfname, 0)
508 assert_equal "1", IO.read($mrbtest_io_wfname, 1)
509 assert_equal "", IO.read($mrbtest_io_wfname, 0, 10)
510 assert_equal "23", IO.read($mrbtest_io_wfname, 2, 1)
511 assert_equal "23", IO.read($mrbtest_io_wfname, 10, 1)
512 assert_equal "", IO.read($mrbtest_io_wfname, nil, 10)
513 assert_equal nil, IO.read($mrbtest_io_wfname, 1, 10)
516 assert('IO#fileno') do
517 fd = IO.sysopen $mrbtest_io_rfname
519 assert_equal io.fileno, fd
520 assert_equal io.to_i, fd
524 assert('IO#close_on_exec') do
525 fd = IO.sysopen $mrbtest_io_wfname, "w"
528 # IO.sysopen opens a file descripter with O_CLOEXEC flag.
529 assert_true io.close_on_exec?
532 skip "IO\#close_on_exec is not implemented."
535 io.close_on_exec = false
536 assert_equal(false, io.close_on_exec?)
537 io.close_on_exec = true
538 assert_equal(true, io.close_on_exec?)
539 io.close_on_exec = false
540 assert_equal(false, io.close_on_exec?)
546 assert_equal(true, r.close_on_exec?)
547 r.close_on_exec = false
548 assert_equal(false, r.close_on_exec?)
549 r.close_on_exec = true
550 assert_equal(true, r.close_on_exec?)
552 assert_equal(true, w.close_on_exec?)
553 w.close_on_exec = false
554 assert_equal(false, w.close_on_exec?)
555 w.close_on_exec = true
556 assert_equal(true, w.close_on_exec?)
558 r.close unless r.closed?
559 w.close unless w.closed?
563 assert('IO#sysseek') do
564 IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
565 assert_equal 2, io.sysseek(2)
566 assert_equal 5, io.sysseek(3, IO::SEEK_CUR) # 2 + 3 => 5
567 assert_equal $mrbtest_io_msg.size - 4, io.sysseek(-4, IO::SEEK_END)
571 assert('IO#pread') do
572 skip "IO#pread is not implemented on this configuration" unless MRubyIOTestUtil::MRB_WITH_IO_PREAD_PWRITE
574 IO.open(IO.sysopen($mrbtest_io_rfname, 'r'), 'r') do |io|
575 assert_equal $mrbtest_io_msg.byteslice(5, 8), io.pread(8, 5)
576 assert_equal 0, io.pos
577 assert_equal $mrbtest_io_msg.byteslice(1, 5), io.pread(5, 1)
578 assert_equal 0, io.pos
579 assert_raise(RuntimeError) { io.pread(20, -9) }
583 assert('IO#pwrite') do
584 skip "IO#pwrite is not implemented on this configuration" unless MRubyIOTestUtil::MRB_WITH_IO_PREAD_PWRITE
586 IO.open(IO.sysopen($mrbtest_io_wfname, 'w+'), 'w+') do |io|
587 assert_equal 6, io.pwrite("Warld!", 7)
588 assert_equal 0, io.pos
589 assert_equal 7, io.pwrite("Hello, ", 0)
590 assert_equal 0, io.pos
591 assert_equal "Hello, Warld!", io.read
592 assert_equal 6, io.pwrite("world!", 7)
593 assert_equal 13, io.pos
595 assert_equal "Hello, world!", io.read
603 assert_true r.kind_of?(IO)
604 assert_true w.kind_of?(IO)
605 assert_false r.closed?
606 assert_false w.closed?
607 assert_true FileTest.pipe?(r)
608 assert_true FileTest.pipe?(w)
611 assert_true 2 < r.fileno
612 assert_true 2 < w.fileno
613 assert_true r.fileno != w.fileno
616 assert_equal 8, w.write('test for')
617 assert_equal 'test', r.read(4)
618 assert_equal ' for', r.read(4)
619 assert_equal 5, w.write(' pipe')
620 assert_equal nil, w.close
621 assert_equal ' pipe', r.read
623 assert_raise(IOError) { r.write 'test' }
625 # This assert expect raise IOError but got RuntimeError
626 # Because mruby-io not have flag for I/O readable
627 # assert_raise(IOError) { w.read }
631 assert_nothing_raised do
632 IO.pipe { |r, w| r.close; w.close }
634 rescue NotImplementedError => e
641 assert_equal `#{$cmd}echo foo`, "foo#{$crlf}"
642 rescue NotImplementedError => e
647 MRubyIOTestUtil.io_test_cleanup