1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Split test_frozen_error.rb

This commit is contained in:
Nobuyoshi Nakada 2019-12-31 14:28:57 +09:00
parent 8caeef7c1d
commit ee4ead8098
Notes: git 2019-12-31 19:43:59 +09:00
2 changed files with 57 additions and 50 deletions

View file

@ -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)

View file

@ -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