* test/ruby/test_require.rb: new tests for library requiring, to

achieve over 90% test coverage of dln.c.

* test/ruby/test_class.rb: add tests to achieve over 90% test coverage
  of class.c.

* test/ruby/test_module.rb: ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16510 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mame 2008-05-21 15:31:15 +00:00
Родитель 4c094940aa
Коммит 2c8dd794e9
4 изменённых файлов: 285 добавлений и 0 удалений

Просмотреть файл

@ -1,3 +1,13 @@
Thu May 22 00:30:06 2008 Yusuke Endoh <mame@tsg.ne.jp>
* test/ruby/test_require.rb: new tests for library requiring, to
achieve over 90% test coverage of dln.c.
* test/ruby/test_class.rb: add tests to achieve over 90% test coverage
of class.c.
* test/ruby/test_module.rb: ditto.
Thu May 22 00:15:44 2008 Koichi Sasada <ko1@atdot.net>
* insns.def, vm_insnhelper.c: specify "const".

Просмотреть файл

@ -1,6 +1,10 @@
require 'test/unit'
require_relative 'envutil'
class TestClass < Test::Unit::TestCase
def ruby(*r, &b)
EnvUtil.rubyexec(*r, &b)
end
# ------------------
# Various test classes
@ -105,4 +109,41 @@ class TestClass < Test::Unit::TestCase
end
end
def test_check_inheritable
assert_raise(TypeError) { Class.new(Object.new) }
o = Object.new
c = class << o; self; end
assert_raise(TypeError) { Class.new(c) }
assert_nothing_raised { Class.new(Class) } # is it OK?
assert_raise(TypeError) { eval("class Foo < Class; end") }
end
def test_initialize_copy
c = Class.new
assert_raise(TypeError) { c.instance_eval { initialize_copy(1) } }
o = Object.new
c = class << o; self; end
assert_raise(TypeError) { c.dup }
end
def test_singleton_class
assert_raise(TypeError) { 1.extend(Module.new) }
assert_raise(TypeError) { :foo.extend(Module.new) }
ruby do |w, r, e|
w.puts "module Foo; def foo; :foo; end; end"
w.puts "false.extend(Foo)"
w.puts "true.extend(Foo)"
w.puts "p false.foo"
w.puts "p true.foo"
w.puts "p FalseClass.include?(Foo)"
w.puts "p TrueClass.include?(Foo)"
w.close
assert_equal("", e.read)
assert_equal(":foo\n:foo\ntrue\ntrue", r.read.chomp)
end
end
end

Просмотреть файл

@ -690,4 +690,23 @@ class TestModule < Test::Unit::TestCase
o.extend(m2)
assert_equal(true, o.respond_to?(:foo))
end
def test_cyclic_include
m1 = Module.new
m2 = Module.new
m1.instance_eval { include(m2) }
assert_raise(ArgumentError) do
m2.instance_eval { include(m1) }
end
end
def test_include_p
m = Module.new
c1 = Class.new
c1.instance_eval { include(m) }
c2 = Class.new(c1)
assert_equal(true, c1.include?(m))
assert_equal(true, c2.include?(m))
assert_equal(false, m.include?(m))
end
end

215
test/ruby/test_require.rb Normal file
Просмотреть файл

@ -0,0 +1,215 @@
require 'test/unit'
require 'tempfile'
require_relative 'envutil'
class TestRequire < Test::Unit::TestCase
def ruby(*r, &b)
EnvUtil.rubyexec(*r, &b)
end
def test_require_invalid_shared_object
t = Tempfile.new(["test_ruby_test_require", ".so"])
t.puts "dummy"
t.close
ruby do |w, r, e|
w.puts "begin"
w.puts " require \"#{ t.path }\""
w.puts "rescue LoadError"
w.puts " p :ok"
w.puts "end"
w.close
assert_equal(":ok", r.read.chomp)
end
end
def test_require_too_long_filename
ruby do |w, r, e|
w.puts "begin"
w.puts " require '#{ "foo/" * 10000 }foo'"
w.puts "rescue LoadError"
w.puts " p :ok"
w.puts "end"
w.close
e.read
assert_equal(":ok", r.read.chomp)
end
end
def test_require_path_home
env_rubypath, env_home = ENV["RUBYPATH"], ENV["HOME"]
ENV["RUBYPATH"] = "~"
ENV["HOME"] = "/foo" * 10000
ruby("-S", "test_ruby_test_require") do |w, r, e|
w.close
e.read
assert_equal("", r.read)
end
ENV["RUBYPATH"] = "~" + "/foo" * 10000
ENV["HOME"] = "/foo"
ruby("-S", "test_ruby_test_require") do |w, r, e|
w.close
e.read
assert_equal("", r.read)
end
t = Tempfile.new(["test_ruby_test_require", ".rb"])
t.puts "p :ok"
t.close
ENV["RUBYPATH"] = "~"
ENV["HOME"], name = File.split(t.path)
ruby("-S", name) do |w, r, e|
w.close
assert_equal(":ok", r.read.chomp)
assert_equal("", e.read)
end
ensure
env_rubypath ? ENV["RUBYPATH"] = env_rubypath : ENV.delete("RUBYPATH")
env_home ? ENV["HOME"] = env_home : ENV.delete("HOME")
end
def test_define_class
begin
require "socket"
rescue LoadError
return
end
ruby do |w, r, e|
w.puts "BasicSocket = 1"
w.puts "begin"
w.puts " require 'socket'"
w.puts " p :ng"
w.puts "rescue TypeError"
w.puts " p :ok"
w.puts "end"
w.close
assert_equal("", e.read)
assert_equal(":ok", r.read.chomp)
end
ruby do |w, r, e|
w.puts "class BasicSocket; end"
w.puts "begin"
w.puts " require 'socket'"
w.puts " p :ng"
w.puts "rescue NameError"
w.puts " p :ok"
w.puts "end"
w.close
assert_equal("", e.read)
assert_equal(":ok", r.read.chomp)
end
ruby do |w, r, e|
w.puts "class BasicSocket < IO; end"
w.puts "begin"
w.puts " require 'socket'"
w.puts " p :ok"
w.puts "rescue Exception"
w.puts " p :ng"
w.puts "end"
w.close
assert_equal("", e.read)
assert_equal(":ok", r.read.chomp)
end
end
def test_define_class_under
begin
require "zlib"
rescue LoadError
return
end
ruby do |w, r, e|
w.puts "module Zlib; end"
w.puts "Zlib::Error = 1"
w.puts "begin"
w.puts " require 'zlib'"
w.puts " p :ng"
w.puts "rescue TypeError"
w.puts " p :ok"
w.puts "end"
w.close
assert_equal("", e.read)
assert_equal(":ok", r.read.chomp)
end
ruby do |w, r, e|
w.puts "module Zlib; end"
w.puts "class Zlib::Error; end"
w.puts "begin"
w.puts " require 'zlib'"
w.puts " p :ng"
w.puts "rescue NameError"
w.puts " p :ok"
w.puts "end"
w.close
assert_equal("", e.read)
assert_equal(":ok", r.read.chomp)
end
ruby do |w, r, e|
w.puts "module Zlib; end"
w.puts "class Zlib::Error < StandardError; end"
w.puts "begin"
w.puts " require 'zlib'"
w.puts " p :ok"
w.puts "rescue Exception"
w.puts " p :ng"
w.puts "end"
w.close
assert_equal("", e.read)
assert_equal(":ok", r.read.chomp)
end
end
def test_define_module
begin
require "zlib"
rescue LoadError
return
end
ruby do |w, r, e|
w.puts "Zlib = 1"
w.puts "begin"
w.puts " require 'zlib'"
w.puts " p :ng"
w.puts "rescue TypeError"
w.puts " p :ok"
w.puts "end"
w.close
assert_equal("", e.read)
assert_equal(":ok", r.read.chomp)
end
end
def test_define_module_under
begin
require "socket"
rescue LoadError
return
end
ruby do |w, r, e|
w.puts "class BasicSocket < IO; end"
w.puts "class Socket < BasicSocket; end"
w.puts "Socket::Constants = 1"
w.puts "begin"
w.puts " require 'socket'"
w.puts " p :ng"
w.puts "rescue TypeError"
w.puts " p :ok"
w.puts "end"
w.close
assert_equal("", e.read)
assert_equal(":ok", r.read.chomp)
end
end
end