~tim/natalie

ref: 8e9e487dd5b7be3438aadcad6503e8b46a72e0b0 natalie/test/natalie/method_test.rb -rw-r--r-- 9.7 KiB
8e9e487dTim Morgan Store method name and owner on the Method object 2 months ago
                                                                                
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
require_relative '../spec_helper'

def foo
  'foo'
end

def double(x)
  x * 2
end

def mul(x, y,
        z)
  x * y * z
end

def block_test
  yield
end

def block_with_args_test
  yield 1, 2, 3
end

def block_with_args_test2
  yield 4, 5
end

def block_arg_test(a, b, &block)
  [a, b, block.call]
end

def block_arg_is_nil(&block)
  block
end

def block_given_test
  block_given?
end

def default(x = 1)
  x
end

def default_calling_another_method(x = default)
  x
end

def default_calling_another_method2(x = default(2))
  x
end

def default_after_regular(x, y = 2)
  [x, y]
end

def default_with_splat_last(x, y = 2, *rest)
  [x, y, rest]
end

class Foo
  def foo
    'instance method foo'
  end

  def self.foo1
    'class method foo'
  end

  def Foo.foo2
    'class method foo2'
  end
end

class Bar
  define_method :bar do
    'bar'
  end
end

class YieldToBlock
  def yield_to_block
    yield
  end
end

class BlockPassTest < YieldToBlock
  def yield_to_block(&b)
    super(&b)
  end
end

describe 'method' do
  it 'can be called' do
    foo.should == 'foo'
  end

  it 'can receive arguments' do
    double(3).should == 6
    mul(1, 2, 3).should == 6
  end

  it 'raises an error when there are not enough arguments' do
    -> { double }.should raise_error(ArgumentError, 'wrong number of arguments (given 0, expected 1)')
    -> { default_after_regular }.should raise_error(ArgumentError, 'wrong number of arguments (given 0, expected 1..2)')
  end

  it 'raises an error when there are too many arguments' do
    -> { double(1, 2) }.should raise_error(ArgumentError, 'wrong number of arguments (given 2, expected 1)')
    -> { default_after_regular(1, 2, 3) }.should raise_error(ArgumentError, 'wrong number of arguments (given 3, expected 1..2)')
  end

  it 'can receive and yield to a block' do
    r = block_test { 1 }
    r.should == 1
    x = 2
    r = block_test do
      x
    end
    r.should == 2
  end

  it 'can receive a block as a Proc argument' do
    r = block_arg_test(1, 2) { 3 }
    r.should == [1, 2, 3]
    r = block_arg_is_nil
    r.should == nil
    r = block_arg_is_nil(&nil)
    r.should == nil
  end

  it 'knows if it received a block' do
    without = block_given_test
    without.should == false
    with = block_given_test { p 'hi' }
    with.should == true
  end

  # FIXME: doesn't actually bubble up because LocalJumpError is trapped by the method
  xit 'raises an error when trying to yield without a block' do
    -> { YieldToBlock.new.yield_to_block }.should raise_error(LocalJumpError)
  end

  xit 'can call super with a block arg' do
    t = BlockPassTest.new
    it_worked = false
    t.yield_to_block do
      it_worked = true
    end
    it_worked.should == true
  end

  it 'can yield args to a block' do
    r = block_with_args_test { |x, y, z| [x, y, z] }
    r.should == [1, 2, 3]
    r = block_with_args_test2 do |x, y, z|
      [x, y, z]
    end
    r.should == [4, 5, nil]
    r = block_with_args_test2 do |x|
      [x]
    end
    r.should == [4]
  end

  it 'can be an instance method' do
    Foo.new.foo.should == 'instance method foo'
  end

  it 'can be a class method' do
    Foo.foo1.should == 'class method foo'
    Foo.foo2.should == 'class method foo2'
  end

  it 'can define a method dynamically' do
    Bar.new.bar.should == 'bar'
  end

  it 'can have default arguments' do
    default.should == 1
    default(2).should == 2
    default_after_regular(1).should == [1, 2]
    default_after_regular(2).should == [2, 2]
    default_after_regular(2, 3).should == [2, 3]
    default_with_splat_last(2).should == [2, 2, []]
    default_calling_another_method.should == 1
    default_calling_another_method2.should == 2
  end

  def default_first1(x = 1)
    x
  end

  def default_first2(a = 1, b = 2, c = 3, v, w, x, y, z)
    [a, b, c, v, w, x, y, z]
  end

  def default_first3(a = 1, b, c, v, w, x, y, z)
    [a, b, c, v, w, x, y, z]
  end

  it 'can have default arguments first' do
    default_first1.should == 1
    default_first1(2).should == 2
    default_first2(4, 5, 6, 7, 8).should == [1, 2, 3, 4, 5, 6, 7, 8]
    default_first2(0, 4, 5, 6, 7, 8).should == [0, 2, 3, 4, 5, 6, 7, 8]
    default_first2(0, 1, 4, 5, 6, 7, 8).should == [0, 1, 3, 4, 5, 6, 7, 8]
    default_first2(0, 1, 2, 4, 5, 6, 7, 8).should == [0, 1, 2, 4, 5, 6, 7, 8]
    default_first3(2, 3, 4, 5, 6, 7, 8).should == [1, 2, 3, 4, 5, 6, 7, 8]
    default_first3(0, 2, 3, 4, 5, 6, 7, 8).should == [0, 2, 3, 4, 5, 6, 7, 8]
  end

  def destructuring((a, b), c)
    [a, b, c]
  end

  it 'can destructure arguments' do
    destructuring([1, 2], 3).should == [1, 2, 3]
    destructuring([1], 3).should == [1, nil, 3]
    destructuring([], 3).should == [nil, nil, 3]
    destructuring(nil, 3).should == [nil, nil, 3]
  end

  def destructuring_with_splat((a, *b), c)
    [a, b, c]
  end

  it 'can destructure arguments with a splat' do
    destructuring_with_splat([1, 2, 3], 3).should == [1, [2, 3], 3]
    destructuring_with_splat([1, 2], 3).should == [1, [2], 3]
    destructuring_with_splat([1], 3).should == [1, [], 3]
    destructuring_with_splat([], 3).should == [nil, [], 3]
    destructuring_with_splat(nil, 3).should == [nil, [], 3]
  end

  def splat_first(*splat, x, y)
    [splat, x, y]
  end

  def blank_splat_first(*, x, y)
    [x, y]
  end

  it 'can have a splat first' do
    splat_first(1, 2, 3, 4).should == [[1, 2], 3, 4]
    blank_splat_first(1, 2, 3, 4).should == [3, 4]
  end

  def splat_middle(x, *splat, y)
    [x, splat, y]
  end

  def blank_splat_middle(x, *, y)
    [x, y]
  end

  it 'can have a splat in the middle' do
    splat_middle(1, 2, 3, 4).should == [1, [2, 3], 4]
    blank_splat_middle(1, 2, 3, 4).should == [1, 4]
  end

  def method_name
    __method__
  end

  def method_name_via_send
    send(:__method__)
  end

  def method_name_from_block
    [1].map do
      __method__
    end
  end

  it 'knows its own name' do
    method_name.should == :method_name
    method_name_via_send.should == :method_name_via_send
    method_name_from_block.should == [:method_name_from_block]
  end

  describe '#method_defined?' do
    it 'returns true for regular methods' do
      Foo.method_defined?(:foo).should == true
    end

    it 'returns false for class methods' do
      Foo.method_defined?('foo1').should == false
      Foo.method_defined?(:foo2).should == false
    end

    it 'returns false for undefined (removed) methods' do
      NilClass.method_defined?('new').should == false
    end
  end

  describe '#respond_to?' do
    it 'works for class methods' do
      Foo.respond_to?('foo1').should == true
      Foo.respond_to?(:foo2).should == true
      Foo.respond_to?('xxx').should == false
    end

    it 'works for instance methods' do
      Foo.new.respond_to?('foo').should == true
      Foo.new.respond_to?(:xxx).should == false
      Bar.new.respond_to?('bar').should == true
      Bar.new.respond_to?(:xxx).should == false
    end

    it 'returns false for undefined methods' do
      NilClass.respond_to?('new').should == false
    end
  end

  describe '#method' do
    it 'returns a method object' do
      Foo.new.method(:foo).should be_an_instance_of(Method)
      Foo.new.method('foo').should be_an_instance_of(Method)
      Foo.new.method('foo').inspect.should =~ /^#<Method: Foo#foo.*>$/
      Foo.method('foo1').inspect.should =~ /^#<Method: Foo.foo1.*>$/
    end
  end
end

def method_with_kwargs1(a, b:)
  [a, b]
end

def method_with_kwargs2(a, b:, c:)
  [a, b, c]
end

def method_with_kwargs3(a, b:, c: 'c')
  [a, b, c]
end

def method_with_kwargs4(a, b: 'b', c:)
  [a, b, c]
end

def method_with_kwargs5(a, b: 'b')
  [a, b]
end

def method_with_kwargs6(a: 'a', b: 'b')
  [a, b]
end

def method_with_kwargs7(a:)
  [a]
end

def method_with_kwargs8(a:, b: nil)
  [a, b]
end

def method_with_kwargs9(a = 1, b: 2)
  [a, b]
end


describe 'method with keyword args' do
  it 'accepts keyword args' do
    method_with_kwargs1(1, b: 2).should == [1, 2]
    method_with_kwargs2(1, b: 2, c: 3).should == [1, 2, 3]
    method_with_kwargs3(1, b: 2, c: 3).should == [1, 2, 3]
    method_with_kwargs3(1, b: 2).should == [1, 2, 'c']
    method_with_kwargs4(1, b: 2, c: 3).should == [1, 2, 3]
    method_with_kwargs4(1, c: 3).should == [1, 'b', 3]
    method_with_kwargs5({ z: 1 }).should == [{ z: 1 }, 'b']
    method_with_kwargs6.should == ['a', 'b']
    method_with_kwargs6(a: 1).should == [1, 'b']
    method_with_kwargs6(a: 1, b: 2).should == [1, 2]
    method_with_kwargs8(a: 1).should == [1, nil]
    method_with_kwargs9.should == [1, 2]
    method_with_kwargs9('a').should == ['a', 2]
    method_with_kwargs9('a', b: 'b').should == ['a', 'b']
  end

  xit 'raises an error when there are too many positional arguments' do
    -> { method_with_kwargs1(1, 2, b: 3) }.should raise_error(ArgumentError, 'wrong number of arguments (given 2, expected 1; required keyword: b)')
  end

  it 'raises an error when a required keyword argument is not supplied' do
    -> { method_with_kwargs7 }.should raise_error(ArgumentError, "missing keyword: :a")
  end

  xit 'raises an error when an extra keyword argument is supplied' do
    -> { method_with_kwargs6(a: 1, b: 2, c: 3) }.should raise_error(ArgumentError, "unknown keyword: :c")
  end

  it 'raises an error when the method is not defined' do
    class Foo; end
    -> { Foo.new.not_a_method }.should raise_error(NoMethodError, /undefined method `not_a_method' for #<Foo:0x.+>/)
  end

  it 'does not loop infinitely when trying to call inspect on BasicObject' do
    -> { BasicObject.new.inspect }.should raise_error(NoMethodError, /undefined method `inspect' for #<BasicObject:0x.+>/)
  end
end

def returns_arg(arg)
  arg
end

describe 'safe navigation operator' do
  it 'returns nil if the receiver is nil, otherwise calls the method' do
    returns_arg(nil)&.first.should be_nil
    returns_arg([1])&.first.should == 1
  end
end