@@ -155,15 +155,62 @@ def foo
155
155
156
156
m . foo . should == [ "m" , "super_m" ]
157
157
end
158
+
159
+ context "methods created with define_method" do
160
+ context "passed a block" do
161
+ it "creates duplicates of the given instance methods" do
162
+ m = Module . new do
163
+ define_method :test1 do ; end
164
+ module_function :test1
165
+ end
166
+
167
+ m . respond_to? ( :test1 ) . should == true
168
+ end
169
+ end
170
+
171
+ context "passed a method" do
172
+ it "creates duplicates of the given instance methods" do
173
+ module_with_method = Module . new do
174
+ def test1 ; end
175
+ end
176
+
177
+ c = Class . new do
178
+ extend module_with_method
179
+ end
180
+
181
+ m = Module . new do
182
+ define_method :test2 , c . method ( :test1 )
183
+ module_function :test2
184
+ end
185
+
186
+ m . respond_to? ( :test2 ) . should == true
187
+ end
188
+ end
189
+
190
+ context "passed an unbound method" do
191
+ it "creates duplicates of the given instance methods" do
192
+ module_with_method = Module . new do
193
+ def test1 ; end
194
+ end
195
+
196
+ m = Module . new do
197
+ define_method :test2 , module_with_method . instance_method ( :test1 )
198
+ module_function :test2
199
+ end
200
+
201
+ m . respond_to? ( :test2 ) . should == true
202
+ end
203
+ end
204
+ end
158
205
end
159
206
160
207
describe "Module#module_function as a toggle (no arguments) in a Module body" do
161
208
it "makes any subsequently defined methods module functions with the normal semantics" do
162
- m = Module . new {
209
+ m = Module . new do
163
210
module_function
164
211
def test1 ( ) end
165
212
def test2 ( ) end
166
- }
213
+ end
167
214
168
215
m . respond_to? ( :test1 ) . should == true
169
216
m . respond_to? ( :test2 ) . should == true
@@ -187,13 +234,13 @@ def test2() end
187
234
188
235
it "stops creating module functions if the body encounters another toggle " \
189
236
"like public/protected/private without arguments" do
190
- m = Module . new {
237
+ m = Module . new do
191
238
module_function
192
239
def test1 ( ) end
193
240
def test2 ( ) end
194
241
public
195
242
def test3 ( ) end
196
- }
243
+ end
197
244
198
245
m . respond_to? ( :test1 ) . should == true
199
246
m . respond_to? ( :test2 ) . should == true
@@ -202,84 +249,131 @@ def test3() end
202
249
203
250
it "does not stop creating module functions if the body encounters " \
204
251
"public/protected/private WITH arguments" do
205
- m = Module . new {
252
+ m = Module . new do
206
253
def foo ( ) end
207
254
module_function
208
255
def test1 ( ) end
209
256
def test2 ( ) end
210
257
public :foo
211
258
def test3 ( ) end
212
- }
259
+ end
213
260
214
261
m . respond_to? ( :test1 ) . should == true
215
262
m . respond_to? ( :test2 ) . should == true
216
263
m . respond_to? ( :test3 ) . should == true
217
264
end
218
265
219
266
it "does not affect module_evaled method definitions also if outside the eval itself" do
220
- m = Module . new {
267
+ m = Module . new do
221
268
module_function
222
269
module_eval { def test1 ( ) end }
223
270
module_eval " def test2() end "
224
- }
271
+ end
225
272
226
273
m . respond_to? ( :test1 ) . should == false
227
274
m . respond_to? ( :test2 ) . should == false
228
275
end
229
276
230
277
it "has no effect if inside a module_eval if the definitions are outside of it" do
231
- m = Module . new {
278
+ m = Module . new do
232
279
module_eval { module_function }
233
280
def test1 ( ) end
234
281
def test2 ( ) end
235
- }
282
+ end
236
283
237
284
m . respond_to? ( :test1 ) . should == false
238
285
m . respond_to? ( :test2 ) . should == false
239
286
end
240
287
241
288
it "functions normally if both toggle and definitions inside a module_eval" do
242
- m = Module . new {
243
- module_eval {
289
+ m = Module . new do
290
+ module_eval do
244
291
module_function
245
292
def test1 ( ) end
246
293
def test2 ( ) end
247
- }
248
- }
294
+ end
295
+ end
249
296
250
297
m . respond_to? ( :test1 ) . should == true
251
298
m . respond_to? ( :test2 ) . should == true
252
299
end
253
300
254
- it "affects evaled method definitions also even when outside the eval itself" do
255
- m = Module . new {
301
+ it "affects eval'ed method definitions also even when outside the eval itself" do
302
+ m = Module . new do
256
303
module_function
257
304
eval "def test1() end"
258
- }
305
+ end
259
306
260
307
m . respond_to? ( :test1 ) . should == true
261
308
end
262
309
263
310
it "doesn't affect definitions when inside an eval even if the definitions are outside of it" do
264
- m = Module . new {
311
+ m = Module . new do
265
312
eval "module_function"
266
313
def test1 ( ) end
267
- }
314
+ end
268
315
269
316
m . respond_to? ( :test1 ) . should == false
270
317
end
271
318
272
319
it "functions normally if both toggle and definitions inside a eval" do
273
- m = Module . new {
320
+ m = Module . new do
274
321
eval <<-CODE
275
322
module_function
276
323
277
324
def test1() end
278
325
def test2() end
279
326
CODE
280
- }
327
+ end
281
328
282
329
m . respond_to? ( :test1 ) . should == true
283
330
m . respond_to? ( :test2 ) . should == true
284
331
end
332
+
333
+ context "methods are defined with define_method" do
334
+ context "passed a block" do
335
+ it "makes any subsequently defined methods module functions with the normal semantics" do
336
+ m = Module . new do
337
+ module_function
338
+ define_method :test1 do ; end
339
+ end
340
+
341
+ m . respond_to? ( :test1 ) . should == true
342
+ end
343
+ end
344
+
345
+ context "passed a method" do
346
+ it "makes any subsequently defined methods module functions with the normal semantics" do
347
+ module_with_method = Module . new do
348
+ def test1 ; end
349
+ end
350
+
351
+ c = Class . new do
352
+ extend module_with_method
353
+ end
354
+
355
+ m = Module . new do
356
+ module_function
357
+ define_method :test2 , c . method ( :test1 )
358
+ end
359
+
360
+ m . respond_to? ( :test2 ) . should == true
361
+ end
362
+ end
363
+
364
+ context "passed an unbound method" do
365
+ it "makes any subsequently defined methods module functions with the normal semantics" do
366
+ module_with_method = Module . new do
367
+ def test1 ; end
368
+ end
369
+
370
+ m = Module . new do
371
+ module_function
372
+ define_method :test2 , module_with_method . instance_method ( :test1 )
373
+ end
374
+
375
+ m . respond_to? ( :test2 ) . should == true
376
+ end
377
+ end
378
+ end
285
379
end
0 commit comments