PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/Languages/Ruby/IronRuby.Tests/Runtime/AssignmentTests.cs

https://github.com/dsblank/main
C# | 404 lines | 352 code | 17 blank | 35 comment | 0 complexity | f2d8a900820af6784776966153f0db54 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 Scenario_Assignment1() {
  18. AssertOutput(delegate() {
  19. CompilerTest(@"
  20. a = 1
  21. a += 2
  22. puts a");
  23. }, @"3");
  24. }
  25. /// <summary>
  26. /// Order of evaluation.
  27. /// </summary>
  28. public void Scenario_ParallelAssignment1() {
  29. AssertOutput(delegate() {
  30. CompilerTest(@"
  31. def one; puts 'one'; 1; end
  32. def two; puts 'two'; 2; end
  33. def three; puts 'three'; 3; end
  34. a,b = one,two,three
  35. puts a.inspect,b.inspect
  36. ");
  37. }, @"
  38. one
  39. two
  40. three
  41. 1
  42. 2
  43. ");
  44. }
  45. public void Scenario_ParallelAssignment2() {
  46. AssertOutput(delegate() {
  47. CompilerTest(@"
  48. def foo()
  49. x, y = 3, 4
  50. puts x, y
  51. x, y = y, x
  52. puts x, y
  53. x, y = 1
  54. puts x, y
  55. x, y = 5, 6, 7
  56. puts x, y
  57. z = (x, y = 5, 6, 7)
  58. puts z
  59. end
  60. foo
  61. ");
  62. }, @"
  63. 3
  64. 4
  65. 4
  66. 3
  67. 1
  68. nil
  69. 5
  70. 6
  71. 5
  72. 6
  73. 7");
  74. }
  75. public void Scenario_ParallelAssignment4() {
  76. TestOutput(@"
  77. ra = (a = *4)
  78. rb = (b = *[4])
  79. rc = (c = *[*4])
  80. rd = (d = 1,*4)
  81. re = (e = 1,*[4])
  82. rf = (f = 1,*[*4])
  83. puts a.inspect,b.inspect,c.inspect,d.inspect,e.inspect,f.inspect
  84. puts ra.inspect,rb.inspect,rc.inspect,rd.inspect,re.inspect,rf.inspect
  85. ", @"
  86. [4]
  87. [4]
  88. [4]
  89. [1, 4]
  90. [1, 4]
  91. [1, 4]
  92. [4]
  93. [4]
  94. [4]
  95. [1, 4]
  96. [1, 4]
  97. [1, 4]
  98. ");
  99. }
  100. public void Scenario_ParallelAssignment5() {
  101. AssertOutput(delegate() {
  102. CompilerTest(@"
  103. r = (x,(y,(z1,z2)),(*u),v,w = 1,[*[1,2]],3,*4)
  104. puts 'r = ' + r.inspect,
  105. 'x = ' + x.inspect,
  106. 'y = ' + y.inspect,
  107. 'z1 = ' + z1.inspect,
  108. 'z2 = ' + z2.inspect,
  109. 'u = ' + u.inspect,
  110. 'v = ' + v.inspect,
  111. 'w = ' + w.inspect
  112. ");
  113. }, @"
  114. r = [1, [1, 2], 3, 4]
  115. x = 1
  116. y = 1
  117. z1 = 2
  118. z2 = nil
  119. u = [3]
  120. v = 4
  121. w = nil
  122. ");
  123. }
  124. /// <summary>
  125. /// Non-simple LHS, simple RHS. Difference between |LHS| > 0 (r0 and r3) and |LHS| == 0 (r1).
  126. /// </summary>
  127. public void Scenario_ParallelAssignment6() {
  128. AssertOutput(delegate() {
  129. CompilerTest(@"
  130. r0 = (x,y = [1,2])
  131. r1 = (*v = [1,2])
  132. r2 = (*w = *[1,2])
  133. r3 = (p,*q = [1,2])
  134. puts r0.inspect, r1.inspect, r2.inspect, r3.inspect, '*'
  135. puts x.inspect, y.inspect, '*', v.inspect, '*', w.inspect, '*', p.inspect, q.inspect
  136. ");
  137. }, @"
  138. [1, 2]
  139. [1, 2]
  140. [1, 2]
  141. [1, 2]
  142. *
  143. 1
  144. 2
  145. *
  146. [1, 2]
  147. *
  148. [1, 2]
  149. *
  150. 1
  151. [2]
  152. ");
  153. }
  154. /// <summary>
  155. /// Simple LHS and splat only RHS.
  156. /// </summary>
  157. public void Scenario_ParallelAssignment7() {
  158. TestOutput(@"
  159. a = (ax = *1)
  160. b = (bx = *[])
  161. c = (cx = *[1])
  162. d = (dx = *[1,2])
  163. puts a.inspect, ax.inspect, b.inspect, bx.inspect, c.inspect, cx.inspect, d.inspect, dx.inspect
  164. ", @"
  165. [1]
  166. [1]
  167. []
  168. []
  169. [1]
  170. [1]
  171. [1, 2]
  172. [1, 2]
  173. ");
  174. }
  175. /// <summary>
  176. /// Simple RHS.
  177. /// </summary>
  178. public void Scenario_ParallelAssignment8() {
  179. TestOutput(@"
  180. r1 = (a = [1,2])
  181. r2 = (b,c = [1,2])
  182. r3 = (d,e = *[1,2])
  183. r4 = (f,g = 1)
  184. puts r1.inspect, r2.inspect, r3.inspect, r4.inspect
  185. puts b.inspect, c.inspect, d.inspect, e.inspect, f.inspect, g.inspect
  186. ", @"
  187. [1, 2]
  188. [1, 2]
  189. [1, 2]
  190. 1
  191. 1
  192. 2
  193. 1
  194. 2
  195. 1
  196. nil
  197. ");
  198. }
  199. /// <summary>
  200. /// Inner splat-only LHS.
  201. /// </summary>
  202. public void Scenario_ParallelAssignment9() {
  203. TestOutput(@"
  204. c = ((*a),(*b) = [1,2],[3,4])
  205. puts a.inspect, b.inspect, c.inspect
  206. ", @"
  207. [1, 2]
  208. [3, 4]
  209. [[1, 2], [3, 4]]
  210. ");
  211. }
  212. /// <summary>
  213. /// Recursion in L(1,-).
  214. /// </summary>
  215. public void Scenario_ParallelAssignment10() {
  216. TestOutput(@"
  217. ra = ((a,) = *[])
  218. rb = ((b,) = 1,*[])
  219. puts a.inspect, ra.inspect
  220. puts b.inspect, rb.inspect
  221. ", @"
  222. nil
  223. []
  224. 1
  225. [1]
  226. ");
  227. }
  228. /// <summary>
  229. /// ArrayItemAccess and AttributeAccess read target ones in an in-place assignment.
  230. /// </summary>
  231. public void SimpleInplaceAsignmentToIndirectLeftValues1() {
  232. TestOutput(@"
  233. class Array
  234. def x; 1; end
  235. def x= value; puts 'x=' end
  236. end
  237. def foo
  238. puts 'foo'
  239. [0]
  240. end
  241. p foo[0] += 1
  242. p foo::x += 2
  243. p foo[0] &&= 3
  244. p foo::x &&= 4
  245. p foo[0] ||= 5
  246. p foo::x ||= 6
  247. ", @"
  248. foo
  249. 1
  250. foo
  251. x=
  252. 3
  253. foo
  254. 3
  255. foo
  256. x=
  257. 4
  258. foo
  259. 0
  260. foo
  261. 1
  262. ");
  263. }
  264. public void SetterCallValue() {
  265. AssertOutput(delegate {
  266. CompilerTest(@"
  267. class C
  268. def x= value
  269. puts value
  270. 'foo'
  271. end
  272. def x value
  273. puts value
  274. puts 'foo'
  275. end
  276. end
  277. foo = C.new
  278. puts(foo.x=('bar'))
  279. puts '---'
  280. puts(foo.x('bar'))
  281. ");
  282. }, @"
  283. bar
  284. bar
  285. ---
  286. bar
  287. foo
  288. nil");
  289. }
  290. private const string/*!*/ MemberAssignmentDefs = @"
  291. class String
  292. def + other
  293. puts ""#{self} + #{other}""
  294. ""#{self}#{other}""
  295. end
  296. end
  297. class C
  298. def x= value
  299. puts ""write: #{value}""
  300. end
  301. def x
  302. puts ""read""
  303. $result
  304. end
  305. end
  306. def target
  307. puts 'target'
  308. C.new
  309. end
  310. def rhs
  311. puts 'rhs'
  312. '_rhs'
  313. end
  314. ";
  315. public void MemberAssignmentExpression1() {
  316. AssertOutput(delegate {
  317. CompilerTest(MemberAssignmentDefs + @"
  318. $result = 'result'
  319. puts(target.x += rhs)
  320. ");
  321. }, @"
  322. target
  323. read
  324. rhs
  325. result + _rhs
  326. write: result_rhs
  327. result_rhs
  328. ");
  329. }
  330. public void MemberAssignmentExpression2() {
  331. AssertOutput(delegate {
  332. CompilerTest(MemberAssignmentDefs + @"
  333. $result = true
  334. puts(target.x &&= rhs)
  335. puts
  336. $result = false
  337. puts(target.x &&= rhs)
  338. ");
  339. }, @"
  340. target
  341. read
  342. rhs
  343. write: _rhs
  344. _rhs
  345. target
  346. read
  347. false
  348. ");
  349. }
  350. public void MemberAssignmentExpression3() {
  351. AssertOutput(delegate {
  352. CompilerTest(MemberAssignmentDefs + @"
  353. $result = true
  354. puts(target.x ||= rhs)
  355. puts
  356. $result = false
  357. puts(target.x ||= rhs)
  358. ");
  359. }, @"
  360. target
  361. read
  362. true
  363. target
  364. read
  365. rhs
  366. write: _rhs
  367. _rhs
  368. ");
  369. }
  370. }
  371. }