/Merlin/Main/Languages/Ruby/IronRuby.Tests/Runtime/ModuleTests.cs

https://github.com/Hank923/ironruby · C# · 277 lines · 241 code · 16 blank · 20 comment · 3 complexity · db94302ef8256ee65b7e177938ebe4a4 MD5 · raw file

  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. 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 Microsoft Public License, 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 Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System.IO;
  16. namespace IronRuby.Tests {
  17. public partial class Tests {
  18. public void ClassDuplication1() {
  19. AssertOutput(delegate() {
  20. CompilerTest(@"
  21. class C
  22. X = 'const'
  23. @@x = 'cls-var'
  24. def foo
  25. puts self.class
  26. puts @@x
  27. end
  28. end
  29. D = C.dup
  30. D.new.foo
  31. puts D::X
  32. ");
  33. }, @"
  34. D
  35. cls-var
  36. const
  37. ");
  38. }
  39. public void ClassDuplication2() {
  40. AssertOutput(delegate() {
  41. CompilerTest(@"
  42. class Object
  43. def foo
  44. puts 'foo'
  45. end
  46. end
  47. O = Object.dup
  48. class Object
  49. remove_method :foo
  50. end
  51. O.new.foo
  52. ");
  53. }, @"
  54. foo
  55. ");
  56. }
  57. public void ClassDuplication3() {
  58. AssertOutput(delegate() {
  59. CompilerTest(@"
  60. S = String.dup
  61. p S.ancestors
  62. puts S.instance_methods(false) - String.instance_methods(false) == []
  63. puts s = S.new('ghk')
  64. puts s.reverse
  65. ");
  66. }, @"
  67. [S, Enumerable, Comparable, Object, Kernel]
  68. true
  69. ghk
  70. khg
  71. ");
  72. }
  73. /// <summary>
  74. /// This is different from MRI. In MRI ancestors of a duplicated Object class are [O, Kernel].
  75. /// </summary>
  76. public void ClassDuplication4() {
  77. AssertOutput(delegate() {
  78. CompilerTest(@"
  79. O = Object.dup
  80. p O.ancestors
  81. p O.constants.include?('Object')
  82. ");
  83. }, @"
  84. [O, Kernel, Object, Kernel]
  85. true
  86. ");
  87. }
  88. public void ClassDuplication5() {
  89. AssertOutput(delegate() {
  90. CompilerTest(@"
  91. require 'mscorlib'
  92. p System::Collections.dup.constants.include?('IEnumerable')
  93. ");
  94. }, @"
  95. true
  96. ");
  97. }
  98. public void ClassDuplication6() {
  99. var output = @"
  100. ""foo""
  101. [1, 2]
  102. ""default""
  103. 1..2
  104. /foo/
  105. """"
  106. [""a"", ""b""]
  107. ""foo""
  108. ";
  109. AssertOutput(delegate() {
  110. CompilerTest(@"
  111. p String.dup.new('foo')
  112. p Array.dup.new([1,2])
  113. p Hash.dup.new('default')[1]
  114. p Range.dup.new(1,2)
  115. p Regexp.dup.new('foo')
  116. p Module.dup.new.name
  117. p Struct.dup.new(:a,:b).dup[1,2].dup.members
  118. p Proc.dup.new { 'foo' }[]
  119. ");
  120. }, output);
  121. AssertOutput(delegate() {
  122. CompilerTest(@"
  123. class S < String; end
  124. class A < Array; end
  125. class H < Hash; end
  126. class R < Range; end
  127. class RE < Regexp; end
  128. class M < Module; end
  129. class St < Struct; end
  130. class P < Proc; end
  131. p S.dup.new('foo')
  132. p A.dup.new([1,2])
  133. p H.dup.new('default')[1]
  134. p R.dup.new(1,2)
  135. p RE.dup.new('foo')
  136. p M.dup.new.name
  137. p St.dup.new(:a,:b).dup[1,2].dup.members
  138. p P.dup.new { 'foo' }[]
  139. ");
  140. }, output);
  141. }
  142. public void Structs1() {
  143. AssertOutput(() => CompilerTest(@"
  144. S = Struct.new(:f,:g)
  145. class S
  146. alias set_f f=
  147. end
  148. s = S[1,2]
  149. s.set_f rescue p $!
  150. s.set_f(3)
  151. puts s.f
  152. s.set_f(4,5) rescue p $!
  153. s.set_f(*[]) rescue p $!
  154. s.set_f(*[6])
  155. puts s.f
  156. s.set_f(*[6,7]) rescue p $!
  157. puts s.f(*[])
  158. puts s.f(*[1]) rescue p $!
  159. "
  160. ), @"
  161. #<ArgumentError: wrong number of arguments (0 for 1)>
  162. 3
  163. #<ArgumentError: wrong number of arguments (2 for 1)>
  164. #<ArgumentError: wrong number of arguments (0 for 1)>
  165. 6
  166. #<ArgumentError: wrong number of arguments (2 for 1)>
  167. 6
  168. #<ArgumentError: wrong number of arguments (1 for 0)>
  169. ");
  170. }
  171. public void MetaModules1() {
  172. AssertOutput(delegate() {
  173. CompilerTest(@"
  174. class Meta < Module
  175. def bar
  176. puts 'bar'
  177. end
  178. end
  179. M = Meta.new
  180. module M
  181. def foo
  182. puts 'foo'
  183. end
  184. end
  185. class C
  186. include M
  187. end
  188. C.new.foo
  189. M.bar
  190. ");
  191. }, @"
  192. foo
  193. bar
  194. ");
  195. }
  196. public void MetaModulesDuplication1() {
  197. AssertOutput(delegate() {
  198. CompilerTest(@"
  199. class Meta < Module
  200. end
  201. M = Meta.new
  202. puts M.name
  203. puts M.class
  204. N = M.dup
  205. puts N.name
  206. puts N.class
  207. ");
  208. }, @"
  209. M
  210. Meta
  211. N
  212. Meta
  213. ");
  214. }
  215. /// <summary>
  216. /// Autoload removes the constant from the declaring module before it loads the target file.
  217. /// </summary>
  218. public void Autoload1() {
  219. if (_driver.PartialTrust) return;
  220. string file = null;
  221. try {
  222. file = Driver.MakeTempFile(".rb", @"
  223. class D
  224. p defined? X
  225. X = 123
  226. end
  227. ");
  228. Context.DefineGlobalVariable("file", file);
  229. TestOutput(@"
  230. class D
  231. autoload(:X, $file)
  232. end
  233. class C < D
  234. p X
  235. end
  236. ", @"
  237. nil
  238. 123
  239. ");
  240. } finally {
  241. if (file != null) {
  242. File.Delete(file);
  243. }
  244. }
  245. }
  246. }
  247. }