summaryrefslogtreecommitdiff
path: root/test/ruby/test_object.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/ruby/test_object.rb')
-rw-r--r--test/ruby/test_object.rb167
1 files changed, 115 insertions, 52 deletions
diff --git a/test/ruby/test_object.rb b/test/ruby/test_object.rb
index 5ef1497869..83208bbcdb 100644
--- a/test/ruby/test_object.rb
+++ b/test/ruby/test_object.rb
@@ -5,7 +5,6 @@ require 'test/unit'
class TestObject < Test::Unit::TestCase
def setup
@verbose = $VERBOSE
- $VERBOSE = nil
end
def teardown
@@ -18,6 +17,16 @@ class TestObject < Test::Unit::TestCase
assert_same(object, object.itself, feature6373)
end
+ def test_yield_self
+ feature = '[ruby-core:46320] [Feature #6721]'
+ object = Object.new
+ assert_same(self, object.yield_self {self}, feature)
+ assert_same(object, object.yield_self {|x| break x}, feature)
+ enum = object.yield_self
+ assert_instance_of(Enumerator, enum)
+ assert_equal(1, enum.size)
+ end
+
def test_dup
assert_equal 1, 1.dup
assert_equal true, true.dup
@@ -37,15 +46,27 @@ class TestObject < Test::Unit::TestCase
a = Object.new
def a.b; 2 end
+ c = a.clone
+ assert_equal(false, c.frozen?)
+ assert_equal(false, a.frozen?)
+ assert_equal(2, c.b)
+
+ c = a.clone(freeze: true)
+ assert_equal(true, c.frozen?)
+ assert_equal(false, a.frozen?)
+ assert_equal(2, c.b)
+
a.freeze
c = a.clone
assert_equal(true, c.frozen?)
+ assert_equal(true, a.frozen?)
assert_equal(2, c.b)
assert_raise(ArgumentError) {a.clone(freeze: [])}
d = a.clone(freeze: false)
def d.e; 3; end
assert_equal(false, d.frozen?)
+ assert_equal(true, a.frozen?)
assert_equal(2, d.b)
assert_equal(3, d.e)
@@ -61,6 +82,34 @@ class TestObject < Test::Unit::TestCase
assert_raise(ArgumentError) {true.clone(freeze: false)}
assert_raise(ArgumentError) {nil.clone(freeze: false)}
assert_raise(ArgumentError) {false.clone(freeze: false)}
+ x = EnvUtil.labeled_class("\u{1f4a9}").new
+ assert_raise_with_message(ArgumentError, /\u{1f4a9}/) do
+ Object.new.clone(freeze: x)
+ end
+
+ c = Class.new do
+ attr_reader :f
+ end
+ o = c.new
+ def o.initialize_clone(_, freeze: true)
+ @f = freeze
+ super
+ end
+ clone = o.clone
+ assert_kind_of c, clone
+ assert_equal true, clone.f
+ clone = o.clone(freeze: false)
+ assert_kind_of c, clone
+ assert_equal false, clone.f
+
+ class << o
+ remove_method(:initialize_clone)
+ end
+ def o.initialize_clone(_)
+ super
+ end
+ assert_kind_of c, o.clone
+ assert_raise(ArgumentError) { o.clone(freeze: false) }
end
def test_init_dupclone
@@ -82,17 +131,6 @@ class TestObject < Test::Unit::TestCase
assert_raise(TypeError) { 1.kind_of?(1) }
end
- def test_taint_frozen_obj
- o = Object.new
- o.freeze
- assert_raise(RuntimeError) { o.taint }
-
- o = Object.new
- o.taint
- o.freeze
- assert_raise(RuntimeError) { o.untaint }
- end
-
def test_freeze_immediate
assert_equal(true, 1.frozen?)
1.freeze
@@ -109,7 +147,7 @@ class TestObject < Test::Unit::TestCase
attr_accessor :foo
}
obj = klass.new.freeze
- assert_raise_with_message(RuntimeError, /#{name}/) {
+ assert_raise_with_message(FrozenError, /#{name}/) {
obj.foo = 1
}
end
@@ -118,6 +156,27 @@ class TestObject < Test::Unit::TestCase
assert_equal(0.0, nil.to_f)
end
+ def test_nil_to_s
+ str = nil.to_s
+ assert_equal("", str)
+ assert_predicate(str, :frozen?)
+ assert_same(str, nil.to_s)
+ end
+
+ def test_true_to_s
+ str = true.to_s
+ assert_equal("true", str)
+ assert_predicate(str, :frozen?)
+ assert_same(str, true.to_s)
+ end
+
+ def test_false_to_s
+ str = false.to_s
+ assert_equal("false", str)
+ assert_predicate(str, :frozen?)
+ assert_same(str, false.to_s)
+ end
+
def test_not
assert_equal(false, Object.new.send(:!))
assert_equal(true, nil.send(:!))
@@ -213,6 +272,14 @@ class TestObject < Test::Unit::TestCase
assert_equal([:foo], o.methods(false), bug8044)
end
+ def test_methods_prepend_singleton
+ c = Class.new(Module) {private def foo; end}
+ k = c.new
+ k.singleton_class
+ c.module_eval {prepend(Module.new)}
+ assert_equal([:foo], k.private_methods(false))
+ end
+
def test_instance_variable_get
o = Object.new
o.instance_eval { @foo = :foo }
@@ -292,6 +359,7 @@ class TestObject < Test::Unit::TestCase
o = Object.new
def o.to_s; 1; end
assert_raise(TypeError) { String(o) }
+ o.singleton_class.remove_method(:to_s)
def o.to_s; "o"; end
assert_equal("o", String(o))
def o.to_str; "O"; end
@@ -304,6 +372,7 @@ class TestObject < Test::Unit::TestCase
o = Object.new
def o.to_a; 1; end
assert_raise(TypeError) { Array(o) }
+ o.singleton_class.remove_method(:to_a)
def o.to_a; [1]; end
assert_equal([1], Array(o))
def o.to_ary; [2]; end
@@ -321,6 +390,7 @@ class TestObject < Test::Unit::TestCase
o = Object.new
def o.to_hash; {a: 1, b: 2}; end
assert_equal({a: 1, b: 2}, Hash(o))
+ o.singleton_class.remove_method(:to_hash)
def o.to_hash; 9; end
assert_raise(TypeError) { Hash(o) }
end
@@ -329,6 +399,7 @@ class TestObject < Test::Unit::TestCase
o = Object.new
def o.to_i; nil; end
assert_raise(TypeError) { Integer(o) }
+ o.singleton_class.remove_method(:to_i)
def o.to_i; 42; end
assert_equal(42, Integer(o))
def o.respond_to?(*) false; end
@@ -385,7 +456,7 @@ class TestObject < Test::Unit::TestCase
def test_remove_method
c = Class.new
c.freeze
- assert_raise(RuntimeError) do
+ assert_raise(FrozenError) do
c.instance_eval { remove_method(:foo) }
end
@@ -563,7 +634,7 @@ class TestObject < Test::Unit::TestCase
called = []
p.singleton_class.class_eval do
- define_method(:respond_to?) do |a|
+ define_method(:respond_to?) do |a, priv = false|
called << [:respond_to?, a]
false
end
@@ -706,7 +777,7 @@ class TestObject < Test::Unit::TestCase
e = assert_raise(NoMethodError) {
o.never_defined_test_no_superclass_method
}
- assert_equal(m1, e.message, bug2312)
+ assert_equal(m1.lines.first, e.message.lines.first, bug2312)
end
def test_superclass_method
@@ -751,36 +822,7 @@ class TestObject < Test::Unit::TestCase
end
end
- def test_untrusted
- verbose = $VERBOSE
- $VERBOSE = false
- begin
- obj = Object.new
- assert_equal(false, obj.untrusted?)
- assert_equal(false, obj.tainted?)
- obj.untrust
- assert_equal(true, obj.untrusted?)
- assert_equal(true, obj.tainted?)
- obj.trust
- assert_equal(false, obj.untrusted?)
- assert_equal(false, obj.tainted?)
- obj.taint
- assert_equal(true, obj.untrusted?)
- assert_equal(true, obj.tainted?)
- obj.untaint
- assert_equal(false, obj.untrusted?)
- assert_equal(false, obj.tainted?)
- ensure
- $VERBOSE = verbose
- end
- end
-
def test_to_s
- x = Object.new
- x.taint
- s = x.to_s
- assert_equal(true, s.tainted?)
-
x = eval(<<-EOS)
class ToS\u{3042}
new.to_s
@@ -789,14 +831,10 @@ class TestObject < Test::Unit::TestCase
assert_match(/\bToS\u{3042}:/, x)
name = "X".freeze
- x = Object.new.taint
+ x = Object.new
class<<x;self;end.class_eval {define_method(:to_s) {name}}
assert_same(name, x.to_s)
- assert_not_predicate(name, :tainted?)
- assert_raise(RuntimeError) {name.taint}
assert_equal("X", [x].join(""))
- assert_not_predicate(name, :tainted?)
- assert_not_predicate(eval('"X".freeze'), :tainted?)
end
def test_inspect
@@ -843,6 +881,29 @@ class TestObject < Test::Unit::TestCase
assert_match(/@\u{3046}=6\b/, x.inspect)
end
+ def test_singleton_methods
+ assert_equal([], Object.new.singleton_methods)
+ assert_equal([], Object.new.singleton_methods(false))
+ c = Class.new
+ def c.foo; end
+ assert_equal([:foo], c.singleton_methods - [:yaml_tag])
+ assert_equal([:foo], c.singleton_methods(false))
+ assert_equal([], c.singleton_class.singleton_methods(false))
+ c.singleton_class.singleton_class
+ assert_equal([], c.singleton_class.singleton_methods(false))
+
+ o = c.new.singleton_class
+ assert_equal([:foo], o.singleton_methods - [:yaml_tag])
+ assert_equal([], o.singleton_methods(false))
+ o.singleton_class
+ assert_equal([:foo], o.singleton_methods - [:yaml_tag])
+ assert_equal([], o.singleton_methods(false))
+
+ c.extend(Module.new{def bar; end})
+ assert_equal([:bar, :foo], c.singleton_methods.sort - [:yaml_tag])
+ assert_equal([:foo], c.singleton_methods(false))
+ end
+
def test_singleton_class
x = Object.new
xs = class << x; self; end
@@ -869,6 +930,7 @@ class TestObject < Test::Unit::TestCase
['ArgumentError.new("bug5473")', 'ArgumentError, "bug5473"', '"bug5473"'].each do |code|
exc = code[/\A[A-Z]\w+/] || 'RuntimeError'
assert_separately([], <<-SRC)
+ $VERBOSE = nil
class ::Object
def method_missing(m, *a, &b)
raise #{code}
@@ -885,8 +947,8 @@ class TestObject < Test::Unit::TestCase
b = yield
assert_nothing_raised("copy") {a.instance_eval {initialize_copy(b)}}
c = a.dup.freeze
- assert_raise(RuntimeError, "frozen") {c.instance_eval {initialize_copy(b)}}
- d = a.dup.trust
+ assert_raise(FrozenError, "frozen") {c.instance_eval {initialize_copy(b)}}
+ d = a.dup
[a, b, c, d]
end
@@ -909,6 +971,7 @@ class TestObject < Test::Unit::TestCase
_issue = "Bug #7539"
assert_raise_with_message(TypeError, "can't convert Array into Integer") {Integer([42])}
assert_raise_with_message(TypeError, 'no implicit conversion of Array into Integer') {[].first([42])}
+ assert_raise_with_message(TypeError, "can't convert Array into Rational") {Rational([42])}
end
def test_copied_ivar_memory_leak