summaryrefslogtreecommitdiff
path: root/spec/ruby/language/assignments_spec.rb
blob: 2773508d8d44ed285b1f481327c1e1e971d24c6a (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
require_relative '../spec_helper'

# Should be synchronized with spec/ruby/language/optional_assignments_spec.rb
# Some specs for assignments are located in language/variables_spec.rb
describe 'Assignments' do
  describe 'using =' do
    describe 'evaluation order' do
      it 'evaluates expressions left to right when assignment with an accessor' do
        object = Object.new
        def object.a=(value) end
        ScratchPad.record []

        (ScratchPad << :receiver; object).a = (ScratchPad << :rhs; :value)
        ScratchPad.recorded.should == [:receiver, :rhs]
      end

      it 'evaluates expressions left to right when assignment with a #[]=' do
        object = Object.new
        def object.[]=(_, _) end
        ScratchPad.record []

        (ScratchPad << :receiver; object)[(ScratchPad << :argument; :a)] = (ScratchPad << :rhs; :value)
        ScratchPad.recorded.should == [:receiver, :argument, :rhs]
      end

      # similar tests for evaluation order are located in language/constants_spec.rb
      ruby_version_is ''...'3.2' do
        it 'evaluates expressions right to left when assignment with compounded constant' do
          m = Module.new
          ScratchPad.record []

          (ScratchPad << :module; m)::A = (ScratchPad << :rhs; :value)
          ScratchPad.recorded.should == [:rhs, :module]
        end
      end

      ruby_version_is '3.2' do
        it 'evaluates expressions left to right when assignment with compounded constant' do
          m = Module.new
          ScratchPad.record []

          (ScratchPad << :module; m)::A = (ScratchPad << :rhs; :value)
          ScratchPad.recorded.should == [:module, :rhs]
        end
      end

      it 'raises TypeError after evaluation of right-hand-side when compounded constant module is not a module' do
        ScratchPad.record []

        -> {
          (:not_a_module)::A = (ScratchPad << :rhs; :value)
        }.should raise_error(TypeError)

        ScratchPad.recorded.should == [:rhs]
      end
    end
  end

  describe 'using +=' do
    describe 'using an accessor' do
      before do
        klass = Class.new { attr_accessor :b }
        @a    = klass.new
      end

      it 'does evaluate receiver only once when assigns' do
        ScratchPad.record []
        @a.b = 1

        (ScratchPad << :evaluated; @a).b += 2

        ScratchPad.recorded.should == [:evaluated]
        @a.b.should == 3
      end

      it 'ignores method visibility when receiver is self' do
        klass_with_private_methods = Class.new do
          def initialize(n) @a = n end
          def public_method(n); self.a += n end
          private
          def a; @a end
          def a=(n) @a = n; 42 end
        end

        a = klass_with_private_methods.new(0)
        a.public_method(2).should == 2
      end
    end

    describe 'using a #[]' do
      before do
        klass = Class.new do
          def [](k)
            @hash ||= {}
            @hash[k]
          end

          def []=(k, v)
            @hash ||= {}
            @hash[k] = v
            7
          end
        end
        @b = klass.new
      end

      it 'evaluates receiver only once when assigns' do
        ScratchPad.record []
        a = {k: 1}

        (ScratchPad << :evaluated; a)[:k] += 2

        ScratchPad.recorded.should == [:evaluated]
        a[:k].should == 3
      end

      it 'ignores method visibility when receiver is self' do
        klass_with_private_methods = Class.new do
          def initialize(h) @a = h end
          def public_method(k, n); self[k] += n end
          private
          def [](k) @a[k] end
          def []=(k, v) @a[k] = v; 42 end
        end

        a = klass_with_private_methods.new(k: 0)
        a.public_method(:k, 2).should == 2
      end

      context 'splatted argument' do
        it 'correctly handles it' do
          @b[:m] = 10
          (@b[*[:m]] += 10).should == 20
          @b[:m].should == 20

          @b[:n] = 10
          (@b[*(1; [:n])] += 10).should == 20
          @b[:n].should == 20

          @b[:k] = 10
          (@b[*begin 1; [:k] end] += 10).should == 20
          @b[:k].should == 20
        end

        it 'calls #to_a only once' do
          k = Object.new
          def k.to_a
            ScratchPad << :to_a
            [:k]
          end

          ScratchPad.record []
          @b[:k] = 10
          (@b[*k] += 10).should == 20
          @b[:k].should == 20
          ScratchPad.recorded.should == [:to_a]
        end

        it 'correctly handles a nested splatted argument' do
          @b[:k] = 10
          (@b[*[*[:k]]] += 10).should == 20
          @b[:k].should == 20
        end

        it 'correctly handles multiple nested splatted arguments' do
          klass_with_multiple_parameters = Class.new do
            def [](k1, k2, k3)
              @hash ||= {}
              @hash[:"#{k1}#{k2}#{k3}"]
            end

            def []=(k1, k2, k3, v)
              @hash ||= {}
              @hash[:"#{k1}#{k2}#{k3}"] = v
              7
            end
          end
          a = klass_with_multiple_parameters.new

          a[:a, :b, :c] = 10
          (a[*[:a], *[:b], *[:c]] += 10).should == 20
          a[:a, :b, :c].should == 20
        end
      end
    end

    describe 'using compounded constants' do
      it 'causes side-effects of the module part to be applied only once (when assigns)' do
        module ConstantSpecs
          OpAssignTrue = 1
        end

        suppress_warning do # already initialized constant
          x = 0
          (x += 1; ConstantSpecs)::OpAssignTrue += 2
          x.should == 1
          ConstantSpecs::OpAssignTrue.should == 3
        end

        ConstantSpecs.send :remove_const, :OpAssignTrue
      end
    end
  end
end

# generic cases
describe 'Multiple assignments' do
  it 'assigns multiple targets when assignment with an accessor' do
    object = Object.new
    class << object
      attr_accessor :a, :b
    end

    object.a, object.b = :a, :b

    object.a.should == :a
    object.b.should == :b
  end

  it 'assigns multiple targets when assignment with a nested accessor' do
    object = Object.new
    class << object
      attr_accessor :a, :b
    end

    (object.a, object.b), c = [:a, :b], nil

    object.a.should == :a
    object.b.should == :b
  end

  it 'assigns multiple targets when assignment with a #[]=' do
    object = Object.new
    class << object
      def []=(k, v) (@h ||= {})[k] = v; end
      def [](k) (@h ||= {})[k]; end
    end

    object[:a], object[:b] = :a, :b

    object[:a].should == :a
    object[:b].should == :b
  end

  it 'assigns multiple targets when assignment with a nested #[]=' do
    object = Object.new
    class << object
      def []=(k, v) (@h ||= {})[k] = v; end
      def [](k) (@h ||= {})[k]; end
    end

    (object[:a], object[:b]), c = [:v1, :v2], nil

    object[:a].should == :v1
    object[:b].should == :v2
  end

  it 'assigns multiple targets when assignment with compounded constant' do
    m = Module.new

    m::A, m::B = :a, :b

    m::A.should == :a
    m::B.should == :b
  end

  it 'assigns multiple targets when assignment with a nested compounded constant' do
    m = Module.new

    (m::A, m::B), c = [:a, :b], nil

    m::A.should == :a
    m::B.should == :b
  end
end

describe 'Multiple assignments' do
  describe 'evaluation order' do
    ruby_version_is ''...'3.1' do
      it 'evaluates expressions right to left when assignment with an accessor' do
        object = Object.new
        def object.a=(value) end
        ScratchPad.record []

        (ScratchPad << :a; object).a, (ScratchPad << :b; object).a = (ScratchPad << :c; :c), (ScratchPad << :d; :d)
        ScratchPad.recorded.should == [:c, :d, :a, :b]
      end

      it 'evaluates expressions right to left when assignment with a nested accessor' do
        object = Object.new
        def object.a=(value) end
        ScratchPad.record []

        ((ScratchPad << :a; object).a, foo), bar = [(ScratchPad << :b; :b)]
        ScratchPad.recorded.should == [:b, :a]
      end
    end

    ruby_version_is '3.1' do
      it 'evaluates expressions left to right when assignment with an accessor' do
        object = Object.new
        def object.a=(value) end
        ScratchPad.record []

        (ScratchPad << :a; object).a, (ScratchPad << :b; object).a = (ScratchPad << :c; :c), (ScratchPad << :d; :d)
        ScratchPad.recorded.should == [:a, :b, :c, :d]
      end

      it 'evaluates expressions left to right when assignment with a nested accessor' do
        object = Object.new
        def object.a=(value) end
        ScratchPad.record []

        ((ScratchPad << :a; object).a, foo), bar = [(ScratchPad << :b; :b)]
        ScratchPad.recorded.should == [:a, :b]
      end

      it 'evaluates expressions left to right when assignment with a deeply nested accessor' do
        o = Object.new
        def o.a=(value) end
        def o.b=(value) end
        def o.c=(value) end
        def o.d=(value) end
        def o.e=(value) end
        def o.f=(value) end
        ScratchPad.record []

        (ScratchPad << :a; o).a,
          ((ScratchPad << :b; o).b,
          ((ScratchPad << :c; o).c, (ScratchPad << :d; o).d),
          (ScratchPad << :e; o).e),
        (ScratchPad << :f; o).f = (ScratchPad << :value; :value)

        ScratchPad.recorded.should == [:a, :b, :c, :d, :e, :f, :value]
      end
    end

    ruby_version_is ''...'3.1' do
      it 'evaluates expressions right to left when assignment with a #[]=' do
        object = Object.new
        def object.[]=(_, _) end
        ScratchPad.record []

        (ScratchPad << :a; object)[(ScratchPad << :b; :b)], (ScratchPad << :c; object)[(ScratchPad << :d; :d)] = (ScratchPad << :e; :e), (ScratchPad << :f; :f)
        ScratchPad.recorded.should == [:e, :f, :a, :b, :c, :d]
      end

      it 'evaluates expressions right to left when assignment with a nested #[]=' do
        object = Object.new
        def object.[]=(_, _) end
        ScratchPad.record []

        ((ScratchPad << :a; object)[(ScratchPad << :b; :b)], foo), bar = [(ScratchPad << :c; :c)]
        ScratchPad.recorded.should == [:c, :a, :b]
      end
    end

    ruby_version_is '3.1' do
      it 'evaluates expressions left to right when assignment with a #[]=' do
        object = Object.new
        def object.[]=(_, _) end
        ScratchPad.record []

        (ScratchPad << :a; object)[(ScratchPad << :b; :b)], (ScratchPad << :c; object)[(ScratchPad << :d; :d)] = (ScratchPad << :e; :e), (ScratchPad << :f; :f)
        ScratchPad.recorded.should == [:a, :b, :c, :d, :e, :f]
      end

      it 'evaluates expressions left to right when assignment with a nested #[]=' do
        object = Object.new
        def object.[]=(_, _) end
        ScratchPad.record []

        ((ScratchPad << :a; object)[(ScratchPad << :b; :b)], foo), bar = [(ScratchPad << :c; :c)]
        ScratchPad.recorded.should == [:a, :b, :c]
      end

      it 'evaluates expressions left to right when assignment with a deeply nested #[]=' do
        o = Object.new
        def o.[]=(_, _) end
        ScratchPad.record []

        (ScratchPad << :ra; o)[(ScratchPad << :aa; :aa)],
          ((ScratchPad << :rb; o)[(ScratchPad << :ab; :ab)],
          ((ScratchPad << :rc; o)[(ScratchPad << :ac; :ac)], (ScratchPad << :rd; o)[(ScratchPad << :ad; :ad)]),
          (ScratchPad << :re; o)[(ScratchPad << :ae; :ae)]),
        (ScratchPad << :rf; o)[(ScratchPad << :af; :af)] = (ScratchPad << :value; :value)

        ScratchPad.recorded.should == [:ra, :aa, :rb, :ab, :rc, :ac, :rd, :ad, :re, :ae, :rf, :af, :value]
      end
    end

    ruby_version_is ''...'3.2' do
      it 'evaluates expressions right to left when assignment with compounded constant' do
        m = Module.new
        ScratchPad.record []

        (ScratchPad << :a; m)::A, (ScratchPad << :b; m)::B = (ScratchPad << :c; :c), (ScratchPad << :d; :d)
        ScratchPad.recorded.should == [:c, :d, :a, :b]
      end
    end

    ruby_version_is '3.2' do
      it 'evaluates expressions left to right when assignment with compounded constant' do
        m = Module.new
        ScratchPad.record []

        (ScratchPad << :a; m)::A, (ScratchPad << :b; m)::B = (ScratchPad << :c; :c), (ScratchPad << :d; :d)
        ScratchPad.recorded.should == [:a, :b, :c, :d]
      end

      it 'evaluates expressions left to right when assignment with a nested compounded constant' do
        m = Module.new
        ScratchPad.record []

        ((ScratchPad << :a; m)::A, foo), bar = [(ScratchPad << :b; :b)]
        ScratchPad.recorded.should == [:a, :b]
      end

      it 'evaluates expressions left to right when assignment with deeply nested compounded constants' do
        m = Module.new
        ScratchPad.record []

        (ScratchPad << :a; m)::A,
          ((ScratchPad << :b; m)::B,
          ((ScratchPad << :c; m)::C, (ScratchPad << :d; m)::D),
          (ScratchPad << :e; m)::E),
        (ScratchPad << :f; m)::F = (ScratchPad << :value; :value)

        ScratchPad.recorded.should == [:a, :b, :c, :d, :e, :f, :value]
      end
    end
  end

  context 'when assignment with method call and receiver is self' do
    it 'assigns values correctly when assignment with accessor' do
      object = Object.new
      class << object
        attr_accessor :a, :b

        def assign(v1, v2)
          self.a, self.b = v1, v2
        end
      end

      object.assign :v1, :v2
      object.a.should == :v1
      object.b.should == :v2
    end

    it 'evaluates expressions right to left when assignment with a nested accessor' do
      object = Object.new
      class << object
        attr_accessor :a, :b

        def assign(v1, v2)
          (self.a, self.b), c = [v1, v2], nil
        end
      end

      object.assign :v1, :v2
      object.a.should == :v1
      object.b.should == :v2
    end

    it 'assigns values correctly when assignment with a #[]=' do
      object = Object.new
      class << object
        def []=(key, v)
          @h ||= {}
          @h[key] = v
        end

        def [](key)
          (@h || {})[key]
        end

        def assign(k1, v1, k2, v2)
          self[k1], self[k2] = v1, v2
        end
      end

      object.assign :k1, :v1, :k2, :v2
      object[:k1].should == :v1
      object[:k2].should == :v2
    end

    it 'assigns values correctly when assignment with a nested #[]=' do
      object = Object.new
      class << object
        def []=(key, v)
          @h ||= {}
          @h[key] = v
        end

        def [](key)
          (@h || {})[key]
        end

        def assign(k1, v1, k2, v2)
          (self[k1], self[k2]), c = [v1, v2], nil
        end
      end

      object.assign :k1, :v1, :k2, :v2
      object[:k1].should == :v1
      object[:k2].should == :v2
    end

    it 'assigns values correctly when assignment with compounded constant' do
      m = Module.new
      m.module_exec do
        self::A, self::B = :v1, :v2
      end

      m::A.should == :v1
      m::B.should == :v2
    end

    it 'assigns values correctly when assignment with a nested compounded constant' do
      m = Module.new
      m.module_exec do
        (self::A, self::B), c = [:v1, :v2], nil
      end

      m::A.should == :v1
      m::B.should == :v2
    end
  end
end