diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb index 8445f85ceb..8a60fafa5e 100644 --- a/test/ruby/test_exception.rb +++ b/test/ruby/test_exception.rb @@ -835,56 +835,6 @@ end.join alias inspect pretty_inspect end - def test_frozen_error_receiver - obj = Object.new.freeze - e = assert_raise(FrozenError) {def obj.foo; end} - assert_same(obj, e.receiver) - e = assert_raise(FrozenError) {obj.singleton_class.const_set(:A, 2)} - assert_same(obj.singleton_class, e.receiver) - end - - def test_frozen_error_initialize - obj = Object.new - exc = FrozenError.new("bar", receiver: obj) - assert_equal("bar", exc.message) - assert_same(obj, exc.receiver) - - exc = FrozenError.new("bar") - assert_equal("bar", exc.message) - assert_raise_with_message(ArgumentError, "no receiver is available") { - exc.receiver - } - - exc = FrozenError.new - assert_equal("FrozenError", exc.message) - assert_raise_with_message(ArgumentError, "no receiver is available") { - exc.receiver - } - end - - def test_frozen_error_message - obj = Object.new.freeze - e = assert_raise_with_message(FrozenError, /can't modify frozen #{obj.class}/) { - obj.instance_variable_set(:@test, true) - } - assert_include(e.message, obj.inspect) - - klass = Class.new do - def init - @x = true - end - def inspect - init - super - end - end - obj = klass.new.freeze - e = assert_raise_with_message(FrozenError, /can't modify frozen #{obj.class}/) { - obj.init - } - assert_include(e.message, klass.inspect) - end - def test_name_error_new_default error = NameError.new assert_equal("NameError", error.message) diff --git a/test/ruby/test_frozen_error.rb b/test/ruby/test_frozen_error.rb new file mode 100644 index 0000000000..ace1e4c775 --- /dev/null +++ b/test/ruby/test_frozen_error.rb @@ -0,0 +1,57 @@ +require 'test/unit' + +class TestFrozenError < Test::Unit::TestCase + def test_new_default + exc = FrozenError.new + assert_equal("FrozenError", exc.message) + assert_raise_with_message(ArgumentError, "no receiver is available") { + exc.receiver + } + end + + def test_new_message + exc = FrozenError.new("bar") + assert_equal("bar", exc.message) + assert_raise_with_message(ArgumentError, "no receiver is available") { + exc.receiver + } + end + + def test_new_receiver + obj = Object.new + exc = FrozenError.new("bar", receiver: obj) + assert_equal("bar", exc.message) + assert_same(obj, exc.receiver) + end + + def test_message + obj = Object.new.freeze + e = assert_raise_with_message(FrozenError, /can't modify frozen #{obj.class}/) { + obj.instance_variable_set(:@test, true) + } + assert_include(e.message, obj.inspect) + + klass = Class.new do + def init + @x = true + end + def inspect + init + super + end + end + obj = klass.new.freeze + e = assert_raise_with_message(FrozenError, /can't modify frozen #{obj.class}/) { + obj.init + } + assert_include(e.message, klass.inspect) + end + + def test_receiver + obj = Object.new.freeze + e = assert_raise(FrozenError) {def obj.foo; end} + assert_same(obj, e.receiver) + e = assert_raise(FrozenError) {obj.singleton_class.const_set(:A, 2)} + assert_same(obj.singleton_class, e.receiver) + end +end