[前][次][番号順一覧][スレッド一覧]

ruby-changes:5298

From: mame <ko1@a...>
Date: Tue, 3 Jun 2008 22:35:04 +0900 (JST)
Subject: [ruby-changes:5298] Ruby:r16797 (trunk): * test/ruby/test_dir.rb: add tests to achieve over 90% test coverage

mame	2008-06-03 22:34:48 +0900 (Tue, 03 Jun 2008)

  New Revision: 16797

  Modified files:
    trunk/ChangeLog
    trunk/test/ruby/test_dir.rb
    trunk/test/ruby/test_encoding.rb
    trunk/test/ruby/test_marshal.rb

  Log:
    * test/ruby/test_dir.rb: add tests to achieve over 90% test coverage
      of dir.c.
    
    * test/ruby/test_encoding.rb: add tests for dummy?, name_list and
      aliases.
    
    * test/ruby/test_marshal.rb: add some tests.


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=16797&r2=16796&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_marshal.rb?r1=16797&r2=16796&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_dir.rb?r1=16797&r2=16796&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_encoding.rb?r1=16797&r2=16796&diff_format=u

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 16796)
+++ ChangeLog	(revision 16797)
@@ -1,3 +1,13 @@
+Tue Jun  3 22:33:29 2008  Yusuke Endoh  <mame@t...>
+
+	* test/ruby/test_dir.rb: add tests to achieve over 90% test coverage
+	  of dir.c.
+
+	* test/ruby/test_encoding.rb: add tests for dummy?, name_list and
+	  aliases.
+
+	* test/ruby/test_marshal.rb: add some tests.
+
 Tue Jun  3 22:25:51 2008  Yusuke Endoh  <mame@t...>
 
 	* test/etc/test_etc.rb: new tests for etc.
Index: test/ruby/test_dir.rb
===================================================================
--- test/ruby/test_dir.rb	(revision 16796)
+++ test/ruby/test_dir.rb	(revision 16797)
@@ -7,6 +7,7 @@
 
   def setup
     @root = Dir.mktmpdir('__test_dir__')
+    @nodir = File.join(@root, "dummy")
     for i in ?a..?z
       if i.ord % 2 == 0
         FileUtils.touch(File.join(@root, i))
@@ -47,4 +48,118 @@
     assert_raise(SecurityError) { b.call }
   end
 
+  def test_nodir
+    assert_raise(Errno::ENOENT) { Dir.open(@nodir) }
+  end
+
+  def test_inspect
+    d = Dir.open(@root)
+    assert_match(/^#<Dir:#{ Regexp.quote(@root) }>$/, d.inspect)
+    assert_match(/^#<Dir:.*>$/, Dir.allocate.inspect)
+  ensure
+    d.close
+  end
+
+  def test_path
+    d = Dir.open(@root)
+    assert_equal(@root, d.path)
+    assert_nil(Dir.allocate.path)
+  ensure
+    d.close
+  end
+
+  def test_set_pos
+    d = Dir.open(@root)
+    loop do
+      i = d.pos
+      break unless x = d.read
+      d.pos = i
+      assert_equal(x, d.read)
+    end
+  ensure
+    d.close
+  end
+
+  def test_rewind
+    d = Dir.open(@root)
+    a = (0..5).map { d.read }
+    d.rewind
+    b = (0..5).map { d.read }
+    assert_equal(a, b)
+    assert_raise(SecurityError) do
+      Thread.new do
+        $SAFE = 4
+        d.rewind
+      end.join
+    end
+  ensure
+    d.close
+  end
+
+  def test_chdir
+    @pwd = Dir.pwd
+    @env_home = ENV["HOME"]
+    @env_logdir = ENV["LOGDIR"]
+    ENV.delete("HOME")
+    ENV.delete("LOGDIR")
+
+    assert_raise(Errno::ENOENT) { Dir.chdir(@nodir) }
+    assert_raise(ArgumentError) { Dir.chdir }
+    ENV["HOME"] = @pwd
+    Dir.chdir do
+      assert_equal(@pwd, Dir.pwd)
+      Dir.chdir(@root)
+      assert_equal(@root, Dir.pwd)
+    end
+
+  ensure
+    begin
+      Dir.chdir(@pwd)
+    rescue
+      abort("cannot return the original directory: #{ @pwd }")
+    end
+    if @env_home
+      ENV["HOME"] = @env_home
+    else
+      ENV.delete("HOME")
+    end
+    if @env_logdir
+      ENV["LOGDIR"] = @env_logdir
+    else
+      ENV.delete("LOGDIR")
+    end
+  end
+
+  def test_chroot_nodir
+    assert_raise(NotImplementedError, Errno::ENOENT) { Dir.chroot(File.join(@nodir, "")) }
+  end
+
+  def test_close
+    d = Dir.open(@root)
+    d.close
+    assert_raise(IOError) { d.read }
+  end
+
+  def test_glob
+    assert_equal((%w(. ..) + (?a..?z).to_a).map{|f| File.join(@root, f) },
+                 Dir.glob(File.join(@root, "*"), File::FNM_DOTMATCH).sort)
+    assert_equal([@root] + (?a..?z).map {|f| File.join(@root, f) },
+                 Dir.glob([@root, File.join(@root, "*")]))
+    assert_equal([@root] + (?a..?z).map {|f| File.join(@root, f) },
+                 Dir.glob(@root + "\0\0\0" + File.join(@root, "*")))
+
+    assert_equal((?a..?z).step(2).map {|f| File.join(File.join(@root, f), "") },
+                 Dir.glob(File.join(@root, "*/")))
+
+    FileUtils.touch(File.join(@root, "{}"))
+    assert_equal(%w({} a).map{|f| File.join(@root, f) },
+                 Dir.glob(File.join(@root, '{\{\},a}')))
+    assert_equal([], Dir.glob(File.join(@root, '[')))
+    assert_equal([], Dir.glob(File.join(@root, '[a-\\')))
+  end
+
+  def test_foreach
+    assert_equal(Dir.foreach(@root).to_a.sort, %w(. ..) + (?a..?z).to_a)
+  end
+
 end
Index: test/ruby/test_encoding.rb
===================================================================
--- test/ruby/test_encoding.rb	(revision 16796)
+++ test/ruby/test_encoding.rb	(revision 16797)
@@ -25,4 +25,30 @@
       assert_equal(e.object_id, Marshal.load(Marshal.dump(e)).object_id)
     end    
   end
+
+  def test_find
+    assert_raise(ArgumentError) { Encoding.find("foobarbazqux") }
+  end
+
+  def test_dummy_p
+    assert_equal(true, Encoding::ISO_2022_JP.dummy?)
+    assert_equal(false, Encoding::UTF_8.dummy?)
+  end
+
+  def test_name_list
+    assert_instance_of(Array, Encoding.name_list)
+    Encoding.name_list.each do |x|
+      assert_instance_of(String, x)
+    end
+  end
+
+  def test_aliases
+    assert_instance_of(Hash, Encoding.aliases)
+    Encoding.aliases.each do |k, v|
+      assert(Encoding.name_list.include?(k))
+      assert(Encoding.name_list.include?(v))
+      assert_instance_of(String, k)
+      assert_instance_of(String, v)
+    end
+  end
 end
Index: test/ruby/test_marshal.rb
===================================================================
--- test/ruby/test_marshal.rb	(revision 16796)
+++ test/ruby/test_marshal.rb	(revision 16797)
@@ -84,4 +84,72 @@
     s2 = o2.str
     assert_equal(s1, s2)
   end
+
+  def test_pipe
+    o1 = C.new("a" * 10000)
+
+    r, w = IO.pipe
+    Marshal.dump(o1, w)
+    o2 = Marshal.load(r)
+    assert_equal(o1.str, o2.str)
+
+    r, w = IO.pipe
+    Marshal.dump(o1, w, 2)
+    o2 = Marshal.load(r)
+    assert_equal(o1.str, o2.str)
+
+    assert_raise(TypeError) { Marshal.dump("foo", Object.new) }
+    assert_raise(TypeError) { Marshal.load(Object.new) }
+  end
+
+  def test_limit
+    assert_equal([[[]]], Marshal.load(Marshal.dump([[[]]], 3)))
+    assert_raise(ArgumentError) { Marshal.dump([[[]]], 2) }
+  end
+
+  def test_userdef_invalid
+    o = C.new(nil)
+    assert_raise(TypeError) { Marshal.dump(o) }
+  end
+
+  def test_class
+    o = class << Object.new; self; end
+    assert_raise(TypeError) { Marshal.dump(o) }
+    assert_equal(Object, Marshal.load(Marshal.dump(Object)))
+    assert_equal(Enumerable, Marshal.load(Marshal.dump(Enumerable)))
+  end
+
+  class C2
+    def initialize(ary)
+      @ary = ary
+    end
+    def _dump(s)
+      @ary.clear
+      "foo"
+    end
+  end
+
+  def test_modify_array_during_dump
+    a = []
+    o = C2.new(a)
+    a << o << nil
+    assert_raise(RuntimeError) { Marshal.dump(a) }
+  end
+
+  def test_change_class_name
+    eval("class C3; def _dump(s); 'foo'; end; end")
+    m = Marshal.dump(C3.new)
+    assert_raise(TypeError) { Marshal.load(m) }
+    eval("C3 = nil")
+    assert_raise(TypeError) { Marshal.load(m) }
+  end
+
+  def test_change_struct
+    eval("C3 = Struct.new(:foo, :bar)")
+    m = Marshal.dump(C3.new("FOO", "BAR"))
+    eval("C3 = Struct.new(:foo)")
+    assert_raise(TypeError) { Marshal.load(m) }
+    eval("C3 = Struct.new(:foo, :baz)")
+    assert_raise(TypeError) { Marshal.load(m) }
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]