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
|
require 'test/unit'
require_relative 'envutil'
class TestMethod < Test::Unit::TestCase
def setup
@verbose = $VERBOSE
$VERBOSE = nil
end
def teardown
$VERBOSE = @verbose
end
def m0() end
def m1(a) end
def m2(a, b) end
def mo1(a = nil, &b) end
def mo2(a, b = nil) end
def mo3(*a) end
def mo4(a, *b, &c) end
def mo5(a, *b, c) end
def mo6(a, *b, c, &d) end
class Base
def foo() :base end
end
class Derived < Base
def foo() :derived end
end
def test_arity
assert_equal(0, method(:m0).arity)
assert_equal(1, method(:m1).arity)
assert_equal(2, method(:m2).arity)
assert_equal(-1, method(:mo1).arity)
assert_equal(-2, method(:mo2).arity)
assert_equal(-1, method(:mo3).arity)
assert_equal(-2, method(:mo4).arity)
assert_equal(-3, method(:mo5).arity)
assert_equal(-3, method(:mo6).arity)
end
def test_unbind
assert_equal(:derived, Derived.new.foo)
um = Derived.new.method(:foo).unbind
assert_instance_of(UnboundMethod, um)
Derived.class_eval do
def foo() :changed end
end
assert_equal(:changed, Derived.new.foo)
assert_equal(:derived, um.bind(Derived.new).call)
assert_raise(TypeError) do
um.bind(Base.new)
end
end
def test_callee
assert_equal(:test_callee, __method__)
assert_equal(:m, Class.new {def m; __method__; end}.new.m)
assert_equal(:m, Class.new {def m; tap{return __method__}; end}.new.m)
assert_equal(:m, Class.new {define_method(:m) {__method__}}.new.m)
assert_equal(:m, Class.new {define_method(:m) {tap{return __method__}}}.new.m)
assert_nil(eval("class TestCallee; __method__; end"))
end
def test_body
o = Object.new
def o.foo; end
assert_nothing_raised { RubyVM::InstructionSequence.disasm(o.method(:foo)) }
end
def test_new
c1 = Class.new
c1.class_eval { def foo; :foo; end }
c2 = Class.new(c1)
c2.class_eval { private :foo }
o = c2.new
o.extend(Module.new)
assert_raise(NameError) { o.method(:bar) }
assert_raise(NameError) { o.public_method(:foo) }
assert_equal(:foo, o.method(:foo).call)
end
def test_eq
o = Object.new
class << o
def foo; end
alias bar foo
def baz; end
end
assert_not_equal(o.method(:foo), nil)
m = o.method(:foo)
def m.foo; end
assert_not_equal(o.method(:foo), m)
assert_equal(o.method(:foo), o.method(:foo))
assert_equal(o.method(:foo), o.method(:bar))
assert_not_equal(o.method(:foo), o.method(:baz))
end
def test_hash
o = Object.new
def o.foo; end
assert_kind_of(Integer, o.method(:foo).hash)
end
def test_receiver_name_owner
o = Object.new
def o.foo; end
m = o.method(:foo)
assert_equal(o, m.receiver)
assert_equal(:foo, m.name)
assert_equal(class << o; self; end, m.owner)
assert_equal(:foo, m.unbind.name)
assert_equal(class << o; self; end, m.unbind.owner)
end
def test_instance_method
c = Class.new
c.class_eval do
def foo; :foo; end
private :foo
end
o = c.new
o.method(:foo).unbind
assert_raise(NoMethodError) { o.foo }
c.instance_method(:foo).bind(o)
assert_equal(:foo, o.instance_eval { foo })
assert_raise(NameError) { c.public_instance_method(:foo) }
def o.bar; end
m = o.method(:bar).unbind
assert_raise(TypeError) { m.bind(Object.new) }
end
def test_define_method
c = Class.new
c.class_eval { def foo; :foo; end }
o = c.new
def o.bar; :bar; end
assert_raise(TypeError) do
c.class_eval { define_method(:foo, :foo) }
end
assert_raise(ArgumentError) do
c.class_eval { define_method }
end
c2 = Class.new(c)
c2.class_eval { define_method(:baz, o.method(:foo)) }
assert_equal(:foo, c2.new.baz)
assert_raise(TypeError) do
Class.new.class_eval { define_method(:foo, o.method(:foo)) }
end
assert_raise(TypeError) do
Class.new.class_eval { define_method(:bar, o.method(:bar)) }
end
o = Object.new
def o.foo(c)
c.class_eval { define_method(:foo) }
end
c = Class.new
o.foo(c) { :foo }
assert_equal(:foo, c.new.foo)
o = Object.new
o.instance_eval { define_singleton_method(:foo) { :foo } }
assert_equal(:foo, o.foo)
end
def test_clone
o = Object.new
def o.foo; :foo; end
m = o.method(:foo)
def m.bar; :bar; end
assert_equal(:foo, m.clone.call)
assert_equal(:bar, m.clone.bar)
end
def test_call
o = Object.new
def o.foo; p 1; end
def o.bar(x); x; end
m = o.method(:foo)
m.taint
assert_raise(SecurityError) { m.call }
end
def test_inspect
o = Object.new
def o.foo; end
m = o.method(:foo)
assert_equal("#<Method: #{ o.inspect }.foo>", m.inspect)
m = o.method(:foo)
assert_equal("#<UnboundMethod: #{ class << o; self; end.inspect }#foo>", m.unbind.inspect)
c = Class.new
c.class_eval { def foo; end; }
m = c.new.method(:foo)
assert_equal("#<Method: #{ c.inspect }#foo>", m.inspect)
m = c.instance_method(:foo)
assert_equal("#<UnboundMethod: #{ c.inspect }#foo>", m.inspect)
c2 = Class.new(c)
c2.class_eval { private :foo }
m2 = c2.new.method(:foo)
assert_equal("#<Method: #{ c2.inspect }(#{ c.inspect })#foo>", m2.inspect)
end
def test_callee_top_level
assert_in_out_err([], "p __callee__", %w(nil), [])
end
def test_caller_negative_level
assert_raise(ArgumentError) { caller(-1) }
end
def test_attrset_ivar
c = Class.new
c.class_eval { attr_accessor :foo }
o = c.new
o.method(:foo=).call(42)
assert_equal(42, o.foo)
assert_raise(ArgumentError) { o.method(:foo=).call(1, 2, 3) }
assert_raise(ArgumentError) { o.method(:foo).call(1) }
end
end
|