/DLR_Main/Languages/Ruby/IronRuby.Tests/Runtime/DefinedTests.cs

https://bitbucket.org/mdavid/dlr · C# · 346 lines · 317 code · 15 blank · 14 comment · 0 complexity · afe2775acac541aa640432783e571724 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. namespace IronRuby.Tests {
  16. public partial class Tests {
  17. public void DefinedOperator_Globals1() {
  18. AssertOutput(delegate() {
  19. CompilerTest(@"
  20. p defined? $+
  21. alias $plus $+
  22. p defined? $+
  23. p defined? $plus
  24. "
  25. );
  26. }, @"
  27. nil
  28. nil
  29. ""global-variable""
  30. ");
  31. }
  32. public void DefinedOperator_Globals2() {
  33. AssertOutput(delegate() {
  34. CompilerTest(@"
  35. alias $foo $bar
  36. p defined? $foo
  37. p defined? $bar
  38. $foo = nil
  39. p defined? $foo
  40. p defined? $bar
  41. $foo = 1
  42. p defined? $foo
  43. p defined? $bar
  44. "
  45. );
  46. }, @"
  47. nil
  48. nil
  49. ""global-variable""
  50. ""global-variable""
  51. ""global-variable""
  52. ""global-variable""
  53. ");
  54. }
  55. public void DefinedOperator_Methods1() {
  56. CompilerTest(@"
  57. module M
  58. def foo; end
  59. end
  60. module N
  61. def foo_defined?
  62. $o1 = defined? foo
  63. undef foo rescue $o2 = 1
  64. end
  65. end
  66. class C
  67. include M,N
  68. end
  69. C.new.foo_defined?
  70. ");
  71. object o = Context.GetGlobalVariable("o1");
  72. AssertEquals(o.ToString(), "method");
  73. o = Context.GetGlobalVariable("o2");
  74. AssertEquals(o, 1);
  75. }
  76. public void DefinedOperator_Methods2() {
  77. AssertOutput(() => CompilerTest(@"
  78. def foo
  79. puts 'foo'
  80. self
  81. end
  82. puts defined? 1.+
  83. puts defined? 1.method_that_doesnt_exist
  84. puts defined? raise.foo
  85. puts defined? foo.foo(puts(1)) # foo is private
  86. public :foo
  87. puts defined? foo.foo
  88. "), @"
  89. method
  90. nil
  91. nil
  92. foo
  93. nil
  94. foo
  95. method
  96. ");
  97. }
  98. public void DefinedOperator_Constants1() {
  99. AssertOutput(delegate() {
  100. CompilerTest(@"
  101. W = 0
  102. module M
  103. X = 1
  104. end
  105. module N
  106. Y = 2
  107. def cs_defined?
  108. puts defined? ::W
  109. puts defined? ::Undef
  110. puts defined? M::X
  111. puts defined? M::Undef
  112. puts defined? X
  113. puts defined? Y
  114. puts defined? Z
  115. end
  116. end
  117. class C
  118. include M,N
  119. Z = 3
  120. end
  121. C.new.cs_defined?
  122. ");
  123. }, @"
  124. constant
  125. nil
  126. constant
  127. nil
  128. nil
  129. constant
  130. nil
  131. ");
  132. }
  133. public void DefinedOperator_Constants2() {
  134. TestOutput(@"
  135. module M
  136. C = 1
  137. end
  138. def foo
  139. M
  140. end
  141. class << Object
  142. def const_missing name
  143. puts ""missing #{name}""
  144. M
  145. end
  146. end
  147. puts defined?(X::C)
  148. puts defined?(raise::C)
  149. puts defined?(foo::C)
  150. ", @"
  151. missing X
  152. constant
  153. nil
  154. constant
  155. ");
  156. }
  157. public void DefinedOperator_Constants3() {
  158. TestOutput(@"
  159. print '1' unless defined? FOO
  160. print '2' unless defined? FOO
  161. print '.' unless not (defined? Object and defined? FOO)
  162. print '.' unless defined? FOO or defined? Object
  163. print '.' if defined? FOO
  164. print '.' if defined? FOO
  165. print '7' if not (defined? Object and defined? FOO)
  166. print '8' if defined? FOO or defined? Object
  167. ", @"
  168. 1278
  169. ");
  170. }
  171. public void DefinedOperator_Expressions1() {
  172. AssertOutput(delegate() {
  173. CompilerTest(@"
  174. puts defined? true
  175. puts defined? false
  176. puts defined? self
  177. puts defined? 1
  178. puts defined? 'foo'
  179. puts defined?((1+3)+1)
  180. puts defined? x = 1
  181. puts defined? x &= 1
  182. puts defined? x &&= 1
  183. ");
  184. }, @"
  185. true
  186. false
  187. self
  188. expression
  189. expression
  190. method
  191. assignment
  192. assignment
  193. expression
  194. ");
  195. }
  196. public void DefinedOperator_InstanceVariables1() {
  197. AssertOutput(delegate() {
  198. CompilerTest(@"
  199. p defined? @x
  200. @x = nil
  201. p defined? @x
  202. @x = 1
  203. p defined? @x
  204. ");
  205. }, @"
  206. nil
  207. ""instance-variable""
  208. ""instance-variable""
  209. ");
  210. }
  211. public void DefinedOperator_ClassVariables1() {
  212. AssertOutput(delegate() {
  213. CompilerTest(@"
  214. module M
  215. def foo; @@foo = 1; end
  216. def foo_defined_on_M?
  217. puts defined? @@foo
  218. end
  219. end
  220. module N
  221. def foo_defined?
  222. puts defined? @@foo
  223. end
  224. end
  225. class C
  226. include M,N
  227. end
  228. c = C.new
  229. c.foo
  230. c.foo_defined?
  231. c.foo_defined_on_M?
  232. ");
  233. }, @"
  234. nil
  235. class variable
  236. ");
  237. }
  238. public void DefinedOperator_ClassVariables2() {
  239. AssertOutput(delegate() {
  240. CompilerTest(@"
  241. @@x = 1
  242. puts defined? @@x
  243. ");
  244. }, @"
  245. class variable
  246. ");
  247. }
  248. public void DefinedOperator_Yield1() {
  249. AssertOutput(delegate() {
  250. CompilerTest(@"
  251. def foo
  252. defined? yield
  253. end
  254. puts foo
  255. puts foo {}
  256. ");
  257. }, @"
  258. nil
  259. yield
  260. ");
  261. }
  262. public void DefinedOperator_Locals1() {
  263. AssertOutput(delegate() {
  264. CompilerTest(@"
  265. def bob a
  266. x = 1
  267. 1.times { |y|
  268. z = 2
  269. puts defined? w, defined? x, defined? y, defined? z, defined? a
  270. }
  271. end
  272. bob 1
  273. ");
  274. }, @"
  275. nil
  276. local-variable
  277. local-variable
  278. local-variable
  279. local-variable
  280. ");
  281. }
  282. public void DefinedOperator_Super1() {
  283. AssertOutput(delegate() {
  284. CompilerTest(@"
  285. class C
  286. def foo
  287. defined?(super(puts(1)))
  288. end
  289. end
  290. class D < C
  291. def foo
  292. defined?(super(puts(1)))
  293. end
  294. def bar
  295. defined?(super(puts(1)))
  296. end
  297. end
  298. puts C.new.foo
  299. puts D.new.foo
  300. puts D.new.bar
  301. ");
  302. }, @"
  303. nil
  304. super
  305. nil
  306. ");
  307. }
  308. }
  309. }