| 1 |
assert('Kernel#send', '15.3.1.3.44') do |
| 2 |
# test with block |
| 3 |
l = send(:lambda) do |
| 4 |
true |
| 5 |
end |
| 6 |
|
| 7 |
assert_true l.call |
| 8 |
assert_equal l.class, Proc |
| 9 |
# test with argument |
| 10 |
assert_true send(:respond_to?, :nil?) |
| 11 |
# test without argument and without block |
| 12 |
assert_equal send(:to_s).class, String |
| 13 |
end |
| 14 |
|
| 15 |
assert('Kernel#instance_variable_defined?', '15.3.1.3.20') do |
| 16 |
o = Object.new |
| 17 |
o.instance_variable_set(:@a, 1) |
| 18 |
|
| 19 |
assert_true o.instance_variable_defined?("@a") |
| 20 |
assert_false o.instance_variable_defined?("@b") |
| 21 |
assert_true o.instance_variable_defined?("@a"[0,2]) |
| 22 |
assert_true o.instance_variable_defined?("@abc"[0,2]) |
| 23 |
assert_raise(NameError) { o.instance_variable_defined?("@0") } |
| 24 |
end |
| 25 |
|
| 26 |
assert('Kernel#instance_variable_get', '15.3.1.3.21') do |
| 27 |
o = Class.new { attr_accessor :foo, :bar }.new |
| 28 |
o.foo = "one" |
| 29 |
o.bar = 2 |
| 30 |
assert_equal("one", o.instance_variable_get(:@foo)) |
| 31 |
assert_equal(2, o.instance_variable_get("@bar")) |
| 32 |
assert_equal(nil, o.instance_variable_get(:@baz)) |
| 33 |
%w[foo @1].each do |n| |
| 34 |
assert_raise(NameError) { o.instance_variable_get(n) } |
| 35 |
end |
| 36 |
end |
| 37 |
|
| 38 |
assert('Kernel#instance_variable_set', '15.3.1.3.22') do |
| 39 |
o = Class.new { attr_reader :foo, :_bar }.new |
| 40 |
assert_equal("one", o.instance_variable_set(:@foo, "one")) |
| 41 |
assert_equal("one", o.foo) |
| 42 |
assert_equal(2, o.instance_variable_set("@_bar", 2)) |
| 43 |
assert_equal(2, o._bar) |
| 44 |
%w[@6 @% @@a @ a].each do |n| |
| 45 |
assert_raise(NameError) { o.instance_variable_set(n, 1) } |
| 46 |
end |
| 47 |
assert_raise(FrozenError) { o.freeze.instance_variable_set(:@a, 2) } |
| 48 |
assert_raise(FrozenError, ArgumentError) { nil.instance_variable_set(:@a, 2) } |
| 49 |
end |
| 50 |
|
| 51 |
assert('Kernel#instance_variables', '15.3.1.3.23') do |
| 52 |
o = Object.new |
| 53 |
o.instance_eval do |
| 54 |
@a = 11 |
| 55 |
@b = 12 |
| 56 |
end |
| 57 |
ivars = o.instance_variables |
| 58 |
|
| 59 |
assert_equal Array, ivars.class |
| 60 |
assert_equal(2, ivars.size) |
| 61 |
assert_true ivars.include?(:@a) |
| 62 |
assert_true ivars.include?(:@b) |
| 63 |
end |
| 64 |
|
| 65 |
assert('Kernel#methods', '15.3.1.3.31') do |
| 66 |
assert_equal Array, methods.class |
| 67 |
assert_equal [:foo], Class.new{def self.foo; end}.methods(false) |
| 68 |
assert_equal [], Class.new{}.methods(false) |
| 69 |
end |
| 70 |
|
| 71 |
assert('Kernel#private_methods', '15.3.1.3.36') do |
| 72 |
assert_equal Array, private_methods.class |
| 73 |
end |
| 74 |
|
| 75 |
assert('Kernel#protected_methods', '15.3.1.3.37') do |
| 76 |
assert_equal Array, protected_methods.class |
| 77 |
end |
| 78 |
|
| 79 |
assert('Kernel#public_methods', '15.3.1.3.38') do |
| 80 |
assert_equal Array, public_methods.class |
| 81 |
class Foo |
| 82 |
def foo |
| 83 |
end |
| 84 |
end |
| 85 |
assert_equal [:foo], Foo.new.public_methods(false) |
| 86 |
end |
| 87 |
|
| 88 |
assert('Kernel#singleton_methods', '15.3.1.3.45') do |
| 89 |
assert_equal singleton_methods.class, Array |
| 90 |
end |
| 91 |
|
| 92 |
assert('Kernel.global_variables', '15.3.1.2.4') do |
| 93 |
assert_equal Array, Kernel.global_variables.class |
| 94 |
end |
| 95 |
|
| 96 |
assert('Kernel#global_variables', '15.3.1.3.14') do |
| 97 |
variables1 = global_variables |
| 98 |
assert_equal Array, variables1.class |
| 99 |
assert_not_include(variables1, :$kernel_global_variables_test) |
| 100 |
|
| 101 |
$kernel_global_variables_test = nil |
| 102 |
variables2 = global_variables |
| 103 |
assert_include(variables2, :$kernel_global_variables_test) |
| 104 |
assert_equal(1, variables2.size - variables1.size) |
| 105 |
end |
| 106 |
|
| 107 |
assert('Kernel#local_variables', '15.3.1.3.28') do |
| 108 |
assert_equal Array, local_variables.class |
| 109 |
|
| 110 |
def local_var_list |
| 111 |
a = "hello" |
| 112 |
local_variables |
| 113 |
end |
| 114 |
|
| 115 |
assert_equal [:a], local_var_list |
| 116 |
end |
| 117 |
|
| 118 |
assert('Kernel.local_variables', '15.3.1.2.7') do |
| 119 |
a, b = 0, 1 |
| 120 |
a += b |
| 121 |
|
| 122 |
vars = Kernel.local_variables.sort |
| 123 |
assert_equal [:a, :b, :vars], vars |
| 124 |
|
| 125 |
assert_equal [:a, :b, :c, :vars], Proc.new { |a, b| |
| 126 |
c = 2 |
| 127 |
# Kernel#local_variables: 15.3.1.3.28 |
| 128 |
local_variables.sort |
| 129 |
}.call(-1, -2) |
| 130 |
|
| 131 |
a = Object.new |
| 132 |
def a.hoge(vars, *, **) |
| 133 |
Proc.new { |
| 134 |
x, y = 1, 2 |
| 135 |
local_variables.sort |
| 136 |
} |
| 137 |
end |
| 138 |
assert_equal([:vars, :x, :y]) { a.hoge(0).call } |
| 139 |
end |
| 140 |
|
| 141 |
assert('Kernel#define_singleton_method') do |
| 142 |
o = Object.new |
| 143 |
ret = o.define_singleton_method(:test_method) do |
| 144 |
:singleton_method_ok |
| 145 |
end |
| 146 |
assert_equal :test_method, ret |
| 147 |
assert_equal :singleton_method_ok, o.test_method |
| 148 |
assert_raise(TypeError) { 2.define_singleton_method(:f){} } |
| 149 |
assert_raise(FrozenError) { [].freeze.define_singleton_method(:f){} } |
| 150 |
end |
| 151 |
|
| 152 |
assert('Kernel#singleton_class') do |
| 153 |
o1 = Object.new |
| 154 |
assert_same(o1.singleton_class, class << o1; self end) |
| 155 |
|
| 156 |
o2 = Object.new |
| 157 |
sc2 = class << o2; self end |
| 158 |
assert_same(o2.singleton_class, sc2) |
| 159 |
|
| 160 |
o3 = Object.new |
| 161 |
sc3 = o3.singleton_class |
| 162 |
o3.freeze |
| 163 |
assert_predicate(sc3, :frozen?) |
| 164 |
|
| 165 |
assert_predicate(Object.new.freeze.singleton_class, :frozen?) |
| 166 |
end |
| 167 |
|
| 168 |
def labeled_module(name, &block) |
| 169 |
Module.new do |
| 170 |
(class <<self; self end).class_eval do |
| 171 |
define_method(:to_s) { name } |
| 172 |
alias_method :inspect, :to_s |
| 173 |
end |
| 174 |
class_eval(&block) if block |
| 175 |
end |
| 176 |
end |
| 177 |
|
| 178 |
def labeled_class(name, supklass = Object, &block) |
| 179 |
Class.new(supklass) do |
| 180 |
(class <<self; self end).class_eval do |
| 181 |
define_method(:to_s) { name } |
| 182 |
alias_method :inspect, :to_s |
| 183 |
end |
| 184 |
class_eval(&block) if block |
| 185 |
end |
| 186 |
end |
| 187 |
|
| 188 |
assert('Module#class_variable_defined?', '15.2.2.4.16') do |
| 189 |
class Test4ClassVariableDefined |
| 190 |
@@cv = 99 |
| 191 |
end |
| 192 |
|
| 193 |
assert_true Test4ClassVariableDefined.class_variable_defined?(:@@cv) |
| 194 |
assert_false Test4ClassVariableDefined.class_variable_defined?(:@@noexisting) |
| 195 |
assert_raise(NameError) { Test4ClassVariableDefined.class_variable_defined?("@@2") } |
| 196 |
end |
| 197 |
|
| 198 |
assert('Module#class_variable_get', '15.2.2.4.17') do |
| 199 |
class Test4ClassVariableGet |
| 200 |
@@cv = 99 |
| 201 |
end |
| 202 |
|
| 203 |
assert_equal 99, Test4ClassVariableGet.class_variable_get(:@@cv) |
| 204 |
assert_raise(NameError) { Test4ClassVariableGet.class_variable_get(:@@a) } |
| 205 |
%w[@@a? @@! @a a].each do |n| |
| 206 |
assert_raise(NameError) { Test4ClassVariableGet.class_variable_get(n) } |
| 207 |
end |
| 208 |
end |
| 209 |
|
| 210 |
assert('Module#class_variable_set', '15.2.2.4.18') do |
| 211 |
class Test4ClassVariableSet |
| 212 |
@@foo = 100 |
| 213 |
def foo |
| 214 |
@@foo |
| 215 |
end |
| 216 |
end |
| 217 |
assert_equal 99, Test4ClassVariableSet.class_variable_set(:@@cv, 99) |
| 218 |
assert_equal 101, Test4ClassVariableSet.class_variable_set(:@@foo, 101) |
| 219 |
assert_true Test4ClassVariableSet.class_variables.include? :@@cv |
| 220 |
assert_equal 99, Test4ClassVariableSet.class_variable_get(:@@cv) |
| 221 |
assert_equal 101, Test4ClassVariableSet.new.foo |
| 222 |
%w[@@ @@1 @@x= @x @ x 1].each do |n| |
| 223 |
assert_raise(NameError) { Test4ClassVariableSet.class_variable_set(n, 1) } |
| 224 |
end |
| 225 |
|
| 226 |
m = Module.new.freeze |
| 227 |
assert_raise(FrozenError) { m.class_variable_set(:@@cv, 1) } |
| 228 |
|
| 229 |
parent = Class.new{ class_variable_set(:@@a, nil) }.freeze |
| 230 |
child = Class.new(parent) |
| 231 |
assert_raise(FrozenError) { child.class_variable_set(:@@a, 1) } |
| 232 |
end |
| 233 |
|
| 234 |
assert('Module#class_variables', '15.2.2.4.19') do |
| 235 |
class Test4ClassVariables1 |
| 236 |
@@var1 = 1 |
| 237 |
end |
| 238 |
class Test4ClassVariables2 < Test4ClassVariables1 |
| 239 |
@@var2 = 2 |
| 240 |
end |
| 241 |
|
| 242 |
assert_equal [:@@var1], Test4ClassVariables1.class_variables |
| 243 |
assert_equal [:@@var2, :@@var1], Test4ClassVariables2.class_variables |
| 244 |
end |
| 245 |
|
| 246 |
assert('Module#constants', '15.2.2.4.24') do |
| 247 |
$n = [] |
| 248 |
module TestA |
| 249 |
C = 1 |
| 250 |
end |
| 251 |
class TestB |
| 252 |
include TestA |
| 253 |
C2 = 1 |
| 254 |
$n = constants.sort |
| 255 |
end |
| 256 |
|
| 257 |
assert_equal [ :C ], TestA.constants |
| 258 |
assert_equal [ :C, :C2 ], $n |
| 259 |
end |
| 260 |
|
| 261 |
assert('Module#included_modules', '15.2.2.4.30') do |
| 262 |
module Test4includedModules |
| 263 |
end |
| 264 |
module Test4includedModules2 |
| 265 |
include Test4includedModules |
| 266 |
end |
| 267 |
r = Test4includedModules2.included_modules |
| 268 |
|
| 269 |
assert_equal Array, r.class |
| 270 |
assert_true r.include?(Test4includedModules) |
| 271 |
end |
| 272 |
|
| 273 |
assert('Module#instance_methods', '15.2.2.4.33') do |
| 274 |
module Test4InstanceMethodsA |
| 275 |
def method1() end |
| 276 |
end |
| 277 |
class Test4InstanceMethodsB |
| 278 |
def method2() end |
| 279 |
end |
| 280 |
class Test4InstanceMethodsC < Test4InstanceMethodsB |
| 281 |
def method3() end |
| 282 |
end |
| 283 |
|
| 284 |
r = Test4InstanceMethodsC.instance_methods(true) |
| 285 |
|
| 286 |
assert_equal [:method1], Test4InstanceMethodsA.instance_methods |
| 287 |
assert_equal [:method2], Test4InstanceMethodsB.instance_methods(false) |
| 288 |
assert_equal [:method3], Test4InstanceMethodsC.instance_methods(false) |
| 289 |
assert_equal Array, r.class |
| 290 |
assert_true r.include?(:method3) |
| 291 |
assert_true r.include?(:method2) |
| 292 |
end |
| 293 |
|
| 294 |
assert 'Module#prepend #instance_methods(false)' do |
| 295 |
bug6660 = '[ruby-dev:45863]' |
| 296 |
assert_equal([:m1], Class.new{ prepend Module.new; def m1; end }.instance_methods(false), bug6660) |
| 297 |
assert_equal([:m1], Class.new(Class.new{def m2;end}){ prepend Module.new; def m1; end }.instance_methods(false), bug6660) |
| 298 |
end |
| 299 |
|
| 300 |
assert('Module#remove_class_variable', '15.2.2.4.39') do |
| 301 |
class Test4RemoveClassVariable |
| 302 |
@@cv = 99 |
| 303 |
end |
| 304 |
|
| 305 |
assert_equal 99, Test4RemoveClassVariable.remove_class_variable(:@@cv) |
| 306 |
assert_false Test4RemoveClassVariable.class_variables.include? :@@cv |
| 307 |
assert_raise(NameError) do |
| 308 |
Test4RemoveClassVariable.remove_class_variable(:@@cv) |
| 309 |
end |
| 310 |
assert_raise(NameError) do |
| 311 |
Test4RemoveClassVariable.remove_class_variable(:@v) |
| 312 |
end |
| 313 |
assert_raise(FrozenError) do |
| 314 |
Test4RemoveClassVariable.freeze.remove_class_variable(:@@cv) |
| 315 |
end |
| 316 |
end |
| 317 |
|
| 318 |
assert('Module#remove_method', '15.2.2.4.41') do |
| 319 |
module Test4RemoveMethod |
| 320 |
class Parent |
| 321 |
def hello |
| 322 |
end |
| 323 |
end |
| 324 |
|
| 325 |
class Child < Parent |
| 326 |
def hello |
| 327 |
end |
| 328 |
end |
| 329 |
end |
| 330 |
|
| 331 |
klass = Test4RemoveMethod::Child |
| 332 |
assert_same klass, klass.class_eval{ remove_method :hello } |
| 333 |
assert_true klass.instance_methods.include? :hello |
| 334 |
assert_false klass.instance_methods(false).include? :hello |
| 335 |
assert_raise(FrozenError) { klass.freeze.remove_method :m } |
| 336 |
end |
| 337 |
|
| 338 |
assert('Module.nesting', '15.2.2.2.2') do |
| 339 |
module Test4ModuleNesting |
| 340 |
module Test4ModuleNesting2 |
| 341 |
assert_equal [Test4ModuleNesting2, Test4ModuleNesting], |
| 342 |
Module.nesting |
| 343 |
end |
| 344 |
end |
| 345 |
module Test4ModuleNesting::Test4ModuleNesting2 |
| 346 |
assert_equal [Test4ModuleNesting::Test4ModuleNesting2], Module.nesting |
| 347 |
end |
| 348 |
end |
| 349 |
|
| 350 |
assert('Moduler#prepend + #instance_methods') do |
| 351 |
bug6655 = '[ruby-core:45915]' |
| 352 |
assert_equal(Object.instance_methods, Class.new {prepend Module.new}.instance_methods, bug6655) |
| 353 |
end |
| 354 |
|
| 355 |
assert 'Module#prepend + #singleton_methods' do |
| 356 |
o = Object.new |
| 357 |
o.singleton_class.class_eval {prepend Module.new} |
| 358 |
assert_equal([], o.singleton_methods) |
| 359 |
end |
| 360 |
|
| 361 |
assert 'Module#prepend + #remove_method' do |
| 362 |
c = Class.new do |
| 363 |
prepend Module.new { def foo; end } |
| 364 |
end |
| 365 |
assert_raise(NameError) do |
| 366 |
c.class_eval do |
| 367 |
remove_method(:foo) |
| 368 |
end |
| 369 |
end |
| 370 |
c.class_eval do |
| 371 |
def foo; end |
| 372 |
end |
| 373 |
removed = nil |
| 374 |
c.singleton_class.class_eval do |
| 375 |
define_method(:method_removed) {|id| removed = id} |
| 376 |
end |
| 377 |
assert_nothing_raised('[Bug #7843]') do |
| 378 |
c.class_eval do |
| 379 |
remove_method(:foo) |
| 380 |
end |
| 381 |
end |
| 382 |
assert_equal(:foo, removed) |
| 383 |
end |
| 384 |
|
| 385 |
assert 'Module#prepend + #included_modules' do |
| 386 |
bug8025 = '[ruby-core:53158] [Bug #8025]' |
| 387 |
mixin = labeled_module("mixin") |
| 388 |
c = labeled_module("c") {prepend mixin} |
| 389 |
im = c.included_modules |
| 390 |
assert_not_include(im, c, bug8025) |
| 391 |
assert_include(im, mixin, bug8025) |
| 392 |
c1 = labeled_class("c1") {prepend mixin} |
| 393 |
c2 = labeled_class("c2", c1) |
| 394 |
im = c2.included_modules |
| 395 |
assert_not_include(im, c1, bug8025) |
| 396 |
assert_not_include(im, c2, bug8025) |
| 397 |
assert_include(im, mixin, bug8025) |
| 398 |
end |
| 399 |
|
| 400 |
assert("remove_method doesn't segfault if the passed in argument isn't a symbol") do |
| 401 |
klass = Class.new |
| 402 |
assert_raise(TypeError) { klass.remove_method nil } |
| 403 |
assert_raise(TypeError) { klass.remove_method 123 } |
| 404 |
assert_raise(TypeError) { klass.remove_method 1.23 } |
| 405 |
assert_raise(NameError) { klass.remove_method "hello" } |
| 406 |
assert_raise(TypeError) { klass.remove_method Class.new } |
| 407 |
end |
| 408 |
|
| 409 |
assert('alias_method and remove_method') do |
| 410 |
begin |
| 411 |
Integer.alias_method :to_s_, :to_s |
| 412 |
Integer.remove_method :to_s |
| 413 |
|
| 414 |
assert_nothing_raised do |
| 415 |
# segfaults if mrb_cptr is used |
| 416 |
1.to_s |
| 417 |
end |
| 418 |
ensure |
| 419 |
Integer.alias_method :to_s, :to_s_ |
| 420 |
Integer.remove_method :to_s_ |
| 421 |
end |
| 422 |
end |