PageRenderTime 23ms CodeModel.GetById 9ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Languages/Ruby/Tests/Runtime/Expression/test_assignment.rb

#
Ruby | 87 lines | 45 code | 22 blank | 20 comment | 0 complexity | aa6ce9961c13ca2c43c2cd23938cc7cb 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
16require '../../util/assert.rb'
17
18# set the variable or attribute on its left side (the lvalue) to refer to the value
19# on the right (the rvalue). 
20
21a = 3
22assert_equal(a, 3)
23
24a = nil
25assert_nil(a)
26
27a = 4.5
28assert_equal(a, 4.5)
29
30def f; -10; end 
31a = f
32assert_equal(a, -10)
33
34a = []
35assert_equal(a, [])
36
37a = *[]
38assert_nil(a)
39
40a = [5.78]
41assert_equal(a, [5.78])
42
43a = *[7]
44assert_equal(a, 7)
45
46a = *[8, 9]
47assert_equal(a, [8, 9])
48
49# multiple rvalues, convert to array
50a = 7, 9
51assert_equal(a, [7, 9])
52
53a = nil, 8
54assert_equal(a, [nil, 8])
55
56a = nil, nil
57assert_equal(a, [nil, nil])
58
59# It then returns that value as the result of the assignment expression
60
61a = b = 10
62assert_equal(a, 10)
63assert_equal(b, 10)
64
65a = b = 20 + 30
66assert_equal(a, 50)
67assert_equal(b, 50)
68
69a = (b = 30 + 40) + 50
70assert_equal(a, 120)
71assert_equal(b, 70)
72
73# order!
74a = b = 99, 100
75assert_equal(a, [99, 100])
76assert_equal(b, 99)
77
78a = (b = 101, 102)
79assert_equal(a, [101, 102])
80assert_equal(b, [101, 102])
81
82s = "hello world"
83assert_equal(s, "hello world")
84
85# object attribute or element reference (todo)
86s[0] = 'H'
87assert_equal(s, "Hello world")