summaryrefslogtreecommitdiff
path: root/spec/ruby/language/block_spec.rb
blob: 42652152a1bdae6f5866f4f98de66b495272e922 (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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
require_relative '../spec_helper'
require_relative 'fixtures/block'

describe "A block yielded a single" do
  before :all do
    def m(a) yield a end
  end

  context "Array" do
    it "assigns the Array to a single argument" do
      m([1, 2]) { |a| a }.should == [1, 2]
    end

    it "receives the identical Array object" do
      ary = [1, 2]
      m(ary) { |a| a }.should equal(ary)
    end

    it "assigns the Array to a single rest argument" do
      m([1, 2, 3]) { |*a| a }.should == [[1, 2, 3]]
    end

    it "assigns the first element to a single argument with trailing comma" do
      m([1, 2]) { |a, | a }.should == 1
    end

    it "assigns elements to required arguments" do
      m([1, 2, 3]) { |a, b| [a, b] }.should == [1, 2]
    end

    it "assigns nil to unassigned required arguments" do
      m([1, 2]) { |a, *b, c, d| [a, b, c, d] }.should == [1, [], 2, nil]
    end

    it "assigns elements to optional arguments" do
      m([1, 2]) { |a=5, b=4, c=3| [a, b, c] }.should == [1, 2, 3]
    end

    it "assigns elements to post arguments" do
      m([1, 2]) { |a=5, b, c, d| [a, b, c, d] }.should == [5, 1, 2, nil]
    end

    ruby_version_is "3.2" do
      it "does not autosplat single argument to required arguments when a keyword rest argument is present" do
        m([1, 2]) { |a, **k| [a, k] }.should == [[1, 2], {}]
      end
    end

    ruby_version_is ''..."3.2" do
      # https://bugs.ruby-lang.org/issues/18633
      it "autosplats single argument to required arguments when a keyword rest argument is present" do
        m([1, 2]) { |a, **k| [a, k] }.should == [1, {}]
      end
    end

    ruby_version_is ''..."3.0" do
      it "assigns elements to mixed argument types" do
        suppress_keyword_warning do
          result = m([1, 2, 3, {x: 9}]) { |a, b=5, *c, d, e: 2, **k| [a, b, c, d, e, k] }
          result.should == [1, 2, [], 3, 2, {x: 9}]
        end
      end

      it "assigns symbol keys from a Hash to keyword arguments" do
        suppress_keyword_warning do
          result = m(["a" => 1, a: 10]) { |a=nil, **b| [a, b] }
          result.should == [{"a" => 1}, a: 10]
        end
      end

      it "assigns symbol keys from a Hash returned by #to_hash to keyword arguments" do
        suppress_keyword_warning do
          obj = mock("coerce block keyword arguments")
          obj.should_receive(:to_hash).and_return({"a" => 1, b: 2})

          result = m([obj]) { |a=nil, **b| [a, b] }
          result.should == [{"a" => 1}, b: 2]
        end
      end
    end

    ruby_version_is "3.0" do
      it "assigns elements to mixed argument types" do
        result = m([1, 2, 3, {x: 9}]) { |a, b=5, *c, d, e: 2, **k| [a, b, c, d, e, k] }
        result.should == [1, 2, [3], {x: 9}, 2, {}]
      end

      it "does not treat final Hash as keyword arguments and does not autosplat" do
        result = m(["a" => 1, a: 10]) { |a=nil, **b| [a, b] }
        result.should == [[{"a" => 1, a: 10}], {}]
      end

      it "does not call #to_hash on final argument to get keyword arguments and does not autosplat" do
        suppress_keyword_warning do
          obj = mock("coerce block keyword arguments")
          obj.should_not_receive(:to_hash)

          result = m([obj]) { |a=nil, **b| [a, b] }
          result.should == [[obj], {}]
        end
      end
    end

    ruby_version_is ""...'3.0' do
      it "calls #to_hash on the argument but ignores result when optional argument and keyword argument accepted" do
        obj = mock("coerce block keyword arguments")
        obj.should_receive(:to_hash).and_return({"a" => 1, "b" => 2})

        result = m([obj]) { |a=nil, **b| [a, b] }
        result.should == [obj, {}]
      end
    end

    ruby_version_is "3.0" do
      it "does not call #to_hash on the argument when optional argument and keyword argument accepted and does not autosplat" do
        obj = mock("coerce block keyword arguments")
        obj.should_not_receive(:to_hash)

        result = m([obj]) { |a=nil, **b| [a, b] }
        result.should == [[obj], {}]
      end
    end

    describe "when non-symbol keys are in a keyword arguments Hash" do
      ruby_version_is ""..."3.0" do
        it "separates non-symbol keys and symbol keys" do
          suppress_keyword_warning do
            result = m(["a" => 10, b: 2]) { |a=nil, **b| [a, b] }
            result.should == [{"a" => 10}, {b: 2}]
          end
        end
      end
      ruby_version_is "3.0" do
        it "does not separate non-symbol keys and symbol keys and does not autosplat" do
          suppress_keyword_warning do
            result = m(["a" => 10, b: 2]) { |a=nil, **b| [a, b] }
            result.should == [[{"a" => 10, b: 2}], {}]
          end
        end
      end
    end

    ruby_version_is ""..."3.0" do
      it "does not treat hashes with string keys as keyword arguments" do
        result = m(["a" => 10]) { |a = nil, **b| [a, b] }
        result.should == [{"a" => 10}, {}]
      end
    end

    ruby_version_is "3.0" do
      it "does not treat hashes with string keys as keyword arguments and does not autosplat" do
        result = m(["a" => 10]) { |a = nil, **b| [a, b] }
        result.should == [[{"a" => 10}], {}]
      end
    end

    ruby_version_is ''...'3.0' do
      it "calls #to_hash on the last element if keyword arguments are present" do
        suppress_keyword_warning do
          obj = mock("destructure block keyword arguments")
          obj.should_receive(:to_hash).and_return({x: 9})

          result = m([1, 2, 3, obj]) { |a, *b, c, **k| [a, b, c, k] }
          result.should == [1, [2], 3, {x: 9}]
        end
      end

      it "assigns the last element to a non-keyword argument if #to_hash returns nil" do
        suppress_keyword_warning do
          obj = mock("destructure block keyword arguments")
          obj.should_receive(:to_hash).and_return(nil)

          result = m([1, 2, 3, obj]) { |a, *b, c, **k| [a, b, c, k] }
          result.should == [1, [2, 3], obj, {}]
        end
      end

      it "calls #to_hash on the last element when there are more arguments than parameters" do
        suppress_keyword_warning do
          x = mock("destructure matching block keyword argument")
          x.should_receive(:to_hash).and_return({x: 9})

          result = m([1, 2, 3, {y: 9}, 4, 5, x]) { |a, b=5, c, **k| [a, b, c, k] }
          result.should == [1, 2, 3, {x: 9}]
        end
      end

      it "raises a TypeError if #to_hash does not return a Hash" do
        obj = mock("destructure block keyword arguments")
        obj.should_receive(:to_hash).and_return(1)

        -> { m([1, 2, 3, obj]) { |a, *b, c, **k| } }.should raise_error(TypeError)
      end

      it "raises the error raised inside #to_hash" do
        obj = mock("destructure block keyword arguments")
        error = RuntimeError.new("error while converting to a hash")
        obj.should_receive(:to_hash).and_raise(error)

        -> { m([1, 2, 3, obj]) { |a, *b, c, **k| } }.should raise_error(error)
      end
    end

    ruby_version_is '3.0' do
      it "does not call #to_hash on the last element if keyword arguments are present" do
        obj = mock("destructure block keyword arguments")
        obj.should_not_receive(:to_hash)

        result = m([1, 2, 3, obj]) { |a, *b, c, **k| [a, b, c, k] }
        result.should == [1, [2, 3], obj, {}]
      end

      it "does not call #to_hash on the last element when there are more arguments than parameters" do
        x = mock("destructure matching block keyword argument")
        x.should_not_receive(:to_hash)

        result = m([1, 2, 3, {y: 9}, 4, 5, x]) { |a, b=5, c, **k| [a, b, c, k] }
        result.should == [1, 2, 3, {}]
      end
    end

    it "does not call #to_ary on the Array" do
      ary = [1, 2]
      ary.should_not_receive(:to_ary)

      m(ary) { |a, b, c| [a, b, c] }.should == [1, 2, nil]
    end
  end

  context "Object" do
    it "calls #to_ary on the object when taking multiple arguments" do
      obj = mock("destructure block arguments")
      obj.should_receive(:to_ary).and_return([1, 2])

      m(obj) { |a, b, c| [a, b, c] }.should == [1, 2, nil]
    end

    it "does not call #to_ary when not taking any arguments" do
      obj = mock("destructure block arguments")
      obj.should_not_receive(:to_ary)

      m(obj) { 1 }.should == 1
    end

    it "does not call #to_ary on the object when taking a single argument" do
      obj = mock("destructure block arguments")
      obj.should_not_receive(:to_ary)

      m(obj) { |a| a }.should == obj
    end

    it "does not call #to_ary on the object when taking a single rest argument" do
      obj = mock("destructure block arguments")
      obj.should_not_receive(:to_ary)

      m(obj) { |*a| a }.should == [obj]
    end

    it "receives the object if #to_ary returns nil" do
      obj = mock("destructure block arguments")
      obj.should_receive(:to_ary).and_return(nil)

      m(obj) { |a, b, c| [a, b, c] }.should == [obj, nil, nil]
    end

    it "raises a TypeError if #to_ary does not return an Array" do
      obj = mock("destructure block arguments")
      obj.should_receive(:to_ary).and_return(1)

      -> { m(obj) { |a, b| } }.should raise_error(TypeError)
    end
  end
end

# TODO: rewrite
describe "A block" do
  before :each do
    @y = BlockSpecs::Yielder.new
  end

  it "captures locals from the surrounding scope" do
    var = 1
    @y.z { var }.should == 1
  end

  it "allows for a leading space before the arguments" do
    res = @y.s (:a){ 1 }
    res.should == 1
  end

  it "allows to define a block variable with the same name as the enclosing block" do
    o = BlockSpecs::OverwriteBlockVariable.new
    o.z { 1 }.should == 1
  end

  it "does not capture a local when an argument has the same name" do
    var = 1
    @y.s(2) { |var| var }.should == 2
    var.should == 1
  end

  it "does not capture a local when the block argument has the same name" do
    var = 1
    proc { |&var|
      var.call(2)
    }.call { |x| x }.should == 2
    var.should == 1
  end

  describe "taking zero arguments" do
    it "does not raise an exception when no values are yielded" do
      @y.z { 1 }.should == 1
    end

    it "does not raise an exception when values are yielded" do
      @y.s(0) { 1 }.should == 1
    end

    it "may include a rescue clause" do
      eval("@y.z do raise ArgumentError; rescue ArgumentError; 7; end").should == 7
    end
  end

  describe "taking || arguments" do
    it "does not raise an exception when no values are yielded" do
      @y.z { || 1 }.should == 1
    end

    it "does not raise an exception when values are yielded" do
      @y.s(0) { || 1 }.should == 1
    end

    it "may include a rescue clause" do
      eval('@y.z do || raise ArgumentError; rescue ArgumentError; 7; end').should == 7
    end
  end

  describe "taking |a| arguments" do
    it "assigns nil to the argument when no values are yielded" do
      @y.z { |a| a }.should be_nil
    end

    it "assigns the value yielded to the argument" do
      @y.s(1) { |a| a }.should == 1
    end

    it "does not call #to_ary to convert a single yielded object to an Array" do
      obj = mock("block yield to_ary")
      obj.should_not_receive(:to_ary)

      @y.s(obj) { |a| a }.should equal(obj)
    end

    it "assigns the first value yielded to the argument" do
      @y.m(1, 2) { |a| a }.should == 1
    end

    it "does not destructure a single Array value" do
      @y.s([1, 2]) { |a| a }.should == [1, 2]
    end

    it "may include a rescue clause" do
      eval('@y.s(1) do |x| raise ArgumentError; rescue ArgumentError; 7; end').should == 7
    end
  end

  describe "taking |a, b| arguments" do
    it "assigns nil to the arguments when no values are yielded" do
      @y.z { |a, b| [a, b] }.should == [nil, nil]
    end

    it "assigns one value yielded to the first argument" do
      @y.s(1) { |a, b| [a, b] }.should == [1, nil]
    end

    it "assigns the first two values yielded to the arguments" do
      @y.m(1, 2, 3) { |a, b| [a, b] }.should == [1, 2]
    end

    it "does not destructure an Array value as one of several values yielded" do
      @y.m([1, 2], 3, 4) { |a, b| [a, b] }.should == [[1, 2], 3]
    end

    it "assigns 'nil' and 'nil' to the arguments when a single, empty Array is yielded" do
      @y.s([]) { |a, b| [a, b] }.should == [nil, nil]
    end

    it "assigns the element of a single element Array to the first argument" do
      @y.s([1]) { |a, b| [a, b] }.should == [1, nil]
      @y.s([nil]) { |a, b| [a, b] }.should == [nil, nil]
      @y.s([[]]) { |a, b| [a, b] }.should == [[], nil]
    end

    it "destructures a single Array value yielded" do
      @y.s([1, 2, 3]) { |a, b| [a, b] }.should == [1, 2]
    end

    it "destructures a splatted Array" do
      @y.r([[]]) { |a, b| [a, b] }.should == [nil, nil]
      @y.r([[1]]) { |a, b| [a, b] }.should == [1, nil]
    end

    it "calls #to_ary to convert a single yielded object to an Array" do
      obj = mock("block yield to_ary")
      obj.should_receive(:to_ary).and_return([1, 2])

      @y.s(obj) { |a, b| [a, b] }.should == [1, 2]
    end

    it "does not call #to_ary if the single yielded object is an Array" do
      obj = [1, 2]
      obj.should_not_receive(:to_ary)

      @y.s(obj) { |a, b| [a, b] }.should == [1, 2]
    end

    it "does not call #to_ary if the object does not respond to #to_ary" do
      obj = mock("block yield no to_ary")

      @y.s(obj) { |a, b| [a, b] }.should == [obj, nil]
    end

    it "raises a TypeError if #to_ary does not return an Array" do
      obj = mock("block yield to_ary invalid")
      obj.should_receive(:to_ary).and_return(1)

      -> { @y.s(obj) { |a, b| } }.should raise_error(TypeError)
    end

    it "raises the original exception if #to_ary raises an exception" do
      obj = mock("block yield to_ary raising an exception")
      obj.should_receive(:to_ary).and_raise(ZeroDivisionError)

      -> { @y.s(obj) { |a, b| } }.should raise_error(ZeroDivisionError)
    end

  end

  describe "taking |a, *b| arguments" do
    it "assigns 'nil' and '[]' to the arguments when no values are yielded" do
      @y.z { |a, *b| [a, b] }.should == [nil, []]
    end

    it "assigns all yielded values after the first to the rest argument" do
      @y.m(1, 2, 3) { |a, *b| [a, b] }.should == [1, [2, 3]]
    end

    it "assigns 'nil' and '[]' to the arguments when a single, empty Array is yielded" do
      @y.s([]) { |a, *b| [a, b] }.should == [nil, []]
    end

    it "assigns the element of a single element Array to the first argument" do
      @y.s([1]) { |a, *b| [a, b] }.should == [1, []]
      @y.s([nil]) { |a, *b| [a, b] }.should == [nil, []]
      @y.s([[]]) { |a, *b| [a, b] }.should == [[], []]
    end

    it "destructures a splatted Array" do
      @y.r([[]]) { |a, *b| [a, b] }.should == [nil, []]
      @y.r([[1]]) { |a, *b| [a, b] }.should == [1, []]
    end

    it "destructures a single Array value assigning the remaining values to the rest argument" do
      @y.s([1, 2, 3]) { |a, *b| [a, b] }.should == [1, [2, 3]]
    end

    it "calls #to_ary to convert a single yielded object to an Array" do
      obj = mock("block yield to_ary")
      obj.should_receive(:to_ary).and_return([1, 2])

      @y.s(obj) { |a, *b| [a, b] }.should == [1, [2]]
    end

    it "does not call #to_ary if the single yielded object is an Array" do
      obj = [1, 2]
      obj.should_not_receive(:to_ary)

      @y.s(obj) { |a, *b| [a, b] }.should == [1, [2]]
    end

    it "does not call #to_ary if the object does not respond to #to_ary" do
      obj = mock("block yield no to_ary")

      @y.s(obj) { |a, *b| [a, b] }.should == [obj, []]
    end

    it "raises a TypeError if #to_ary does not return an Array" do
      obj = mock("block yield to_ary invalid")
      obj.should_receive(:to_ary).and_return(1)

      -> { @y.s(obj) { |a, *b| } }.should raise_error(TypeError)
    end
  end

  describe "taking |*| arguments" do
    it "does not raise an exception when no values are yielded" do
      @y.z { |*| 1 }.should == 1
    end

    it "does not raise an exception when values are yielded" do
      @y.s(0) { |*| 1 }.should == 1
    end

    it "does not call #to_ary if the single yielded object is an Array" do
      obj = [1, 2]
      obj.should_not_receive(:to_ary)

      @y.s(obj) { |*| 1 }.should == 1
    end

    it "does not call #to_ary if the object does not respond to #to_ary" do
      obj = mock("block yield no to_ary")

      @y.s(obj) { |*| 1 }.should == 1
    end

    it "does not call #to_ary to convert a single yielded object to an Array" do
      obj = mock("block yield to_ary")
      obj.should_not_receive(:to_ary)

      @y.s(obj) { |*| 1 }.should == 1
    end
  end

  describe "taking |*a| arguments" do
    it "assigns '[]' to the argument when no values are yielded" do
      @y.z { |*a| a }.should == []
    end

    it "assigns a single value yielded to the argument as an Array" do
      @y.s(1) { |*a| a }.should == [1]
    end

    it "assigns all the values passed to the argument as an Array" do
      @y.m(1, 2, 3) { |*a| a }.should == [1, 2, 3]
    end

    it "assigns '[[]]' to the argument when passed an empty Array" do
      @y.s([]) { |*a| a }.should == [[]]
    end

    it "assigns a single Array value passed to the argument by wrapping it in an Array" do
      @y.s([1, 2, 3]) { |*a| a }.should == [[1, 2, 3]]
    end

    it "does not call #to_ary if the single yielded object is an Array" do
      obj = [1, 2]
      obj.should_not_receive(:to_ary)

      @y.s(obj) { |*a| a }.should == [[1, 2]]
    end

    it "does not call #to_ary if the object does not respond to #to_ary" do
      obj = mock("block yield no to_ary")

      @y.s(obj) { |*a| a }.should == [obj]
    end

    it "does not call #to_ary to convert a single yielded object to an Array" do
      obj = mock("block yield to_ary")
      obj.should_not_receive(:to_ary)

      @y.s(obj) { |*a| a }.should == [obj]
    end
  end

  describe "taking |a, | arguments" do
    it "assigns nil to the argument when no values are yielded" do
      @y.z { |a, | a }.should be_nil
    end

    it "assigns the argument a single value yielded" do
      @y.s(1) { |a, | a }.should == 1
    end

    it "assigns the argument the first value yielded" do
      @y.m(1, 2) { |a, | a }.should == 1
    end

    it "assigns the argument the first of several values yielded when it is an Array" do
      @y.m([1, 2], 3) { |a, | a }.should == [1, 2]
    end

    it "assigns nil to the argument when passed an empty Array" do
      @y.s([]) { |a, | a }.should be_nil
    end

    it "assigns the argument the first element of the Array when passed a single Array" do
      @y.s([1, 2]) { |a, | a }.should == 1
    end

    it "calls #to_ary to convert a single yielded object to an Array" do
      obj = mock("block yield to_ary")
      obj.should_receive(:to_ary).and_return([1, 2])

      @y.s(obj) { |a, | a }.should == 1
    end

    it "does not call #to_ary if the single yielded object is an Array" do
      obj = [1, 2]
      obj.should_not_receive(:to_ary)

      @y.s(obj) { |a, | a }.should == 1
    end

    it "does not call #to_ary if the object does not respond to #to_ary" do
      obj = mock("block yield no to_ary")

      @y.s(obj) { |a, | a }.should == obj
    end

    it "raises a TypeError if #to_ary does not return an Array" do
      obj = mock("block yield to_ary invalid")
      obj.should_receive(:to_ary).and_return(1)

      -> { @y.s(obj) { |a, | } }.should raise_error(TypeError)
    end
  end

  describe "taking |(a, b)| arguments" do
    it "assigns nil to the arguments when yielded no values" do
      @y.z { |(a, b)| [a, b] }.should == [nil, nil]
    end

    it "destructures a single Array value yielded" do
      @y.s([1, 2]) { |(a, b)| [a, b] }.should == [1, 2]
    end

    it "destructures a single Array value yielded when shadowing an outer variable" do
      a = 9
      @y.s([1, 2]) { |(a, b)| [a, b] }.should == [1, 2]
    end

    it "calls #to_ary to convert a single yielded object to an Array" do
      obj = mock("block yield to_ary")
      obj.should_receive(:to_ary).and_return([1, 2])

      @y.s(obj) { |(a, b)| [a, b] }.should == [1, 2]
    end

    it "does not call #to_ary if the single yielded object is an Array" do
      obj = [1, 2]
      obj.should_not_receive(:to_ary)

      @y.s(obj) { |(a, b)| [a, b] }.should == [1, 2]
    end

    it "does not call #to_ary if the object does not respond to #to_ary" do
      obj = mock("block yield no to_ary")

      @y.s(obj) { |(a, b)| [a, b] }.should == [obj, nil]
    end

    it "raises a TypeError if #to_ary does not return an Array" do
      obj = mock("block yield to_ary invalid")
      obj.should_receive(:to_ary).and_return(1)

      -> { @y.s(obj) { |(a, b)| } }.should raise_error(TypeError)
    end
  end

  describe "taking |(a, b), c| arguments" do
    it "assigns nil to the arguments when yielded no values" do
      @y.z { |(a, b), c| [a, b, c] }.should == [nil, nil, nil]
    end

    it "destructures a single one-level Array value yielded" do
      @y.s([1, 2]) { |(a, b), c| [a, b, c] }.should == [1, nil, 2]
    end

    it "destructures a single multi-level Array value yielded" do
      @y.s([[1, 2, 3], 4]) { |(a, b), c| [a, b, c] }.should == [1, 2, 4]
    end

    it "calls #to_ary to convert a single yielded object to an Array" do
      obj = mock("block yield to_ary")
      obj.should_receive(:to_ary).and_return([1, 2])

      @y.s(obj) { |(a, b), c| [a, b, c] }.should == [1, nil, 2]
    end

    it "does not call #to_ary if the single yielded object is an Array" do
      obj = [1, 2]
      obj.should_not_receive(:to_ary)

      @y.s(obj) { |(a, b), c| [a, b, c] }.should == [1, nil, 2]
    end

    it "does not call #to_ary if the object does not respond to #to_ary" do
      obj = mock("block yield no to_ary")

      @y.s(obj) { |(a, b), c| [a, b, c] }.should == [obj, nil, nil]
    end

    it "raises a TypeError if #to_ary does not return an Array" do
      obj = mock("block yield to_ary invalid")
      obj.should_receive(:to_ary).and_return(1)

      -> { @y.s(obj) { |(a, b), c| } }.should raise_error(TypeError)
    end
  end

  describe "taking nested |a, (b, (c, d))|" do
    it "assigns nil to the arguments when yielded no values" do
      @y.m { |a, (b, (c, d))| [a, b, c, d] }.should == [nil, nil, nil, nil]
    end

    it "destructures separate yielded values" do
      @y.m(1, 2) { |a, (b, (c, d))| [a, b, c, d] }.should == [1, 2, nil, nil]
    end

    it "destructures a nested Array value yielded" do
      @y.m(1, [2, 3]) { |a, (b, (c, d))| [a, b, c, d] }.should == [1, 2, 3, nil]
    end

    it "destructures a single multi-level Array value yielded" do
      @y.m(1, [2, [3, 4]]) { |a, (b, (c, d))| [a, b, c, d] }.should == [1, 2, 3, 4]
    end
  end

  describe "taking nested |a, ((b, c), d)|" do
    it "assigns nil to the arguments when yielded no values" do
      @y.m { |a, ((b, c), d)| [a, b, c, d] }.should == [nil, nil, nil, nil]
    end

    it "destructures separate yielded values" do
      @y.m(1, 2) { |a, ((b, c), d)| [a, b, c, d] }.should == [1, 2, nil, nil]
    end

    it "destructures a nested value yielded" do
      @y.m(1, [2, 3]) { |a, ((b, c), d)| [a, b, c, d] }.should == [1, 2, nil, 3]
    end

    it "destructures a single multi-level Array value yielded" do
      @y.m(1, [[2, 3], 4]) { |a, ((b, c), d)| [a, b, c, d] }.should == [1, 2, 3, 4]
    end
  end

  describe "taking |*a, b:|" do
    it "merges the hash into the splatted array" do
      @y.k { |*a, b:| [a, b] }.should == [[], true]
    end
  end

  describe "arguments with _" do
    it "extracts arguments with _" do
      @y.m([[1, 2, 3], 4]) { |(_, a, _), _| a }.should == 2
      @y.m([1, [2, 3, 4]]) { |_, (_, a, _)| a }.should == 3
    end

    it "assigns the first variable named" do
      @y.m(1, 2) { |_, _| _ }.should == 1
    end
  end

  describe "taking identically-named arguments" do
    it "raises a SyntaxError for standard arguments" do
      -> { eval "lambda { |x,x| }" }.should raise_error(SyntaxError)
      -> { eval "->(x,x) {}" }.should raise_error(SyntaxError)
      -> { eval "Proc.new { |x,x| }" }.should raise_error(SyntaxError)
    end

    it "accepts unnamed arguments" do
      eval("lambda { |_,_| }").should be_an_instance_of(Proc)
      eval("->(_,_) {}").should be_an_instance_of(Proc)
      eval("Proc.new { |_,_| }").should be_an_instance_of(Proc)
    end
  end
end

describe "Block-local variables" do
  it "are introduced with a semi-colon in the parameter list" do
    [1].map {|one; bl| bl }.should == [nil]
  end

  it "can be specified in a comma-separated list after the semi-colon" do
    [1].map {|one; bl, bl2| [bl, bl2] }.should == [[nil, nil]]
  end

  it "can not have the same name as one of the standard parameters" do
    -> { eval "[1].each {|foo; foo| }" }.should raise_error(SyntaxError)
    -> { eval "[1].each {|foo, bar; glark, bar| }" }.should raise_error(SyntaxError)
  end

  it "can not be prefixed with an asterisk" do
    -> { eval "[1].each {|foo; *bar| }" }.should raise_error(SyntaxError)
    -> do
      eval "[1].each {|foo, bar; glark, *fnord| }"
    end.should raise_error(SyntaxError)
  end

  it "can not be prefixed with an ampersand" do
    -> { eval "[1].each {|foo; &bar| }" }.should raise_error(SyntaxError)
    -> do
      eval "[1].each {|foo, bar; glark, &fnord| }"
    end.should raise_error(SyntaxError)
  end

  it "can not be assigned default values" do
    -> { eval "[1].each {|foo; bar=1| }" }.should raise_error(SyntaxError)
    -> do
      eval "[1].each {|foo, bar; glark, fnord=:fnord| }"
    end.should raise_error(SyntaxError)
  end

  it "need not be preceded by standard parameters" do
    [1].map {|; foo| foo }.should == [nil]
    [1].map {|; glark, bar| [glark, bar] }.should == [[nil, nil]]
  end

  it "only allow a single semi-colon in the parameter list" do
    -> { eval "[1].each {|foo; bar; glark| }" }.should raise_error(SyntaxError)
    -> { eval "[1].each {|; bar; glark| }" }.should raise_error(SyntaxError)
  end

  it "override shadowed variables from the outer scope" do
    out = :out
    [1].each {|; out| out = :in }
    out.should == :out

    a = :a
    b = :b
    c = :c
    d = :d
    {ant: :bee}.each_pair do |a, b; c, d|
      a = :A
      b = :B
      c = :C
      d = :D
    end
    a.should == :a
    b.should == :b
    c.should == :c
    d.should == :d
  end

  it "are not automatically instantiated in the outer scope" do
    defined?(glark).should be_nil
    [1].each {|;glark| 1}
    defined?(glark).should be_nil
  end

  it "are automatically instantiated in the block" do
    [1].each do |;glark|
      glark.should be_nil
    end
  end

  it "are visible in deeper scopes before initialization" do
    [1].each {|;glark|
      [1].each {
        defined?(glark).should_not be_nil
        glark = 1
      }
      glark.should == 1
    }
  end
end

describe "Post-args" do
  it "appear after a splat" do
    proc do |*a, b|
      [a, b]
    end.call(1, 2, 3).should == [[1, 2], 3]

    proc do |*a, b, c|
      [a, b, c]
    end.call(1, 2, 3).should == [[1], 2, 3]

    proc do |*a, b, c, d|
      [a, b, c, d]
    end.call(1, 2, 3).should == [[], 1, 2, 3]
  end

  it "are required for a lambda" do
    -> {
      -> *a, b do
        [a, b]
      end.call
    }.should raise_error(ArgumentError)
  end

  it "are assigned to nil when not enough arguments are given to a proc" do
    proc do |a, *b, c|
      [a, b, c]
    end.call.should == [nil, [], nil]
  end

  describe "with required args" do

    it "gathers remaining args in the splat" do
      proc do |a, *b, c|
        [a, b, c]
      end.call(1, 2, 3).should == [1, [2], 3]
    end

    it "has an empty splat when there are no remaining args" do
      proc do |a, b, *c, d|
        [a, b, c, d]
      end.call(1, 2, 3).should == [1, 2, [], 3]

      proc do |a, *b, c, d|
        [a, b, c, d]
      end.call(1, 2, 3).should == [1, [], 2, 3]
    end
  end

  describe "with optional args" do

    it "gathers remaining args in the splat" do
      proc do |a=5, *b, c|
        [a, b, c]
      end.call(1, 2, 3).should == [1, [2], 3]
    end

    it "overrides the optional arg before gathering in the splat" do
      proc do |a=5, *b, c|
        [a, b, c]
      end.call(2, 3).should == [2, [], 3]

      proc do |a=5, b=6, *c, d|
        [a, b, c, d]
      end.call(1, 2, 3).should == [1, 2, [], 3]

      proc do |a=5, *b, c, d|
        [a, b, c, d]
      end.call(1, 2, 3).should == [1, [], 2, 3]
    end

    it "uses the required arg before the optional and the splat" do
      proc do |a=5, *b, c|
        [a, b, c]
      end.call(3).should == [5, [], 3]

      proc do |a=5, b=6, *c, d|
        [a, b, c, d]
      end.call(3).should == [5, 6, [], 3]

      proc do |a=5, *b, c, d|
        [a, b, c, d]
      end.call(2, 3).should == [5, [], 2, 3]
    end

    it "overrides the optional args from left to right before gathering the splat" do
      proc do |a=5, b=6, *c, d|
        [a, b, c, d]
      end.call(2, 3).should == [2, 6, [], 3]
    end

    describe "with a circular argument reference" do
      it "raises a SyntaxError if using an existing local with the same name as the argument" do
        a = 1
        -> {
          @proc = eval "proc { |a=a| a }"
        }.should raise_error(SyntaxError)
      end

      it "raises a SyntaxError if there is an existing method with the same name as the argument" do
        def a; 1; end
        -> {
          @proc = eval "proc { |a=a| a }"
        }.should raise_error(SyntaxError)
      end

      it "calls an existing method with the same name as the argument if explicitly using ()" do
        def a; 1; end
        proc { |a=a()| a }.call.should == 1
      end
    end
  end

  describe "with pattern matching" do
    it "extracts matched blocks with post arguments" do
      proc do |(a, *b, c), d, e|
        [a, b, c, d, e]
      end.call([1, 2, 3, 4], 5, 6).should == [1, [2, 3], 4, 5, 6]
    end

    it "allows empty splats" do
      proc do |a, (*), b|
        [a, b]
      end.call([1, 2, 3]).should == [1, 3]
    end
  end
end