PageRenderTime 49ms CodeModel.GetById 27ms RepoModel.GetById 0ms 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
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  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. require '../../util/assert.rb'
  16. # set the variable or attribute on its left side (the lvalue) to refer to the value
  17. # on the right (the rvalue).
  18. a = 3
  19. assert_equal(a, 3)
  20. a = nil
  21. assert_nil(a)
  22. a = 4.5
  23. assert_equal(a, 4.5)
  24. def f; -10; end
  25. a = f
  26. assert_equal(a, -10)
  27. a = []
  28. assert_equal(a, [])
  29. a = *[]
  30. assert_nil(a)
  31. a = [5.78]
  32. assert_equal(a, [5.78])
  33. a = *[7]
  34. assert_equal(a, 7)
  35. a = *[8, 9]
  36. assert_equal(a, [8, 9])
  37. # multiple rvalues, convert to array
  38. a = 7, 9
  39. assert_equal(a, [7, 9])
  40. a = nil, 8
  41. assert_equal(a, [nil, 8])
  42. a = nil, nil
  43. assert_equal(a, [nil, nil])
  44. # It then returns that value as the result of the assignment expression
  45. a = b = 10
  46. assert_equal(a, 10)
  47. assert_equal(b, 10)
  48. a = b = 20 + 30
  49. assert_equal(a, 50)
  50. assert_equal(b, 50)
  51. a = (b = 30 + 40) + 50
  52. assert_equal(a, 120)
  53. assert_equal(b, 70)
  54. # order!
  55. a = b = 99, 100
  56. assert_equal(a, [99, 100])
  57. assert_equal(b, 99)
  58. a = (b = 101, 102)
  59. assert_equal(a, [101, 102])
  60. assert_equal(b, [101, 102])
  61. s = "hello world"
  62. assert_equal(s, "hello world")
  63. # object attribute or element reference (todo)
  64. s[0] = 'H'
  65. assert_equal(s, "Hello world")