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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
require 'test/unit'
class TestClass < Test::Unit::TestCase
class Top
def Top.inherited(sub)
$hierarchy << sub
end
end
$hierarchy = [Top]
class Middle < Top
end
class Bottom < Middle
end
def test_class_inheritance_for_a_global_variable
assert_equal([Top, Middle, Bottom] , $hierarchy)
end
class AttrTest
attr :attribute1
attr_writer :attribute1
end
def test_attr_and_attr_writer_behavior
attrTest = AttrTest.new
attrTest.attribute1 = 1
assert_equal(1 , attrTest.attribute1)
end
def test_attr_methods_have_optional_arity
assert_nothing_raised do
Module.new do
attr_reader
attr_writer
attr_accessor
end
end
end
class Froboz
include Enumerable
end
def test_class_ancestry_is_correct_when_a_module_is_mixed_in
f = Froboz.new
assert_equal([Froboz, Enumerable, Object, Kernel], f.class.ancestors)
assert(f.kind_of?(Froboz))
assert(f.kind_of?(Enumerable))
end
class CM1; end
class CM2
def CM2::const_missing (constant)
constant.to_s
end
end
def test_missing_constant_raises_name_error
assert_raise(NameError) {CM1::A}
end
def test_const_missing_is_available_for_a_class
assert_equal(CM2::A, "A")
end
class GV1
def initialize
@@a = 1;
end
def modifyAfterFreeze
freeze
@aa = 2;
end
def createAfterFreeze
@@b = 2;
end
end
def test_freeze_halts_execution
g = GV1.new
assert_raise(TypeError) { g.modifyAfterFreeze }
assert_nothing_raised {g = GV1.new}
g.class.freeze
assert_raise(TypeError) {g.createAfterFreeze}
end
module A
class Failure
def Failure.bar()
print "bar\n"
end
end
end
module B
class Failure
def Failure.foo()
print "foo\n"
end
end
end
def test_scope_within_modules
# minirunit test passed asseting this was a NameError?
assert_raise(NoMethodError) {B::Failure.bar}
end
E = Hash.new
class << E
def a() "A" end
end
def test_singleton_class_adds_methods_to_an_object_not_superclasses
assert_equal("A", E.a)
assert_raise(NoMethodError) { Hash.new.a }
assert_raise(NoMethodError) { Object.new.a }
end
# test singleton method scoping
class C
VAR1 = 1
def C.get_var1
VAR1
end
class << self
VAR2 = 2
def other_get_var1
VAR1
end
def get_var2
VAR2
end
end
end
def test_singleton_method_scoping
assert_equal(1, C.get_var1)
assert_equal(1, C.other_get_var1)
assert_equal(2, C.get_var2)
end
class << C; end
def test_scoping_of_above_methods_does_not_change_with_new_singleton_class_declaration
assert_equal(1, C.get_var1)
assert_equal(1, C.other_get_var1)
assert_equal(2, C.get_var2)
end
def test_singleton_decaration_as_a_block
a = Class.new do
def method_missing(name, *args)
self.class.send(:define_method, name) do |*a|
"#{name}"
end
send(name)
end
end
b = a.new
assert_equal("foo", b.foo)
end
class ClassVarTest
@@foo = "foo"
def self.x
@@bar = "bar"
@@foo = "foonew"
end
def z
@@baz = "baz"
end
def y
@@foo
end
def self.w
@@foo
end
def self.zz
@@baz
end
end
def test_class_var_declaration
assert_equal(ClassVarTest.new.y, "foo")
assert_nothing_raised {ClassVarTest.x }
assert_raise(NameError) {ClassVarTest.zz }
assert_nothing_raised {ClassVarTest.new.z }
assert_equal(ClassVarTest.zz, "baz")
assert_equal(ClassVarTest.new.y, "foonew")
end
def test_class_var_get_and_set
##### JRUBY-793 test class var get/set #####
assert_raise(NameError) { ClassVarTest.send(:class_variable_get, :@foo) }
assert_raise(NameError) { ClassVarTest.send(:class_variable_set, :@foo, "foodoo") }
assert_nothing_raised { ClassVarTest.send(:class_variable_get, :@@foo) }
assert_nothing_raised { ClassVarTest.send(:class_variable_set, :@@foo, "fooset") }
assert_equal(ClassVarTest.send(:class_variable_get, :@@foo), "fooset")
assert_equal(ClassVarTest.w, "fooset")
assert_equal(ClassVarTest.new.y, "fooset")
end
# test class variable assignment in a singleton method
class TestClassVarAssignmentInSingleton
@@a = nil
class << self
def bar
# test_equal(nil, @@a)
@@a = 1 unless @@a
# test_equal(1, @@a)
end
end
def a
@@a
end
end
def test_class_var_assignment_in_singleton
test_instance = TestClassVarAssignmentInSingleton.new
assert_equal nil, test_instance.a
TestClassVarAssignmentInSingleton.bar
assert_equal 1, test_instance.a
end
# test define_method behavior to be working properly
$foo_calls = []
class BaseClass
def foo
$foo_calls << BaseClass
end
end
class SubClass < BaseClass
define_method(:foo) do
$foo_calls << SubClass
super
end
end
def test_define_method_behavior
x = SubClass.new
assert_nothing_raised { x.foo }
assert_equal([SubClass, BaseClass], $foo_calls)
end
# test constants do not appear in instance variables
class NoConstantInInstanceVariables
@@b = 4
B = 2
end
def test_constants_should_not_be_instance_vars
assert_equal([], NoConstantInInstanceVariables.new.class.instance_variables)
# this was in the minirunti tests but I cannot see why?
assert_equal(3, @@a = 3)
end
class C7; end
class C7A; end
module M7A; end
def test_eval_class_as_module_should_raise_type_exception
assert_raise(TypeError) do
C7A.module_eval { include C7 }
end
assert_raise(TypeError) do
M7A.module_eval { include C7 }
end
end
class Foo
def self.action(name, &block)
define_method(name) {
instance_eval &block
}
end
action(:boo) { baz }
protected
def baz
'here'
end
end
# JRUBY-1381
def test_define_method_with_instance_eval_has_correct_self
assert_equal('here', Foo.new.boo)
end
class AliasMethodTester
METHODS = []
def AliasMethodTester.method_added(name)
METHODS << name
end
alias_method :puts2, :puts
end
# JRUBY-1419
def test_alias_method_calls_method_added
assert_equal([:puts2], AliasMethodTester::METHODS)
end
# JRUBY-4266
def test_sclass_return
c = Class.new do
def foo
class << self
return 'foo'
end
end
end
assert_equal 'foo', c.new.foo
end
end
|