From 39eadca76b48fc7841da688f6745e40897ec37ff Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Sat, 6 Apr 2019 00:02:11 -0700 Subject: Add FrozenError#receiver Similar to NameError#receiver, this returns the object on which the modification was attempted. This is useful as it can pinpoint exactly what is frozen. In many cases when a FrozenError is raised, you cannot determine from the context which object is frozen that you attempted to modify. Users of the current rb_error_frozen C function will have to switch to using rb_error_frozen_object or the new rb_frozen_error_raise in order to set the receiver of the FrozenError. To allow the receiver to be set from Ruby, support an optional second argument to FrozenError#initialize. Implements [Feature #15751] --- test/ruby/test_exception.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb index cabd20c0e8..9a0de5c430 100644 --- a/test/ruby/test_exception.rb +++ b/test/ruby/test_exception.rb @@ -853,6 +853,33 @@ end.join alias inspect pretty_inspect end + def test_frozen_error_receiver + obj = Object.new.freeze + (obj.foo = 1) rescue (e = $!) + assert_same(obj, e.receiver) + obj.singleton_class.const_set(:A, 2) rescue (e = $!) + assert_same(obj.singleton_class, e.receiver) + end + + def test_frozen_error_initialize + obj = Object.new + exc = FrozenError.new("bar", 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_name_error_new_default error = NameError.new assert_equal("NameError", error.message) -- cgit v1.2.3