1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16797 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mame 2008-06-03 13:34:48 +00:00
parent 87050da59f
commit 9f42dfa012
4 changed files with 219 additions and 0 deletions

View file

@ -1,3 +1,13 @@
Tue Jun 3 22:33:29 2008 Yusuke Endoh <mame@tsg.ne.jp>
* 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@tsg.ne.jp>
* test/etc/test_etc.rb: new tests for etc.

View file

@ -7,6 +7,7 @@ class TestDir < Test::Unit::TestCase
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 @@ class TestDir < Test::Unit::TestCase
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

View file

@ -25,4 +25,30 @@ class TestEncoding < Test::Unit::TestCase
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

View file

@ -84,4 +84,72 @@ class TestMarshal < Test::Unit::TestCase
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