summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-02-10 07:31:06 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-02-10 07:31:06 +0000
commitf3f43cdbee1703afd92f9f6dbb471642bcede9f5 (patch)
tree4f97958bcbb0220be3dd81af4fff045127369f76 /test
parent6942afef5f8eb829b85b05bbea59c9eb9214739c (diff)
* array.c (rb_ary_push_m, rb_ary_unshift_m, rb_ary_aset),
(rb_ary_insert, rb_ary_replace, rb_ary_concat), (rb_ary_uniq_bang, rb_ary_flatten_bang): check if frozen after wrong number of arguments but before TypeError. [ruby-core:28140] * hash.c (rb_hash_replace): ditto. * string.c (rb_str_replace): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26632 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r--test/ruby/test_array.rb38
-rw-r--r--test/ruby/test_hash.rb7
-rw-r--r--test/ruby/test_string.rb6
3 files changed, 46 insertions, 5 deletions
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index f05c9e4e95..a0dbef0cb8 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -539,6 +539,9 @@ class TestArray < Test::Unit::TestCase
a = @cls[1, 2, 3]
a.concat(a)
assert_equal([1, 2, 3, 1, 2, 3], a)
+
+ assert_raise(TypeError) { [0].concat(:foo) }
+ assert_raise(RuntimeError) { [0].freeze.concat(:foo) }
end
def test_count
@@ -1033,6 +1036,13 @@ class TestArray < Test::Unit::TestCase
assert_equal(@cls[4, 5, 6], a)
assert_equal(a_id, a.__id__)
assert_equal(@cls[], a.replace(@cls[]))
+
+ fa = a.dup.freeze
+ assert_nothing_raised(RuntimeError) { a.replace(a) }
+ assert_raise(RuntimeError) { fa.replace(fa) }
+ assert_raise(ArgumentError) { fa.replace() }
+ assert_raise(TypeError) { a.replace(42) }
+ assert_raise(RuntimeError) { fa.replace(42) }
end
def test_reverse
@@ -1325,6 +1335,11 @@ class TestArray < Test::Unit::TestCase
assert_equal(@cls[ "a:def", "b:abc", "c:jkl" ], c)
assert_nil(@cls[1, 2, 3].uniq!)
+
+ f = a.dup.freeze
+ assert_raise(ArgumentError) { a.uniq!(1) }
+ assert_raise(ArgumentError) { f.uniq!(1) }
+ assert_raise(RuntimeError) { f.uniq! }
end
def test_unshift
@@ -1452,7 +1467,7 @@ class TestArray < Test::Unit::TestCase
assert_equal([1, 1, 1], Array.new(3, 1) { 1 })
end
- def test_aset
+ def test_aset_error
assert_raise(IndexError) { [0][-2] = 1 }
assert_raise(IndexError) { [0][LONGP] = 2 }
assert_raise(IndexError) { [0][(LONGP + 1) / 2 - 1] = 2 }
@@ -1461,6 +1476,9 @@ class TestArray < Test::Unit::TestCase
a[2] = 4
assert_equal([0, nil, 4], a)
assert_raise(ArgumentError) { [0][0, 0, 0] = 0 }
+ assert_raise(ArgumentError) { [0].freeze[0, 0, 0] = 0 }
+ assert_raise(TypeError) { [0][:foo] = 0 }
+ assert_raise(RuntimeError) { [0].freeze[:foo] = 0 }
end
def test_first2
@@ -1477,8 +1495,9 @@ class TestArray < Test::Unit::TestCase
assert_equal([2, 3], a)
end
- def test_unshift2
- Struct.new(:a, :b, :c)
+ def test_unshift_error
+ assert_raise(RuntimeError) { [].freeze.unshift('cat') }
+ assert_raise(RuntimeError) { [].freeze.unshift() }
end
def test_aref
@@ -1537,6 +1556,8 @@ class TestArray < Test::Unit::TestCase
assert_raise(ArgumentError) { a.insert }
assert_equal([0, 1, 2], a.insert(-1, 2))
assert_equal([0, 1, 3, 2], a.insert(-2, 3))
+ assert_raise(RuntimeError) { [0].freeze.insert(0)}
+ assert_raise(ArgumentError) { [0].freeze.insert }
end
def test_join2
@@ -1628,10 +1649,17 @@ class TestArray < Test::Unit::TestCase
assert_not_equal([a, a].hash, a.hash) # Implementation dependent
end
- def test_flatten2
+ def test_flatten_error
a = []
a << a
assert_raise(ArgumentError) { a.flatten }
+
+ f = [].freeze
+ assert_raise(ArgumentError) { a.flatten!(1, 2) }
+ assert_raise(TypeError) { a.flatten!(:foo) }
+ assert_raise(ArgumentError) { f.flatten!(1, 2) }
+ assert_raise(RuntimeError) { f.flatten! }
+ assert_raise(RuntimeError) { f.flatten!(:foo) }
end
def test_shuffle
@@ -1756,7 +1784,7 @@ class TestArray < Test::Unit::TestCase
assert_equal((1..10).to_a, a)
end
- def test_slice_freezed_array
+ def test_slice_frozen_array
a = [1,2,3,4,5].freeze
assert_equal([1,2,3,4], a[0,4])
assert_equal([2,3,4,5], a[1,4])
diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb
index 5c9b08b917..6f703d947c 100644
--- a/test/ruby/test_hash.rb
+++ b/test/ruby/test_hash.rb
@@ -748,6 +748,13 @@ class TestHash < Test::Unit::TestCase
h2 = {}
h2.replace h1
assert_equal(:foo, h2[0])
+
+ assert_raise(ArgumentError) { h2.replace() }
+ assert_raise(TypeError) { h2.replace(1) }
+ h2.freeze
+ assert_raise(ArgumentError) { h2.replace() }
+ assert_raise(RuntimeError) { h2.replace(h1) }
+ assert_raise(RuntimeError) { h2.replace(42) }
end
def test_size2
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index 5d8edc1684..64205f6082 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -875,6 +875,12 @@ class TestString < Test::Unit::TestCase
s2 = ["foo"].pack("p")
s.replace(s2)
assert_equal(s2, s)
+
+ fs = "".freeze
+ assert_raise(RuntimeError) { fs.replace("a") }
+ assert_raise(RuntimeError) { fs.replace(fs) }
+ assert_raise(ArgumentError) { fs.replace() }
+ assert_raise(RuntimeError) { fs.replace(42) }
end
def test_reverse