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