summaryrefslogtreecommitdiff
path: root/spec/ruby/language/variables_spec.rb
blob: ce072baa2c4b6d97c570a258d66484d2ea43879e (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
require_relative '../spec_helper'
require_relative 'fixtures/variables'

describe "Multiple assignment" do
  context "with a single RHS value" do
    it "assigns a simple MLHS" do
      (a, b, c = 1).should == 1
      [a, b, c].should == [1, nil, nil]
    end

    it "calls #to_ary to convert an Object RHS when assigning a simple MLHS" do
      x = mock("multi-assign single RHS")
      x.should_receive(:to_ary).and_return([1, 2])

      (a, b, c = x).should == x
      [a, b, c].should == [1, 2, nil]
    end

    it "calls #to_ary if it is private" do
      x = mock("multi-assign single RHS")
      x.should_receive(:to_ary).and_return([1, 2])
      class << x; private :to_ary; end

      (a, b, c = x).should == x
      [a, b, c].should == [1, 2, nil]
    end

    it "does not call #to_ary if #respond_to? returns false" do
      x = mock("multi-assign single RHS")
      x.should_receive(:respond_to?).with(:to_ary, true).and_return(false)
      x.should_not_receive(:to_ary)

      (a, b, c = x).should == x
      [a, b, c].should == [x, nil, nil]
    end

    it "wraps the Object in an Array if #to_ary returns nil" do
      x = mock("multi-assign single RHS")
      x.should_receive(:to_ary).and_return(nil)

      (a, b, c = x).should == x
      [a, b, c].should == [x, nil, nil]
    end

    it "raises a TypeError of #to_ary does not return an Array" do
      x = mock("multi-assign single RHS")
      x.should_receive(:to_ary).and_return(1)

      -> { a, b, c = x }.should raise_error(TypeError)
    end

    it "does not call #to_a to convert an Object RHS when assigning a simple MLHS" do
      x = mock("multi-assign single RHS")
      x.should_not_receive(:to_a)

      (a, b, c = x).should == x
      [a, b, c].should == [x, nil, nil]
    end

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

      (a, b = x).should == x
      [a, b].should == [1, 2]
    end

    it "does not call #to_a on an Array instance" do
      x = [1, 2]
      x.should_not_receive(:to_a)

      (a, b = x).should == x
      [a, b].should == [1, 2]
    end

    it "returns the RHS when it is an Array" do
      ary = [1, 2]

      x = (a, b = ary)
      x.should equal(ary)
    end

    it "returns the RHS when it is an Array subclass" do
      cls = Class.new(Array)
      ary = cls.new [1, 2]

      x = (a, b = ary)
      x.should equal(ary)
    end

    it "does not call #to_ary on an Array subclass instance" do
      x = Class.new(Array).new [1, 2]
      x.should_not_receive(:to_ary)

      (a, b = x).should == x
      [a, b].should == [1, 2]
    end

    it "does not call #to_a on an Array subclass instance" do
      x = Class.new(Array).new [1, 2]
      x.should_not_receive(:to_a)

      (a, b = x).should == x
      [a, b].should == [1, 2]
    end

    it "assigns a MLHS with a trailing comma" do
      a, = 1
      b, c, = []
      [a, b, c].should == [1, nil, nil]
    end

    it "assigns a single LHS splat" do
      (*a = 1).should == 1
      a.should == [1]
    end

    it "calls #to_ary to convert an Object RHS" do
      x = mock("multi-assign splat")
      x.should_receive(:to_ary).and_return([1, 2])

      (*a = x).should == x
      a.should == [1, 2]
    end

    it "raises a TypeError if #to_ary does not return an Array" do
      x = mock("multi-assign splat")
      x.should_receive(:to_ary).and_return(1)

      -> { *a = x }.should raise_error(TypeError)
    end

    it "does not call #to_ary on an Array subclass" do
      cls = Class.new(Array)
      ary = cls.new [1, 2]
      ary.should_not_receive(:to_ary)

      (*a = ary).should == [1, 2]
      a.should == [1, 2]
    end

    it "assigns an Array when the RHS is an Array subclass" do
      cls = Class.new(Array)
      ary = cls.new [1, 2]

      x = (*a = ary)
      x.should equal(ary)
      a.should be_an_instance_of(Array)
    end

    it "calls #to_ary to convert an Object RHS with MLHS" do
      x = mock("multi-assign splat")
      x.should_receive(:to_ary).and_return([1, 2])

      (a, *b, c = x).should == x
      [a, b, c].should == [1, [], 2]
    end

    it "raises a TypeError if #to_ary does not return an Array with MLHS" do
      x = mock("multi-assign splat")
      x.should_receive(:to_ary).and_return(1)

      -> { a, *b, c = x }.should raise_error(TypeError)
    end

    it "does not call #to_a to convert an Object RHS with a MLHS" do
      x = mock("multi-assign splat")
      x.should_not_receive(:to_a)

      (a, *b = x).should == x
      [a, b].should == [x, []]
    end

    it "assigns a MLHS with leading splat" do
      (*a, b, c = 1).should == 1
      [a, b, c].should == [[], 1, nil]
    end

    it "assigns a MLHS with a middle splat" do
      a, b, *c, d, e = 1
      [a, b, c, d, e].should == [1, nil, [], nil, nil]
    end

    it "assigns a MLHS with a trailing splat" do
      a, b, *c = 1
      [a, b, c].should == [1, nil, []]
    end

    it "assigns a grouped LHS without splat" do
      ((a, b), c), (d, (e,), (f, (g, h))) = 1
      [a, b, c, d, e, f, g, h].should == [1, nil, nil, nil, nil, nil, nil, nil]
    end

    it "assigns a single grouped LHS splat" do
      (*a) = nil
      a.should == [nil]
    end

    it "assigns a grouped LHS with splats" do
      (a, *b), c, (*d, (e, *f, g)) = 1
      [a, b, c, d, e, f, g].should == [1, [], nil, [], nil, [], nil]
    end

    it "consumes values for an anonymous splat" do
      (* = 1).should == 1
    end

    it "consumes values for a grouped anonymous splat" do
      ((*) = 1).should == 1
    end

    it "does not mutate a RHS Array" do
      x = [1, 2, 3, 4]
      a, *b, c, d = x
      [a, b, c, d].should == [1, [2], 3, 4]
      x.should == [1, 2, 3, 4]
    end

    it "assigns values from a RHS method call" do
      def x() 1 end

      (a, b = x).should == 1
      [a, b].should == [1, nil]
    end

    it "assigns values from a RHS method call with arguments" do
      def x(a) a end

      (a, b = x []).should == []
      [a, b].should == [nil, nil]
    end

    it "assigns values from a RHS method call with receiver" do
      x = mock("multi-assign attributes")
      x.should_receive(:m).and_return([1, 2, 3])

      a, b = x.m
      [a, b].should == [1, 2]
    end

    it "calls #to_ary on the value returned by the method call" do
      y = mock("multi-assign method return value")
      y.should_receive(:to_ary).and_return([1, 2])

      x = mock("multi-assign attributes")
      x.should_receive(:m).and_return(y)

      (a, b = x.m).should == y
      [a, b].should == [1, 2]
    end

    it "raises a TypeError if #to_ary does not return an Array on a single RHS" do
      y = mock("multi-assign method return value")
      y.should_receive(:to_ary).and_return(1)

      x = mock("multi-assign attributes")
      x.should_receive(:m).and_return(y)

      -> { a, b = x.m }.should raise_error(TypeError)
    end

    it "assigns values from a RHS method call with receiver and arguments" do
      x = mock("multi-assign attributes")
      x.should_receive(:m).with(1, 2).and_return([1, 2, 3])

      a, b = x.m 1, 2
      [a, b].should == [1, 2]
    end

    it "assigns global variables" do
      $spec_a, $spec_b = 1
      [$spec_a, $spec_b].should == [1, nil]
    end

    it "assigns instance variables" do
      @a, @b = 1
      [@a, @b].should == [1, nil]
    end

    it "assigns attributes" do
      a = mock("multi-assign attributes")
      a.should_receive(:x=).with(1)
      a.should_receive(:y=).with(nil)

      a.x, a.y = 1
    end

    it "assigns indexed elements" do
      a = []
      a[1], a[2] = 1
      a.should == [nil, 1, nil]
    end

    it "assigns constants" do
      module VariableSpecs
        SINGLE_RHS_1, SINGLE_RHS_2 = 1
        [SINGLE_RHS_1, SINGLE_RHS_2].should == [1, nil]
      end
    end
  end

  context "with a single splatted RHS value" do
    it "assigns a single grouped LHS splat" do
      (*a) = *1
      a.should == [1]
    end

    it "assigns an empty Array to a single LHS value when passed nil" do
      (a = *nil).should == []
      a.should == []
    end

    it "calls #to_a to convert nil to an empty Array" do
      nil.should_receive(:to_a).and_return([])

      (*a = *nil).should == []
      a.should == []
    end

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

      (a = *ary).should == [1, 2]
      a.should == [1, 2]
    end

    it "returns a copy of a splatted Array" do
      ary = [1, 2]

      (a = *ary).should == [1, 2]
      a.should_not equal(ary)
    end

    it "does not call #to_a on an Array subclass" do
      cls = Class.new(Array)
      ary = cls.new [1, 2]
      ary.should_not_receive(:to_a)

      (a = *ary).should == [1, 2]
      a.should == [1, 2]
    end

    it "returns an Array when the splatted object is an Array subclass" do
      cls = Class.new(Array)
      ary = cls.new [1, 2]

      x = (a = *ary)

      x.should == [1, 2]
      x.should be_an_instance_of(Array)

      a.should == [1, 2]
      a.should be_an_instance_of(Array)
    end

    it "unfreezes the array returned from calling 'to_a' on the splatted value" do
      obj = Object.new
      def obj.to_a
        [1,2].freeze
      end
      res = *obj
      res.should == [1,2]
      res.should_not.frozen?
    end

    it "consumes values for an anonymous splat" do
      a = 1
      (* = *a).should == [1]
    end

    it "consumes values for a grouped anonymous splat" do
      ((*) = *1).should == [1]
    end

    it "assigns a single LHS splat" do
      x = 1
      (*a = *x).should == [1]
      a.should == [1]
    end

    it "calls #to_a to convert an Object RHS with a single splat LHS" do
      x = mock("multi-assign RHS splat")
      x.should_receive(:to_a).and_return([1, 2])

      (*a = *x).should == [1, 2]
      a.should == [1, 2]
    end

    it "calls #to_a if it is private" do
      x = mock("multi-assign RHS splat")
      x.should_receive(:to_a).and_return([1, 2])
      class << x; private :to_a; end

      (*a = *x).should == [1, 2]
      a.should == [1, 2]
    end

    it "does not call #to_a if #respond_to? returns false" do
      x = mock("multi-assign RHS splat")
      x.should_receive(:respond_to?).with(:to_a, true).and_return(false)
      x.should_not_receive(:to_a)

      (*a = *x).should == [x]
      a.should == [x]
    end

    it "wraps the Object in an Array if #to_a returns nil" do
      x = mock("multi-assign RHS splat")
      x.should_receive(:to_a).and_return(nil)

      (*a = *x).should == [x]
      a.should == [x]
    end

    it "raises a TypeError if #to_a does not return an Array" do
      x = mock("multi-assign RHS splat")
      x.should_receive(:to_a).and_return(1)

      -> { *a = *x }.should raise_error(TypeError)
    end

    it "does not call #to_ary to convert an Object RHS with a single splat LHS" do
      x = mock("multi-assign RHS splat")
      x.should_not_receive(:to_ary)

      (*a = *x).should == [x]
      a.should == [x]
    end

    it "assigns a MLHS with leading splat" do
      (*a, b, c = *1).should == [1]
      [a, b, c].should == [[], 1, nil]
    end

    it "assigns a MLHS with a middle splat" do
      a, b, *c, d, e = *1
      [a, b, c, d, e].should == [1, nil, [], nil, nil]
    end

    it "assigns a MLHS with a trailing splat" do
      a, b, *c = *nil
      [a, b, c].should == [nil, nil, []]
    end

    it "calls #to_a to convert an Object RHS with a single LHS" do
      x = mock("multi-assign RHS splat")
      x.should_receive(:to_a).and_return([1, 2])

      (a = *x).should == [1, 2]
      a.should == [1, 2]
    end

    it "does not call #to_ary to convert an Object RHS with a single LHS" do
      x = mock("multi-assign RHS splat")
      x.should_not_receive(:to_ary)

      (a = *x).should == [x]
      a.should == [x]
    end

    it "raises a TypeError if #to_a does not return an Array with a single LHS" do
      x = mock("multi-assign splat")
      x.should_receive(:to_a).and_return(1)

      -> { a = *x }.should raise_error(TypeError)
    end

    it "calls #to_a to convert an Object splat RHS when assigned to a simple MLHS" do
      x = mock("multi-assign splat")
      x.should_receive(:to_a).and_return([1, 2])

      (a, b, c = *x).should == [1, 2]
      [a, b, c].should == [1, 2, nil]
    end

    it "raises a TypeError if #to_a does not return an Array with a simple MLHS" do
      x = mock("multi-assign splat")
      x.should_receive(:to_a).and_return(1)

      -> { a, b, c = *x }.should raise_error(TypeError)
    end

    it "does not call #to_ary to convert an Object splat RHS when assigned to a simple MLHS" do
      x = mock("multi-assign splat")
      x.should_not_receive(:to_ary)

      (a, b, c = *x).should == [x]
      [a, b, c].should == [x, nil, nil]
    end

    it "calls #to_a to convert an Object RHS with MLHS" do
      x = mock("multi-assign splat")
      x.should_receive(:to_a).and_return([1, 2])

      a, *b, c = *x
      [a, b, c].should == [1, [], 2]
    end

    it "raises a TypeError if #to_a does not return an Array with MLHS" do
      x = mock("multi-assign splat")
      x.should_receive(:to_a).and_return(1)

      -> { a, *b, c = *x }.should raise_error(TypeError)
    end

    it "does not call #to_ary to convert an Object RHS with a MLHS" do
      x = mock("multi-assign splat")
      x.should_not_receive(:to_ary)

      a, *b = *x
      [a, b].should == [x, []]
    end

    it "assigns a grouped LHS without splats" do
      ((a, b), c), (d, (e,), (f, (g, h))) = *1
      [a, b, c, d, e, f, g, h].should == [1, nil, nil, nil, nil, nil, nil, nil]
    end

    it "assigns a grouped LHS with splats" do
      (a, *b), c, (*d, (e, *f, g)) = *1
      [a, b, c, d, e, f, g].should == [1, [], nil, [], nil, [], nil]
    end

    it "does not mutate a RHS Array" do
      x = [1, 2, 3, 4]
      a, *b, c, d = *x
      [a, b, c, d].should == [1, [2], 3, 4]
      x.should == [1, 2, 3, 4]
    end

    it "assigns constants" do
      module VariableSpecs
        (*SINGLE_SPLATTED_RHS) = *1
        SINGLE_SPLATTED_RHS.should == [1]
      end
    end
  end

  context "with a MRHS value" do
    it "consumes values for an anonymous splat" do
      (* = 1, 2, 3).should == [1, 2, 3]
    end

    it "consumes values for a grouped anonymous splat" do
      ((*) = 1, 2, 3).should == [1, 2, 3]
    end

    it "consumes values for multiple '_' variables" do
      a, _, b, _, c = 1, 2, 3, 4, 5
      [a, b, c].should == [1, 3, 5]
    end

    it "does not call #to_a to convert an Object in a MRHS" do
      x = mock("multi-assign MRHS")
      x.should_not_receive(:to_a)

      (a, b = 1, x).should == [1, x]
      [a, b].should == [1, x]
    end

    it "does not call #to_ary to convert an Object in a MRHS" do
      x = mock("multi-assign MRHS")
      x.should_not_receive(:to_ary)

      (a, b = 1, x).should == [1, x]
      [a, b].should == [1, x]
    end

    it "calls #to_a to convert a splatted Object as part of a MRHS with a splat MLHS" do
      x = mock("multi-assign splat MRHS")
      x.should_receive(:to_a).and_return([3, 4])

      (a, *b = 1, *x).should == [1, 3, 4]
      [a, b].should == [1, [3, 4]]
    end

    it "raises a TypeError if #to_a does not return an Array with a splat MLHS" do
      x = mock("multi-assign splat MRHS")
      x.should_receive(:to_a).and_return(1)

      -> { a, *b = 1, *x }.should raise_error(TypeError)
    end

    it "does not call #to_ary to convert a splatted Object as part of a MRHS with a splat MRHS" do
      x = mock("multi-assign splat MRHS")
      x.should_not_receive(:to_ary)

      (a, *b = 1, *x).should == [1, x]
      [a, b].should == [1, [x]]
    end

    it "calls #to_a to convert a splatted Object as part of a MRHS" do
      x = mock("multi-assign splat MRHS")
      x.should_receive(:to_a).and_return([3, 4])

      (a, *b = *x, 1).should == [3, 4, 1]
      [a, b].should == [3, [4, 1]]
    end

    it "raises a TypeError if #to_a does not return an Array with a splat MRHS" do
      x = mock("multi-assign splat MRHS")
      x.should_receive(:to_a).and_return(1)

      -> { a, *b = *x, 1 }.should raise_error(TypeError)
    end

    it "does not call #to_ary to convert a splatted Object with a splat MRHS" do
      x = mock("multi-assign splat MRHS")
      x.should_not_receive(:to_ary)

      (a, *b = *x, 1).should == [x, 1]
      [a, b].should == [x, [1]]
    end

    it "assigns a grouped LHS without splat from a simple Array" do
      ((a, b), c), (d, (e,), (f, (g, h))) = 1, 2, 3, 4, 5
      [a, b, c, d, e, f, g, h].should == [1, nil, nil, 2, nil, nil, nil, nil]
    end

    it "assigns a grouped LHS without splat from nested Arrays" do
      ary = [[1, 2, 3], 4], [[5], [6, 7], [8, [9, 10]]]
      ((a, b), c), (d, (e,), (f, (g, h))) = ary
      [a, b, c, d, e, f, g, h].should == [1, 2, 4, [5], 6, 8, 9, 10]
    end

    it "assigns a single grouped LHS splat" do
      (*a) = 1, 2, 3
      a.should == [1, 2, 3]
    end

    it "assigns a grouped LHS with splats from nested Arrays for simple values" do
      (a, *b), c, (*d, (e, *f, g)) = 1, 2, 3, 4
      [a, b, c, d, e, f, g].should == [1, [], 2, [], 3, [], nil]
    end

    it "assigns a grouped LHS with splats from nested Arrays for nested arrays" do
      (a, *b), c, (*d, (e, *f, g)) = [1, [2, 3]], [4, 5], [6, 7, 8]
      [a, b, c, d, e, f, g].should == [1, [[2, 3]], [4, 5], [6, 7], 8, [], nil]
    end

    it "calls #to_ary to convert an Object when the position receiving the value is a multiple assignment" do
      x = mock("multi-assign mixed RHS")
      x.should_receive(:to_ary).and_return([1, 2])

      (a, (b, c), d, e = 1, x, 3, 4).should == [1, x, 3, 4]
      [a, b, c, d, e].should == [1, 1, 2, 3, 4]
    end

    it "raises a TypeError if #to_ary does not return an Array" do
      x = mock("multi-assign mixed RHS")
      x.should_receive(:to_ary).and_return(x)

      -> { a, (b, c), d = 1, x, 3, 4 }.should raise_error(TypeError)
    end

    it "calls #to_a to convert a splatted Object value in a MRHS" do
      x = mock("multi-assign mixed splatted RHS")
      x.should_receive(:to_a).and_return([4, 5])

      (a, *b, (c, d) = 1, 2, 3, *x).should == [1, 2, 3, 4, 5]
      [a, b, c, d].should == [1, [2, 3, 4], 5, nil]

    end

    it "calls #to_ary to convert a splatted Object when the position receiving the value is a multiple assignment" do
      x = mock("multi-assign mixed splatted RHS")
      x.should_receive(:to_ary).and_return([4, 5])

      (a, *b, (c, d) = 1, 2, 3, *x).should == [1, 2, 3, x]
      [a, b, c, d].should == [1, [2, 3], 4, 5]
    end

    it "raises a TypeError if #to_ary does not return an Array in a MRHS" do
      x = mock("multi-assign mixed splatted RHS")
      x.should_receive(:to_ary).and_return(x)

      -> { a, *b, (c, d) = 1, 2, 3, *x }.should raise_error(TypeError)
    end

    it "does not call #to_ary to convert an Object when the position receiving the value is a simple variable" do
      x = mock("multi-assign mixed RHS")
      x.should_not_receive(:to_ary)

      a, b, c, d = 1, x, 3, 4
      [a, b, c, d].should == [1, x, 3, 4]
    end

    it "does not call #to_ary to convert an Object when the position receiving the value is a rest variable" do
      x = mock("multi-assign mixed RHS")
      x.should_not_receive(:to_ary)

      a, *b, c, d = 1, x, 3, 4
      [a, b, c, d].should == [1, [x], 3, 4]
    end

    it "does not call #to_ary to convert a splatted Object when the position receiving the value is a simple variable" do
      x = mock("multi-assign mixed splatted RHS")
      x.should_not_receive(:to_ary)

      a, *b, c = 1, 2, *x
      [a, b, c].should == [1, [2], x]
    end

    it "does not call #to_ary to convert a splatted Object when the position receiving the value is a rest variable" do
      x = mock("multi-assign mixed splatted RHS")
      x.should_not_receive(:to_ary)

      a, b, *c = 1, 2, *x
      [a, b, c].should == [1, 2, [x]]
    end

    it "does not mutate the assigned Array" do
      x = ((a, *b, c, d) = 1, 2, 3, 4, 5)
      x.should == [1, 2, 3, 4, 5]
    end

    it "assigns RHS values to LHS constants" do
      module VariableSpecs
        MRHS_VALUES_1, MRHS_VALUES_2 = 1, 2
        MRHS_VALUES_1.should == 1
        MRHS_VALUES_2.should == 2
      end
    end

    it "assigns all RHS values as an array to a single LHS constant" do
      module VariableSpecs
        MRHS_VALUES = 1, 2, 3
        MRHS_VALUES.should == [1, 2, 3]
      end
    end
  end

  context "with a RHS assignment value" do
    it "consumes values for an anonymous splat" do
      (* = (a = 1)).should == 1
      a.should == 1
    end

    it "does not mutate a RHS Array" do
      a, *b, c, d = (e = [1, 2, 3, 4])
      [a, b, c, d].should == [1, [2], 3, 4]
      e.should == [1, 2, 3, 4]
    end
  end
end

describe "A local variable assigned only within a conditional block" do
  context "accessed from a later closure" do
    it "is defined?" do
      if VariablesSpecs.false
        a = 1
      end

      1.times do
        defined?(a).should == "local-variable"
      end
    end

    it "is nil" do
      if VariablesSpecs.false
        a = 1
      end

      1.times do
        a.inspect.should == "nil"
      end
    end
  end
end

describe 'Local variable shadowing' do
  ruby_version_is ""..."2.6" do
    it "leads to warning in verbose mode" do
      -> do
        eval <<-CODE
          a = [1, 2, 3]
          a.each { |a| a = 3 }
        CODE
      end.should complain(/shadowing outer local variable/, verbose: true)
    end
  end

  ruby_version_is "2.6" do
    it "does not warn in verbose mode" do
      result = nil

      -> do
        eval <<-CODE
          a = [1, 2, 3]
          result = a.map { |a| a = 3 }
        CODE
      end.should_not complain(verbose: true)

      result.should == [3, 3, 3]
    end
  end
end

describe 'Allowed characters' do
  ruby_version_is "2.6" do
    # new feature in 2.6 -- https://bugs.ruby-lang.org/issues/13770
    it 'does not allow non-ASCII upcased characters at the beginning' do
      -> do
        eval <<-CODE
          def test
            ἍBB = 1
          end
        CODE
      end.should raise_error(SyntaxError, /dynamic constant assignment/)
    end
  end

  it 'allows non-ASCII lowercased characters at the beginning' do
    result = nil

    eval <<-CODE
      def test
        μ = 1
      end

      result = test
    CODE

    result.should == 1
  end
end