summaryrefslogtreecommitdiff
path: root/test/ruby
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2025-11-22 19:51:46 -0800
committerJeremy Evans <code@jeremyevans.net>2025-12-10 07:35:50 +0900
commit76fb0d244b95a23116bfe72bb2422395c3a76477 (patch)
tree3a4c0d1fc7b600b8ca017e8b382829f679fd6a6d /test/ruby
parent1e7cf7b2bc1f9b356b2e980e1e18548618da6363 (diff)
Use actual class instead of singleton class in frozen error message
With the following code: ```ruby object = [] object.singleton_class object.freeze object.instance_variable_set(:@a, 42) ``` The previous error message was: ``` can't modify frozen #<Class:#<Array:0x00000631d1308f78>>: [] ``` With this change, the error message is: ``` can't modify frozen Array: [] ``` Since we show the inspect value of the affected object, I think including the singleton class instead of the actual class if it exists makes the error message harder to understand.
Diffstat (limited to 'test/ruby')
-rw-r--r--test/ruby/test_frozen.rb16
-rw-r--r--test/ruby/test_zjit.rb2
2 files changed, 17 insertions, 1 deletions
diff --git a/test/ruby/test_frozen.rb b/test/ruby/test_frozen.rb
index 2918a2afd8..6721cb1128 100644
--- a/test/ruby/test_frozen.rb
+++ b/test/ruby/test_frozen.rb
@@ -27,4 +27,20 @@ class TestFrozen < Test::Unit::TestCase
str.freeze
assert_raise(FrozenError) { str.instance_variable_set(:@b, 1) }
end
+
+ def test_setting_ivar_on_frozen_string_with_singleton_class
+ str = "str"
+ str.singleton_class
+ str.freeze
+ assert_raise_with_message(FrozenError, "can't modify frozen String: \"str\"") { str.instance_variable_set(:@a, 1) }
+ end
+
+ class A
+ freeze
+ end
+
+ def test_setting_ivar_on_frozen_class
+ assert_raise_with_message(FrozenError, "can't modify frozen Class: TestFrozen::A") { A.instance_variable_set(:@a, 1) }
+ assert_raise_with_message(FrozenError, "can't modify frozen Class: #<Class:TestFrozen::A>") { A.singleton_class.instance_variable_set(:@a, 1) }
+ end
end
diff --git a/test/ruby/test_zjit.rb b/test/ruby/test_zjit.rb
index 81d9404070..49b3616425 100644
--- a/test/ruby/test_zjit.rb
+++ b/test/ruby/test_zjit.rb
@@ -2057,7 +2057,7 @@ class TestZJIT < Test::Unit::TestCase
end
def test_setclassvariable_raises
- assert_compiles '"can\'t modify frozen #<Class:Foo>: Foo"', %q{
+ assert_compiles '"can\'t modify frozen Class: Foo"', %q{
class Foo
def self.test = @@x = 42
freeze