PageRenderTime 101ms CodeModel.GetById 93ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/Languages/Ruby/IronRuby.Tests/Runtime/AssignmentTests.cs

https://github.com/thomo13/ironruby
C# | 404 lines | 352 code | 17 blank | 35 comment | 0 complexity | f2d8a900820af6784776966153f0db54 MD5 | raw file
  1/* ****************************************************************************
  2 *
  3 * Copyright (c) Microsoft Corporation. 
  4 *
  5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
  6 * copy of the license can be found in the License.html file at the root of this distribution. If 
  7 * you cannot locate the  Apache License, Version 2.0, please send an email to 
  8 * ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
  9 * by the terms of the Apache License, Version 2.0.
 10 *
 11 * You must not remove this notice, or any other, from this software.
 12 *
 13 *
 14 * ***************************************************************************/
 15
 16namespace IronRuby.Tests {
 17    public partial class Tests {
 18        public void Scenario_Assignment1() {
 19            AssertOutput(delegate() {
 20                CompilerTest(@"
 21a = 1
 22a += 2
 23puts a");
 24            }, @"3");
 25        }
 26
 27        /// <summary>
 28        /// Order of evaluation.
 29        /// </summary>
 30        public void Scenario_ParallelAssignment1() {
 31            AssertOutput(delegate() {
 32                CompilerTest(@"
 33def one; puts 'one'; 1; end
 34def two; puts 'two'; 2; end
 35def three; puts 'three'; 3; end
 36
 37a,b = one,two,three
 38puts a.inspect,b.inspect
 39");
 40            }, @"
 41one
 42two
 43three
 441
 452
 46");
 47        }
 48
 49        public void Scenario_ParallelAssignment2() {
 50            AssertOutput(delegate() {
 51                CompilerTest(@"
 52def foo()
 53  x, y = 3, 4
 54  puts x, y
 55  x, y = y, x
 56  puts x, y
 57  x, y = 1
 58  puts x, y
 59  x, y = 5, 6, 7
 60  puts x, y
 61  z = (x, y = 5, 6, 7)
 62  puts z
 63end
 64
 65foo
 66");
 67            }, @"
 683
 694
 704
 713
 721
 73nil
 745
 756
 765
 776
 787");
 79        }
 80
 81        public void Scenario_ParallelAssignment4() {
 82            TestOutput(@"
 83ra = (a = *4)
 84rb = (b = *[4])
 85rc = (c = *[*4])
 86rd = (d = 1,*4)
 87re = (e = 1,*[4])
 88rf = (f = 1,*[*4])
 89puts a.inspect,b.inspect,c.inspect,d.inspect,e.inspect,f.inspect
 90puts ra.inspect,rb.inspect,rc.inspect,rd.inspect,re.inspect,rf.inspect
 91", @"
 92[4]
 93[4]
 94[4]
 95[1, 4]
 96[1, 4]
 97[1, 4]
 98[4]
 99[4]
100[4]
101[1, 4]
102[1, 4]
103[1, 4]
104");
105        }
106
107        public void Scenario_ParallelAssignment5() {
108            AssertOutput(delegate() {
109                CompilerTest(@"
110r = (x,(y,(z1,z2)),(*u),v,w = 1,[*[1,2]],3,*4)
111
112puts 'r = ' + r.inspect,
113  'x = ' + x.inspect,
114  'y = ' + y.inspect,
115  'z1 = ' + z1.inspect,
116  'z2 = ' + z2.inspect,
117  'u = ' + u.inspect,
118  'v = ' + v.inspect,
119  'w = ' + w.inspect
120");
121            }, @"
122r = [1, [1, 2], 3, 4]
123x = 1
124y = 1
125z1 = 2
126z2 = nil
127u = [3]
128v = 4
129w = nil
130");
131        }
132
133        /// <summary>
134        /// Non-simple LHS, simple RHS. Difference between |LHS| > 0 (r0 and r3) and |LHS| == 0 (r1).
135        /// </summary>
136        public void Scenario_ParallelAssignment6() {
137            AssertOutput(delegate() {
138                CompilerTest(@"
139r0 = (x,y = [1,2])
140r1 = (*v = [1,2])
141r2 = (*w = *[1,2])
142r3 = (p,*q = [1,2])
143puts r0.inspect, r1.inspect, r2.inspect, r3.inspect, '*'
144puts x.inspect, y.inspect, '*', v.inspect, '*', w.inspect, '*', p.inspect, q.inspect
145");
146            }, @"
147[1, 2]
148[1, 2]
149[1, 2]
150[1, 2]
151*
1521
1532
154*
155[1, 2]
156*
157[1, 2]
158*
1591
160[2]
161");
162        }
163
164        /// <summary>
165        /// Simple LHS and splat only RHS.
166        /// </summary>
167        public void Scenario_ParallelAssignment7() {
168            TestOutput(@"
169a = (ax = *1)
170b = (bx = *[])
171c = (cx = *[1])
172d = (dx = *[1,2])
173
174puts a.inspect, ax.inspect, b.inspect, bx.inspect, c.inspect, cx.inspect, d.inspect, dx.inspect
175", @"
176[1]
177[1]
178[]
179[]
180[1]
181[1]
182[1, 2]
183[1, 2]
184");
185        }
186
187        /// <summary>
188        /// Simple RHS.
189        /// </summary>
190        public void Scenario_ParallelAssignment8() {
191            TestOutput(@"
192r1 = (a = [1,2])
193r2 = (b,c = [1,2])
194r3 = (d,e = *[1,2])
195r4 = (f,g = 1)
196
197puts r1.inspect, r2.inspect, r3.inspect, r4.inspect
198puts b.inspect, c.inspect, d.inspect, e.inspect, f.inspect, g.inspect
199", @"
200[1, 2]
201[1, 2]
202[1, 2]
2031
2041
2052
2061
2072
2081
209nil
210");
211        }
212
213        /// <summary>
214        /// Inner splat-only LHS.
215        /// </summary>
216        public void Scenario_ParallelAssignment9() {
217            TestOutput(@"
218c = ((*a),(*b) = [1,2],[3,4])
219puts a.inspect, b.inspect, c.inspect
220", @"
221[1, 2]
222[3, 4]
223[[1, 2], [3, 4]]
224");
225        }
226
227        /// <summary>
228        /// Recursion in L(1,-).
229        /// </summary>
230        public void Scenario_ParallelAssignment10() {
231            TestOutput(@"
232ra = ((a,) = *[])
233rb = ((b,) = 1,*[])
234puts a.inspect, ra.inspect
235puts b.inspect, rb.inspect
236", @"
237nil
238[]
2391
240[1]
241");
242        }
243
244        /// <summary>
245        /// ArrayItemAccess and AttributeAccess read target ones in an in-place assignment.
246        /// </summary>
247        public void SimpleInplaceAsignmentToIndirectLeftValues1() {
248            TestOutput(@"
249class Array
250  def x; 1; end
251  def x= value; puts 'x=' end
252end
253
254def foo
255  puts 'foo'
256  [0]
257end
258
259p foo[0] += 1
260p foo::x += 2
261p foo[0] &&= 3
262p foo::x &&= 4
263p foo[0] ||= 5
264p foo::x ||= 6
265", @"
266foo
2671
268foo
269x=
2703
271foo
2723
273foo
274x=
2754
276foo
2770
278foo
2791
280");
281        }
282
283        public void SetterCallValue() {
284            AssertOutput(delegate {
285                CompilerTest(@"
286class C
287  def x= value
288    puts value
289    'foo'
290  end
291  
292  def x value
293    puts value
294    puts 'foo'
295  end
296end
297
298foo = C.new
299
300puts(foo.x=('bar'))
301puts '---'
302puts(foo.x('bar'))
303");
304            }, @"
305bar
306bar
307---
308bar
309foo
310nil");
311
312        }
313
314        private const string/*!*/ MemberAssignmentDefs = @"
315class String
316  def + other
317    puts ""#{self} + #{other}""
318    ""#{self}#{other}""
319  end
320end
321
322class C
323  def x= value
324    puts ""write: #{value}""
325  end
326  
327  def x 
328    puts ""read""
329    $result
330  end
331end
332
333def target
334  puts 'target'
335  C.new
336end
337
338def rhs
339  puts 'rhs'
340  '_rhs'
341end
342";
343
344        public void MemberAssignmentExpression1() {
345            AssertOutput(delegate {
346                CompilerTest(MemberAssignmentDefs + @"
347$result = 'result'
348puts(target.x += rhs)
349");
350            }, @"
351target
352read
353rhs
354result + _rhs
355write: result_rhs
356result_rhs
357");
358        }
359
360        public void MemberAssignmentExpression2() {
361            AssertOutput(delegate {
362                CompilerTest(MemberAssignmentDefs + @"
363$result = true
364puts(target.x &&= rhs)
365puts
366$result = false
367puts(target.x &&= rhs)
368");
369            }, @"
370target
371read
372rhs
373write: _rhs
374_rhs
375
376target
377read
378false
379");
380        }
381
382        public void MemberAssignmentExpression3() {
383            AssertOutput(delegate {
384                CompilerTest(MemberAssignmentDefs + @"
385$result = true
386puts(target.x ||= rhs)
387puts
388$result = false
389puts(target.x ||= rhs)
390");
391            }, @"
392target
393read
394true
395
396target
397read
398rhs
399write: _rhs
400_rhs
401");
402        }
403    }
404}