2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2006-12-31 10:02:22 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'pp'
|
|
|
|
|
|
|
|
$m0 = Module.nesting
|
|
|
|
|
|
|
|
class TestModule < Test::Unit::TestCase
|
2008-10-05 23:46:11 -04:00
|
|
|
def _wrap_assertion
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
2008-05-16 04:08:06 -04:00
|
|
|
def assert_method_defined?(klass, mid, message="")
|
|
|
|
message = build_message(message, "#{klass}\##{mid} expected to be defined.")
|
|
|
|
_wrap_assertion do
|
|
|
|
klass.method_defined?(mid) or
|
|
|
|
raise Test::Unit::AssertionFailedError, message, caller(3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_method_not_defined?(klass, mid, message="")
|
|
|
|
message = build_message(message, "#{klass}\##{mid} expected to not be defined.")
|
|
|
|
_wrap_assertion do
|
|
|
|
klass.method_defined?(mid) and
|
|
|
|
raise Test::Unit::AssertionFailedError, message, caller(3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-14 08:52:17 -04:00
|
|
|
def setup
|
|
|
|
@verbose = $VERBOSE
|
|
|
|
$VERBOSE = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
$VERBOSE = @verbose
|
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_LT_0
|
|
|
|
assert_equal true, String < Object
|
|
|
|
assert_equal false, Object < String
|
|
|
|
assert_nil String < Array
|
|
|
|
assert_equal true, Array < Enumerable
|
|
|
|
assert_equal false, Enumerable < Array
|
|
|
|
assert_nil Proc < Comparable
|
|
|
|
assert_nil Comparable < Proc
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_GT_0
|
|
|
|
assert_equal false, String > Object
|
|
|
|
assert_equal true, Object > String
|
|
|
|
assert_nil String > Array
|
|
|
|
assert_equal false, Array > Enumerable
|
|
|
|
assert_equal true, Enumerable > Array
|
|
|
|
assert_nil Comparable > Proc
|
|
|
|
assert_nil Proc > Comparable
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_CMP_0
|
2009-09-29 01:47:21 -04:00
|
|
|
assert_equal(-1, (String <=> Object))
|
2006-12-31 10:02:22 -05:00
|
|
|
assert_equal 1, (Object <=> String)
|
|
|
|
assert_nil(Array <=> String)
|
|
|
|
end
|
|
|
|
|
|
|
|
ExpectedException = NoMethodError
|
|
|
|
|
|
|
|
# Support stuff
|
|
|
|
|
|
|
|
module Mixin
|
|
|
|
MIXIN = 1
|
|
|
|
def mixin
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module User
|
|
|
|
USER = 2
|
|
|
|
include Mixin
|
|
|
|
def user
|
|
|
|
end
|
2015-10-29 23:36:21 -04:00
|
|
|
|
|
|
|
def user2
|
|
|
|
end
|
|
|
|
protected :user2
|
|
|
|
|
|
|
|
def user3
|
|
|
|
end
|
|
|
|
private :user3
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
module Other
|
|
|
|
def other
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class AClass
|
|
|
|
def AClass.cm1
|
|
|
|
"cm1"
|
|
|
|
end
|
|
|
|
def AClass.cm2
|
|
|
|
cm1 + "cm2" + cm3
|
|
|
|
end
|
|
|
|
def AClass.cm3
|
|
|
|
"cm3"
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
private_class_method :cm1, "cm3"
|
|
|
|
|
|
|
|
def aClass
|
2009-07-26 05:30:08 -04:00
|
|
|
:aClass
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def aClass1
|
2009-07-26 05:30:08 -04:00
|
|
|
:aClass1
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def aClass2
|
2009-07-26 05:30:08 -04:00
|
|
|
:aClass2
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private :aClass1
|
|
|
|
protected :aClass2
|
|
|
|
end
|
|
|
|
|
|
|
|
class BClass < AClass
|
|
|
|
def bClass1
|
2009-07-26 05:30:08 -04:00
|
|
|
:bClass1
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def bClass2
|
2009-07-26 05:30:08 -04:00
|
|
|
:bClass2
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
def bClass3
|
2009-07-26 05:30:08 -04:00
|
|
|
:bClass3
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2010-03-29 01:59:46 -04:00
|
|
|
class CClass < BClass
|
|
|
|
def self.cClass
|
|
|
|
end
|
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
MyClass = AClass.clone
|
|
|
|
class MyClass
|
|
|
|
public_class_method :cm1
|
|
|
|
end
|
|
|
|
|
|
|
|
# -----------------------------------------------------------
|
|
|
|
|
|
|
|
def test_CMP # '<=>'
|
|
|
|
assert_equal( 0, Mixin <=> Mixin)
|
|
|
|
assert_equal(-1, User <=> Mixin)
|
|
|
|
assert_equal( 1, Mixin <=> User)
|
|
|
|
|
|
|
|
assert_equal( 0, Object <=> Object)
|
|
|
|
assert_equal(-1, String <=> Object)
|
|
|
|
assert_equal( 1, Object <=> String)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_GE # '>='
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(Mixin, :>=, User)
|
|
|
|
assert_operator(Mixin, :>=, Mixin)
|
|
|
|
assert_not_operator(User, :>=, Mixin)
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(Object, :>=, String)
|
|
|
|
assert_operator(String, :>=, String)
|
|
|
|
assert_not_operator(String, :>=, Object)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_GT # '>'
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(Mixin, :>, User)
|
|
|
|
assert_not_operator(Mixin, :>, Mixin)
|
|
|
|
assert_not_operator(User, :>, Mixin)
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(Object, :>, String)
|
|
|
|
assert_not_operator(String, :>, String)
|
|
|
|
assert_not_operator(String, :>, Object)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_LE # '<='
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(User, :<=, Mixin)
|
|
|
|
assert_operator(Mixin, :<=, Mixin)
|
|
|
|
assert_not_operator(Mixin, :<=, User)
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(String, :<=, Object)
|
|
|
|
assert_operator(String, :<=, String)
|
|
|
|
assert_not_operator(Object, :<=, String)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_LT # '<'
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(User, :<, Mixin)
|
|
|
|
assert_not_operator(Mixin, :<, Mixin)
|
|
|
|
assert_not_operator(Mixin, :<, User)
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(String, :<, Object)
|
|
|
|
assert_not_operator(String, :<, String)
|
|
|
|
assert_not_operator(Object, :<, String)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_VERY_EQUAL # '==='
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(Object, :===, self)
|
|
|
|
assert_operator(Test::Unit::TestCase, :===, self)
|
|
|
|
assert_operator(TestModule, :===, self)
|
|
|
|
assert_not_operator(String, :===, self)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_ancestors
|
|
|
|
assert_equal([User, Mixin], User.ancestors)
|
|
|
|
assert_equal([Mixin], Mixin.ancestors)
|
|
|
|
|
2011-11-29 22:32:05 -05:00
|
|
|
ancestors = Object.ancestors
|
|
|
|
mixins = ancestors - [Object, Kernel, BasicObject]
|
2011-11-30 10:19:02 -05:00
|
|
|
mixins << JSON::Ext::Generator::GeneratorMethods::String if defined?(JSON::Ext::Generator::GeneratorMethods::String)
|
2011-11-29 22:32:05 -05:00
|
|
|
assert_equal([Object, Kernel, BasicObject], ancestors - mixins)
|
|
|
|
assert_equal([String, Comparable, Object, Kernel, BasicObject], String.ancestors - mixins)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
2009-12-03 13:25:57 -05:00
|
|
|
CLASS_EVAL = 2
|
|
|
|
@@class_eval = 'b'
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_class_eval
|
|
|
|
Other.class_eval("CLASS_EVAL = 1")
|
|
|
|
assert_equal(1, Other::CLASS_EVAL)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_include(Other.constants, :CLASS_EVAL)
|
2009-12-03 13:25:57 -05:00
|
|
|
assert_equal(2, Other.class_eval { CLASS_EVAL })
|
|
|
|
|
|
|
|
Other.class_eval("@@class_eval = 'a'")
|
|
|
|
assert_equal('a', Other.class_variable_get(:@@class_eval))
|
|
|
|
assert_equal('b', Other.class_eval { @@class_eval })
|
|
|
|
|
|
|
|
Other.class_eval do
|
|
|
|
module_function
|
|
|
|
|
|
|
|
def class_eval_test
|
|
|
|
"foo"
|
|
|
|
end
|
|
|
|
end
|
2010-04-29 01:33:43 -04:00
|
|
|
assert_equal("foo", Other.class_eval_test)
|
2009-12-03 13:25:57 -05:00
|
|
|
|
2009-12-10 03:24:11 -05:00
|
|
|
assert_equal([Other], Other.class_eval { |*args| args })
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_const_defined?
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(Math, :const_defined?, :PI)
|
|
|
|
assert_operator(Math, :const_defined?, "PI")
|
|
|
|
assert_not_operator(Math, :const_defined?, :IP)
|
|
|
|
assert_not_operator(Math, :const_defined?, "IP")
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
2013-12-13 22:38:22 -05:00
|
|
|
def each_bad_constants(m, &b)
|
2012-11-06 06:42:24 -05:00
|
|
|
[
|
|
|
|
"#<Class:0x7b8b718b>",
|
|
|
|
":Object",
|
|
|
|
"",
|
|
|
|
":",
|
2012-12-22 05:26:40 -05:00
|
|
|
["String::", "[Bug #7573]"],
|
2013-10-09 09:27:51 -04:00
|
|
|
"\u3042",
|
2013-12-13 22:38:22 -05:00
|
|
|
"Name?",
|
2012-12-22 05:26:40 -05:00
|
|
|
].each do |name, msg|
|
2016-04-08 21:25:11 -04:00
|
|
|
expected = "wrong constant name %s" % name
|
2013-10-09 04:43:12 -04:00
|
|
|
msg = "#{msg}#{': ' if msg}wrong constant name #{name.dump}"
|
2013-12-13 22:38:22 -05:00
|
|
|
assert_raise_with_message(NameError, expected, "#{msg} to #{m}") do
|
|
|
|
yield name
|
|
|
|
end
|
2012-11-06 06:42:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-13 22:38:22 -05:00
|
|
|
def test_bad_constants_get
|
|
|
|
each_bad_constants("get") {|name|
|
|
|
|
Object.const_get name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_bad_constants_defined
|
|
|
|
each_bad_constants("defined?") {|name|
|
|
|
|
Object.const_defined? name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-11-06 06:42:24 -05:00
|
|
|
def test_leading_colons
|
|
|
|
assert_equal Object, AClass.const_get('::Object')
|
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_const_get
|
|
|
|
assert_equal(Math::PI, Math.const_get("PI"))
|
|
|
|
assert_equal(Math::PI, Math.const_get(:PI))
|
2013-04-07 23:05:15 -04:00
|
|
|
|
|
|
|
n = Object.new
|
|
|
|
def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "PI"; end
|
|
|
|
def n.count; @count; end
|
|
|
|
assert_equal(Math::PI, Math.const_get(n))
|
|
|
|
assert_equal(1, n.count)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
2012-10-26 17:31:23 -04:00
|
|
|
def test_nested_get
|
|
|
|
assert_equal Other, Object.const_get([self.class, Other].join('::'))
|
|
|
|
assert_equal User::USER, self.class.const_get([User, 'USER'].join('::'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nested_get_symbol
|
|
|
|
const = [self.class, Other].join('::').to_sym
|
2012-12-22 06:31:13 -05:00
|
|
|
assert_raise(NameError) {Object.const_get(const)}
|
2012-10-26 17:31:23 -04:00
|
|
|
|
2012-12-22 06:31:13 -05:00
|
|
|
const = [User, 'USER'].join('::').to_sym
|
|
|
|
assert_raise(NameError) {self.class.const_get(const)}
|
2012-10-26 17:31:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_nested_get_const_missing
|
|
|
|
classes = []
|
|
|
|
klass = Class.new {
|
|
|
|
define_singleton_method(:const_missing) { |name|
|
2015-10-26 09:43:05 -04:00
|
|
|
classes << name
|
|
|
|
klass
|
2012-10-26 17:31:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
klass.const_get("Foo::Bar::Baz")
|
|
|
|
assert_equal [:Foo, :Bar, :Baz], classes
|
|
|
|
end
|
|
|
|
|
2013-12-13 21:24:35 -05:00
|
|
|
def test_nested_get_bad_class
|
2013-10-09 04:41:13 -04:00
|
|
|
assert_raise(TypeError) do
|
2012-10-26 17:31:23 -04:00
|
|
|
self.class.const_get([User, 'USER', 'Foo'].join('::'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-13 21:25:58 -05:00
|
|
|
def test_nested_defined
|
|
|
|
assert_send([Object, :const_defined?, [self.class.name, 'Other'].join('::')])
|
|
|
|
assert_send([self.class, :const_defined?, 'User::USER'])
|
|
|
|
assert_not_send([self.class, :const_defined?, 'User::Foo'])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nested_defined_symbol
|
|
|
|
const = [self.class, Other].join('::').to_sym
|
|
|
|
assert_raise(NameError) {Object.const_defined?(const)}
|
|
|
|
|
|
|
|
const = [User, 'USER'].join('::').to_sym
|
|
|
|
assert_raise(NameError) {self.class.const_defined?(const)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nested_defined_bad_class
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
self.class.const_defined?('User::USER::Foo')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_const_set
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_not_operator(Other, :const_defined?, :KOALA)
|
2006-12-31 10:02:22 -05:00
|
|
|
Other.const_set(:KOALA, 99)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(Other, :const_defined?, :KOALA)
|
2006-12-31 10:02:22 -05:00
|
|
|
assert_equal(99, Other::KOALA)
|
|
|
|
Other.const_set("WOMBAT", "Hi")
|
|
|
|
assert_equal("Hi", Other::WOMBAT)
|
2013-04-07 23:05:15 -04:00
|
|
|
|
|
|
|
n = Object.new
|
|
|
|
def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "HOGE"; end
|
|
|
|
def n.count; @count; end
|
|
|
|
def n.count=(v); @count=v; end
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_not_operator(Other, :const_defined?, :HOGE)
|
2013-04-07 23:05:15 -04:00
|
|
|
Other.const_set(n, 999)
|
|
|
|
assert_equal(1, n.count)
|
|
|
|
n.count = 0
|
|
|
|
assert_equal(999, Other.const_get(n))
|
|
|
|
assert_equal(1, n.count)
|
|
|
|
n.count = 0
|
|
|
|
assert_equal(true, Other.const_defined?(n))
|
|
|
|
assert_equal(1, n.count)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_constants
|
2007-02-08 08:24:11 -05:00
|
|
|
assert_equal([:MIXIN], Mixin.constants)
|
|
|
|
assert_equal([:MIXIN, :USER], User.constants.sort)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
2014-02-21 06:42:03 -05:00
|
|
|
def test_self_initialize_copy
|
|
|
|
bug9535 = '[ruby-dev:47989] [Bug #9535]'
|
|
|
|
m = Module.new do
|
|
|
|
def foo
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
initialize_copy(self)
|
|
|
|
end
|
|
|
|
assert_equal(:ok, Object.new.extend(m).foo, bug9535)
|
|
|
|
end
|
|
|
|
|
2014-05-08 01:34:31 -04:00
|
|
|
def test_initialize_copy_empty
|
|
|
|
bug9813 = '[ruby-dev:48182] [Bug #9813]'
|
|
|
|
m = Module.new do
|
|
|
|
def x
|
|
|
|
end
|
|
|
|
const_set(:X, 1)
|
|
|
|
@x = 2
|
|
|
|
end
|
|
|
|
assert_equal([:x], m.instance_methods)
|
|
|
|
assert_equal([:@x], m.instance_variables)
|
|
|
|
assert_equal([:X], m.constants)
|
|
|
|
m.module_eval do
|
|
|
|
initialize_copy(Module.new)
|
|
|
|
end
|
|
|
|
assert_empty(m.instance_methods, bug9813)
|
|
|
|
assert_empty(m.instance_variables, bug9813)
|
|
|
|
assert_empty(m.constants, bug9813)
|
|
|
|
end
|
|
|
|
|
2012-05-22 16:15:28 -04:00
|
|
|
def test_dup
|
|
|
|
bug6454 = '[ruby-core:45132]'
|
|
|
|
|
|
|
|
a = Module.new
|
|
|
|
Other.const_set :BUG6454, a
|
|
|
|
b = a.dup
|
|
|
|
Other.const_set :BUG6454_dup, b
|
|
|
|
|
|
|
|
assert_equal "TestModule::Other::BUG6454_dup", b.inspect, bug6454
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dup_anonymous
|
|
|
|
bug6454 = '[ruby-core:45132]'
|
|
|
|
|
|
|
|
a = Module.new
|
|
|
|
original = a.inspect
|
|
|
|
|
|
|
|
b = a.dup
|
|
|
|
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_not_equal original, b.inspect, bug6454
|
2012-05-22 16:15:28 -04:00
|
|
|
end
|
|
|
|
|
2013-09-03 08:42:01 -04:00
|
|
|
def test_public_include
|
|
|
|
assert_nothing_raised('#8846') do
|
|
|
|
Module.new.include(Module.new { def foo; end }).instance_methods == [:foo]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-22 21:42:34 -04:00
|
|
|
def test_include_toplevel
|
|
|
|
assert_separately([], <<-EOS)
|
|
|
|
Mod = Module.new {def foo; :include_foo end}
|
|
|
|
TOPLEVEL_BINDING.eval('include Mod')
|
|
|
|
|
|
|
|
assert_equal(:include_foo, TOPLEVEL_BINDING.eval('foo'))
|
|
|
|
assert_equal([Object, Mod], Object.ancestors.slice(0, 2))
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2016-11-06 01:42:53 -04:00
|
|
|
def test_include_with_no_args
|
|
|
|
assert_raise(ArgumentError) { Module.new { include } }
|
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_included_modules
|
|
|
|
assert_equal([], Mixin.included_modules)
|
|
|
|
assert_equal([Mixin], User.included_modules)
|
2011-11-29 22:32:05 -05:00
|
|
|
|
|
|
|
mixins = Object.included_modules - [Kernel]
|
2011-11-30 10:19:02 -05:00
|
|
|
mixins << JSON::Ext::Generator::GeneratorMethods::String if defined?(JSON::Ext::Generator::GeneratorMethods::String)
|
2011-11-29 22:32:05 -05:00
|
|
|
assert_equal([Kernel], Object.included_modules - mixins)
|
|
|
|
assert_equal([Comparable, Kernel], String.included_modules - mixins)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_instance_methods
|
2016-11-14 14:27:24 -05:00
|
|
|
assert_equal([:user, :user2], User.instance_methods(false).sort)
|
2015-10-29 23:36:21 -04:00
|
|
|
assert_equal([:user, :user2, :mixin].sort, User.instance_methods(true).sort)
|
2007-02-08 08:24:11 -05:00
|
|
|
assert_equal([:mixin], Mixin.instance_methods)
|
|
|
|
assert_equal([:mixin], Mixin.instance_methods(true))
|
2010-03-29 01:59:46 -04:00
|
|
|
assert_equal([:cClass], (class << CClass; self; end).instance_methods(false))
|
|
|
|
assert_equal([], (class << BClass; self; end).instance_methods(false))
|
|
|
|
assert_equal([:cm2], (class << AClass; self; end).instance_methods(false))
|
2007-02-08 08:24:11 -05:00
|
|
|
assert_equal([:aClass, :aClass2], AClass.instance_methods(false).sort)
|
|
|
|
assert_equal([:aClass, :aClass2],
|
2006-12-31 10:02:22 -05:00
|
|
|
(AClass.instance_methods(true) - Object.instance_methods(true)).sort)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_method_defined?
|
2015-10-29 23:36:21 -04:00
|
|
|
assert !User.method_defined?(:wombat)
|
|
|
|
assert User.method_defined?(:mixin)
|
|
|
|
assert User.method_defined?(:user)
|
|
|
|
assert User.method_defined?(:user2)
|
|
|
|
assert !User.method_defined?(:user3)
|
|
|
|
|
|
|
|
assert !User.method_defined?("wombat")
|
|
|
|
assert User.method_defined?("mixin")
|
|
|
|
assert User.method_defined?("user")
|
|
|
|
assert User.method_defined?("user2")
|
|
|
|
assert !User.method_defined?("user3")
|
2008-05-16 04:08:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def module_exec_aux
|
|
|
|
Proc.new do
|
|
|
|
def dynamically_added_method_3; end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def module_exec_aux_2(&block)
|
|
|
|
User.module_exec(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_module_exec
|
|
|
|
User.module_exec do
|
|
|
|
def dynamically_added_method_1; end
|
|
|
|
end
|
|
|
|
assert_method_defined?(User, :dynamically_added_method_1)
|
|
|
|
|
|
|
|
block = Proc.new do
|
|
|
|
def dynamically_added_method_2; end
|
|
|
|
end
|
|
|
|
User.module_exec(&block)
|
|
|
|
assert_method_defined?(User, :dynamically_added_method_2)
|
|
|
|
|
|
|
|
User.module_exec(&module_exec_aux)
|
|
|
|
assert_method_defined?(User, :dynamically_added_method_3)
|
|
|
|
|
|
|
|
module_exec_aux_2 do
|
|
|
|
def dynamically_added_method_4; end
|
|
|
|
end
|
|
|
|
assert_method_defined?(User, :dynamically_added_method_4)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_module_eval
|
|
|
|
User.module_eval("MODULE_EVAL = 1")
|
|
|
|
assert_equal(1, User::MODULE_EVAL)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_include(User.constants, :MODULE_EVAL)
|
2006-12-31 10:02:22 -05:00
|
|
|
User.instance_eval("remove_const(:MODULE_EVAL)")
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_not_include(User.constants, :MODULE_EVAL)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_name
|
2016-05-01 12:15:29 -04:00
|
|
|
assert_equal("Integer", Integer.name)
|
2006-12-31 10:02:22 -05:00
|
|
|
assert_equal("TestModule::Mixin", Mixin.name)
|
|
|
|
assert_equal("TestModule::User", User.name)
|
|
|
|
end
|
|
|
|
|
2012-07-30 01:24:24 -04:00
|
|
|
def test_classpath
|
|
|
|
m = Module.new
|
|
|
|
n = Module.new
|
|
|
|
m.const_set(:N, n)
|
|
|
|
assert_nil(m.name)
|
|
|
|
assert_nil(n.name)
|
|
|
|
assert_equal([:N], m.constants)
|
|
|
|
m.module_eval("module O end")
|
2015-12-30 20:49:27 -05:00
|
|
|
assert_equal([:N, :O], m.constants.sort)
|
2012-07-30 20:45:57 -04:00
|
|
|
m.module_eval("class C; end")
|
2015-12-30 12:47:43 -05:00
|
|
|
assert_equal([:C, :N, :O], m.constants.sort)
|
2012-07-30 01:24:24 -04:00
|
|
|
assert_nil(m::N.name)
|
2012-07-30 08:00:56 -04:00
|
|
|
assert_match(/\A#<Module:.*>::O\z/, m::O.name)
|
2012-07-30 20:45:57 -04:00
|
|
|
assert_match(/\A#<Module:.*>::C\z/, m::C.name)
|
2012-07-30 01:24:24 -04:00
|
|
|
self.class.const_set(:M, m)
|
|
|
|
prefix = self.class.name + "::M::"
|
|
|
|
assert_equal(prefix+"N", m.const_get(:N).name)
|
|
|
|
assert_equal(prefix+"O", m.const_get(:O).name)
|
2012-07-30 08:00:56 -04:00
|
|
|
assert_equal(prefix+"C", m.const_get(:C).name)
|
2012-07-30 01:24:24 -04:00
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_private_class_method
|
|
|
|
assert_raise(ExpectedException) { AClass.cm1 }
|
|
|
|
assert_raise(ExpectedException) { AClass.cm3 }
|
|
|
|
assert_equal("cm1cm2cm3", AClass.cm2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_private_instance_methods
|
2007-02-08 08:24:11 -05:00
|
|
|
assert_equal([:aClass1], AClass.private_instance_methods(false))
|
|
|
|
assert_equal([:bClass2], BClass.private_instance_methods(false))
|
|
|
|
assert_equal([:aClass1, :bClass2],
|
2006-12-31 10:02:22 -05:00
|
|
|
(BClass.private_instance_methods(true) -
|
|
|
|
Object.private_instance_methods(true)).sort)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_protected_instance_methods
|
2007-02-08 08:24:11 -05:00
|
|
|
assert_equal([:aClass2], AClass.protected_instance_methods)
|
|
|
|
assert_equal([:bClass3], BClass.protected_instance_methods(false))
|
|
|
|
assert_equal([:bClass3, :aClass2].sort,
|
2006-12-31 10:02:22 -05:00
|
|
|
(BClass.protected_instance_methods(true) -
|
|
|
|
Object.protected_instance_methods(true)).sort)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_public_class_method
|
|
|
|
assert_equal("cm1", MyClass.cm1)
|
|
|
|
assert_equal("cm1cm2cm3", MyClass.cm2)
|
|
|
|
assert_raise(ExpectedException) { eval "MyClass.cm3" }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_public_instance_methods
|
2007-02-08 08:24:11 -05:00
|
|
|
assert_equal([:aClass], AClass.public_instance_methods(false))
|
|
|
|
assert_equal([:bClass1], BClass.public_instance_methods(false))
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_s_constants
|
|
|
|
c1 = Module.constants
|
|
|
|
Object.module_eval "WALTER = 99"
|
|
|
|
c2 = Module.constants
|
2007-02-08 08:24:11 -05:00
|
|
|
assert_equal([:WALTER], c2 - c1)
|
2010-01-22 10:03:32 -05:00
|
|
|
|
|
|
|
assert_equal([], Module.constants(true))
|
|
|
|
assert_equal([], Module.constants(false))
|
|
|
|
|
|
|
|
src = <<-INPUT
|
2010-01-22 10:24:22 -05:00
|
|
|
ary = Module.constants
|
2010-01-22 10:03:32 -05:00
|
|
|
module M
|
|
|
|
WALTER = 99
|
|
|
|
end
|
|
|
|
class Module
|
|
|
|
include M
|
|
|
|
end
|
2010-01-22 10:24:22 -05:00
|
|
|
p Module.constants - ary, Module.constants(true), Module.constants(false)
|
2010-01-22 10:03:32 -05:00
|
|
|
INPUT
|
2010-01-22 10:24:22 -05:00
|
|
|
assert_in_out_err([], src, %w([:M] [:WALTER] []), [])
|
2011-03-31 01:54:34 -04:00
|
|
|
|
|
|
|
klass = Class.new do
|
|
|
|
const_set(:X, 123)
|
|
|
|
end
|
|
|
|
assert_equal(false, klass.class_eval { Module.constants }.include?(:X))
|
2016-05-27 04:38:50 -04:00
|
|
|
|
|
|
|
assert_equal(false, Complex.constants(false).include?(:compatible))
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
module M1
|
|
|
|
$m1 = Module.nesting
|
|
|
|
module M2
|
|
|
|
$m2 = Module.nesting
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_s_nesting
|
|
|
|
assert_equal([], $m0)
|
|
|
|
assert_equal([TestModule::M1, TestModule], $m1)
|
|
|
|
assert_equal([TestModule::M1::M2,
|
|
|
|
TestModule::M1, TestModule], $m2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_s_new
|
|
|
|
m = Module.new
|
|
|
|
assert_instance_of(Module, m)
|
|
|
|
end
|
|
|
|
|
2008-05-14 08:52:17 -04:00
|
|
|
def test_freeze
|
2015-11-15 04:28:45 -05:00
|
|
|
m = Module.new do
|
|
|
|
def self.baz; end
|
|
|
|
def bar; end
|
|
|
|
end
|
2008-05-14 08:52:17 -04:00
|
|
|
m.freeze
|
2017-12-11 19:46:34 -05:00
|
|
|
assert_raise(FrozenError) do
|
2008-05-14 08:52:17 -04:00
|
|
|
m.module_eval do
|
|
|
|
def foo; end
|
|
|
|
end
|
|
|
|
end
|
2017-12-11 19:46:34 -05:00
|
|
|
assert_raise(FrozenError) do
|
2015-11-15 04:28:45 -05:00
|
|
|
m.__send__ :private, :bar
|
|
|
|
end
|
2017-12-11 19:46:34 -05:00
|
|
|
assert_raise(FrozenError) do
|
2015-11-15 04:28:45 -05:00
|
|
|
m.private_class_method :baz
|
|
|
|
end
|
2008-05-14 08:52:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_attr_obsoleted_flag
|
|
|
|
c = Class.new
|
|
|
|
c.class_eval do
|
|
|
|
def initialize
|
|
|
|
@foo = :foo
|
|
|
|
@bar = :bar
|
|
|
|
end
|
|
|
|
attr :foo, true
|
|
|
|
attr :bar, false
|
|
|
|
end
|
|
|
|
o = c.new
|
|
|
|
assert_equal(true, o.respond_to?(:foo))
|
|
|
|
assert_equal(true, o.respond_to?(:foo=))
|
|
|
|
assert_equal(true, o.respond_to?(:bar))
|
|
|
|
assert_equal(false, o.respond_to?(:bar=))
|
|
|
|
end
|
|
|
|
|
2011-06-30 07:12:38 -04:00
|
|
|
def test_const_get_evaled
|
2008-05-14 08:52:17 -04:00
|
|
|
c1 = Class.new
|
|
|
|
c2 = Class.new(c1)
|
|
|
|
|
|
|
|
eval("c1::Foo = :foo")
|
|
|
|
assert_equal(:foo, c1::Foo)
|
|
|
|
assert_equal(:foo, c2::Foo)
|
|
|
|
assert_equal(:foo, c2.const_get(:Foo))
|
|
|
|
assert_raise(NameError) { c2.const_get(:Foo, false) }
|
|
|
|
|
|
|
|
eval("c1::Foo = :foo")
|
|
|
|
assert_raise(NameError) { c1::Bar }
|
|
|
|
assert_raise(NameError) { c2::Bar }
|
|
|
|
assert_raise(NameError) { c2.const_get(:Bar) }
|
|
|
|
assert_raise(NameError) { c2.const_get(:Bar, false) }
|
2011-07-26 12:05:35 -04:00
|
|
|
assert_raise(NameError) { c2.const_get("Bar", false) }
|
|
|
|
assert_raise(NameError) { c2.const_get("BaR11", false) }
|
|
|
|
assert_raise(NameError) { Object.const_get("BaR11", false) }
|
2008-05-14 08:52:17 -04:00
|
|
|
|
|
|
|
c1.instance_eval do
|
|
|
|
def const_missing(x)
|
|
|
|
x
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(:Bar, c1::Bar)
|
|
|
|
assert_equal(:Bar, c2::Bar)
|
|
|
|
assert_equal(:Bar, c2.const_get(:Bar))
|
|
|
|
assert_equal(:Bar, c2.const_get(:Bar, false))
|
2011-07-26 12:05:35 -04:00
|
|
|
assert_equal(:Bar, c2.const_get("Bar"))
|
|
|
|
assert_equal(:Bar, c2.const_get("Bar", false))
|
|
|
|
|
|
|
|
v = c2.const_get("Bar11", false)
|
|
|
|
assert_equal("Bar11".to_sym, v)
|
2008-05-14 08:52:17 -04:00
|
|
|
|
|
|
|
assert_raise(NameError) { c1.const_get(:foo) }
|
|
|
|
end
|
|
|
|
|
2011-06-30 07:12:38 -04:00
|
|
|
def test_const_set_invalid_name
|
2008-05-14 08:52:17 -04:00
|
|
|
c1 = Class.new
|
2014-11-17 13:23:09 -05:00
|
|
|
assert_raise_with_message(NameError, /foo/) { c1.const_set(:foo, :foo) }
|
|
|
|
assert_raise_with_message(NameError, /bar/) { c1.const_set("bar", :foo) }
|
|
|
|
assert_raise_with_message(TypeError, /1/) { c1.const_set(1, :foo) }
|
2014-02-05 01:49:35 -05:00
|
|
|
assert_nothing_raised(NameError) { c1.const_set("X\u{3042}", :foo) }
|
|
|
|
assert_raise(NameError) { c1.const_set("X\u{3042}".encode("utf-16be"), :foo) }
|
|
|
|
assert_raise(NameError) { c1.const_set("X\u{3042}".encode("utf-16le"), :foo) }
|
|
|
|
assert_raise(NameError) { c1.const_set("X\u{3042}".encode("utf-32be"), :foo) }
|
|
|
|
assert_raise(NameError) { c1.const_set("X\u{3042}".encode("utf-32le"), :foo) }
|
2014-11-17 13:23:09 -05:00
|
|
|
cx = EnvUtil.labeled_class("X\u{3042}")
|
2016-04-08 21:25:11 -04:00
|
|
|
assert_raise_with_message(TypeError, /X\u{3042}/) { c1.const_set(cx, :foo) }
|
2008-05-14 08:52:17 -04:00
|
|
|
end
|
|
|
|
|
2011-06-30 07:12:38 -04:00
|
|
|
def test_const_get_invalid_name
|
2012-12-19 05:34:13 -05:00
|
|
|
c1 = Class.new
|
|
|
|
assert_raise(NameError) { c1.const_get(:foo) }
|
|
|
|
bug5084 = '[ruby-dev:44200]'
|
|
|
|
assert_raise(TypeError, bug5084) { c1.const_get(1) }
|
2012-12-22 10:04:57 -05:00
|
|
|
bug7574 = '[ruby-dev:46749]'
|
2013-10-09 04:43:12 -04:00
|
|
|
assert_raise_with_message(NameError, "wrong constant name \"String\\u0000\"", bug7574) {
|
|
|
|
Object.const_get("String\0")
|
|
|
|
}
|
2012-12-19 05:34:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_const_defined_invalid_name
|
2008-05-14 08:52:17 -04:00
|
|
|
c1 = Class.new
|
|
|
|
assert_raise(NameError) { c1.const_defined?(:foo) }
|
2011-07-22 23:12:28 -04:00
|
|
|
bug5084 = '[ruby-dev:44200]'
|
|
|
|
assert_raise(TypeError, bug5084) { c1.const_defined?(1) }
|
2012-12-22 10:04:57 -05:00
|
|
|
bug7574 = '[ruby-dev:46749]'
|
2013-10-09 04:43:12 -04:00
|
|
|
assert_raise_with_message(NameError, "wrong constant name \"String\\u0000\"", bug7574) {
|
|
|
|
Object.const_defined?("String\0")
|
|
|
|
}
|
2008-05-14 08:52:17 -04:00
|
|
|
end
|
|
|
|
|
2011-06-30 07:12:38 -04:00
|
|
|
def test_const_get_no_inherited
|
|
|
|
bug3422 = '[ruby-core:30719]'
|
|
|
|
assert_in_out_err([], <<-INPUT, %w[1 NameError A], [], bug3422)
|
|
|
|
BasicObject::A = 1
|
|
|
|
puts [true, false].map {|inh|
|
|
|
|
begin
|
|
|
|
Object.const_get(:A, inh)
|
|
|
|
rescue NameError => e
|
|
|
|
[e.class, e.name]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
INPUT
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_const_get_inherited
|
|
|
|
bug3423 = '[ruby-core:30720]'
|
|
|
|
assert_in_out_err([], <<-INPUT, %w[NameError A NameError A], [], bug3423)
|
|
|
|
module Foo; A = 1; end
|
|
|
|
class Object; include Foo; end
|
|
|
|
class Bar; include Foo; end
|
|
|
|
|
|
|
|
puts [Object, Bar].map {|klass|
|
|
|
|
begin
|
|
|
|
klass.const_get(:A, false)
|
|
|
|
rescue NameError => e
|
|
|
|
[e.class, e.name]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
INPUT
|
|
|
|
end
|
|
|
|
|
2011-09-01 04:31:24 -04:00
|
|
|
def test_const_in_module
|
|
|
|
bug3423 = '[ruby-core:37698]'
|
|
|
|
assert_in_out_err([], <<-INPUT, %w[ok], [], bug3423)
|
2011-09-02 01:36:49 -04:00
|
|
|
module LangModuleSpecInObject
|
|
|
|
module LangModuleTop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
include LangModuleSpecInObject
|
|
|
|
module LangModuleTop
|
|
|
|
end
|
|
|
|
puts "ok" if LangModuleSpecInObject::LangModuleTop == LangModuleTop
|
|
|
|
INPUT
|
|
|
|
|
|
|
|
bug5264 = '[ruby-core:39227]'
|
|
|
|
assert_in_out_err([], <<-'INPUT', [], [], bug5264)
|
|
|
|
class A
|
|
|
|
class X; end
|
|
|
|
end
|
|
|
|
class B < A
|
|
|
|
module X; end
|
|
|
|
end
|
|
|
|
INPUT
|
2011-09-01 04:31:24 -04:00
|
|
|
end
|
|
|
|
|
2008-10-09 09:16:07 -04:00
|
|
|
def test_class_variable_get
|
2008-05-14 08:52:17 -04:00
|
|
|
c = Class.new
|
2009-12-03 13:25:57 -05:00
|
|
|
c.class_eval('@@foo = :foo')
|
2008-05-14 08:52:17 -04:00
|
|
|
assert_equal(:foo, c.class_variable_get(:@@foo))
|
|
|
|
assert_raise(NameError) { c.class_variable_get(:@@bar) } # c.f. instance_variable_get
|
2013-09-20 02:01:57 -04:00
|
|
|
assert_raise(NameError) { c.class_variable_get(:'@@') }
|
2013-04-07 08:18:26 -04:00
|
|
|
assert_raise(NameError) { c.class_variable_get('@@') }
|
2008-05-14 08:52:17 -04:00
|
|
|
assert_raise(NameError) { c.class_variable_get(:foo) }
|
2013-04-04 17:36:38 -04:00
|
|
|
assert_raise(NameError) { c.class_variable_get("bar") }
|
|
|
|
assert_raise(TypeError) { c.class_variable_get(1) }
|
2013-04-07 23:05:15 -04:00
|
|
|
|
|
|
|
n = Object.new
|
|
|
|
def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "@@foo"; end
|
|
|
|
def n.count; @count; end
|
|
|
|
assert_equal(:foo, c.class_variable_get(n))
|
|
|
|
assert_equal(1, n.count)
|
2008-05-14 08:52:17 -04:00
|
|
|
end
|
|
|
|
|
2008-10-09 09:16:07 -04:00
|
|
|
def test_class_variable_set
|
2008-05-14 08:52:17 -04:00
|
|
|
c = Class.new
|
|
|
|
c.class_variable_set(:@@foo, :foo)
|
2009-12-03 13:25:57 -05:00
|
|
|
assert_equal(:foo, c.class_eval('@@foo'))
|
2013-09-20 02:01:57 -04:00
|
|
|
assert_raise(NameError) { c.class_variable_set(:'@@', 1) }
|
2013-04-07 08:18:26 -04:00
|
|
|
assert_raise(NameError) { c.class_variable_set('@@', 1) }
|
2008-05-14 08:52:17 -04:00
|
|
|
assert_raise(NameError) { c.class_variable_set(:foo, 1) }
|
2013-04-04 17:36:38 -04:00
|
|
|
assert_raise(NameError) { c.class_variable_set("bar", 1) }
|
|
|
|
assert_raise(TypeError) { c.class_variable_set(1, 1) }
|
2013-04-07 23:05:15 -04:00
|
|
|
|
|
|
|
n = Object.new
|
|
|
|
def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "@@foo"; end
|
|
|
|
def n.count; @count; end
|
|
|
|
c.class_variable_set(n, :bar)
|
|
|
|
assert_equal(:bar, c.class_eval('@@foo'))
|
|
|
|
assert_equal(1, n.count)
|
2008-05-14 08:52:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_class_variable_defined
|
|
|
|
c = Class.new
|
2009-12-03 13:25:57 -05:00
|
|
|
c.class_eval('@@foo = :foo')
|
2008-05-14 08:52:17 -04:00
|
|
|
assert_equal(true, c.class_variable_defined?(:@@foo))
|
|
|
|
assert_equal(false, c.class_variable_defined?(:@@bar))
|
2013-09-20 02:01:57 -04:00
|
|
|
assert_raise(NameError) { c.class_variable_defined?(:'@@') }
|
|
|
|
assert_raise(NameError) { c.class_variable_defined?('@@') }
|
2008-05-14 08:52:17 -04:00
|
|
|
assert_raise(NameError) { c.class_variable_defined?(:foo) }
|
2013-04-04 17:36:38 -04:00
|
|
|
assert_raise(NameError) { c.class_variable_defined?("bar") }
|
|
|
|
assert_raise(TypeError) { c.class_variable_defined?(1) }
|
2013-04-07 23:05:15 -04:00
|
|
|
n = Object.new
|
|
|
|
def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "@@foo"; end
|
|
|
|
def n.count; @count; end
|
|
|
|
assert_equal(true, c.class_variable_defined?(n))
|
|
|
|
assert_equal(1, n.count)
|
2008-05-14 08:52:17 -04:00
|
|
|
end
|
|
|
|
|
2008-10-09 09:16:07 -04:00
|
|
|
def test_remove_class_variable
|
|
|
|
c = Class.new
|
2009-12-03 13:25:57 -05:00
|
|
|
c.class_eval('@@foo = :foo')
|
2008-10-09 09:16:07 -04:00
|
|
|
c.class_eval { remove_class_variable(:@@foo) }
|
|
|
|
assert_equal(false, c.class_variable_defined?(:@@foo))
|
2015-05-22 08:10:52 -04:00
|
|
|
assert_raise(NameError) do
|
|
|
|
c.class_eval { remove_class_variable(:@var) }
|
|
|
|
end
|
2008-10-09 09:16:07 -04:00
|
|
|
end
|
|
|
|
|
2008-05-14 08:52:17 -04:00
|
|
|
def test_export_method
|
|
|
|
m = Module.new
|
|
|
|
assert_raise(NameError) do
|
|
|
|
m.instance_eval { public(:foo) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_attr
|
2015-05-21 02:40:27 -04:00
|
|
|
assert_in_out_err([], <<-INPUT, %w(nil))
|
2008-07-15 11:26:04 -04:00
|
|
|
$VERBOSE = true
|
|
|
|
c = Class.new
|
|
|
|
c.instance_eval do
|
|
|
|
private
|
|
|
|
attr_reader :foo
|
|
|
|
end
|
|
|
|
o = c.new
|
|
|
|
p(o.instance_eval { foo })
|
|
|
|
INPUT
|
2008-05-14 08:52:17 -04:00
|
|
|
|
|
|
|
c = Class.new
|
|
|
|
assert_raise(NameError) do
|
2009-07-26 05:30:08 -04:00
|
|
|
c.instance_eval { attr_reader :"." }
|
2008-05-14 08:52:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_undef
|
|
|
|
c = Class.new
|
|
|
|
assert_raise(NameError) do
|
|
|
|
c.instance_eval { undef_method(:foo) }
|
|
|
|
end
|
|
|
|
|
|
|
|
m = Module.new
|
|
|
|
assert_raise(NameError) do
|
|
|
|
m.instance_eval { undef_method(:foo) }
|
|
|
|
end
|
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
assert_raise(NameError) do
|
|
|
|
class << o; self; end.instance_eval { undef_method(:foo) }
|
|
|
|
end
|
|
|
|
|
2009-09-29 01:47:21 -04:00
|
|
|
%w(object_id __send__ initialize).each do |n|
|
2012-08-20 03:31:48 -04:00
|
|
|
assert_in_out_err([], <<-INPUT, [], %r"warning: undefining `#{n}' may cause serious problems$")
|
2008-07-15 11:26:04 -04:00
|
|
|
$VERBOSE = false
|
2009-09-29 01:47:21 -04:00
|
|
|
Class.new.instance_eval { undef_method(:#{n}) }
|
2008-07-15 11:26:04 -04:00
|
|
|
INPUT
|
2008-05-14 08:52:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_alias
|
|
|
|
m = Module.new
|
|
|
|
assert_raise(NameError) do
|
|
|
|
m.class_eval { alias foo bar }
|
|
|
|
end
|
|
|
|
|
2009-07-22 14:21:32 -04:00
|
|
|
assert_in_out_err([], <<-INPUT, %w(2), /discarding old foo$/)
|
2008-07-15 11:26:04 -04:00
|
|
|
$VERBOSE = true
|
|
|
|
c = Class.new
|
|
|
|
c.class_eval do
|
|
|
|
def foo; 1; end
|
|
|
|
def bar; 2; end
|
|
|
|
end
|
|
|
|
c.class_eval { alias foo bar }
|
|
|
|
p c.new.foo
|
|
|
|
INPUT
|
2008-05-14 08:52:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mod_constants
|
2008-10-05 23:49:17 -04:00
|
|
|
m = Module.new
|
|
|
|
m.const_set(:Foo, :foo)
|
|
|
|
assert_equal([:Foo], m.constants(true))
|
|
|
|
assert_equal([:Foo], m.constants(false))
|
|
|
|
m.instance_eval { remove_const(:Foo) }
|
2008-05-14 08:52:17 -04:00
|
|
|
end
|
|
|
|
|
2014-01-17 03:58:05 -05:00
|
|
|
class Bug9413
|
|
|
|
class << self
|
|
|
|
Foo = :foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_singleton_constants
|
|
|
|
bug9413 = '[ruby-core:59763] [Bug #9413]'
|
|
|
|
c = Bug9413.singleton_class
|
|
|
|
assert_include(c.constants(true), :Foo, bug9413)
|
|
|
|
assert_include(c.constants(false), :Foo, bug9413)
|
|
|
|
end
|
|
|
|
|
2014-09-18 21:45:45 -04:00
|
|
|
def test_frozen_module
|
2008-05-14 08:52:17 -04:00
|
|
|
m = Module.new
|
|
|
|
m.freeze
|
2017-12-11 19:46:34 -05:00
|
|
|
assert_raise(FrozenError) do
|
2008-05-14 08:52:17 -04:00
|
|
|
m.instance_eval { undef_method(:foo) }
|
|
|
|
end
|
2014-09-18 21:45:45 -04:00
|
|
|
end
|
2008-05-14 08:52:17 -04:00
|
|
|
|
2014-09-18 21:45:45 -04:00
|
|
|
def test_frozen_class
|
2008-05-14 08:52:17 -04:00
|
|
|
c = Class.new
|
|
|
|
c.freeze
|
2017-12-11 19:46:34 -05:00
|
|
|
assert_raise(FrozenError) do
|
2008-05-14 08:52:17 -04:00
|
|
|
c.instance_eval { undef_method(:foo) }
|
|
|
|
end
|
2014-09-18 21:45:45 -04:00
|
|
|
end
|
2008-05-14 08:52:17 -04:00
|
|
|
|
2014-09-18 21:45:45 -04:00
|
|
|
def test_frozen_singleton_class
|
|
|
|
klass = Class.new
|
|
|
|
o = klass.new
|
2008-05-14 08:52:17 -04:00
|
|
|
c = class << o; self; end
|
|
|
|
c.freeze
|
2017-12-11 19:46:34 -05:00
|
|
|
assert_raise_with_message(FrozenError, /frozen/) do
|
2008-05-14 08:52:17 -04:00
|
|
|
c.instance_eval { undef_method(:foo) }
|
|
|
|
end
|
2014-09-18 21:45:45 -04:00
|
|
|
klass.class_eval do
|
|
|
|
def self.foo
|
|
|
|
end
|
|
|
|
end
|
2008-05-14 08:52:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_method_defined
|
|
|
|
c = Class.new
|
|
|
|
c.class_eval do
|
|
|
|
def foo; end
|
|
|
|
def bar; end
|
|
|
|
def baz; end
|
|
|
|
public :foo
|
|
|
|
protected :bar
|
|
|
|
private :baz
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(true, c.public_method_defined?(:foo))
|
|
|
|
assert_equal(false, c.public_method_defined?(:bar))
|
|
|
|
assert_equal(false, c.public_method_defined?(:baz))
|
|
|
|
|
2015-10-26 09:43:19 -04:00
|
|
|
# Test if string arguments are converted to symbols
|
|
|
|
assert_equal(true, c.public_method_defined?("foo"))
|
|
|
|
assert_equal(false, c.public_method_defined?("bar"))
|
|
|
|
assert_equal(false, c.public_method_defined?("baz"))
|
|
|
|
|
2008-05-14 08:52:17 -04:00
|
|
|
assert_equal(false, c.protected_method_defined?(:foo))
|
|
|
|
assert_equal(true, c.protected_method_defined?(:bar))
|
|
|
|
assert_equal(false, c.protected_method_defined?(:baz))
|
|
|
|
|
2015-10-26 09:43:19 -04:00
|
|
|
# Test if string arguments are converted to symbols
|
|
|
|
assert_equal(false, c.protected_method_defined?("foo"))
|
|
|
|
assert_equal(true, c.protected_method_defined?("bar"))
|
|
|
|
assert_equal(false, c.protected_method_defined?("baz"))
|
|
|
|
|
2008-05-14 08:52:17 -04:00
|
|
|
assert_equal(false, c.private_method_defined?(:foo))
|
|
|
|
assert_equal(false, c.private_method_defined?(:bar))
|
|
|
|
assert_equal(true, c.private_method_defined?(:baz))
|
2015-10-26 09:43:19 -04:00
|
|
|
|
|
|
|
# Test if string arguments are converted to symbols
|
|
|
|
assert_equal(false, c.private_method_defined?("foo"))
|
|
|
|
assert_equal(false, c.private_method_defined?("bar"))
|
|
|
|
assert_equal(true, c.private_method_defined?("baz"))
|
2008-05-14 08:52:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_top_public_private
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], <<-INPUT, %w([:foo] [:bar]), [])
|
|
|
|
private
|
|
|
|
def foo; :foo; end
|
|
|
|
public
|
|
|
|
def bar; :bar; end
|
|
|
|
p self.private_methods.grep(/^foo$|^bar$/)
|
|
|
|
p self.methods.grep(/^foo$|^bar$/)
|
|
|
|
INPUT
|
2008-05-14 08:52:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_append_features
|
|
|
|
t = nil
|
|
|
|
m = Module.new
|
|
|
|
m.module_eval do
|
|
|
|
def foo; :foo; end
|
|
|
|
end
|
|
|
|
class << m; self; end.class_eval do
|
|
|
|
define_method(:append_features) do |mod|
|
|
|
|
t = mod
|
|
|
|
super(mod)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
m2 = Module.new
|
|
|
|
m2.module_eval { include(m) }
|
|
|
|
assert_equal(m2, t)
|
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
o.extend(m2)
|
|
|
|
assert_equal(true, o.respond_to?(:foo))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_append_features_raise
|
|
|
|
m = Module.new
|
|
|
|
m.module_eval do
|
|
|
|
def foo; :foo; end
|
|
|
|
end
|
|
|
|
class << m; self; end.class_eval do
|
|
|
|
define_method(:append_features) {|mod| raise }
|
|
|
|
end
|
|
|
|
|
|
|
|
m2 = Module.new
|
|
|
|
assert_raise(RuntimeError) do
|
|
|
|
m2.module_eval { include(m) }
|
|
|
|
end
|
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
o.extend(m2)
|
|
|
|
assert_equal(false, o.respond_to?(:foo))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_append_features_type_error
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
Module.new.instance_eval { append_features(1) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_included
|
|
|
|
m = Module.new
|
|
|
|
m.module_eval do
|
|
|
|
def foo; :foo; end
|
|
|
|
end
|
|
|
|
class << m; self; end.class_eval do
|
|
|
|
define_method(:included) {|mod| raise }
|
|
|
|
end
|
|
|
|
|
|
|
|
m2 = Module.new
|
|
|
|
assert_raise(RuntimeError) do
|
|
|
|
m2.module_eval { include(m) }
|
|
|
|
end
|
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
o.extend(m2)
|
|
|
|
assert_equal(true, o.respond_to?(:foo))
|
|
|
|
end
|
2008-05-21 11:31:15 -04:00
|
|
|
|
|
|
|
def test_cyclic_include
|
|
|
|
m1 = Module.new
|
|
|
|
m2 = Module.new
|
|
|
|
m1.instance_eval { include(m2) }
|
|
|
|
assert_raise(ArgumentError) do
|
|
|
|
m2.instance_eval { include(m1) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_include_p
|
|
|
|
m = Module.new
|
|
|
|
c1 = Class.new
|
|
|
|
c1.instance_eval { include(m) }
|
|
|
|
c2 = Class.new(c1)
|
|
|
|
assert_equal(true, c1.include?(m))
|
|
|
|
assert_equal(true, c2.include?(m))
|
|
|
|
assert_equal(false, m.include?(m))
|
|
|
|
end
|
* object.c (rb_obj_untrusted): new method Object#untrusted?.
(rb_obj_untrust): new method Object#untrust.
(rb_obj_trust): new method Object#trust.
* array.c, debug.c, time.c, include/ruby/ruby.h, re.c, variable.c,
string.c, io.c, dir.c, vm_method.c, struct.c, class.c, hash.c,
ruby.c, marshal.c: fixes for Object#untrusted?.
* test/ruby/test_module.rb, test/ruby/test_array.rb,
test/ruby/test_object.rb, test/ruby/test_string.rb,
test/ruby/test_marshal.rb, test/ruby/test_hash.rb: added tests for
Object#untrusted?.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-08-13 03:25:05 -04:00
|
|
|
|
2009-07-26 05:30:08 -04:00
|
|
|
def test_send
|
|
|
|
a = AClass.new
|
|
|
|
assert_equal(:aClass, a.__send__(:aClass))
|
|
|
|
assert_equal(:aClass1, a.__send__(:aClass1))
|
|
|
|
assert_equal(:aClass2, a.__send__(:aClass2))
|
|
|
|
b = BClass.new
|
|
|
|
assert_equal(:aClass, b.__send__(:aClass))
|
|
|
|
assert_equal(:aClass1, b.__send__(:aClass1))
|
|
|
|
assert_equal(:aClass2, b.__send__(:aClass2))
|
|
|
|
assert_equal(:bClass1, b.__send__(:bClass1))
|
|
|
|
assert_equal(:bClass2, b.__send__(:bClass2))
|
|
|
|
assert_equal(:bClass3, b.__send__(:bClass3))
|
|
|
|
end
|
2009-07-30 03:45:42 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_nonascii_name
|
|
|
|
c = eval("class ::C\u{df}; self; end")
|
|
|
|
assert_equal("C\u{df}", c.name, '[ruby-core:24600]')
|
|
|
|
c = eval("class C\u{df}; self; end")
|
|
|
|
assert_equal("TestModule::C\u{df}", c.name, '[ruby-core:24600]')
|
2015-02-28 01:02:06 -05:00
|
|
|
c = Module.new.module_eval("class X\u{df} < Module; self; end")
|
|
|
|
assert_match(/::X\u{df}:/, c.new.to_s)
|
2009-07-30 03:45:42 -04:00
|
|
|
end
|
2009-09-12 11:03:02 -04:00
|
|
|
|
|
|
|
def test_method_added
|
|
|
|
memo = []
|
|
|
|
mod = Module.new do
|
|
|
|
mod = self
|
|
|
|
(class << self ; self ; end).class_eval do
|
|
|
|
define_method :method_added do |sym|
|
|
|
|
memo << sym
|
|
|
|
memo << mod.instance_methods(false)
|
|
|
|
memo << (mod.instance_method(sym) rescue nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def f
|
|
|
|
end
|
2009-09-12 11:21:03 -04:00
|
|
|
alias g f
|
|
|
|
attr_reader :a
|
|
|
|
attr_writer :a
|
2009-09-12 11:03:02 -04:00
|
|
|
end
|
|
|
|
assert_equal :f, memo.shift
|
2009-09-12 11:21:03 -04:00
|
|
|
assert_equal [:f], memo.shift, '[ruby-core:25536]'
|
2009-09-12 11:03:02 -04:00
|
|
|
assert_equal mod.instance_method(:f), memo.shift
|
2009-09-12 11:21:03 -04:00
|
|
|
assert_equal :g, memo.shift
|
2015-08-04 03:41:32 -04:00
|
|
|
assert_equal [:f, :g].sort, memo.shift.sort
|
2009-09-12 11:21:03 -04:00
|
|
|
assert_equal mod.instance_method(:f), memo.shift
|
|
|
|
assert_equal :a, memo.shift
|
2015-08-04 03:41:32 -04:00
|
|
|
assert_equal [:f, :g, :a].sort, memo.shift.sort
|
2009-09-12 11:21:03 -04:00
|
|
|
assert_equal mod.instance_method(:a), memo.shift
|
|
|
|
assert_equal :a=, memo.shift
|
2015-08-04 03:41:32 -04:00
|
|
|
assert_equal [:f, :g, :a, :a=].sort, memo.shift.sort
|
2009-09-12 11:21:03 -04:00
|
|
|
assert_equal mod.instance_method(:a=), memo.shift
|
2009-09-12 11:03:02 -04:00
|
|
|
end
|
2009-09-29 02:26:46 -04:00
|
|
|
|
2011-07-16 23:17:17 -04:00
|
|
|
def test_method_undefined
|
|
|
|
added = []
|
|
|
|
undefed = []
|
|
|
|
removed = []
|
|
|
|
mod = Module.new do
|
|
|
|
mod = self
|
|
|
|
def f
|
|
|
|
end
|
|
|
|
(class << self ; self ; end).class_eval do
|
|
|
|
define_method :method_added do |sym|
|
|
|
|
added << sym
|
|
|
|
end
|
|
|
|
define_method :method_undefined do |sym|
|
|
|
|
undefed << sym
|
|
|
|
end
|
|
|
|
define_method :method_removed do |sym|
|
|
|
|
removed << sym
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_method_defined?(mod, :f)
|
|
|
|
mod.module_eval do
|
|
|
|
undef :f
|
|
|
|
end
|
|
|
|
assert_equal [], added
|
|
|
|
assert_equal [:f], undefed
|
|
|
|
assert_equal [], removed
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_method_removed
|
|
|
|
added = []
|
|
|
|
undefed = []
|
|
|
|
removed = []
|
|
|
|
mod = Module.new do
|
|
|
|
mod = self
|
|
|
|
def f
|
|
|
|
end
|
|
|
|
(class << self ; self ; end).class_eval do
|
|
|
|
define_method :method_added do |sym|
|
|
|
|
added << sym
|
|
|
|
end
|
|
|
|
define_method :method_undefined do |sym|
|
|
|
|
undefed << sym
|
|
|
|
end
|
|
|
|
define_method :method_removed do |sym|
|
|
|
|
removed << sym
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_method_defined?(mod, :f)
|
|
|
|
mod.module_eval do
|
|
|
|
remove_method :f
|
|
|
|
end
|
|
|
|
assert_equal [], added
|
|
|
|
assert_equal [], undefed
|
|
|
|
assert_equal [:f], removed
|
|
|
|
end
|
|
|
|
|
2009-09-29 02:26:46 -04:00
|
|
|
def test_method_redefinition
|
2009-09-30 00:15:46 -04:00
|
|
|
feature2155 = '[ruby-dev:39400]'
|
|
|
|
|
2009-09-29 02:59:42 -04:00
|
|
|
line = __LINE__+4
|
2009-09-29 02:26:46 -04:00
|
|
|
stderr = EnvUtil.verbose_warning do
|
|
|
|
Module.new do
|
|
|
|
def foo; end
|
|
|
|
def foo; end
|
|
|
|
end
|
|
|
|
end
|
2009-09-29 02:59:42 -04:00
|
|
|
assert_match(/:#{line}: warning: method redefined; discarding old foo/, stderr)
|
2009-09-30 00:15:46 -04:00
|
|
|
assert_match(/:#{line-1}: warning: previous definition of foo/, stderr, feature2155)
|
2009-09-29 02:26:46 -04:00
|
|
|
|
2013-03-30 00:33:28 -04:00
|
|
|
assert_warning '' do
|
2009-09-29 02:26:46 -04:00
|
|
|
Module.new do
|
|
|
|
def foo; end
|
|
|
|
alias bar foo
|
2009-09-29 02:59:42 -04:00
|
|
|
def foo; end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-30 00:33:28 -04:00
|
|
|
assert_warning '' do
|
2009-09-29 02:59:42 -04:00
|
|
|
Module.new do
|
|
|
|
def foo; end
|
|
|
|
alias bar foo
|
|
|
|
alias bar foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
line = __LINE__+4
|
|
|
|
stderr = EnvUtil.verbose_warning do
|
|
|
|
Module.new do
|
|
|
|
define_method(:foo) do end
|
|
|
|
def foo; end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_match(/:#{line}: warning: method redefined; discarding old foo/, stderr)
|
2009-09-30 00:15:46 -04:00
|
|
|
assert_match(/:#{line-1}: warning: previous definition of foo/, stderr, feature2155)
|
2009-09-29 02:59:42 -04:00
|
|
|
|
2013-03-30 00:33:28 -04:00
|
|
|
assert_warning '' do
|
2009-09-29 02:59:42 -04:00
|
|
|
Module.new do
|
|
|
|
define_method(:foo) do end
|
2009-09-29 02:26:46 -04:00
|
|
|
alias bar foo
|
2012-03-16 04:38:27 -04:00
|
|
|
alias bar foo
|
2009-09-29 02:26:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-30 00:33:28 -04:00
|
|
|
assert_warning('', '[ruby-dev:39397]') do
|
2009-09-29 02:26:46 -04:00
|
|
|
Module.new do
|
|
|
|
module_function
|
|
|
|
def foo; end
|
|
|
|
module_function :foo
|
|
|
|
end
|
|
|
|
end
|
2009-10-03 08:19:19 -04:00
|
|
|
|
2013-03-30 00:33:28 -04:00
|
|
|
assert_warning '' do
|
2009-10-03 08:19:19 -04:00
|
|
|
Module.new do
|
|
|
|
def foo; end
|
|
|
|
undef foo
|
|
|
|
end
|
|
|
|
end
|
2015-11-18 03:15:51 -05:00
|
|
|
|
|
|
|
stderr = EnvUtil.verbose_warning do
|
|
|
|
Module.new do
|
|
|
|
def foo; end
|
|
|
|
mod = self
|
|
|
|
c = Class.new do
|
|
|
|
include mod
|
|
|
|
end
|
|
|
|
c.new.foo
|
|
|
|
def foo; end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_match(/: warning: method redefined; discarding old foo/, stderr)
|
|
|
|
assert_match(/: warning: previous definition of foo/, stderr)
|
2009-09-29 02:26:46 -04:00
|
|
|
end
|
2009-11-16 02:01:44 -05:00
|
|
|
|
|
|
|
def test_protected_singleton_method
|
|
|
|
klass = Class.new
|
|
|
|
x = klass.new
|
|
|
|
class << x
|
|
|
|
protected
|
|
|
|
|
|
|
|
def foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_raise(NoMethodError) do
|
|
|
|
x.foo
|
|
|
|
end
|
|
|
|
klass.send(:define_method, :bar) do
|
|
|
|
x.foo
|
|
|
|
end
|
|
|
|
assert_nothing_raised do
|
|
|
|
x.bar
|
|
|
|
end
|
|
|
|
y = klass.new
|
|
|
|
assert_raise(NoMethodError) do
|
|
|
|
y.bar
|
|
|
|
end
|
|
|
|
end
|
2010-06-12 00:06:34 -04:00
|
|
|
|
|
|
|
def test_uninitialized_toplevel_constant
|
|
|
|
bug3123 = '[ruby-dev:40951]'
|
|
|
|
e = assert_raise(NameError) {eval("Bug3123", TOPLEVEL_BINDING)}
|
|
|
|
assert_not_match(/Object::/, e.message, bug3123)
|
|
|
|
end
|
2010-06-12 03:56:23 -04:00
|
|
|
|
|
|
|
def test_attr_inherited_visibility
|
2010-07-02 04:40:51 -04:00
|
|
|
bug3406 = '[ruby-core:30638]'
|
2010-06-12 03:56:23 -04:00
|
|
|
c = Class.new do
|
|
|
|
class << self
|
|
|
|
private
|
|
|
|
def attr_accessor(*); super; end
|
|
|
|
end
|
|
|
|
attr_accessor :x
|
|
|
|
end.new
|
|
|
|
assert_nothing_raised(bug3406) {c.x = 1}
|
|
|
|
assert_equal(1, c.x, bug3406)
|
|
|
|
end
|
2010-10-26 13:27:44 -04:00
|
|
|
|
2013-06-18 10:01:32 -04:00
|
|
|
def test_attr_writer_with_no_arguments
|
|
|
|
bug8540 = "[ruby-core:55543]"
|
|
|
|
c = Class.new do
|
|
|
|
attr_writer :foo
|
|
|
|
end
|
2014-03-05 08:27:22 -05:00
|
|
|
assert_raise(ArgumentError, bug8540) { c.new.send :foo= }
|
2013-06-18 10:01:32 -04:00
|
|
|
end
|
|
|
|
|
2010-10-26 13:27:44 -04:00
|
|
|
def test_private_constant
|
|
|
|
c = Class.new
|
|
|
|
c.const_set(:FOO, "foo")
|
|
|
|
assert_equal("foo", c::FOO)
|
|
|
|
c.private_constant(:FOO)
|
|
|
|
assert_raise(NameError) { c::FOO }
|
|
|
|
assert_equal("foo", c.class_eval("FOO"))
|
2011-01-28 12:57:34 -05:00
|
|
|
assert_equal("foo", c.const_get("FOO"))
|
2011-01-28 12:57:42 -05:00
|
|
|
$VERBOSE, verbose = nil, $VERBOSE
|
|
|
|
c.const_set(:FOO, "foo")
|
|
|
|
$VERBOSE = verbose
|
|
|
|
assert_raise(NameError) { c::FOO }
|
2016-09-08 21:07:41 -04:00
|
|
|
assert_raise_with_message(NameError, /#{c}::FOO/) do
|
|
|
|
Class.new(c)::FOO
|
|
|
|
end
|
2011-01-28 12:57:34 -05:00
|
|
|
end
|
|
|
|
|
2011-12-03 06:52:08 -05:00
|
|
|
def test_private_constant2
|
|
|
|
c = Class.new
|
|
|
|
c.const_set(:FOO, "foo")
|
|
|
|
c.const_set(:BAR, "bar")
|
|
|
|
assert_equal("foo", c::FOO)
|
|
|
|
assert_equal("bar", c::BAR)
|
|
|
|
c.private_constant(:FOO, :BAR)
|
|
|
|
assert_raise(NameError) { c::FOO }
|
|
|
|
assert_raise(NameError) { c::BAR }
|
|
|
|
assert_equal("foo", c.class_eval("FOO"))
|
|
|
|
assert_equal("bar", c.class_eval("BAR"))
|
|
|
|
end
|
|
|
|
|
2013-05-24 06:33:05 -04:00
|
|
|
def test_private_constant_with_no_args
|
|
|
|
assert_in_out_err([], <<-RUBY, [], ["-:3: warning: private_constant with no argument is just ignored"])
|
|
|
|
$-w = true
|
|
|
|
class X
|
|
|
|
private_constant
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
2011-01-28 12:57:34 -05:00
|
|
|
class PrivateClass
|
|
|
|
end
|
|
|
|
private_constant :PrivateClass
|
|
|
|
|
|
|
|
def test_define_module_under_private_constant
|
|
|
|
assert_raise(NameError) do
|
|
|
|
eval %q{class TestModule::PrivateClass; end}
|
|
|
|
end
|
|
|
|
assert_raise(NameError) do
|
|
|
|
eval %q{module TestModule::PrivateClass::TestModule; end}
|
|
|
|
end
|
|
|
|
eval %q{class PrivateClass; end}
|
|
|
|
eval %q{module PrivateClass::TestModule; end}
|
|
|
|
assert_instance_of(Module, PrivateClass::TestModule)
|
|
|
|
PrivateClass.class_eval { remove_const(:TestModule) }
|
2010-10-26 13:27:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_public_constant
|
|
|
|
c = Class.new
|
|
|
|
c.const_set(:FOO, "foo")
|
|
|
|
assert_equal("foo", c::FOO)
|
|
|
|
c.private_constant(:FOO)
|
|
|
|
assert_raise(NameError) { c::FOO }
|
|
|
|
assert_equal("foo", c.class_eval("FOO"))
|
|
|
|
c.public_constant(:FOO)
|
|
|
|
assert_equal("foo", c::FOO)
|
|
|
|
end
|
2011-01-28 12:57:48 -05:00
|
|
|
|
2015-07-30 00:20:00 -04:00
|
|
|
def test_deprecate_constant
|
|
|
|
c = Class.new
|
|
|
|
c.const_set(:FOO, "foo")
|
|
|
|
c.deprecate_constant(:FOO)
|
|
|
|
assert_warn(/deprecated/) {c::FOO}
|
2016-09-08 21:10:01 -04:00
|
|
|
assert_warn(/#{c}::FOO is deprecated/) {Class.new(c)::FOO}
|
2016-05-14 21:57:28 -04:00
|
|
|
bug12382 = '[ruby-core:75505] [Bug #12382]'
|
|
|
|
assert_warn(/deprecated/, bug12382) {c.class_eval "FOO"}
|
2015-07-30 00:20:00 -04:00
|
|
|
end
|
|
|
|
|
2018-02-10 11:54:47 -05:00
|
|
|
NIL = nil
|
|
|
|
FALSE = false
|
|
|
|
deprecate_constant(:NIL, :FALSE)
|
|
|
|
|
|
|
|
def test_deprecate_nil_constant
|
|
|
|
w = EnvUtil.verbose_warning {2.times {FALSE}}
|
|
|
|
assert_equal(1, w.scan("::FALSE").size, w)
|
|
|
|
w = EnvUtil.verbose_warning {2.times {NIL}}
|
|
|
|
assert_equal(1, w.scan("::NIL").size, w)
|
|
|
|
end
|
|
|
|
|
2011-01-28 12:57:48 -05:00
|
|
|
def test_constants_with_private_constant
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_not_include(::TestModule.constants, :PrivateClass)
|
2016-07-19 02:39:01 -04:00
|
|
|
assert_not_include(::TestModule.constants(true), :PrivateClass)
|
|
|
|
assert_not_include(::TestModule.constants(false), :PrivateClass)
|
2011-01-28 12:57:48 -05:00
|
|
|
end
|
2011-01-28 21:01:19 -05:00
|
|
|
|
|
|
|
def test_toplevel_private_constant
|
|
|
|
src = <<-INPUT
|
|
|
|
class Object
|
|
|
|
private_constant :Object
|
|
|
|
end
|
|
|
|
p Object
|
|
|
|
begin
|
|
|
|
p ::Object
|
|
|
|
rescue
|
|
|
|
p :ok
|
|
|
|
end
|
|
|
|
INPUT
|
|
|
|
assert_in_out_err([], src, %w(Object :ok), [])
|
|
|
|
end
|
2011-03-30 01:38:50 -04:00
|
|
|
|
2011-12-08 09:47:19 -05:00
|
|
|
def test_private_constants_clear_inlinecache
|
|
|
|
bug5702 = '[ruby-dev:44929]'
|
|
|
|
src = <<-INPUT
|
|
|
|
class A
|
|
|
|
C = :Const
|
|
|
|
def self.get_C
|
|
|
|
A::C
|
|
|
|
end
|
|
|
|
# fill cache
|
|
|
|
A.get_C
|
|
|
|
private_constant :C, :D rescue nil
|
|
|
|
begin
|
|
|
|
A.get_C
|
|
|
|
rescue NameError
|
|
|
|
puts "A.get_C"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
INPUT
|
|
|
|
assert_in_out_err([], src, %w(A.get_C), [], bug5702)
|
|
|
|
end
|
|
|
|
|
2011-03-30 01:38:50 -04:00
|
|
|
def test_constant_lookup_in_method_defined_by_class_eval
|
2011-03-31 03:52:40 -04:00
|
|
|
src = <<-INPUT
|
|
|
|
class A
|
|
|
|
B = 42
|
2011-03-30 01:38:50 -04:00
|
|
|
end
|
|
|
|
|
2011-03-31 03:52:40 -04:00
|
|
|
A.class_eval do
|
|
|
|
def self.f
|
|
|
|
B
|
|
|
|
end
|
|
|
|
|
|
|
|
def f
|
|
|
|
B
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
A.f
|
|
|
|
rescue NameError
|
|
|
|
puts "A.f"
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
A.new.f
|
|
|
|
rescue NameError
|
|
|
|
puts "A.new.f"
|
|
|
|
end
|
|
|
|
INPUT
|
|
|
|
assert_in_out_err([], src, %w(A.f A.new.f), [])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_constant_lookup_in_toplevel_class_eval
|
|
|
|
src = <<-INPUT
|
|
|
|
module X
|
|
|
|
A = 123
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
X.class_eval { A }
|
|
|
|
rescue NameError => e
|
|
|
|
puts e
|
|
|
|
end
|
|
|
|
INPUT
|
|
|
|
assert_in_out_err([], src, ["uninitialized constant A"], [])
|
2011-03-30 01:38:50 -04:00
|
|
|
end
|
2011-03-31 05:07:42 -04:00
|
|
|
|
|
|
|
def test_constant_lookup_in_module_in_class_eval
|
|
|
|
src = <<-INPUT
|
|
|
|
class A
|
|
|
|
B = 42
|
|
|
|
end
|
|
|
|
|
|
|
|
A.class_eval do
|
|
|
|
module C
|
|
|
|
begin
|
|
|
|
B
|
|
|
|
rescue NameError
|
|
|
|
puts "NameError"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
INPUT
|
|
|
|
assert_in_out_err([], src, ["NameError"], [])
|
|
|
|
end
|
2012-06-27 03:48:50 -04:00
|
|
|
|
|
|
|
module M0
|
|
|
|
def m1; [:M0] end
|
|
|
|
end
|
|
|
|
module M1
|
2012-07-02 04:06:37 -04:00
|
|
|
def m1; [:M1, *super] end
|
2012-06-27 03:48:50 -04:00
|
|
|
end
|
|
|
|
module M2
|
2012-07-02 04:06:37 -04:00
|
|
|
def m1; [:M2, *super] end
|
2012-06-27 03:48:50 -04:00
|
|
|
end
|
|
|
|
M3 = Module.new do
|
2012-07-02 04:06:37 -04:00
|
|
|
def m1; [:M3, *super] end
|
2012-06-27 03:48:50 -04:00
|
|
|
end
|
|
|
|
module M4
|
2012-07-02 04:06:37 -04:00
|
|
|
def m1; [:M4, *super] end
|
2012-06-27 03:48:50 -04:00
|
|
|
end
|
2012-07-02 04:06:37 -04:00
|
|
|
class C
|
|
|
|
def m1; end
|
|
|
|
end
|
|
|
|
class C0 < C
|
2012-06-27 03:48:50 -04:00
|
|
|
include M0
|
|
|
|
prepend M1
|
2012-07-02 04:06:37 -04:00
|
|
|
def m1; [:C0, *super] end
|
2012-06-27 03:48:50 -04:00
|
|
|
end
|
|
|
|
class C1 < C0
|
|
|
|
prepend M2, M3
|
|
|
|
include M4
|
2012-07-02 04:06:37 -04:00
|
|
|
def m1; [:C1, *super] end
|
2012-06-27 03:48:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_prepend
|
2012-07-02 04:06:37 -04:00
|
|
|
obj = C0.new
|
|
|
|
expected = [:M1,:C0,:M0]
|
|
|
|
assert_equal(expected, obj.m1)
|
2012-06-27 03:48:50 -04:00
|
|
|
obj = C1.new
|
2012-07-02 04:06:37 -04:00
|
|
|
expected = [:M2,:M3,:C1,:M4,:M1,:C0,:M0]
|
2012-06-27 03:48:50 -04:00
|
|
|
assert_equal(expected, obj.m1)
|
|
|
|
end
|
2012-06-27 08:01:39 -04:00
|
|
|
|
2013-09-03 08:42:01 -04:00
|
|
|
def test_public_prepend
|
|
|
|
assert_nothing_raised('#8846') do
|
|
|
|
Class.new.prepend(Module.new)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-29 19:58:58 -05:00
|
|
|
def test_prepend_CMP
|
|
|
|
bug11878 = '[ruby-core:72493] [Bug #11878]'
|
|
|
|
assert_equal(-1, C1 <=> M2)
|
|
|
|
assert_equal(+1, M2 <=> C1, bug11878)
|
|
|
|
end
|
|
|
|
|
2012-06-27 08:31:17 -04:00
|
|
|
def test_prepend_inheritance
|
|
|
|
bug6654 = '[ruby-core:45914]'
|
2012-06-27 17:12:46 -04:00
|
|
|
a = labeled_module("a")
|
|
|
|
b = labeled_module("b") {include a}
|
|
|
|
c = labeled_class("c") {prepend b}
|
2012-06-27 08:31:17 -04:00
|
|
|
assert_operator(c, :<, b, bug6654)
|
|
|
|
assert_operator(c, :<, a, bug6654)
|
2013-05-02 10:23:56 -04:00
|
|
|
bug8357 = '[ruby-core:54736] [Bug #8357]'
|
|
|
|
b = labeled_module("b") {prepend a}
|
|
|
|
c = labeled_class("c") {include b}
|
|
|
|
assert_operator(c, :<, b, bug8357)
|
|
|
|
assert_operator(c, :<, a, bug8357)
|
|
|
|
bug8357 = '[ruby-core:54742] [Bug #8357]'
|
|
|
|
assert_kind_of(b, c.new, bug8357)
|
2012-06-27 08:31:17 -04:00
|
|
|
end
|
|
|
|
|
2012-06-27 08:01:39 -04:00
|
|
|
def test_prepend_instance_methods
|
|
|
|
bug6655 = '[ruby-core:45915]'
|
|
|
|
assert_equal(Object.instance_methods, Class.new {prepend Module.new}.instance_methods, bug6655)
|
|
|
|
end
|
2012-06-27 09:40:34 -04:00
|
|
|
|
|
|
|
def test_prepend_singleton_methods
|
|
|
|
o = Object.new
|
|
|
|
o.singleton_class.class_eval {prepend Module.new}
|
|
|
|
assert_equal([], o.singleton_methods)
|
|
|
|
end
|
2012-06-27 09:40:37 -04:00
|
|
|
|
|
|
|
def test_prepend_remove_method
|
2013-02-13 20:30:47 -05:00
|
|
|
c = Class.new do
|
|
|
|
prepend Module.new {def foo; end}
|
|
|
|
end
|
2012-06-27 09:40:37 -04:00
|
|
|
assert_raise(NameError) do
|
2013-02-13 20:30:47 -05:00
|
|
|
c.class_eval do
|
|
|
|
remove_method(:foo)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
c.class_eval do
|
|
|
|
def foo; end
|
|
|
|
end
|
|
|
|
removed = nil
|
|
|
|
c.singleton_class.class_eval do
|
|
|
|
define_method(:method_removed) {|id| removed = id}
|
|
|
|
end
|
2013-02-13 20:38:07 -05:00
|
|
|
assert_nothing_raised(NoMethodError, NameError, '[Bug #7843]') do
|
2013-02-13 20:30:47 -05:00
|
|
|
c.class_eval do
|
2012-06-27 09:40:37 -04:00
|
|
|
remove_method(:foo)
|
|
|
|
end
|
|
|
|
end
|
2013-02-13 20:30:47 -05:00
|
|
|
assert_equal(:foo, removed)
|
2012-06-27 09:40:37 -04:00
|
|
|
end
|
2012-06-27 17:12:46 -04:00
|
|
|
|
|
|
|
def test_prepend_class_ancestors
|
|
|
|
bug6658 = '[ruby-core:45919]'
|
|
|
|
m = labeled_module("m")
|
|
|
|
c = labeled_class("c") {prepend m}
|
|
|
|
assert_equal([m, c], c.ancestors[0, 2], bug6658)
|
2012-07-02 04:06:37 -04:00
|
|
|
|
|
|
|
bug6662 = '[ruby-dev:45868]'
|
|
|
|
c2 = labeled_class("c2", c)
|
|
|
|
anc = c2.ancestors
|
|
|
|
assert_equal([c2, m, c, Object], anc[0..anc.index(Object)], bug6662)
|
2012-06-27 17:12:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_prepend_module_ancestors
|
|
|
|
bug6659 = '[ruby-dev:45861]'
|
2012-07-02 04:06:37 -04:00
|
|
|
m0 = labeled_module("m0") {def x; [:m0, *super] end}
|
|
|
|
m1 = labeled_module("m1") {def x; [:m1, *super] end; prepend m0}
|
|
|
|
m2 = labeled_module("m2") {def x; [:m2, *super] end; prepend m1}
|
|
|
|
c0 = labeled_class("c0") {def x; [:c0] end}
|
|
|
|
c1 = labeled_class("c1") {def x; [:c1] end; prepend m2}
|
|
|
|
c2 = labeled_class("c2", c0) {def x; [:c2, *super] end; include m2}
|
|
|
|
|
2012-06-27 17:12:46 -04:00
|
|
|
assert_equal([m0, m1], m1.ancestors, bug6659)
|
2012-07-02 04:06:37 -04:00
|
|
|
|
|
|
|
bug6662 = '[ruby-dev:45868]'
|
|
|
|
assert_equal([m0, m1, m2], m2.ancestors, bug6662)
|
|
|
|
assert_equal([m0, m1, m2, c1], c1.ancestors[0, 4], bug6662)
|
|
|
|
assert_equal([:m0, :m1, :m2, :c1], c1.new.x)
|
|
|
|
assert_equal([c2, m0, m1, m2, c0], c2.ancestors[0, 5], bug6662)
|
|
|
|
assert_equal([:c2, :m0, :m1, :m2, :c0], c2.new.x)
|
2012-07-31 11:33:23 -04:00
|
|
|
|
|
|
|
m3 = labeled_module("m3") {include m1; prepend m1}
|
|
|
|
assert_equal([m3, m0, m1], m3.ancestors)
|
|
|
|
m3 = labeled_module("m3") {prepend m1; include m1}
|
|
|
|
assert_equal([m0, m1, m3], m3.ancestors)
|
|
|
|
m3 = labeled_module("m3") {prepend m1; prepend m1}
|
|
|
|
assert_equal([m0, m1, m3], m3.ancestors)
|
|
|
|
m3 = labeled_module("m3") {include m1; include m1}
|
|
|
|
assert_equal([m3, m0, m1], m3.ancestors)
|
2012-06-27 17:12:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def labeled_module(name, &block)
|
2014-02-25 23:26:09 -05:00
|
|
|
EnvUtil.labeled_module(name, &block)
|
2012-06-27 17:12:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def labeled_class(name, superclass = Object, &block)
|
2014-02-25 23:26:09 -05:00
|
|
|
EnvUtil.labeled_class(name, superclass, &block)
|
2012-06-27 17:12:46 -04:00
|
|
|
end
|
2012-06-27 20:39:52 -04:00
|
|
|
|
|
|
|
def test_prepend_instance_methods_false
|
|
|
|
bug6660 = '[ruby-dev:45863]'
|
|
|
|
assert_equal([:m1], Class.new{ prepend Module.new; def m1; end }.instance_methods(false), bug6660)
|
|
|
|
assert_equal([:m1], Class.new(Class.new{def m2;end}){ prepend Module.new; def m1; end }.instance_methods(false), bug6660)
|
|
|
|
end
|
2012-07-19 02:41:47 -04:00
|
|
|
|
2013-02-13 21:08:31 -05:00
|
|
|
def test_cyclic_prepend
|
|
|
|
bug7841 = '[ruby-core:52205] [Bug #7841]'
|
|
|
|
m1 = Module.new
|
|
|
|
m2 = Module.new
|
|
|
|
m1.instance_eval { prepend(m2) }
|
|
|
|
assert_raise(ArgumentError, bug7841) do
|
|
|
|
m2.instance_eval { prepend(m1) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-05 07:36:45 -05:00
|
|
|
def test_prepend_optmethod
|
|
|
|
bug7983 = '[ruby-dev:47124] [Bug #7983]'
|
|
|
|
assert_separately [], %{
|
|
|
|
module M
|
|
|
|
def /(other)
|
|
|
|
to_f / other
|
|
|
|
end
|
|
|
|
end
|
Use Integer instead of Fixnum and Bignum.
* object.c, numeric.c, enum.c, ext/-test-/bignum/mul.c,
lib/rexml/quickpath.rb, lib/rexml/text.rb, lib/rexml/xpath_parser.rb,
lib/rubygems/specification.rb, lib/uri/generic.rb,
bootstraptest/test_eval.rb, basictest/test.rb,
test/-ext-/bignum/test_big2str.rb, test/-ext-/bignum/test_div.rb,
test/-ext-/bignum/test_mul.rb, test/-ext-/bignum/test_str2big.rb,
test/csv/test_data_converters.rb, test/date/test_date.rb,
test/json/test_json_generate.rb, test/minitest/test_minitest_mock.rb,
test/openssl/test_cipher.rb, test/rexml/test_jaxen.rb,
test/ruby/test_array.rb, test/ruby/test_basicinstructions.rb,
test/ruby/test_bignum.rb, test/ruby/test_case.rb,
test/ruby/test_class.rb, test/ruby/test_complex.rb,
test/ruby/test_enum.rb, test/ruby/test_eval.rb,
test/ruby/test_iseq.rb, test/ruby/test_literal.rb,
test/ruby/test_math.rb, test/ruby/test_module.rb,
test/ruby/test_numeric.rb, test/ruby/test_range.rb,
test/ruby/test_rational.rb, test/ruby/test_refinement.rb,
test/ruby/test_rubyvm.rb, test/ruby/test_struct.rb,
test/ruby/test_variable.rb, test/rubygems/test_gem_specification.rb,
test/thread/test_queue.rb: Use Integer instead of Fixnum and Bignum.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55029 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-17 09:15:57 -04:00
|
|
|
Integer.send(:prepend, M)
|
2013-03-05 07:36:45 -05:00
|
|
|
assert_equal(0.5, 1 / 2, "#{bug7983}")
|
|
|
|
}
|
|
|
|
assert_equal(0, 1 / 2)
|
|
|
|
end
|
|
|
|
|
2015-12-17 17:43:35 -05:00
|
|
|
def test_redefine_optmethod_after_prepend
|
|
|
|
bug11826 = '[ruby-core:72188] [Bug #11826]'
|
|
|
|
assert_separately [], %{
|
|
|
|
module M
|
|
|
|
end
|
Use Integer instead of Fixnum and Bignum.
* object.c, numeric.c, enum.c, ext/-test-/bignum/mul.c,
lib/rexml/quickpath.rb, lib/rexml/text.rb, lib/rexml/xpath_parser.rb,
lib/rubygems/specification.rb, lib/uri/generic.rb,
bootstraptest/test_eval.rb, basictest/test.rb,
test/-ext-/bignum/test_big2str.rb, test/-ext-/bignum/test_div.rb,
test/-ext-/bignum/test_mul.rb, test/-ext-/bignum/test_str2big.rb,
test/csv/test_data_converters.rb, test/date/test_date.rb,
test/json/test_json_generate.rb, test/minitest/test_minitest_mock.rb,
test/openssl/test_cipher.rb, test/rexml/test_jaxen.rb,
test/ruby/test_array.rb, test/ruby/test_basicinstructions.rb,
test/ruby/test_bignum.rb, test/ruby/test_case.rb,
test/ruby/test_class.rb, test/ruby/test_complex.rb,
test/ruby/test_enum.rb, test/ruby/test_eval.rb,
test/ruby/test_iseq.rb, test/ruby/test_literal.rb,
test/ruby/test_math.rb, test/ruby/test_module.rb,
test/ruby/test_numeric.rb, test/ruby/test_range.rb,
test/ruby/test_rational.rb, test/ruby/test_refinement.rb,
test/ruby/test_rubyvm.rb, test/ruby/test_struct.rb,
test/ruby/test_variable.rb, test/rubygems/test_gem_specification.rb,
test/thread/test_queue.rb: Use Integer instead of Fixnum and Bignum.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55029 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-17 09:15:57 -04:00
|
|
|
class Integer
|
2015-12-17 17:43:35 -05:00
|
|
|
prepend M
|
|
|
|
def /(other)
|
|
|
|
quo(other)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal(1 / 2r, 1 / 2, "#{bug11826}")
|
|
|
|
}, ignore_stderr: true
|
|
|
|
assert_equal(0, 1 / 2)
|
|
|
|
end
|
|
|
|
|
2015-12-17 21:32:17 -05:00
|
|
|
def test_override_optmethod_after_prepend
|
|
|
|
bug11836 = '[ruby-core:72226] [Bug #11836]'
|
|
|
|
assert_separately [], %{
|
|
|
|
module M
|
|
|
|
end
|
Use Integer instead of Fixnum and Bignum.
* object.c, numeric.c, enum.c, ext/-test-/bignum/mul.c,
lib/rexml/quickpath.rb, lib/rexml/text.rb, lib/rexml/xpath_parser.rb,
lib/rubygems/specification.rb, lib/uri/generic.rb,
bootstraptest/test_eval.rb, basictest/test.rb,
test/-ext-/bignum/test_big2str.rb, test/-ext-/bignum/test_div.rb,
test/-ext-/bignum/test_mul.rb, test/-ext-/bignum/test_str2big.rb,
test/csv/test_data_converters.rb, test/date/test_date.rb,
test/json/test_json_generate.rb, test/minitest/test_minitest_mock.rb,
test/openssl/test_cipher.rb, test/rexml/test_jaxen.rb,
test/ruby/test_array.rb, test/ruby/test_basicinstructions.rb,
test/ruby/test_bignum.rb, test/ruby/test_case.rb,
test/ruby/test_class.rb, test/ruby/test_complex.rb,
test/ruby/test_enum.rb, test/ruby/test_eval.rb,
test/ruby/test_iseq.rb, test/ruby/test_literal.rb,
test/ruby/test_math.rb, test/ruby/test_module.rb,
test/ruby/test_numeric.rb, test/ruby/test_range.rb,
test/ruby/test_rational.rb, test/ruby/test_refinement.rb,
test/ruby/test_rubyvm.rb, test/ruby/test_struct.rb,
test/ruby/test_variable.rb, test/rubygems/test_gem_specification.rb,
test/thread/test_queue.rb: Use Integer instead of Fixnum and Bignum.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55029 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-17 09:15:57 -04:00
|
|
|
class Integer
|
2015-12-17 21:32:17 -05:00
|
|
|
prepend M
|
|
|
|
end
|
|
|
|
module M
|
|
|
|
def /(other)
|
|
|
|
quo(other)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal(1 / 2r, 1 / 2, "#{bug11836}")
|
|
|
|
}, ignore_stderr: true
|
|
|
|
assert_equal(0, 1 / 2)
|
|
|
|
end
|
|
|
|
|
2013-03-13 01:51:32 -04:00
|
|
|
def test_prepend_visibility
|
|
|
|
bug8005 = '[ruby-core:53106] [Bug #8005]'
|
|
|
|
c = Class.new do
|
|
|
|
prepend Module.new {}
|
|
|
|
def foo() end
|
|
|
|
protected :foo
|
|
|
|
end
|
|
|
|
a = c.new
|
2013-04-07 23:08:03 -04:00
|
|
|
assert_respond_to a, [:foo, true], bug8005
|
|
|
|
assert_nothing_raised(NoMethodError, bug8005) {a.send :foo}
|
2013-03-13 01:51:32 -04:00
|
|
|
end
|
|
|
|
|
2013-08-29 04:03:23 -04:00
|
|
|
def test_prepend_visibility_inherited
|
|
|
|
bug8238 = '[ruby-core:54105] [Bug #8238]'
|
2014-07-20 18:42:47 -04:00
|
|
|
assert_separately [], <<-"end;", timeout: 20
|
2013-08-29 04:03:23 -04:00
|
|
|
class A
|
|
|
|
def foo() A; end
|
|
|
|
private :foo
|
|
|
|
end
|
|
|
|
class B < A
|
|
|
|
public :foo
|
|
|
|
prepend Module.new
|
|
|
|
end
|
|
|
|
assert_equal(A, B.new.foo, "#{bug8238}")
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
2013-05-08 09:47:11 -04:00
|
|
|
def test_prepend_included_modules
|
|
|
|
bug8025 = '[ruby-core:53158] [Bug #8025]'
|
|
|
|
mixin = labeled_module("mixin")
|
|
|
|
c = labeled_module("c") {prepend mixin}
|
2013-05-08 12:05:50 -04:00
|
|
|
im = c.included_modules
|
|
|
|
assert_not_include(im, c, bug8025)
|
|
|
|
assert_include(im, mixin, bug8025)
|
|
|
|
c1 = labeled_class("c1") {prepend mixin}
|
|
|
|
c2 = labeled_class("c2", c1)
|
|
|
|
im = c2.included_modules
|
|
|
|
assert_not_include(im, c1, bug8025)
|
|
|
|
assert_not_include(im, c2, bug8025)
|
|
|
|
assert_include(im, mixin, bug8025)
|
2013-05-08 09:47:11 -04:00
|
|
|
end
|
|
|
|
|
2013-12-13 08:29:23 -05:00
|
|
|
def test_prepend_super_in_alias
|
|
|
|
bug7842 = '[Bug #7842]'
|
|
|
|
|
|
|
|
p = labeled_module("P") do
|
|
|
|
def m; "P"+super; end
|
|
|
|
end
|
|
|
|
a = labeled_class("A") do
|
|
|
|
def m; "A"; end
|
|
|
|
end
|
|
|
|
b = labeled_class("B", a) do
|
|
|
|
def m; "B"+super; end
|
|
|
|
alias m2 m
|
|
|
|
prepend p
|
|
|
|
alias m3 m
|
|
|
|
end
|
|
|
|
assert_equal("BA", b.new.m2, bug7842)
|
|
|
|
assert_equal("PBA", b.new.m3, bug7842)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_include_super_in_alias
|
|
|
|
bug9236 = '[Bug #9236]'
|
|
|
|
|
|
|
|
fun = labeled_module("Fun") do
|
|
|
|
def hello
|
|
|
|
orig_hello
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
m1 = labeled_module("M1") do
|
|
|
|
def hello
|
|
|
|
'hello!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
m2 = labeled_module("M2") do
|
|
|
|
def hello
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
foo = labeled_class("Foo") do
|
|
|
|
include m1
|
|
|
|
include m2
|
|
|
|
|
|
|
|
alias orig_hello hello
|
|
|
|
include fun
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal('hello!', foo.new.hello, bug9236)
|
|
|
|
end
|
|
|
|
|
2015-01-19 20:58:52 -05:00
|
|
|
def test_prepend_each_classes
|
2015-01-19 08:08:28 -05:00
|
|
|
m = labeled_module("M")
|
2015-01-19 20:58:52 -05:00
|
|
|
c1 = labeled_class("C1") {prepend m}
|
|
|
|
c2 = labeled_class("C2", c1) {prepend m}
|
|
|
|
assert_equal([m, c2, m, c1], c2.ancestors[0, 4], "should be able to prepend each classes")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_prepend_no_duplication
|
|
|
|
m = labeled_module("M")
|
|
|
|
c = labeled_class("C") {prepend m; prepend m}
|
|
|
|
assert_equal([m, c], c.ancestors[0, 2], "should never duplicate")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_prepend_in_superclass
|
|
|
|
m = labeled_module("M")
|
|
|
|
c1 = labeled_class("C1")
|
|
|
|
c2 = labeled_class("C2", c1) {prepend m}
|
|
|
|
c1.class_eval {prepend m}
|
|
|
|
assert_equal([m, c2, m, c1], c2.ancestors[0, 4], "should accesisble prepended module in superclass")
|
2015-01-19 08:08:28 -05:00
|
|
|
end
|
|
|
|
|
2015-03-05 20:31:03 -05:00
|
|
|
def test_prepend_call_super
|
|
|
|
assert_separately([], <<-'end;') #do
|
|
|
|
bug10847 = '[ruby-core:68093] [Bug #10847]'
|
|
|
|
module M; end
|
|
|
|
Float.prepend M
|
|
|
|
assert_nothing_raised(SystemStackError, bug10847) do
|
|
|
|
0.3.numerator
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
2016-11-06 01:42:53 -04:00
|
|
|
def test_prepend_module_with_no_args
|
|
|
|
assert_raise(ArgumentError) { Module.new { prepend } }
|
|
|
|
end
|
|
|
|
|
2018-03-12 02:54:43 -04:00
|
|
|
def test_prepend_private_super
|
|
|
|
wrapper = Module.new do
|
|
|
|
def wrapped
|
|
|
|
super + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
klass = Class.new do
|
|
|
|
prepend wrapper
|
|
|
|
|
|
|
|
def wrapped
|
|
|
|
1
|
|
|
|
end
|
|
|
|
private :wrapped
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(2, klass.new.wrapped)
|
|
|
|
end
|
|
|
|
|
2012-07-19 02:41:47 -04:00
|
|
|
def test_class_variables
|
|
|
|
m = Module.new
|
|
|
|
m.class_variable_set(:@@foo, 1)
|
|
|
|
m2 = Module.new
|
|
|
|
m2.send(:include, m)
|
|
|
|
m2.class_variable_set(:@@bar, 2)
|
|
|
|
assert_equal([:@@foo], m.class_variables)
|
2015-06-02 13:32:50 -04:00
|
|
|
assert_equal([:@@bar, :@@foo], m2.class_variables.sort)
|
|
|
|
assert_equal([:@@bar, :@@foo], m2.class_variables(true).sort)
|
2012-07-19 02:41:47 -04:00
|
|
|
assert_equal([:@@bar], m2.class_variables(false))
|
|
|
|
end
|
2012-08-20 07:36:34 -04:00
|
|
|
|
|
|
|
Bug6891 = '[ruby-core:47241]'
|
|
|
|
|
|
|
|
def test_extend_module_with_protected_method
|
|
|
|
list = []
|
|
|
|
|
|
|
|
x = Class.new {
|
|
|
|
@list = list
|
|
|
|
|
|
|
|
extend Module.new {
|
|
|
|
protected
|
|
|
|
|
|
|
|
def inherited(klass)
|
|
|
|
@list << "protected"
|
|
|
|
super(klass)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
extend Module.new {
|
|
|
|
def inherited(klass)
|
|
|
|
@list << "public"
|
|
|
|
super(klass)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_nothing_raised(NoMethodError, Bug6891) {Class.new(x)}
|
|
|
|
assert_equal(['public', 'protected'], list)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_extend_module_with_protected_bmethod
|
|
|
|
list = []
|
|
|
|
|
|
|
|
x = Class.new {
|
|
|
|
extend Module.new {
|
|
|
|
protected
|
|
|
|
|
|
|
|
define_method(:inherited) do |klass|
|
|
|
|
list << "protected"
|
|
|
|
super(klass)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
extend Module.new {
|
|
|
|
define_method(:inherited) do |klass|
|
|
|
|
list << "public"
|
|
|
|
super(klass)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_nothing_raised(NoMethodError, Bug6891) {Class.new(x)}
|
|
|
|
assert_equal(['public', 'protected'], list)
|
|
|
|
end
|
2012-08-27 13:57:32 -04:00
|
|
|
|
2016-11-06 01:42:53 -04:00
|
|
|
def test_extend_module_with_no_args
|
|
|
|
assert_raise(ArgumentError) { Module.new { extend } }
|
|
|
|
end
|
|
|
|
|
2012-08-27 13:57:32 -04:00
|
|
|
def test_invalid_attr
|
2013-10-09 05:03:07 -04:00
|
|
|
%W[
|
2012-08-27 13:57:32 -04:00
|
|
|
foo?
|
|
|
|
@foo
|
|
|
|
@@foo
|
|
|
|
$foo
|
2013-10-09 09:27:51 -04:00
|
|
|
\u3042$
|
2012-08-27 13:57:32 -04:00
|
|
|
].each do |name|
|
2013-10-09 09:27:51 -04:00
|
|
|
assert_raise_with_message(NameError, /#{Regexp.quote(quote(name))}/) do
|
2012-08-27 13:57:32 -04:00
|
|
|
Module.new { attr_accessor name.to_sym }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-12-11 23:40:55 -05:00
|
|
|
|
2013-10-09 09:27:51 -04:00
|
|
|
private def quote(name)
|
|
|
|
encoding = Encoding.default_internal || Encoding.default_external
|
|
|
|
(name.encoding == encoding || name.ascii_only?) ? name : name.inspect
|
|
|
|
end
|
|
|
|
|
2012-12-11 23:40:55 -05:00
|
|
|
class AttrTest
|
|
|
|
class << self
|
|
|
|
attr_accessor :cattr
|
|
|
|
end
|
|
|
|
attr_accessor :iattr
|
|
|
|
def ivar
|
|
|
|
@ivar
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_uninitialized_instance_variable
|
|
|
|
a = AttrTest.new
|
2013-04-07 23:08:03 -04:00
|
|
|
assert_warning(/instance variable @ivar not initialized/) do
|
2012-12-11 23:40:55 -05:00
|
|
|
assert_nil(a.ivar)
|
|
|
|
end
|
|
|
|
a.instance_variable_set(:@ivar, 42)
|
2013-03-30 00:33:28 -04:00
|
|
|
assert_warning '' do
|
2012-12-11 23:40:55 -05:00
|
|
|
assert_equal(42, a.ivar)
|
|
|
|
end
|
2014-10-20 01:20:39 -04:00
|
|
|
|
|
|
|
name = "@\u{5909 6570}"
|
|
|
|
assert_warning(/instance variable #{name} not initialized/) do
|
2016-04-08 21:25:11 -04:00
|
|
|
assert_nil(a.instance_eval(name))
|
2014-10-20 01:20:39 -04:00
|
|
|
end
|
2012-12-11 23:40:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_uninitialized_attr
|
|
|
|
a = AttrTest.new
|
2013-03-30 00:33:28 -04:00
|
|
|
assert_warning '' do
|
2012-12-11 23:40:55 -05:00
|
|
|
assert_nil(a.iattr)
|
|
|
|
end
|
|
|
|
a.iattr = 42
|
2013-03-30 00:33:28 -04:00
|
|
|
assert_warning '' do
|
2012-12-11 23:40:55 -05:00
|
|
|
assert_equal(42, a.iattr)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_uninitialized_attr_class
|
2013-03-30 00:33:28 -04:00
|
|
|
assert_warning '' do
|
2012-12-11 23:40:55 -05:00
|
|
|
assert_nil(AttrTest.cattr)
|
|
|
|
end
|
|
|
|
AttrTest.cattr = 42
|
2013-03-30 00:33:28 -04:00
|
|
|
assert_warning '' do
|
2012-12-11 23:40:55 -05:00
|
|
|
assert_equal(42, AttrTest.cattr)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_uninitialized_attr_non_object
|
|
|
|
a = Class.new(Array) do
|
|
|
|
attr_accessor :iattr
|
|
|
|
end.new
|
2013-03-30 00:33:28 -04:00
|
|
|
assert_warning '' do
|
2012-12-11 23:40:55 -05:00
|
|
|
assert_nil(a.iattr)
|
|
|
|
end
|
|
|
|
a.iattr = 42
|
2013-03-30 00:33:28 -04:00
|
|
|
assert_warning '' do
|
2012-12-11 23:40:55 -05:00
|
|
|
assert_equal(42, a.iattr)
|
|
|
|
end
|
|
|
|
end
|
2012-12-27 11:45:36 -05:00
|
|
|
|
|
|
|
def test_remove_const
|
|
|
|
m = Module.new
|
|
|
|
assert_raise(NameError){ m.instance_eval { remove_const(:__FOO__) } }
|
|
|
|
end
|
2013-01-07 06:55:17 -05:00
|
|
|
|
2017-11-29 12:47:47 -05:00
|
|
|
def test_public_methods
|
|
|
|
public_methods = %i[
|
|
|
|
include
|
|
|
|
prepend
|
|
|
|
attr
|
|
|
|
attr_accessor
|
|
|
|
attr_reader
|
|
|
|
attr_writer
|
2017-11-29 12:47:59 -05:00
|
|
|
define_method
|
|
|
|
alias_method
|
|
|
|
undef_method
|
|
|
|
remove_method
|
2017-11-29 12:47:47 -05:00
|
|
|
]
|
|
|
|
assert_equal public_methods.sort, (Module.public_methods & public_methods).sort
|
|
|
|
end
|
|
|
|
|
2013-01-07 07:42:48 -05:00
|
|
|
def test_private_top_methods
|
|
|
|
assert_top_method_is_private(:include)
|
|
|
|
assert_top_method_is_private(:public)
|
|
|
|
assert_top_method_is_private(:private)
|
|
|
|
assert_top_method_is_private(:define_method)
|
|
|
|
end
|
2013-01-07 06:55:17 -05:00
|
|
|
|
2013-01-10 19:58:08 -05:00
|
|
|
module PrivateConstantReopen
|
|
|
|
PRIVATE_CONSTANT = true
|
|
|
|
private_constant :PRIVATE_CONSTANT
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_private_constant_reopen
|
|
|
|
assert_raise(NameError) do
|
|
|
|
eval <<-EOS, TOPLEVEL_BINDING
|
|
|
|
module TestModule::PrivateConstantReopen::PRIVATE_CONSTANT
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
assert_raise(NameError) do
|
|
|
|
eval <<-EOS, TOPLEVEL_BINDING
|
|
|
|
class TestModule::PrivateConstantReopen::PRIVATE_CONSTANT
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-16 07:39:29 -04:00
|
|
|
def test_frozen_visibility
|
|
|
|
bug11532 = '[ruby-core:70828] [Bug #11532]'
|
|
|
|
|
|
|
|
c = Class.new {const_set(:A, 1)}.freeze
|
2017-12-11 19:46:34 -05:00
|
|
|
assert_raise_with_message(FrozenError, /frozen class/, bug11532) {
|
2015-09-16 07:39:29 -04:00
|
|
|
c.class_eval {private_constant :A}
|
|
|
|
}
|
|
|
|
|
|
|
|
c = Class.new {const_set(:A, 1); private_constant :A}.freeze
|
2017-12-11 19:46:34 -05:00
|
|
|
assert_raise_with_message(FrozenError, /frozen class/, bug11532) {
|
2015-09-16 07:39:29 -04:00
|
|
|
c.class_eval {public_constant :A}
|
|
|
|
}
|
|
|
|
|
|
|
|
c = Class.new {const_set(:A, 1)}.freeze
|
2017-12-11 19:46:34 -05:00
|
|
|
assert_raise_with_message(FrozenError, /frozen class/, bug11532) {
|
2015-09-16 07:39:29 -04:00
|
|
|
c.class_eval {deprecate_constant :A}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-03-07 12:54:49 -05:00
|
|
|
def test_singleton_class_ancestors
|
|
|
|
feature8035 = '[ruby-core:53171]'
|
|
|
|
obj = Object.new
|
|
|
|
assert_equal [obj.singleton_class, Object], obj.singleton_class.ancestors.first(2), feature8035
|
|
|
|
|
|
|
|
mod = Module.new
|
|
|
|
obj.extend mod
|
|
|
|
assert_equal [obj.singleton_class, mod, Object], obj.singleton_class.ancestors.first(3)
|
|
|
|
|
|
|
|
obj = Object.new
|
|
|
|
obj.singleton_class.send :prepend, mod
|
|
|
|
assert_equal [mod, obj.singleton_class, Object], obj.singleton_class.ancestors.first(3)
|
|
|
|
end
|
|
|
|
|
2013-04-19 04:09:42 -04:00
|
|
|
def test_visibility_by_public_class_method
|
2013-04-18 03:59:06 -04:00
|
|
|
bug8284 = '[ruby-core:54404] [Bug #8284]'
|
2017-11-29 12:47:59 -05:00
|
|
|
assert_raise(NoMethodError) {Object.remove_const}
|
|
|
|
Module.new.public_class_method(:remove_const)
|
|
|
|
assert_raise(NoMethodError, bug8284) {Object.remove_const}
|
2013-04-18 03:59:06 -04:00
|
|
|
end
|
|
|
|
|
2015-01-02 02:21:36 -05:00
|
|
|
def test_include_module_with_constants_does_not_invalidate_method_cache
|
|
|
|
assert_in_out_err([], <<-RUBY, %w(123 456 true), [])
|
2013-06-08 06:48:33 -04:00
|
|
|
A = 123
|
|
|
|
|
|
|
|
class Foo
|
|
|
|
def self.a
|
|
|
|
A
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module M
|
|
|
|
A = 456
|
|
|
|
end
|
|
|
|
|
|
|
|
puts Foo.a
|
2015-01-02 02:21:36 -05:00
|
|
|
starting = RubyVM.stat[:global_method_state]
|
|
|
|
|
2013-06-08 06:48:33 -04:00
|
|
|
Foo.send(:include, M)
|
2015-01-02 02:21:36 -05:00
|
|
|
|
|
|
|
ending = RubyVM.stat[:global_method_state]
|
2013-06-08 06:48:33 -04:00
|
|
|
puts Foo.a
|
2015-01-02 02:21:36 -05:00
|
|
|
puts starting == ending
|
2013-06-08 06:48:33 -04:00
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
2013-08-14 01:35:21 -04:00
|
|
|
def test_return_value_of_define_method
|
|
|
|
retvals = []
|
|
|
|
Class.new.class_eval do
|
|
|
|
retvals << define_method(:foo){}
|
|
|
|
retvals << define_method(:bar, instance_method(:foo))
|
|
|
|
end
|
|
|
|
assert_equal :foo, retvals[0]
|
|
|
|
assert_equal :bar, retvals[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_return_value_of_define_singleton_method
|
|
|
|
retvals = []
|
|
|
|
Class.new do
|
|
|
|
retvals << define_singleton_method(:foo){}
|
|
|
|
retvals << define_singleton_method(:bar, method(:foo))
|
|
|
|
end
|
|
|
|
assert_equal :foo, retvals[0]
|
|
|
|
assert_equal :bar, retvals[1]
|
|
|
|
end
|
|
|
|
|
2013-08-12 02:19:15 -04:00
|
|
|
def test_prepend_gc
|
|
|
|
assert_separately [], %{
|
|
|
|
module Foo
|
|
|
|
end
|
|
|
|
class Object
|
|
|
|
prepend Foo
|
|
|
|
end
|
|
|
|
GC.start # make created T_ICLASS old (or remembered shady)
|
|
|
|
class Object # add methods into T_ICLASS (need WB if it is old)
|
|
|
|
def foo; end
|
|
|
|
attr_reader :bar
|
|
|
|
end
|
|
|
|
1_000_000.times{''} # cause GC
|
|
|
|
}
|
|
|
|
end
|
2013-09-22 21:34:01 -04:00
|
|
|
|
2014-09-25 16:29:15 -04:00
|
|
|
def test_inspect_segfault
|
|
|
|
bug_10282 = '[ruby-core:65214] [Bug #10282]'
|
|
|
|
assert_separately [], <<-RUBY
|
|
|
|
module ShallowInspect
|
|
|
|
def shallow_inspect
|
|
|
|
"foo"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module InspectIsShallow
|
|
|
|
include ShallowInspect
|
|
|
|
alias_method :inspect, :shallow_inspect
|
|
|
|
end
|
|
|
|
|
|
|
|
class A
|
|
|
|
end
|
|
|
|
|
|
|
|
A.prepend InspectIsShallow
|
|
|
|
|
2015-05-30 14:45:28 -04:00
|
|
|
expect = "#<Method: A(ShallowInspect)#inspect(shallow_inspect)>"
|
2014-09-25 16:29:15 -04:00
|
|
|
assert_equal expect, A.new.method(:inspect).inspect, "#{bug_10282}"
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
2015-01-06 15:56:49 -05:00
|
|
|
def test_define_method_with_unbound_method
|
|
|
|
# Passing an UnboundMethod to define_method succeeds if it is from an ancestor
|
|
|
|
assert_nothing_raised do
|
|
|
|
cls = Class.new(String) do
|
|
|
|
define_method('foo', String.instance_method(:to_s))
|
|
|
|
end
|
|
|
|
|
|
|
|
obj = cls.new('bar')
|
|
|
|
assert_equal('bar', obj.foo)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Passing an UnboundMethod to define_method fails if it is not from an ancestor
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
Class.new do
|
|
|
|
define_method('foo', String.instance_method(:to_s))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-28 12:35:58 -04:00
|
|
|
def test_redefinition_mismatch
|
|
|
|
m = Module.new
|
|
|
|
m.module_eval "A = 1"
|
|
|
|
assert_raise_with_message(TypeError, /is not a module/) {
|
|
|
|
m.module_eval "module A; end"
|
|
|
|
}
|
2015-06-28 13:06:59 -04:00
|
|
|
n = "M\u{1f5ff}"
|
|
|
|
m.module_eval "#{n} = 42"
|
|
|
|
assert_raise_with_message(TypeError, "#{n} is not a module") {
|
|
|
|
m.module_eval "module #{n}; end"
|
|
|
|
}
|
2015-09-27 10:32:50 -04:00
|
|
|
|
|
|
|
assert_separately([], <<-"end;")
|
|
|
|
Etc = (class C\u{1f5ff}; self; end).new
|
|
|
|
assert_raise_with_message(TypeError, /C\u{1f5ff}/) {
|
|
|
|
require 'etc'
|
|
|
|
}
|
|
|
|
end;
|
2015-06-28 12:35:58 -04:00
|
|
|
end
|
|
|
|
|
2013-09-22 21:34:01 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def assert_top_method_is_private(method)
|
|
|
|
assert_separately [], %{
|
|
|
|
methods = singleton_class.private_instance_methods(false)
|
|
|
|
assert_include(methods, :#{method}, ":#{method} should be private")
|
|
|
|
|
|
|
|
assert_raise_with_message(NoMethodError, "private method `#{method}' called for main:Object") {
|
|
|
|
self.#{method}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|