summaryrefslogtreecommitdiff
path: root/spec/ruby/core/module/module_function_spec.rb
blob: 1c3ec5471b12924fe7cdd6b9cc31357b62777d42 (plain)
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Module#module_function" do
  it "is a private method" do
    Module.should have_private_instance_method(:module_function)
  end

  describe "on Class" do
    it "is undefined" do
      Class.should_not have_private_instance_method(:module_function, true)
    end

    it "raises a TypeError if calling after rebinded to Class" do
      -> {
        Module.instance_method(:module_function).bind(Class.new).call
      }.should raise_error(TypeError)

      -> {
        Module.instance_method(:module_function).bind(Class.new).call :foo
      }.should raise_error(TypeError)
    end
  end
end

describe "Module#module_function with specific method names" do
  it "creates duplicates of the given instance methods on the Module object" do
    m = Module.new do
      def test()  end
      def test2() end
      def test3() end

      module_function :test, :test2
    end

    m.respond_to?(:test).should == true
    m.respond_to?(:test2).should == true
    m.respond_to?(:test3).should == false
  end

  ruby_version_is ""..."3.1" do
    it "returns self" do
      Module.new do
        def foo; end
        module_function(:foo).should equal(self)
      end
    end
  end

  ruby_version_is "3.1" do
    it "returns argument or arguments if given" do
      Module.new do
        def foo; end
        module_function(:foo).should equal(:foo)
        module_function(:foo, :foo).should == [:foo, :foo]
      end
    end
  end

  it "creates an independent copy of the method, not a redirect" do
    module Mixin
      def test
        "hello"
      end
      module_function :test
    end

    class BaseClass
      include Mixin
      def call_test
        test
      end
    end

    Mixin.test.should == "hello"
    c = BaseClass.new
    c.call_test.should == "hello"

    module Mixin
      def test
        "goodbye"
      end
    end

    Mixin.test.should == "hello"
    c.call_test.should == "goodbye"
  end

  it "makes the instance methods private" do
    m = Module.new do
      def test() "hello" end
      module_function :test
    end

    (o = mock('x')).extend(m)
    o.respond_to?(:test).should == false
    m.should have_private_instance_method(:test)
    o.send(:test).should == "hello"
    -> { o.test }.should raise_error(NoMethodError)
  end

  it "makes the new Module methods public" do
    m = Module.new do
      def test() "hello" end
      module_function :test
    end

    m.public_methods.map {|me| me.to_s }.include?('test').should == true
  end

  it "tries to convert the given names to strings using to_str" do
    (o = mock('test')).should_receive(:to_str).any_number_of_times.and_return("test")
    (o2 = mock('test2')).should_receive(:to_str).any_number_of_times.and_return("test2")

    m = Module.new do
      def test() end
      def test2() end
      module_function o, o2
    end

    m.respond_to?(:test).should == true
    m.respond_to?(:test2).should == true
  end

  it "raises a TypeError when the given names can't be converted to string using to_str" do
    o = mock('123')

    -> { Module.new { module_function(o) } }.should raise_error(TypeError)

    o.should_receive(:to_str).and_return(123)
    -> { Module.new { module_function(o) } }.should raise_error(TypeError)
  end

  it "can make accessible private methods" do # JRUBY-4214
    m = Module.new do
      module_function :require
    end
    m.respond_to?(:require).should be_true
  end

  it "creates Module methods that super up the singleton class of the module" do
    super_m = Module.new do
      def foo
        "super_m"
      end
    end

    m = Module.new do
      extend super_m
      module_function
      def foo
        ["m", super]
      end
    end

    m.foo.should == ["m", "super_m"]
  end

  context "methods created with define_method" do
    context "passed a block" do
      it "creates duplicates of the given instance methods" do
        m = Module.new do
          define_method :test1 do; end
          module_function :test1
        end

        m.respond_to?(:test1).should == true
      end
    end

    context "passed a method" do
      it "creates duplicates of the given instance methods" do
        module_with_method = Module.new do
          def test1; end
        end

        c = Class.new do
          extend module_with_method
        end

        m = Module.new do
          define_method :test2, c.method(:test1)
          module_function :test2
        end

        m.respond_to?(:test2).should == true
      end
    end

    context "passed an unbound method" do
      it "creates duplicates of the given instance methods" do
        module_with_method = Module.new do
          def test1; end
        end

        m = Module.new do
          define_method :test2, module_with_method.instance_method(:test1)
          module_function :test2
        end

        m.respond_to?(:test2).should == true
      end
    end
  end
end

describe "Module#module_function as a toggle (no arguments) in a Module body" do
  it "makes any subsequently defined methods module functions with the normal semantics" do
    m = Module.new do
      module_function
      def test1() end
      def test2() end
    end

    m.respond_to?(:test1).should == true
    m.respond_to?(:test2).should == true
  end

  ruby_version_is ""..."3.1" do
    it "returns self" do
      Module.new do
        module_function.should equal(self)
      end
    end
  end

  ruby_version_is "3.1" do
    it "returns nil" do
      Module.new do
        module_function.should equal(nil)
      end
    end
  end

  it "stops creating module functions if the body encounters another toggle " \
     "like public/protected/private without arguments" do
    m = Module.new do
      module_function
      def test1() end
      def test2() end
      public
      def test3() end
    end

    m.respond_to?(:test1).should == true
    m.respond_to?(:test2).should == true
    m.respond_to?(:test3).should == false
  end

  it "does not stop creating module functions if the body encounters " \
     "public/protected/private WITH arguments" do
    m = Module.new do
      def foo() end
      module_function
      def test1() end
      def test2() end
      public :foo
      def test3() end
    end

    m.respond_to?(:test1).should == true
    m.respond_to?(:test2).should == true
    m.respond_to?(:test3).should == true
  end

  it "does not affect module_evaled method definitions also if outside the eval itself" do
    m = Module.new do
      module_function
      module_eval { def test1() end }
      module_eval " def test2() end "
    end

    m.respond_to?(:test1).should == false
    m.respond_to?(:test2).should == false
  end

  it "has no effect if inside a module_eval if the definitions are outside of it" do
    m = Module.new do
      module_eval { module_function }
      def test1() end
      def test2() end
    end

    m.respond_to?(:test1).should == false
    m.respond_to?(:test2).should == false
  end

  it "functions normally if both toggle and definitions inside a module_eval" do
    m = Module.new do
      module_eval do
        module_function
        def test1() end
        def test2() end
      end
    end

    m.respond_to?(:test1).should == true
    m.respond_to?(:test2).should == true
  end

  it "affects eval'ed method definitions also even when outside the eval itself" do
    m = Module.new do
      module_function
      eval "def test1() end"
    end

    m.respond_to?(:test1).should == true
  end

  it "doesn't affect definitions when inside an eval even if the definitions are outside of it" do
    m = Module.new do
      eval "module_function"
      def test1() end
    end

    m.respond_to?(:test1).should == false
  end

  it "functions normally if both toggle and definitions inside a eval" do
    m = Module.new do
      eval <<-CODE
        module_function

        def test1() end
        def test2() end
      CODE
    end

    m.respond_to?(:test1).should == true
    m.respond_to?(:test2).should == true
  end

  context "methods are defined with define_method" do
    context "passed a block" do
      it "makes any subsequently defined methods module functions with the normal semantics" do
        m = Module.new do
          module_function
          define_method :test1 do; end
        end

        m.respond_to?(:test1).should == true
      end
    end

    context "passed a method" do
      it "makes any subsequently defined methods module functions with the normal semantics" do
        module_with_method = Module.new do
          def test1; end
        end

        c = Class.new do
          extend module_with_method
        end

        m = Module.new do
          module_function
          define_method :test2, c.method(:test1)
        end

        m.respond_to?(:test2).should == true
      end
    end

    context "passed an unbound method" do
      it "makes any subsequently defined methods module functions with the normal semantics" do
        module_with_method = Module.new do
          def test1; end
        end

        m = Module.new do
          module_function
          define_method :test2, module_with_method.instance_method(:test1)
        end

        m.respond_to?(:test2).should == true
      end
    end
  end
end