summaryrefslogtreecommitdiff
path: root/yarvtest/test_block.rb
blob: 87800da5f65c6f7e4336ba40171604a4683c4b39 (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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
require 'yarvtest/yarvtest'

class TestBlock < YarvTestBase
  def test_simple
    ae %q(
      def m
        yield
      end
      m{
        1
      }
    )
  end

  def test_param
    ae %q(
      def m
        yield 1
      end
      m{|ib|
        ib*2
      }
    )
    
    ae %q(
      def m
        yield 12345, 67890
      end
      m{|ib,jb|
        ib*2+jb
      }
    )
  end

  def test_param2
    ae %q{
      def iter
        yield 10
      end

      a = nil
      [iter{|a|
        a
      }, a]
    }
    ae %q{
      def iter
        yield 10
      end

      iter{|a|
        iter{|a|
          a + 1
        } + a
      }
    }
    ae %q{
      def iter
        yield 10, 20, 30, 40
      end

      a = b = c = d = nil
      iter{|a, b, c, d|
        [a, b, c, d]
      } + [a, b, c, d]
    }
    ae %q{
      def iter
        yield 10, 20, 30, 40
      end

      a = b = nil
      iter{|a, b, c, d|
        [a, b, c, d]
      } + [a, b]
    }
    ae %q{
      def iter
        yield 10, 20, 30, 40
      end

      a = nil
      iter{|a, $b, @c, d|
        [a, $b]
      } + [a, $b, @c]
    } if false # 1.9 doesn't support expr block parameters
  end

  def test_param3
    if false
      # TODO: Ruby 1.9 doesn't support expr block parameter
      ae %q{
        h = {}
        [1].each{|h[:foo]|}
        h
      }
      ae %q{
        obj = Object.new
        def obj.x=(y)
        $ans = y
      end
      [1].each{|obj.x|}
        $ans
      }
    end
  end

  def test_blocklocal
    ae %q{
      1.times{
        begin
          a = 1
        ensure
          foo = nil
        end
      }
    }
  end

  def test_simplenest
    ae %q(
      def m
        yield 123
      end
      m{|ib|
        m{|jb|
          ib*jb
        }
      }
    )
  end

  def test_simplenest2
    ae %q(
      def m a
        yield a
      end
      m(1){|ib|
        m(2){|jb|
          ib*jb
        }
      }
    )
  end

  def test_nest2
    ae %q(
      def m
        yield
      end
      def n
        yield
      end

      m{
        n{
          100
        }
      }
    )

    ae %q(
      def m
        yield 1
      end
      
      m{|ib|
        m{|jb|
          i = 20
        }
      }
    )

    ae %q(
      def m
        yield 1
      end
      
      m{|ib|
        m{|jb|
          ib = 20
          kb = 2
        }
      }
    )

    ae %q(
      def iter1
        iter2{
          yield
        }
      end
      
      def iter2
        yield
      end
      
      iter1{
        jb = 2
        iter1{
          jb = 3
        }
        jb
      }
    )
    
    ae %q(
      def iter1
        iter2{
          yield
        }
      end
      
      def iter2
        yield
      end
      
      iter1{
        jb = 2
        iter1{
          jb
        }
        jb
      }
    )
  end

  def test_ifunc
    ae %q{
      (1..3).to_a
    }

    ae %q{
      (1..3).map{|e|
        e * 4
      }
    }

    ae %q{
      class C
        include Enumerable
        def each
          [1,2,3].each{|e|
            yield e
          }
        end
      end
      
      C.new.to_a
    }

    ae %q{
      class C
        include Enumerable
        def each
          [1,2,3].each{|e|
            yield e
          }
        end
      end
      
      C.new.map{|e|
        e + 3
      }
    }
  end

  def test_times
    ae %q{
      sum = 0
      3.times{|ib|
        2.times{|jb|
          sum += ib + jb
        }}
      sum
    }
    ae %q{
      3.times{|bl|
        break 10
      }
    }
  end

  def test_for
    ae %q{
      sum = 0
      for x in [1, 2, 3]
        sum += x
      end
      sum
    }
    ae %q{
      sum = 0
      for x in (1..5)
        sum += x
      end
      sum
    }
    ae %q{
      sum = 0
      for x in []
        sum += x
      end
      sum
    }
    ae %q{
      ans = []
      1.times{
        for n in 1..3
          a = n
          ans << a
        end
      }
    }
    ae %q{
      ans = []
      for m in 1..3
        for n in 1..3
          a = [m, n]
          ans << a
        end
      end
    }
  end
  
  def test_unmatched_params
    ae %q{
      def iter
        yield 1,2,3
      end

      iter{|i, j|
        [i, j]
      }
    }
    ae %q{
      def iter
        yield 1
      end

      iter{|i, j|
        [i, j]
      }
    }
  end

  def test_rest
    # TODO: known bug
    #ae %q{
    #  def iter
    #    yield 1, 2
    #  end
    #
    #  iter{|a, |
    #    [a]
    #  }
    #}
    ae %q{
      def iter
        yield 1, 2
      end

      iter{|a, *b|
        [a, b]
      }
    }
    ae %q{
      def iter
        yield 1, 2
      end

      iter{|*a|
        [a]
      }
    }
    ae %q{
      def iter
        yield 1, 2
      end

      iter{|a, b, *c|
        [a, b, c]
      }
    }
    ae %q{
      def iter
        yield 1, 2
      end

      iter{|a, b, c, *d|
        [a, b, c, d]
      }
    }
  end

  def test_param_and_locals
    ae %q{
      $a = []
      
      def iter
        yield 1
      end
      
      def m
        x = iter{|x|
          $a << x
          y = 0
        }
      end
      m
      $a
    }
  end

  def test_c_break
    ae %q{
      [1,2,3].find{|x| x == 2}
    }
    ae %q{
      class E
        include Enumerable
        def each(&block)
          [1, 2, 3].each(&block)
        end
      end
      E.new.find {|x| x == 2 }
    }
  end
end