From bebd7e4511aca3141532d95ee5bd0237f621c059 Mon Sep 17 00:00:00 2001 From: mame Date: Fri, 22 Jan 2010 15:03:32 +0000 Subject: * test/ruby/test_array.rb: add a test for Array#rotate, rotate!. * test/ruby/test_dir.rb, test/ruby/test_fnmatch.rb: add some tests (for coverage of dir.c). * test/ruby/test_enum.rb: add a test for Enumerable#minmax. * test/ruby/test_enumerator.rb: add some tests for Enumerator#inspect, Enumerator::Generator and Yielder. * test/ruby/test_env.rb: add a test for ENV#index. * test/ruby/test_exception.rb: add some tests (for coverage of error.c). * test/ruby/test_hash.rb: add a test for recursive check. * test/ruby/test_integer.rb: add a test for number of argument of Integer. * test/ruby/test_method.rb: add a test for define_method. * test/ruby/test_module.rb: add a test for constant of included module. * test/ruby/test_proc.rb: add a test for parameters with cfunc. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26379 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_array.rb | 4 ++++ test/ruby/test_dir.rb | 41 ++++++++++++++++++++++++++++++++++++ test/ruby/test_enum.rb | 1 + test/ruby/test_enumerator.rb | 40 ++++++++++++++++++++++++++++++++++++ test/ruby/test_env.rb | 1 + test/ruby/test_exception.rb | 49 ++++++++++++++++++++++++++++++++++++++++++++ test/ruby/test_fnmatch.rb | 2 ++ test/ruby/test_hash.rb | 5 +++++ test/ruby/test_integer.rb | 11 +++++----- test/ruby/test_method.rb | 4 ++++ test/ruby/test_module.rb | 19 +++++++++++++++++ test/ruby/test_proc.rb | 4 ++++ 12 files changed, 176 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 8c6fa8b4e1..f05c9e4e95 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -1788,6 +1788,8 @@ class TestArray < Test::Unit::TestCase assert_equal([], a.rotate(-4)) assert_equal([], a.rotate(13)) assert_equal([], a.rotate(-13)) + a = [1,2,3] + assert_raise(ArgumentError) { a.rotate(1, 1) } end def test_rotate! @@ -1813,5 +1815,7 @@ class TestArray < Test::Unit::TestCase a = [].freeze e = assert_raise(RuntimeError) {a.rotate!} assert_match(/can't modify frozen array/, e.message) + a = [1,2,3] + assert_raise(ArgumentError) { a.rotate!(1, 1) } end end diff --git a/test/ruby/test_dir.rb b/test/ruby/test_dir.rb index eb315d3d85..cabe38270e 100644 --- a/test/ruby/test_dir.rb +++ b/test/ruby/test_dir.rb @@ -162,6 +162,9 @@ class TestDir < Test::Unit::TestCase assert_equal([], Dir.glob(File.join(@root, '['))) assert_equal([], Dir.glob(File.join(@root, '[a-\\'))) + assert_equal([File.join(@root, "a")], Dir.glob(File.join(@root, 'a\\'))) + assert_equal((?a..?f).map {|f| File.join(@root, f) }.sort, Dir.glob(File.join(@root, '[abc/def]')).sort) + d = "\u{3042}\u{3044}".encode("utf-16le") assert_raise(Encoding::CompatibilityError) {Dir.glob(d)} m = Class.new {define_method(:to_path) {d}} @@ -172,4 +175,42 @@ class TestDir < Test::Unit::TestCase assert_equal(Dir.foreach(@root).to_a.sort, %w(. ..) + (?a..?z).to_a) end + def test_dir_enc + dir = Dir.open(@root, encoding: "UTF-8") + begin + while name = dir.read + assert_equal(Encoding.find("UTF-8"), name.encoding) + end + ensure + dir.close + end + + dir = Dir.open(@root, encoding: "ASCII-8BIT") + begin + while name = dir.read + assert_equal(Encoding.find("ASCII-8BIT"), name.encoding) + end + ensure + dir.close + end + end + + def test_symlink + begin + [:dummy, *?a..?z].each do |f| + File.symlink(File.join(@root, f), + File.join(@root, "symlink-#{ f }")) + end + rescue NotImplementedError + return + end + + assert_equal([*?a..?z, *"symlink-a".."symlink-z"].each_slice(2).map {|f, _| File.join(@root, f + "/") }.sort, + Dir.glob(File.join(@root, "*/")).sort) + + puts("1"); + Dir.glob(File.join(@root, "**/")) + puts("2"); + end + end diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb index 4b71f5b6cc..4a594545e3 100644 --- a/test/ruby/test_enum.rb +++ b/test/ruby/test_enum.rb @@ -166,6 +166,7 @@ class TestEnumerable < Test::Unit::TestCase assert_equal(["dog", "albatross"], a.minmax {|a,b| a.length <=> b.length }) assert_equal([1, 3], [2,3,1].minmax) assert_equal([3, 1], [2,3,1].minmax {|a,b| b <=> a }) + assert_equal([1, 3], [2,2,3,3,1,1].minmax) end def test_min_by diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb index fe8a573b2b..0ee33ca6f1 100644 --- a/test/ruby/test_enumerator.rb +++ b/test/ruby/test_enumerator.rb @@ -332,6 +332,46 @@ class TestEnumerator < Test::Unit::TestCase assert_equal(10, exc.result) end + def test_inspect + e = (0..10).each_cons(2) + assert_equal("#", e.inspect) + e = Enumerator.new {|y| x = y.yield; 10 } + assert_match(/\A#/, e.inspect) + + a = [] + e = a.each_with_object(a) + a << e + assert_equal("#]:each_with_object([#])>", + e.inspect) + end + + def test_generator + # note: Enumerator::Generator is a class just for internal + g = Enumerator::Generator.new {|y| y << 1 << 2 << 3; :foo } + g2 = g.dup + a = [] + assert_equal(:foo, g.each {|x| a << x }) + assert_equal([1, 2, 3], a) + a = [] + assert_equal(:foo, g2.each {|x| a << x }) + assert_equal([1, 2, 3], a) + end + + def test_yielder + # note: Enumerator::Yielder is a class just for internal + a = [] + y = Enumerator::Yielder.new {|x| a << x } + assert_equal(y, y << 1 << 2 << 3) + assert_equal([1, 2, 3], a) + + a = [] + y = Enumerator::Yielder.new {|x| a << x } + assert_equal([1], y.yield(1)) + assert_equal([1, 2], y.yield(2)) + assert_equal([1, 2, 3], y.yield(3)) + + assert_raise(LocalJumpError) { Enumerator::Yielder.new } + end end diff --git a/test/ruby/test_env.rb b/test/ruby/test_env.rb index df26444436..94f978e71c 100644 --- a/test/ruby/test_env.rb +++ b/test/ruby/test_env.rb @@ -68,6 +68,7 @@ class TestEnv < Test::Unit::TestCase ENV['test'] = val[0...-1] assert_nil(ENV.key(val)) + assert_nil(ENV.index(val)) assert_nil(ENV.key(val.upcase)) ENV['test'] = val if IGNORE_CASE diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb index 54a5fdc487..29483925f1 100644 --- a/test/ruby/test_exception.rb +++ b/test/ruby/test_exception.rb @@ -255,4 +255,53 @@ end.join RUBY assert_not_match(/:0/, stderr, "[ruby-dev:39116]") end + + def test_errinfo + begin + raise "foo" + assert(false) + rescue => e + assert_equal(e, $!) + 1.times { assert_equal(e, $!) } + end + + assert_equal(nil, $!) + end + + def test_inspect + assert_equal("#", Exception.new.inspect) + + e = Class.new(Exception) + e.class_eval do + def to_s; ""; end + end + assert_equal(e.inspect, e.new.inspect) + end + + def test_set_backtrace + e = Exception.new + + e.set_backtrace("foo") + assert_equal(["foo"], e.backtrace) + + e.set_backtrace(%w(foo bar baz)) + assert_equal(%w(foo bar baz), e.backtrace) + + assert_raise(TypeError) { e.set_backtrace(1) } + assert_raise(TypeError) { e.set_backtrace([1]) } + end + + def test_exit_success_p + begin + exit + rescue SystemExit => e + end + assert(e.success?) + + begin + abort + rescue SystemExit => e + end + assert(!e.success?) + end end diff --git a/test/ruby/test_fnmatch.rb b/test/ruby/test_fnmatch.rb index 1c1a158477..e5f5ba6a4f 100644 --- a/test/ruby/test_fnmatch.rb +++ b/test/ruby/test_fnmatch.rb @@ -79,6 +79,8 @@ class TestFnmatch < Test::Unit::TestCase assert(File.fnmatch('cat', 'CAT', File::FNM_CASEFOLD)) assert(!File.fnmatch('[a-z]', 'D')) assert(File.fnmatch('[a-z]', 'D', File::FNM_CASEFOLD)) + assert(!File.fnmatch('[abc]', 'B')) + assert(File.fnmatch('[abc]', 'B', File::FNM_CASEFOLD)) # wildcard doesn't match '/' if FNM_PATHNAME is set assert(File.fnmatch('foo?boo', 'foo/boo')) assert(File.fnmatch('foo*', 'foo/boo')) diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index acff4821f5..d03783129f 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -875,4 +875,9 @@ class TestHash < Test::Unit::TestCase def test_hash_poped assert_nothing_raised { eval("a = 1; {a => a}; a") } end + + def test_recursive_check + h = {} + assert_raise(ArgumentError) { h[h] = :foo } + end end diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb index b3dd11d55a..840192c5ad 100644 --- a/test/ruby/test_integer.rb +++ b/test/ruby/test_integer.rb @@ -84,6 +84,12 @@ class TestInteger < Test::Unit::TestCase assert_raise(ArgumentError) { Integer("0x123", 10) } assert_raise(ArgumentError) { Integer(1234, 10) } assert_raise(ArgumentError) { Integer(12.34, 10) } + assert_raise(ArgumentError) { Integer(Object.new, 1) } + + assert_raise(ArgumentError) { Integer(1, 1, 1) } + + assert_equal(2 ** 50, Integer(2.0 ** 50)) + assert_raise(TypeError) { Integer(nil) } end def test_int_p @@ -192,9 +198,4 @@ class TestInteger < Test::Unit::TestCase assert_equal(-1111_1111_1111_1111_1111_1111_1111_1110, (-1111_1111_1111_1111_1111_1111_1111_1111).round(-1)) assert_equal(Bignum, (-1111_1111_1111_1111_1111_1111_1111_1111).round(-1).class) end - - def test_Integer2 - assert_equal(2 ** 50, Integer(2.0 ** 50)) - assert_raise(TypeError) { Integer(nil) } - end end diff --git a/test/ruby/test_method.rb b/test/ruby/test_method.rb index 08603c9981..ddeb6d6c87 100644 --- a/test/ruby/test_method.rb +++ b/test/ruby/test_method.rb @@ -179,6 +179,10 @@ class TestMethod < Test::Unit::TestCase o = Object.new o.instance_eval { define_singleton_method(:foo) { :foo } } assert_equal(:foo, o.foo) + + assert_raise(TypeError) do + Class.new.class_eval { define_method(:foo, Object.new) } + end end def test_clone diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb index 2df31d06df..990fc017b5 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -380,6 +380,25 @@ class TestModule < Test::Unit::TestCase Object.module_eval "WALTER = 99" c2 = Module.constants assert_equal([:WALTER], c2 - c1) + + assert_equal([], Module.constants(true)) + assert_equal([], Module.constants(false)) + + src = <<-INPUT + module M + WALTER = 99 + end + class Module + include M + end + p Module.constants, Module.constants(true), Module.constants(false) + INPUT + assert_in_out_err([], src) do |out, err| + assert_equal([:BasicObject, :M], eval(out[0]).sort - Module.constants) + assert_equal("[:WALTER]", out[1]) + assert_equal("[]", out[2]) + assert_equal([], err) + end end module M1 diff --git a/test/ruby/test_proc.rb b/test/ruby/test_proc.rb index de95ed5676..6b8fb974ee 100644 --- a/test/ruby/test_proc.rb +++ b/test/ruby/test_proc.rb @@ -735,6 +735,10 @@ class TestProc < Test::Unit::TestCase assert_equal([[:req, :a], [:rest, :b], [:req, :c], [:block, :d]], method(:pmo6).to_proc.parameters) assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:block, :e]], method(:pmo7).to_proc.parameters) assert_equal([[:req], [:block, :b]], method(:pma1).to_proc.parameters) + + assert_equal([], "".method(:upcase).to_proc.parameters) + assert_equal([[:rest]], "".method(:gsub).to_proc.parameters) + assert_equal([[:rest]], proc {}.curry.parameters) end def test_to_s -- cgit v1.2.3