summaryrefslogtreecommitdiff
path: root/spec/ruby/core/refinement/import_methods_spec.rb
blob: 13c0b1004cdfeaee40253dcd5b600a723d6cfb9d (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
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Refinement#import_methods" do
  context "when methods are defined in Ruby code" do
    it "imports methods" do
      str_utils = Module.new do
        def indent(level)
          " " * level + self
        end
      end

      Module.new do
        refine String do
          import_methods str_utils
          "foo".indent(3).should == "   foo"
        end
      end
    end

    it "throws an exception when argument is not a module" do
      Module.new do
        refine String do
          -> {
            import_methods Integer
          }.should raise_error(TypeError, "wrong argument type Class (expected Module)")
        end
      end
    end

    it "imports methods from multiple modules" do
      str_utils = Module.new do
        def indent(level)
          " " * level + self
        end
      end

      str_utils_fancy = Module.new do
        def indent_star(level)
          "*" * level + self
        end
      end

      Module.new do
        refine String do
          import_methods str_utils, str_utils_fancy
          "foo".indent(3).should == "   foo"
          "foo".indent_star(3).should == "***foo"
        end
      end
    end

    it "imports a method defined in the last module if method with same name is defined in multiple modules" do
      str_utils = Module.new do
        def indent(level)
          " " * level + self
        end
      end

      str_utils_fancy = Module.new do
        def indent(level)
          "*" * level + self
        end
      end

      Module.new do
        refine String do
          import_methods str_utils, str_utils_fancy
          "foo".indent(3).should == "***foo"
        end
      end
    end

    it "still imports methods of modules listed before a module that contains method not defined in Ruby" do
      str_utils = Module.new do
        def indent(level)
          " " * level + self
        end
      end

      string_refined = Module.new do
        refine String do
          -> {
            import_methods str_utils, Kernel
          }.should raise_error(ArgumentError)
        end
      end

      Module.new do
        using string_refined
        "foo".indent(3).should == "   foo"
      end
    end
  end

  it "warns if a module includes/prepends some other module" do
    module1 = Module.new do
    end

    module2 = Module.new do
      include module1
    end

    Module.new do
      refine String do
        -> {
          import_methods module2
        }.should complain(/warning: #<Module:\w*> has ancestors, but Refinement#import_methods doesn't import their methods/)
      end
    end

    Module.new do
      refine String do
        -> {
          import_methods RefinementSpec::ModuleWithAncestors
        }.should complain(/warning: RefinementSpec::ModuleWithAncestors has ancestors, but Refinement#import_methods doesn't import their methods/)
      end
    end
  end

  it "doesn't import methods from included/prepended modules" do
    Module.new do
      refine String do
        suppress_warning { import_methods RefinementSpec::ModuleWithAncestors }
      end

      using self
      -> {
        "foo".indent(3)
      }.should raise_error(NoMethodError, /undefined method [`']indent' for ("foo":String|an instance of String)/)
    end
  end

  it "doesn't import any methods if one of the arguments is not a module" do
    str_utils = Module.new do
      def indent(level)
        " " * level + self
      end
    end

    string_refined = Module.new do
      refine String do
        -> {
          import_methods str_utils, Integer
        }.should raise_error(TypeError)
      end
    end

    Module.new do
      using string_refined
      -> {
        "foo".indent(3)
      }.should raise_error(NoMethodError)
    end
  end

  it "imports methods from multiple modules so that methods see other's module's methods" do
    str_utils = Module.new do
      def indent(level)
        " " * level + self
      end
    end

    str_utils_normal = Module.new do
      def indent_normal(level)
        self.indent(level)
      end
    end

    Module.new do
      refine String do
        import_methods str_utils, str_utils_normal
      end

      using self
      "foo".indent_normal(3).should == "   foo"
    end
  end

  it "imports methods from module so that methods can see each other" do
    str_utils = Module.new do
      def indent(level)
        " " * level + self
      end

      def indent_with_dot(level)
        self.indent(level) + "."
      end
    end

    Module.new do
      refine String do
        import_methods str_utils
      end

      using self
      "foo".indent_with_dot(3).should == "   foo."
    end
  end

  it "doesn't import module's class methods" do
    str_utils = Module.new do
      def self.indent(level)
        " " * level + self
      end
    end

    Module.new do
      refine String do
        import_methods str_utils
      end

      using self
      -> {
        String.indent(3)
      }.should raise_error(NoMethodError, /undefined method [`']indent' for (String:Class|class String)/)
    end
  end

  it "imports module methods with super" do
    class_to_refine = Class.new do
      def foo(number)
        2 * number
      end
    end

    extension = Module.new do
      def foo(number)
        super * 2
      end
    end

    refinement = Module.new do
      refine class_to_refine do
        import_methods extension
      end
    end

    Module.new do
      using refinement
      class_to_refine.new.foo(2).should == 8
    end
  end

  context "when methods are not defined in Ruby code" do
    it "raises ArgumentError" do
      Module.new do
        refine String do
          -> {
            import_methods Kernel
          }.should raise_error(ArgumentError)
        end
      end
    end

    it "raises ArgumentError when importing methods from C extension" do
      require 'zlib'
      Module.new do
        refine String do
          -> {
            import_methods Zlib
          }.should raise_error(ArgumentError, /Can't import method which is not defined with Ruby code: Zlib#*/)
        end
      end
    end
  end
end