/Languages/Ruby/Tests/Legacy/Runtime/Class/test_change_self.rb

https://github.com/kumaryu/IronLanguages-main · Ruby · 176 lines · 113 code · 39 blank · 24 comment · 0 complexity · cd91f92a9565df2e48328b71c1c6e544 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. # status: almost complete, few nice-to-have improvements:
  16. # - try other members types in the "CHANGE BASE TYPE" scenario
  17. require '../../util/assert.rb'
  18. # CHANGE THE MEMBERS
  19. # - redefining the class with members of the new or same names
  20. class My
  21. CONST1 = 10
  22. CONST2 = 20
  23. @@sv1 = 30
  24. @@sv2 = 40
  25. def initialize
  26. @iv1 = 50
  27. @iv2 = 60
  28. end
  29. def My.sm1; 70; end
  30. def My.sm2; 80; end
  31. def im1; 90; end
  32. def im2; 100; end
  33. def check
  34. return @iv1, @iv2, @iv3, @@sv1, @@sv2, @@sv3
  35. end
  36. class Nested
  37. def m1; 110; end
  38. def m2; 120; end
  39. end
  40. end
  41. x_before = My.new
  42. y_before = My::Nested.new
  43. # try those to-be-added/updated members
  44. assert_equal(My::CONST2, 20)
  45. assert_raise(NameError) { My::CONST3 }
  46. assert_equal(My::sm2, 80)
  47. assert_raise(NoMethodError) { My::sm3 }
  48. assert_equal(x_before.im2, 100)
  49. assert_raise(NoMethodError) { x_before.im3 }
  50. assert_equal(y_before.m2, 120)
  51. assert_raise(NoMethodError) { y_before.m3 }
  52. # let us create something else in the middle
  53. class Other; end
  54. class My
  55. CONST2 = -10 # warning issued
  56. CONST3 = -20
  57. @@sv2 = -30
  58. @@sv3 = -40
  59. def initialize
  60. @iv2 = -50
  61. @iv3 = -60
  62. end
  63. def My.sm2; -70; end
  64. def My.sm3; -80; end
  65. def im2; -90; end
  66. def im3; -100; end
  67. class Nested
  68. def m2; -110; end
  69. def m3; -120; end
  70. end
  71. end
  72. x_after = My.new
  73. y_after = My::Nested.new
  74. assert_equal(My::CONST1, 10)
  75. assert_equal(My::CONST2, -10)
  76. assert_equal(My::CONST3, -20)
  77. assert_equal(My.sm1, 70)
  78. assert_equal(My.sm2, -70)
  79. assert_equal(My.sm3, -80)
  80. assert_equal(x_after.im1, 90)
  81. assert_equal(x_after.im2, -90)
  82. assert_equal(x_after.im3, -100)
  83. assert_equal(x_after.check, [nil, -50, -60, 30, -30, -40])
  84. assert_equal(y_after.m1, 110)
  85. assert_equal(y_after.m2, -110)
  86. assert_equal(y_after.m3, -120)
  87. # let us then check the object created before the re-design/open
  88. assert_equal(x_before.im1, 90)
  89. assert_equal(x_before.im2, -90)
  90. assert_equal(x_before.im3, -100)
  91. assert_equal(x_before.check, [50, 60, nil, 30, -30, -40]) # difference is those variables assigned in "initialize"
  92. assert_equal(y_before.m1, 110)
  93. assert_equal(y_before.m2, -110)
  94. assert_equal(y_before.m3, -120)
  95. # CHANGE THE BASE TYPE
  96. # - if a class of the same name already exists, the class and superclass must match.
  97. class My_base1;
  98. def m0; 10; end
  99. end
  100. class My_base2; end
  101. class My_derived < My_base1;
  102. def m1; 100; end
  103. def m2; 200; end
  104. end
  105. assert_raise(TypeError) { class My_derived < My_base2; end } # superclass mismatch for class My_derived (TypeError)
  106. assert_raise(TypeError) { class My_derived < Object; end }
  107. assert_raise(TypeError) { class My_base1 < My_base2; end } # My_base1 superclass was not specified
  108. x = My_derived.new
  109. assert_equal(x.m2, 200)
  110. assert_raise(NoMethodError) { x.m3 }
  111. class My_derived # able to change it WITHOUT superclass specified
  112. def m2; -200; end
  113. def m3; -300; end
  114. end
  115. assert_equal(x.m0, 10)
  116. assert_equal(x.m1, 100)
  117. assert_equal(x.m2, -200)
  118. assert_equal(x.m3, -300)
  119. class My_base1;
  120. def m0; 1000; end
  121. def m4; 4000; end
  122. end
  123. assert_equal(x.m0, 1000)
  124. assert_equal(x.m4, 4000)
  125. # CHANGE TYPE BY INCLUDING MODULE
  126. module Mod
  127. def helper; 24; end
  128. end
  129. assert_raise(NameError) { My_base8 }
  130. class My_base8; end
  131. class My_derived8 < My_base8; end
  132. x = My_base8.new
  133. assert_raise(NoMethodError) { x.helper }
  134. class My_base8;
  135. include Mod
  136. end
  137. assert_equal(x.helper, 24)