1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
require 'test/minirunit'
test_check "Test Object"
class Dummy
attr_reader :var
def initialize
@var = 99
end
def remove
remove_instance_variable(:@var)
end
def remove_bad
remove_instance_variable(:@foo)
end
end
d = Dummy.new
test_equal(99, d.var)
test_equal(99, d.remove)
test_equal(nil, d.var)
test_exception(NameError) { d.remove_bad }
#test singleton_methods
module Other
def three() end
end
class Single
def Single.four() end
end
a = Single.new
def a.one() end
class << a
include Other
def two() end
end
test_equal(%w(four),Single.singleton_methods)
a1 = a.singleton_methods(false)
a2 = a.singleton_methods(true)
a3 = a.singleton_methods
test_equal(2,a1.length)
test_ok(a1.include?('one'))
test_ok(a1.include?('two'))
test_equal(3,a2.length)
test_ok(a2.include?('one'))
test_ok(a2.include?('two'))
test_ok(a2.include?('three'))
test_equal(3,a3.length)
test_ok(a3.include?('one'))
test_ok(a3.include?('two'))
test_ok(a3.include?('three'))
class TestClz
def call_private_bad; self.private_method; end
def call_private_good; private_method; end
private
def private_method; 1; end
end
class PrivateSetter
def foo
self.setter=:bar
end
private
def setter= arg
end
end
test_exception(NoMethodError) { TestClz.new.private_method }
test_exception(NoMethodError) { TestClz.new.call_private_bad }
test_equal 1, TestClz.new.call_private_good
test_no_exception { PrivateSetter.new.foo }
# JRUBY-147: These all crashed at one point.
# FIXME: These could be merged into more meaningful tests.
test_no_exception do
o1 = Object.new
class << o1; end
o2 = o1.clone
o1.object_id
o1.hash
o2.object_id
o2.hash
end
# This should not crash the interpreter
class BadHash
def hash
"NOWAY"
end
end
b = BadHash.new
{b => b}
# JRUBY-800: default initialize should not accept arguments
test_exception(ArgumentError) { Object.new(1) }
class NoArgClass
end
test_exception(ArgumentError) { NoArgClass.new(1) }
# JRUBY-980: public_methods, private_methods
# and protected_methods should take a boolean arg
# to include or exclude inherited methods or not
class MethodFixture
class << self
def method; end
public
def method0; end
protected
def method1; end
end
def method2; end
public
def method3; end
protected
def method4; end
private
def method5; end
end
test_equal(3, MethodFixture.singleton_methods(false).length)
m = MethodFixture.new
test_equal(3 + Object.new.methods.length, m.methods.length)
test_equal(2, m.public_methods(false).length)
test_equal(1, m.protected_methods(false).length)
test_equal(1, m.private_methods(false).length)
class Class1
def initialize
@one = 123
@two = nil
end
def initialize_copy
end
end
c1 = Class1.new
test_exception(NameError) do
c1.instance_variable_defined? :abc
end
test_ok !c1.instance_variable_defined?(:@three)
test_ok c1.instance_variable_defined?(:@one)
test_ok c1.instance_variable_defined?(:@two)
# JRUBY-2624: Object#initialize_copy should always be private
test_ok c1.private_methods.include?("initialize")
test_ok c1.private_methods.include?("initialize_copy")
# JRUBY-2247
test_equal ['now'], Time.methods.grep('now')
test_equal ["_load", "at", "gm", "local", "mktime", "now", "times", "utc"], Time.methods(false).sort
|