summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorAkira Yumiyama <[email protected]>2013-04-01 09:12:01 +0900
committerAkira Yumiyama <[email protected]>2013-04-01 09:12:01 +0900
commite8ca5e516983fe720ec46887744f0d21b8f16ce1 (patch)
treee2c5dc2aab35cab78365f45918f140d82dcb45dd /test
downloadmruby-e8ca5e516983fe720ec46887744f0d21b8f16ce1.tar.gz
mruby-e8ca5e516983fe720ec46887744f0d21b8f16ce1.zip
initial commit
Diffstat (limited to 'test')
-rw-r--r--test/file.rb75
-rw-r--r--test/file_test.rb90
-rw-r--r--test/io.rb237
-rw-r--r--test/mruby_io_test.c94
4 files changed, 496 insertions, 0 deletions
diff --git a/test/file.rb b/test/file.rb
new file mode 100644
index 000000000..f1bef32e5
--- /dev/null
+++ b/test/file.rb
@@ -0,0 +1,75 @@
+##
+# 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 + "\n", io.read
+ assert_equal $mrbtest_io_rfname, io.path
+ io.close
+ assert_equal $mrbtest_io_rfname, io.path
+ io.closed?
+end
+
+assert('File.dirname') do
+ path = File.dirname("filename")
+ "." == path
+end
+
+assert('File.basename') do
+ name = File.basename("../somewhere/filename")
+ name == "filename"
+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('File.size') do
+ File.size($mrbtest_io_rfname) == $mrbtest_io_msg.size + 1 and
+ File.size($mrbtest_io_wfname) == 0
+end
+
+assert('File.join') do
+ File.join() == "" and
+ File.join("a") == "a" and
+ File.join("/a") == "/a" and
+ File.join("a/") == "a/" and
+ File.join("a", "b", "c") == "a/b/c" and
+ File.join("/a", "b", "c") == "/a/b/c" and
+ File.join("a", "b", "c/") == "a/b/c/" and
+ File.join("a/", "/b/", "/c") == "a/b/c"
+end
+
+assert('File.realpath') do
+ usrbin = IO.popen("cd bin; /bin/pwd -P") { |f| f.read.chomp }
+ assert_equal usrbin, File.realpath("bin")
+end
+
+assert('File TEST CLEANUP') do
+ assert_nil MRubyIOTestUtil.io_test_cleanup
+end
diff --git a/test/file_test.rb b/test/file_test.rb
new file mode 100644
index 000000000..296679a6b
--- /dev/null
+++ b/test/file_test.rb
@@ -0,0 +1,90 @@
+##
+# FileTest
+
+assert('FileTest TEST SETUP') do
+ MRubyIOTestUtil.io_test_setup
+end
+
+assert("FileTest.directory?") do
+ assert_equal true, FileTest.directory?("/tmp")
+ assert_equal false, FileTest.directory?("/bin/sh")
+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
+ assert_equal false, FileTest.file?("/tmp")
+ assert_equal true, FileTest.file?("/bin/sh")
+end
+
+assert("FileTest.pipe?") do
+ io = IO.popen("ls")
+ assert_equal true, FileTest.pipe?(io)
+ assert_equal false, FileTest.pipe?("/tmp")
+end
+
+assert("FileTest.size?") do
+ assert_equal $mrbtest_io_msg.size+1, 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+1, 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
+ skip
+end
+
+assert("FileTest.symlink?") do
+ skip
+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/test/io.rb b/test/io.rb
new file mode 100644
index 000000000..727c67303
--- /dev/null
+++ b/test/io.rb
@@ -0,0 +1,237 @@
+##
+# IO Test
+
+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.new') do
+ IO.new(0)
+end
+
+assert('IO gc check') do
+ 100.times { IO.new(0) }
+end
+
+assert('IO TEST SETUP') do
+ MRubyIOTestUtil.io_test_setup
+end
+
+assert('IO.sysopen, IO#close, IO#closed?') do
+ fd = IO.sysopen $mrbtest_io_rfname
+ assert_equal Fixnum, fd.class
+ io = IO.new fd
+ assert_equal IO, io.class
+ assert_equal false, io.closed?, "IO not closed"
+ assert_equal nil, io.close, "IO#close should return nil"
+ assert_equal true, io.closed?, "IO#closed? should return true"
+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
+ io.close
+ io.closed?
+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.closed?
+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 + 1
+ assert_equal '', io._buf
+ assert_equal $mrbtest_io_msg + "\n", io._read_buf
+ assert_equal $mrbtest_io_msg + "\n", 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] + "\n", 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#read argument check') do
+ fd = IO.sysopen $mrbtest_io_rfname
+ io = IO.new fd
+ assert_raise TypeError do
+ io.read("str")
+ end
+ assert_raise ArgumentError do
+ io.read(-5)
+ end
+ io.close
+ io.closed?
+end
+
+assert('IO#read') do
+ fd = IO.sysopen $mrbtest_io_rfname
+ io = IO.new fd
+ assert_equal 'mruby', io.read(5)
+ assert_equal $mrbtest_io_msg[5,100] + "\n", io.read
+ assert_equal nil, io.read
+ io.close
+ io.closed?
+end
+
+assert('IO#readchar, IO#getc') do
+ fd = IO.sysopen $mrbtest_io_rfname
+ io = IO.new fd
+ def io._buf
+ @buf
+ end
+ assert_equal 'm', io.readchar
+ assert_equal 1, io.pos
+ assert_equal 'r', io.getc
+ assert_equal 2, io.pos
+ io.gets
+ assert_raise EOFError do
+ io.readchar
+ end
+ assert_equal nil, io.getc
+ io.close
+ io.closed?
+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#gets') do
+ fd = IO.sysopen $mrbtest_io_rfname
+ io = IO.new fd
+
+ # gets without arguments
+ assert_equal $mrbtest_io_msg + "\n", 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
+ io = IO.popen("ls")
+ assert_equal Fixnum, io.pid.class
+ ls = io.read
+ assert_equal ls.class, String
+ assert_include ls, 'AUTHORS'
+ assert_include ls, 'mrblib'
+ io.close
+ io.closed?
+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 TEST CLEANUP') do
+ assert_nil MRubyIOTestUtil.io_test_cleanup
+end
diff --git a/test/mruby_io_test.c b/test/mruby_io_test.c
new file mode 100644
index 000000000..71ef715a5
--- /dev/null
+++ b/test/mruby_io_test.c
@@ -0,0 +1,94 @@
+#include "mruby.h"
+#include "mruby/array.h"
+#include "mruby/string.h"
+#include "mruby/variable.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static mrb_value
+mrb_io_test_io_setup(mrb_state *mrb, mrb_value self)
+{
+ char rfname[] = "tmp.XXXXXXXX";
+ char wfname[] = "tmp.XXXXXXXX";
+ char msg[] = "mruby io test";
+ FILE *fp;
+ mrb_value ary = mrb_ary_new(mrb);
+
+ mktemp(rfname);
+ mktemp(wfname);
+ mrb_gv_set(mrb, mrb_intern(mrb, "$mrbtest_io_rfname"), mrb_str_new_cstr(mrb, rfname));
+ mrb_gv_set(mrb, mrb_intern(mrb, "$mrbtest_io_wfname"), mrb_str_new_cstr(mrb, wfname));
+ mrb_gv_set(mrb, mrb_intern(mrb, "$mrbtest_io_msg"), mrb_str_new_cstr(mrb, msg));
+
+ mrb_ary_push(mrb, ary, mrb_str_new_cstr(mrb, rfname));
+ mrb_ary_push(mrb, ary, mrb_str_new_cstr(mrb, wfname));
+ mrb_ary_push(mrb, ary, mrb_str_new_cstr(mrb, msg));
+
+ fp = fopen(rfname, "w");
+ if (fp == NULL) {
+ mrb_raise(mrb, E_RUNTIME_ERROR, "can't open temporary file");
+ return mrb_nil_value();
+ }
+ fprintf(fp, "%s\n", msg);
+ fclose(fp);
+
+ fp = fopen(wfname, "w");
+ if (fp == NULL) {
+ mrb_raise(mrb, E_RUNTIME_ERROR, "can't open temporary file");
+ return mrb_nil_value();
+ }
+ fclose(fp);
+
+ return ary;
+}
+
+static mrb_value
+mrb_io_test_io_cleanup(mrb_state *mrb, mrb_value self)
+{
+ mrb_value rfname = mrb_gv_get(mrb, mrb_intern(mrb, "$mrbtest_io_rfname"));
+ mrb_value wfname = mrb_gv_get(mrb, mrb_intern(mrb, "$mrbtest_io_wfname"));
+
+ if (mrb_type(rfname) == MRB_TT_STRING) {
+ remove(RSTRING_PTR(rfname));
+ }
+ if (mrb_type(wfname) == MRB_TT_STRING) {
+ remove(RSTRING_PTR(wfname));
+ }
+
+ mrb_gv_set(mrb, mrb_intern(mrb, "$mrbtest_io_rfname"), mrb_nil_value());
+ mrb_gv_set(mrb, mrb_intern(mrb, "$mrbtest_io_wfname"), mrb_nil_value());
+ mrb_gv_set(mrb, mrb_intern(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);
+ symlink("/usr/bin", "test-bin");
+
+ 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();
+}
+
+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, ARGS_NONE());
+ mrb_define_class_method(mrb, io_test, "io_test_cleanup", mrb_io_test_io_cleanup, ARGS_NONE());
+
+ mrb_define_class_method(mrb, io_test, "file_test_setup", mrb_io_test_file_setup, ARGS_NONE());
+ mrb_define_class_method(mrb, io_test, "file_test_cleanup", mrb_io_test_file_cleanup, ARGS_NONE());
+
+}