summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-io/test
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2017-12-07 18:11:06 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2017-12-07 18:11:06 +0900
commitd75266dd1bade53255044460a9cd74596addaa84 (patch)
treeac97feb393da5597855dd8f79a7b8feba17c5c14 /mrbgems/mruby-io/test
parent10ed730e4bd921cf4d8fe6f6d2e3cb3f0840f3b7 (diff)
parent3c8e1f94c44252c836f79a48bb17726da28e2756 (diff)
downloadmruby-d75266dd1bade53255044460a9cd74596addaa84.tar.gz
mruby-d75266dd1bade53255044460a9cd74596addaa84.zip
Add 'mrbgems/mruby-io/' from commit '3c8e1f94c44252c836f79a48bb17726da28e2756'
git-subtree-dir: mrbgems/mruby-io git-subtree-mainline: 10ed730e4bd921cf4d8fe6f6d2e3cb3f0840f3b7 git-subtree-split: 3c8e1f94c44252c836f79a48bb17726da28e2756
Diffstat (limited to 'mrbgems/mruby-io/test')
-rw-r--r--mrbgems/mruby-io/test/file.rb188
-rw-r--r--mrbgems/mruby-io/test/file_test.rb117
-rw-r--r--mrbgems/mruby-io/test/gc_filedes.sh4
-rw-r--r--mrbgems/mruby-io/test/io.rb609
-rw-r--r--mrbgems/mruby-io/test/mruby_io_test.c191
5 files changed, 1109 insertions, 0 deletions
diff --git a/mrbgems/mruby-io/test/file.rb b/mrbgems/mruby-io/test/file.rb
new file mode 100644
index 000000000..941e91ac4
--- /dev/null
+++ b/mrbgems/mruby-io/test/file.rb
@@ -0,0 +1,188 @@
+##
+# IO Test
+
+assert('File', '15.2.21') do
+ File.class == Class
+end
+
+assert('File', '15.2.21.2') do
+ File.superclass == IO
+end
+
+assert('File TEST SETUP') do
+ MRubyIOTestUtil.io_test_setup
+end
+
+assert('File#initialize', '15.2.21.4.1') do
+ io = File.open($mrbtest_io_rfname, "r")
+ assert_nil io.close
+ assert_raise IOError do
+ io.close
+ end
+end
+
+assert('File#path', '15.2.21.4.2') do
+ io = File.open($mrbtest_io_rfname, "r")
+ assert_equal $mrbtest_io_msg, io.read
+ assert_equal $mrbtest_io_rfname, io.path
+ io.close
+ assert_equal $mrbtest_io_rfname, io.path
+ io.closed?
+end
+
+assert('File.basename') do
+ assert_equal '/', File.basename('//')
+ assert_equal 'a', File.basename('/a/')
+ assert_equal 'b', File.basename('/a/b')
+ assert_equal 'b', File.basename('../a/b')
+end
+
+assert('File.dirname') do
+ assert_equal '.', File.dirname('')
+ assert_equal '.', File.dirname('a')
+ assert_equal '/', File.dirname('/a')
+ assert_equal 'a', File.dirname('a/b')
+ assert_equal '/a', File.dirname('/a/b')
+end
+
+assert('File.extname') do
+ assert_equal '.txt', File.extname('foo/foo.txt')
+ assert_equal '.gz', File.extname('foo/foo.tar.gz')
+ assert_equal '', File.extname('foo/bar')
+ assert_equal '', File.extname('foo/.bar')
+ assert_equal '', File.extname('foo.txt/bar')
+ assert_equal '', File.extname('.foo')
+end
+
+assert('IO#flock') do
+ f = File.open $mrbtest_io_rfname
+ begin
+ assert_equal(f.flock(File::LOCK_SH), 0)
+ assert_equal(f.flock(File::LOCK_UN), 0)
+ assert_equal(f.flock(File::LOCK_EX | File::LOCK_NB), 0)
+ assert_equal(f.flock(File::LOCK_UN), 0)
+ rescue NotImplementedError => e
+ skip e.message
+ ensure
+ f.close
+ end
+end
+
+assert('File.join') do
+ assert_equal "", File.join()
+ assert_equal "a", File.join("a")
+ assert_equal "/a", File.join("/a")
+ assert_equal "a/", File.join("a/")
+ assert_equal "a/b/c", File.join("a", "b", "c")
+ assert_equal "/a/b/c", File.join("/a", "b", "c")
+ assert_equal "a/b/c/", File.join("a", "b", "c/")
+ assert_equal "a/b/c", File.join("a/", "/b/", "/c")
+ assert_equal "a/b/c", File.join(["a", "b", "c"])
+ assert_equal "a/b/c", File.join("a", ["b", ["c"]])
+end
+
+assert('File.realpath') do
+ if File::ALT_SEPARATOR
+ readme_path = File._getwd + File::ALT_SEPARATOR + "README.md"
+ assert_equal readme_path, File.realpath("README.md")
+ else
+ dir = MRubyIOTestUtil.mkdtemp("mruby-io-test.XXXXXX")
+ begin
+ dir1 = File.realpath($mrbtest_io_rfname)
+ dir2 = File.realpath("./#{dir}//./../#{$mrbtest_io_symlinkname}")
+ assert_equal dir1, dir2
+ ensure
+ MRubyIOTestUtil.rmdir dir
+ end
+ end
+end
+
+assert("File.readlink") do
+ begin
+ assert_equal $mrbtest_io_rfname, File.readlink($mrbtest_io_symlinkname)
+ rescue NotImplementedError => e
+ skip e.message
+ end
+end
+
+assert("File.readlink fails with non-symlink") do
+ begin
+ assert_raise(RuntimeError) {
+ begin
+ File.readlink($mrbtest_io_rfname)
+ rescue => e
+ if Object.const_defined?(:SystemCallError) and e.kind_of?(SystemCallError)
+ raise RuntimeError, "SystemCallError converted to RuntimeError"
+ end
+ raise e
+ end
+ }
+ rescue NotImplementedError => e
+ skip e.message
+ end
+end
+
+assert('File TEST CLEANUP') do
+ assert_nil MRubyIOTestUtil.io_test_cleanup
+end
+
+assert('File.expand_path') do
+ assert_equal "/", File.expand_path("..", "/tmp"), "parent path with base_dir (1)"
+ assert_equal "/tmp", File.expand_path("..", "/tmp/mruby"), "parent path with base_dir (2)"
+
+ assert_equal "/home", File.expand_path("/home"), "absolute"
+ assert_equal "/home", File.expand_path("/home", "."), "absolute with base_dir"
+
+ assert_equal "/hoge", File.expand_path("/tmp/..//hoge")
+ assert_equal "/hoge", File.expand_path("////tmp/..///////hoge")
+
+ assert_equal "/", File.expand_path("../../../..", "/")
+ if File._getwd[1] == ":"
+ drive_letter = File._getwd[0]
+ assert_equal drive_letter + ":\\", File.expand_path(([".."] * 100).join("/"))
+ else
+ assert_equal "/", File.expand_path(([".."] * 100).join("/"))
+ end
+end
+
+assert('File.expand_path (with ENV)') do
+ skip unless Object.const_defined?(:ENV) && ENV['HOME']
+
+ assert_equal ENV['HOME'], File.expand_path("~/"), "home"
+ assert_equal ENV['HOME'], File.expand_path("~/", "/"), "home with base_dir"
+
+ assert_equal "#{ENV['HOME']}/user", File.expand_path("user", ENV['HOME']), "relative with base_dir"
+end
+
+assert('File.path') do
+ assert_equal "", File.path("")
+ assert_equal "a/b/c", File.path("a/b/c")
+ assert_equal "a/../b/./c", File.path("a/../b/./c")
+ assert_raise(TypeError) { File.path(nil) }
+ assert_raise(TypeError) { File.path(123) }
+
+end
+
+assert('File.symlink') do
+ target_name = "/usr/bin"
+ symlink_name = "test-bin-dummy"
+ if !File.exist?(target_name)
+ skip("target directory of File.symlink is not found")
+ else
+ assert_equal 0, File.symlink(target_name, symlink_name)
+ begin
+ assert_equal true, File.symlink?(symlink_name)
+ ensure
+ File.delete symlink_name
+ end
+ end
+end
+
+assert('File.chmod') do
+ File.open('chmod-test', 'w') {}
+ begin
+ assert_equal 1, File.chmod(0400, 'chmod-test')
+ ensure
+ File.delete('chmod-test')
+ end
+end
diff --git a/mrbgems/mruby-io/test/file_test.rb b/mrbgems/mruby-io/test/file_test.rb
new file mode 100644
index 000000000..2c831f0d5
--- /dev/null
+++ b/mrbgems/mruby-io/test/file_test.rb
@@ -0,0 +1,117 @@
+##
+# FileTest
+
+assert('FileTest TEST SETUP') do
+ MRubyIOTestUtil.io_test_setup
+end
+
+assert("FileTest.directory?") do
+ dir = MRubyIOTestUtil.mkdtemp("mruby-io-test.XXXXXX")
+ begin
+ assert_true FileTest.directory?(dir)
+ assert_false FileTest.directory?($mrbtest_io_rfname)
+ ensure
+ MRubyIOTestUtil.rmdir dir
+ end
+end
+
+assert("FileTest.exist?") do
+ assert_equal true, FileTest.exist?($mrbtest_io_rfname), "filename - exist"
+ assert_equal false, FileTest.exist?($mrbtest_io_rfname + "-"), "filename - not exist"
+ io = IO.new(IO.sysopen($mrbtest_io_rfname))
+ assert_equal true, FileTest.exist?(io), "io obj - exist"
+ io.close
+ assert_equal true, io.closed?
+ assert_raise IOError do
+ FileTest.exist?(io)
+ end
+end
+
+assert("FileTest.file?") do
+ dir = MRubyIOTestUtil.mkdtemp("mruby-io-test.XXXXXX")
+ begin
+ assert_true FileTest.file?($mrbtest_io_rfname)
+ assert_false FileTest.file?(dir)
+ ensure
+ MRubyIOTestUtil.rmdir dir
+ end
+end
+
+assert("FileTest.pipe?") do
+ begin
+ assert_equal false, FileTest.pipe?("/tmp")
+ io = IO.popen("ls")
+ assert_equal true, FileTest.pipe?(io)
+ rescue NotImplementedError => e
+ skip e.message
+ end
+end
+
+assert('FileTest.size') do
+ assert_equal FileTest.size($mrbtest_io_rfname), $mrbtest_io_msg.size
+ assert_equal FileTest.size($mrbtest_io_wfname), 0
+end
+
+assert("FileTest.size?") do
+ assert_equal $mrbtest_io_msg.size, FileTest.size?($mrbtest_io_rfname)
+ assert_equal nil, FileTest.size?($mrbtest_io_wfname)
+ assert_equal nil, FileTest.size?("not-exist-test-target-file")
+
+ fp1 = File.open($mrbtest_io_rfname)
+ fp2 = File.open($mrbtest_io_wfname)
+ assert_equal $mrbtest_io_msg.size, FileTest.size?(fp1)
+ assert_equal nil, FileTest.size?(fp2)
+ fp1.close
+ fp2.close
+
+ assert_raise IOError do
+ FileTest.size?(fp1)
+ end
+ assert_raise IOError do
+ FileTest.size?(fp2)
+ end
+
+ fp1.closed? && fp2.closed?
+end
+
+assert("FileTest.socket?") do
+ begin
+ assert_true FileTest.socket?($mrbtest_io_socketname)
+ rescue NotImplementedError => e
+ skip e.message
+ end
+end
+
+assert("FileTest.symlink?") do
+ begin
+ assert_true FileTest.symlink?($mrbtest_io_symlinkname)
+ rescue NotImplementedError => e
+ skip e.message
+ end
+end
+
+assert("FileTest.zero?") do
+ assert_equal false, FileTest.zero?($mrbtest_io_rfname)
+ assert_equal true, FileTest.zero?($mrbtest_io_wfname)
+ assert_equal false, FileTest.zero?("not-exist-test-target-file")
+
+ fp1 = File.open($mrbtest_io_rfname)
+ fp2 = File.open($mrbtest_io_wfname)
+ assert_equal false, FileTest.zero?(fp1)
+ assert_equal true, FileTest.zero?(fp2)
+ fp1.close
+ fp2.close
+
+ assert_raise IOError do
+ FileTest.zero?(fp1)
+ end
+ assert_raise IOError do
+ FileTest.zero?(fp2)
+ end
+
+ fp1.closed? && fp2.closed?
+end
+
+assert('FileTest TEST CLEANUP') do
+ assert_nil MRubyIOTestUtil.io_test_cleanup
+end
diff --git a/mrbgems/mruby-io/test/gc_filedes.sh b/mrbgems/mruby-io/test/gc_filedes.sh
new file mode 100644
index 000000000..6e5d1bbf1
--- /dev/null
+++ b/mrbgems/mruby-io/test/gc_filedes.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+ulimit -n 20
+mruby -e '100.times { File.open "'$0'" }'
diff --git a/mrbgems/mruby-io/test/io.rb b/mrbgems/mruby-io/test/io.rb
new file mode 100644
index 000000000..df646977a
--- /dev/null
+++ b/mrbgems/mruby-io/test/io.rb
@@ -0,0 +1,609 @@
+##
+# IO Test
+
+unless Object.respond_to? :assert_nothing_raised
+ def assert_nothing_raised(*exp)
+ ret = true
+ if $mrbtest_assert
+ $mrbtest_assert_idx += 1
+ msg = exp.last.class == String ? exp.pop : ""
+ begin
+ yield
+ rescue Exception => e
+ msg = "#{msg} exception raised."
+ diff = " Class: <#{e.class}>\n" +
+ " Message: #{e.message}"
+ $mrbtest_assert.push([$mrbtest_assert_idx, msg, diff])
+ ret = false
+ end
+ end
+ ret
+ end
+end
+
+assert('IO TEST SETUP') do
+ MRubyIOTestUtil.io_test_setup
+end
+
+assert('IO', '15.2.20') do
+ assert_equal(Class, IO.class)
+end
+
+assert('IO', '15.2.20.2') do
+ assert_equal(Object, IO.superclass)
+end
+
+assert('IO', '15.2.20.3') do
+ assert_include(IO.included_modules, Enumerable)
+end
+
+assert('IO.open', '15.2.20.4.1') do
+ fd = IO.sysopen $mrbtest_io_rfname
+ assert_equal Fixnum, fd.class
+ io = IO.open fd
+ assert_equal IO, io.class
+ assert_equal $mrbtest_io_msg, io.read
+ io.close
+
+ fd = IO.sysopen $mrbtest_io_rfname
+ IO.open(fd) do |io|
+ assert_equal $mrbtest_io_msg, io.read
+ end
+
+ true
+end
+
+assert('IO#close', '15.2.20.5.1') do
+ io = IO.new(IO.sysopen($mrbtest_io_rfname))
+ assert_nil io.close
+end
+
+assert('IO#closed?', '15.2.20.5.2') do
+ io = IO.new(IO.sysopen($mrbtest_io_rfname))
+ assert_false io.closed?
+ io.close
+ assert_true io.closed?
+end
+
+#assert('IO#each', '15.2.20.5.3') do
+#assert('IO#each_byte', '15.2.20.5.4') do
+#assert('IO#each_line', '15.2.20.5.5') do
+
+assert('IO#eof?', '15.2.20.5.6') do
+ io = IO.new(IO.sysopen($mrbtest_io_wfname, 'w'), 'w')
+ assert_raise(IOError) do
+ io.eof?
+ end
+ io.close
+
+ # empty file
+ io = IO.open(IO.sysopen($mrbtest_io_wfname, 'w'), 'w')
+ io.close
+ io = IO.open(IO.sysopen($mrbtest_io_wfname, 'r'), 'r')
+ assert_true io.eof?
+ io.close
+
+ # nonempty file
+ io = IO.new(IO.sysopen($mrbtest_io_rfname))
+ assert_false io.eof?
+ io.readchar
+ assert_false io.eof?
+ io.read
+ assert_true io.eof?
+ io.close
+
+ true
+end
+
+assert('IO#flush', '15.2.20.5.7') do
+ # Note: mruby-io does not have any buffer to be flushed now.
+ io = IO.new(IO.sysopen($mrbtest_io_wfname))
+ assert_equal io, io.flush
+ io.close
+ assert_raise(IOError) do
+ io.flush
+ end
+end
+
+assert('IO#getc', '15.2.20.5.8') do
+ io = IO.new(IO.sysopen($mrbtest_io_rfname))
+ $mrbtest_io_msg.each_char { |ch|
+ assert_equal ch, io.getc
+ }
+ assert_equal nil, io.getc
+ io.close
+ true
+end
+
+#assert('IO#gets', '15.2.20.5.9') do
+#assert('IO#initialize_copy', '15.2.20.5.10') do
+#assert('IO#print', '15.2.20.5.11') do
+#assert('IO#putc', '15.2.20.5.12') do
+#assert('IO#puts', '15.2.20.5.13') do
+
+assert('IO#read', '15.2.20.5.14') do
+ IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
+ assert_raise(ArgumentError) { io.read(-5) }
+ assert_raise(TypeError) { io.read("str") }
+
+ len = $mrbtest_io_msg.length
+ assert_equal '', io.read(0)
+ assert_equal 'mruby', io.read(5)
+ assert_equal $mrbtest_io_msg[5,len], io.read(len)
+
+ assert_equal "", io.read
+ assert_nil io.read(1)
+ end
+
+ IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
+ assert_equal $mrbtest_io_msg, io.read
+ end
+end
+
+assert "IO#read(n) with n > IO::BUF_SIZE" do
+ r,w = IO.pipe
+ n = IO::BUF_SIZE+1
+ w.write 'a'*n
+ assert_equal r.read(n), 'a'*n
+end
+
+assert('IO#readchar', '15.2.20.5.15') do
+ # almost same as IO#getc
+ IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
+ $mrbtest_io_msg.each_char { |ch|
+ assert_equal ch, io.readchar
+ }
+ assert_raise(EOFError) do
+ io.readchar
+ end
+ end
+end
+
+#assert('IO#readline', '15.2.20.5.16') do
+#assert('IO#readlines', '15.2.20.5.17') do
+
+assert('IO#sync', '15.2.20.5.18') do
+ io = IO.new(IO.sysopen($mrbtest_io_rfname))
+ s = io.sync
+ assert_true(s == true || s == false)
+ io.close
+ assert_raise(IOError) do
+ io.sync
+ end
+end
+
+assert('IO#sync=', '15.2.20.5.19') do
+ io = IO.new(IO.sysopen($mrbtest_io_rfname))
+ io.sync = true
+ assert_true io.sync
+ io.sync = false
+ assert_false io.sync
+ io.close
+ assert_raise(IOError) do
+ io.sync = true
+ end
+end
+
+assert('IO#write', '15.2.20.5.20') do
+ io = IO.open(IO.sysopen($mrbtest_io_wfname))
+ assert_equal 0, io.write("")
+ io.close
+
+ io = IO.open(IO.sysopen($mrbtest_io_wfname, "r+"), "r+")
+ assert_equal 7, io.write("abcdefg")
+ io.rewind
+ assert_equal "ab", io.read(2)
+ assert_equal 3, io.write("123")
+ io.rewind
+ assert_equal "ab123fg", io.read
+ io.close
+
+ true
+end
+
+assert('IO#<<') do
+ io = IO.open(IO.sysopen($mrbtest_io_wfname))
+ io << "" << ""
+ assert_equal 0, io.pos
+ io.close
+ true
+end
+
+assert('IO.for_fd') do
+ fd = IO.sysopen($mrbtest_io_rfname)
+ io = IO.for_fd(fd)
+ assert_equal $mrbtest_io_msg, io.read
+ io.close
+ true
+end
+
+assert('IO.new') do
+ io = IO.new(0)
+ io.close
+ true
+end
+
+assert('IO gc check') do
+ 100.times { IO.new(0) }
+end
+
+assert('IO.sysopen("./nonexistent")') do
+ if Object.const_defined? :Errno
+ eclass = Errno::ENOENT
+ else
+ eclass = RuntimeError
+ end
+ assert_raise eclass do
+ fd = IO.sysopen "./nonexistent"
+ IO._sysclose fd
+ end
+end
+
+assert('IO.sysopen, IO#sysread') do
+ fd = IO.sysopen $mrbtest_io_rfname
+ io = IO.new fd
+ str1 = " "
+ str2 = io.sysread(5, str1)
+ assert_equal $mrbtest_io_msg[0,5], str1
+ assert_equal $mrbtest_io_msg[0,5], str2
+ assert_raise EOFError do
+ io.sysread(10000)
+ io.sysread(10000)
+ end
+
+ assert_raise RuntimeError do
+ io.sysread(5, "abcde".freeze)
+ end
+
+ io.close
+ assert_equal "", io.sysread(0)
+ assert_raise(IOError) { io.sysread(1) }
+ assert_raise(ArgumentError) { io.sysread(-1) }
+ io.closed?
+
+ fd = IO.sysopen $mrbtest_io_wfname, "w"
+ io = IO.new fd, "w"
+ assert_raise(IOError) { io.sysread(1) }
+ io.close
+ true
+end
+
+assert('IO.sysopen, IO#syswrite') do
+ fd = IO.sysopen $mrbtest_io_wfname, "w"
+ io = IO.new fd, "w"
+ str = "abcdefg"
+ len = io.syswrite(str)
+ assert_equal str.size, len
+ io.close
+
+ io = IO.new(IO.sysopen($mrbtest_io_rfname), "r")
+ assert_raise(IOError) { io.syswrite("a") }
+ io.close
+
+ true
+end
+
+assert('IO#_read_buf') do
+ fd = IO.sysopen $mrbtest_io_rfname
+ io = IO.new fd
+ def io._buf
+ @buf
+ end
+ msg_len = $mrbtest_io_msg.size
+ assert_equal '', io._buf
+ assert_equal $mrbtest_io_msg, io._read_buf
+ assert_equal $mrbtest_io_msg, io._buf
+ assert_equal 'mruby', io.read(5)
+ assert_equal 5, io.pos
+ assert_equal msg_len - 5, io._buf.size
+ assert_equal $mrbtest_io_msg[5,100], io.read
+ assert_equal 0, io._buf.size
+ assert_raise EOFError do
+ io._read_buf
+ end
+ assert_equal true, io.eof
+ assert_equal true, io.eof?
+ io.close
+ io.closed?
+end
+
+assert('IO#isatty') do
+ f1 = File.open("/dev/tty")
+ f2 = File.open($mrbtest_io_rfname)
+
+ assert_true f1.isatty
+ assert_false f2.isatty
+
+ f1.close
+ f2.close
+ true
+end
+
+assert('IO#pos=, IO#seek') do
+ fd = IO.sysopen $mrbtest_io_rfname
+ io = IO.new fd
+ def io._buf
+ @buf
+ end
+ assert_equal 'm', io.getc
+ assert_equal 1, io.pos
+ assert_equal 0, io.seek(0)
+ assert_equal 0, io.pos
+ io.close
+ io.closed?
+end
+
+assert('IO#rewind') do
+ fd = IO.sysopen $mrbtest_io_rfname
+ io = IO.new fd
+ assert_equal 'm', io.getc
+ assert_equal 1, io.pos
+ assert_equal 0, io.rewind
+ assert_equal 0, io.pos
+ io.close
+ io.closed?
+end
+
+assert('IO#gets') do
+ fd = IO.sysopen $mrbtest_io_rfname
+ io = IO.new fd
+
+ # gets without arguments
+ assert_equal $mrbtest_io_msg, io.gets, "gets without arguments"
+ assert_equal nil, io.gets, "gets returns nil, when EOF"
+
+ # gets with limit
+ io.pos = 0
+ assert_equal $mrbtest_io_msg[0, 5], io.gets(5), "gets with limit"
+
+ # gets with rs
+ io.pos = 0
+ assert_equal $mrbtest_io_msg[0, 6], io.gets(' '), "gets with rs"
+
+ # gets with rs, limit
+ io.pos = 0
+ assert_equal $mrbtest_io_msg[0, 5], io.gets(' ', 5), "gets with rs, limit"
+ io.close
+ assert_equal true, io.closed?, "close success"
+
+ # reading many-lines file.
+ fd = IO.sysopen $mrbtest_io_wfname, "w"
+ io = IO.new fd, "w"
+ io.write "0123456789" * 2 + "\na"
+ assert_equal 22, io.pos
+ io.close
+ assert_equal true, io.closed?
+
+ fd = IO.sysopen $mrbtest_io_wfname
+ io = IO.new fd
+ line = io.gets
+
+ # gets first line
+ assert_equal "0123456789" * 2 + "\n", line, "gets first line"
+ assert_equal 21, line.size
+ assert_equal 21, io.pos
+
+ # gets second line
+ assert_equal "a", io.gets, "gets second line"
+
+ # gets third line
+ assert_equal nil, io.gets, "gets third line; returns nil"
+
+ io.close
+ io.closed?
+end
+
+assert('IO#gets - paragraph mode') do
+ fd = IO.sysopen $mrbtest_io_wfname, "w"
+ io = IO.new fd, "w"
+ io.write "0" * 10 + "\n"
+ io.write "1" * 10 + "\n\n"
+ io.write "2" * 10 + "\n"
+ assert_equal 34, io.pos
+ io.close
+ assert_equal true, io.closed?
+
+ fd = IO.sysopen $mrbtest_io_wfname
+ io = IO.new fd
+ para1 = "#{'0' * 10}\n#{'1' * 10}\n\n"
+ text1 = io.gets("")
+ assert_equal para1, text1
+ para2 = "#{'2' * 10}\n"
+ text2 = io.gets("")
+ assert_equal para2, text2
+ io.close
+ io.closed?
+end
+
+assert('IO.popen') do
+ begin
+ $? = nil
+ io = IO.popen("echo mruby-io")
+ assert_true io.close_on_exec?
+ assert_equal Fixnum, io.pid.class
+
+ out = io.read
+ assert_equal out.class, String
+ assert_include out, 'mruby-io'
+
+ io.close
+ if Object.const_defined? :Process
+ assert_true $?.success?
+ else
+ assert_equal 0, $?
+ end
+
+ assert_true io.closed?
+ rescue NotImplementedError => e
+ skip e.message
+ end
+end
+
+assert('IO.popen with in option') do
+ begin
+ IO.pipe do |r, w|
+ w.write 'hello'
+ w.close
+ assert_equal "hello", IO.popen("cat", "r", in: r) { |i| i.read }
+ assert_equal "", r.read
+ end
+ assert_raise(ArgumentError) { IO.popen("hello", "r", in: Object.new) }
+ rescue NotImplementedError => e
+ skip e.message
+ end
+end
+
+assert('IO.popen with out option') do
+ begin
+ IO.pipe do |r, w|
+ IO.popen("echo 'hello'", "w", out: w) {}
+ w.close
+ assert_equal "hello\n", r.read
+ end
+ rescue NotImplementedError => e
+ skip e.message
+ end
+end
+
+assert('IO.popen with err option') do
+ begin
+ IO.pipe do |r, w|
+ assert_equal "", IO.popen("echo 'hello' 1>&2", "r", err: w) { |i| i.read }
+ w.close
+ assert_equal "hello\n", r.read
+ end
+ rescue NotImplementedError => e
+ skip e.message
+ end
+end
+
+assert('IO.read') do
+ # empty file
+ fd = IO.sysopen $mrbtest_io_wfname, "w"
+ io = IO.new fd, "w"
+ io.close
+ assert_equal "", IO.read($mrbtest_io_wfname)
+ assert_equal nil, IO.read($mrbtest_io_wfname, 1)
+
+ # one byte file
+ fd = IO.sysopen $mrbtest_io_wfname, "w"
+ io = IO.new fd, "w"
+ io.write "123"
+ io.close
+ assert_equal "123", IO.read($mrbtest_io_wfname)
+ assert_equal "", IO.read($mrbtest_io_wfname, 0)
+ assert_equal "1", IO.read($mrbtest_io_wfname, 1)
+ assert_equal "", IO.read($mrbtest_io_wfname, 0, 10)
+ assert_equal "23", IO.read($mrbtest_io_wfname, 2, 1)
+ assert_equal "23", IO.read($mrbtest_io_wfname, 10, 1)
+ assert_equal "", IO.read($mrbtest_io_wfname, nil, 10)
+ assert_equal nil, IO.read($mrbtest_io_wfname, 1, 10)
+end
+
+assert('IO#fileno') do
+ fd = IO.sysopen $mrbtest_io_rfname
+ io = IO.new fd
+ assert_equal io.fileno, fd
+ assert_equal io.to_i, fd
+ io.close
+ io.closed?
+end
+
+assert('IO#close_on_exec') do
+ fd = IO.sysopen $mrbtest_io_wfname, "w"
+ io = IO.new fd, "w"
+ begin
+ # IO.sysopen opens a file descripter with O_CLOEXEC flag.
+ assert_true io.close_on_exec?
+ rescue ScriptError
+ skip "IO\#close_on_exec is not implemented."
+ end
+
+ io.close_on_exec = false
+ assert_equal(false, io.close_on_exec?)
+ io.close_on_exec = true
+ assert_equal(true, io.close_on_exec?)
+ io.close_on_exec = false
+ assert_equal(false, io.close_on_exec?)
+
+ io.close
+ io.closed?
+
+ begin
+ r, w = IO.pipe
+ assert_equal(true, r.close_on_exec?)
+ r.close_on_exec = false
+ assert_equal(false, r.close_on_exec?)
+ r.close_on_exec = true
+ assert_equal(true, r.close_on_exec?)
+
+ assert_equal(true, w.close_on_exec?)
+ w.close_on_exec = false
+ assert_equal(false, w.close_on_exec?)
+ w.close_on_exec = true
+ assert_equal(true, w.close_on_exec?)
+ ensure
+ r.close unless r.closed?
+ w.close unless w.closed?
+ end
+end
+
+assert('IO#sysseek') do
+ IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
+ assert_equal 2, io.sysseek(2)
+ assert_equal 5, io.sysseek(3, IO::SEEK_CUR) # 2 + 3 => 5
+ assert_equal $mrbtest_io_msg.size - 4, io.sysseek(-4, IO::SEEK_END)
+ end
+end
+
+assert('IO.pipe') do
+ begin
+ called = false
+ IO.pipe do |r, w|
+ assert_true r.kind_of?(IO)
+ assert_true w.kind_of?(IO)
+ assert_false r.closed?
+ assert_false w.closed?
+ assert_true FileTest.pipe?(r)
+ assert_true FileTest.pipe?(w)
+ assert_nil r.pid
+ assert_nil w.pid
+ assert_true 2 < r.fileno
+ assert_true 2 < w.fileno
+ assert_true r.fileno != w.fileno
+ assert_false r.sync
+ assert_true w.sync
+ assert_equal 8, w.write('test for')
+ assert_equal 'test', r.read(4)
+ assert_equal ' for', r.read(4)
+ assert_equal 5, w.write(' pipe')
+ assert_equal nil, w.close
+ assert_equal ' pipe', r.read
+ called = true
+ assert_raise(IOError) { r.write 'test' }
+ # TODO:
+ # This assert expect raise IOError but got RuntimeError
+ # Because mruby-io not have flag for I/O readable
+ # assert_raise(IOError) { w.read }
+ end
+ assert_true called
+
+ assert_nothing_raised do
+ IO.pipe { |r, w| r.close; w.close }
+ end
+ rescue NotImplementedError => e
+ skip e.message
+ end
+end
+
+assert('`cmd`') do
+ begin
+ assert_equal `echo foo`, "foo\n"
+ rescue NotImplementedError => e
+ skip e.message
+ end
+end
+
+assert('IO TEST CLEANUP') do
+ assert_nil MRubyIOTestUtil.io_test_cleanup
+end
diff --git a/mrbgems/mruby-io/test/mruby_io_test.c b/mrbgems/mruby-io/test/mruby_io_test.c
new file mode 100644
index 000000000..ffaa27ff4
--- /dev/null
+++ b/mrbgems/mruby-io/test/mruby_io_test.c
@@ -0,0 +1,191 @@
+#include <sys/types.h>
+#include <errno.h>
+
+#if defined(_WIN32) || defined(_WIN64)
+ #include <winsock.h>
+ #include <io.h>
+#else
+ #include <sys/socket.h>
+ #include <unistd.h>
+ #include <sys/un.h>
+#endif
+
+#include <sys/stat.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "mruby.h"
+#include "mruby/array.h"
+#include "mruby/error.h"
+#include "mruby/string.h"
+#include "mruby/variable.h"
+
+static mrb_value
+mrb_io_test_io_setup(mrb_state *mrb, mrb_value self)
+{
+ char rfname[] = "tmp.mruby-io-test.XXXXXXXX";
+ char wfname[] = "tmp.mruby-io-test.XXXXXXXX";
+ char symlinkname[] = "tmp.mruby-io-test.XXXXXXXX";
+ char socketname[] = "/tmp/mruby-io-test.XXXXXXXX";
+ char msg[] = "mruby io test\n";
+ mode_t mask;
+ int fd0, fd1, fd2, fd3;
+ FILE *fp;
+
+#ifndef _WIN32
+ struct sockaddr_un sun0;
+#endif
+
+ mask = umask(077);
+ fd0 = mkstemp(rfname);
+ fd1 = mkstemp(wfname);
+#ifndef _WIN32
+ fd2 = mkstemp(symlinkname);
+ fd3 = mkstemp(socketname);
+#endif
+ if (fd0 == -1 || fd1 == -1 || fd2 == -1 || fd3 == -1) {
+ mrb_raise(mrb, E_RUNTIME_ERROR, "can't create temporary file");
+ return mrb_nil_value();
+ }
+ umask(mask);
+
+ mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_rfname"), mrb_str_new_cstr(mrb, rfname));
+ mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_wfname"), mrb_str_new_cstr(mrb, wfname));
+ mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_symlinkname"), mrb_str_new_cstr(mrb, symlinkname));
+ mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_socketname"), mrb_str_new_cstr(mrb, socketname));
+ mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_msg"), mrb_str_new_cstr(mrb, msg));
+
+ fp = fopen(rfname, "wb");
+ if (fp == NULL) {
+ mrb_raise(mrb, E_RUNTIME_ERROR, "can't open temporary file");
+ return mrb_nil_value();
+ }
+ fputs(msg, fp);
+ fclose(fp);
+
+ fp = fopen(wfname, "wb");
+ if (fp == NULL) {
+ mrb_raise(mrb, E_RUNTIME_ERROR, "can't open temporary file");
+ return mrb_nil_value();
+ }
+ fclose(fp);
+
+#ifndef _WIN32
+ unlink(symlinkname);
+ close(fd2);
+ if (symlink(rfname, symlinkname) == -1) {
+ mrb_raise(mrb, E_RUNTIME_ERROR, "can't make a symbolic link");
+ }
+
+ unlink(socketname);
+ close(fd3);
+ fd3 = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (fd3 == -1) {
+ mrb_raise(mrb, E_RUNTIME_ERROR, "can't make a socket");
+ }
+ sun0.sun_family = AF_UNIX;
+ snprintf(sun0.sun_path, sizeof(sun0.sun_path), "%s", socketname);
+ if (bind(fd3, (struct sockaddr *)&sun0, sizeof(sun0)) == -1) {
+ mrb_raisef(mrb, E_RUNTIME_ERROR, "can't bind AF_UNIX socket to %S: %S",
+ mrb_str_new_cstr(mrb, sun0.sun_path),
+ mrb_fixnum_value(errno));
+ }
+ close(fd3);
+#endif
+
+ return mrb_true_value();
+}
+
+static mrb_value
+mrb_io_test_io_cleanup(mrb_state *mrb, mrb_value self)
+{
+ mrb_value rfname = mrb_gv_get(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_rfname"));
+ mrb_value wfname = mrb_gv_get(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_wfname"));
+ mrb_value symlinkname = mrb_gv_get(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_symlinkname"));
+ mrb_value socketname = mrb_gv_get(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_socketname"));
+
+ if (mrb_type(rfname) == MRB_TT_STRING) {
+ remove(RSTRING_PTR(rfname));
+ }
+ if (mrb_type(wfname) == MRB_TT_STRING) {
+ remove(RSTRING_PTR(wfname));
+ }
+ if (mrb_type(symlinkname) == MRB_TT_STRING) {
+ remove(RSTRING_PTR(symlinkname));
+ }
+ if (mrb_type(socketname) == MRB_TT_STRING) {
+ remove(RSTRING_PTR(socketname));
+ }
+
+ mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_rfname"), mrb_nil_value());
+ mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_wfname"), mrb_nil_value());
+ mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_symlinkname"), mrb_nil_value());
+ mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_socketname"), mrb_nil_value());
+ mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_msg"), mrb_nil_value());
+
+ return mrb_nil_value();
+}
+
+static mrb_value
+mrb_io_test_file_setup(mrb_state *mrb, mrb_value self)
+{
+ mrb_value ary = mrb_io_test_io_setup(mrb, self);
+#ifndef _WIN32
+ if (symlink("/usr/bin", "test-bin") == -1) {
+ mrb_raise(mrb, E_RUNTIME_ERROR, "can't make a symbolic link");
+ }
+#endif
+
+ return ary;
+}
+
+static mrb_value
+mrb_io_test_file_cleanup(mrb_state *mrb, mrb_value self)
+{
+ mrb_io_test_io_cleanup(mrb, self);
+ remove("test-bin");
+
+ return mrb_nil_value();
+}
+
+static mrb_value
+mrb_io_test_mkdtemp(mrb_state *mrb, mrb_value klass)
+{
+ mrb_value str;
+ char *cp;
+
+ mrb_get_args(mrb, "S", &str);
+ cp = mrb_str_to_cstr(mrb, str);
+ if (mkdtemp(cp) == NULL) {
+ mrb_sys_fail(mrb, "mkdtemp");
+ }
+ return mrb_str_new_cstr(mrb, cp);
+}
+
+static mrb_value
+mrb_io_test_rmdir(mrb_state *mrb, mrb_value klass)
+{
+ mrb_value str;
+ char *cp;
+
+ mrb_get_args(mrb, "S", &str);
+ cp = mrb_str_to_cstr(mrb, str);
+ if (rmdir(cp) == -1) {
+ mrb_sys_fail(mrb, "rmdir");
+ }
+ return mrb_true_value();
+}
+
+void
+mrb_mruby_io_gem_test(mrb_state* mrb)
+{
+ struct RClass *io_test = mrb_define_module(mrb, "MRubyIOTestUtil");
+ mrb_define_class_method(mrb, io_test, "io_test_setup", mrb_io_test_io_setup, MRB_ARGS_NONE());
+ mrb_define_class_method(mrb, io_test, "io_test_cleanup", mrb_io_test_io_cleanup, MRB_ARGS_NONE());
+
+ mrb_define_class_method(mrb, io_test, "file_test_setup", mrb_io_test_file_setup, MRB_ARGS_NONE());
+ mrb_define_class_method(mrb, io_test, "file_test_cleanup", mrb_io_test_file_cleanup, MRB_ARGS_NONE());
+
+ mrb_define_class_method(mrb, io_test, "mkdtemp", mrb_io_test_mkdtemp, MRB_ARGS_REQ(1));
+ mrb_define_class_method(mrb, io_test, "rmdir", mrb_io_test_rmdir, MRB_ARGS_REQ(1));
+}