/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/ObjectCreateExpressionTests.cs

http://github.com/icsharpcode/ILSpy · C# · 252 lines · 222 code · 13 blank · 17 comment · 0 complexity · fb5dbd0e7918aac3c0c2b9516cc48705 MD5 · raw file

  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using NUnit.Framework;
  20. namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
  21. {
  22. [TestFixture]
  23. public class ObjectCreateExpressionTests
  24. {
  25. [Test]
  26. public void Simple()
  27. {
  28. ParseUtilCSharp.AssertExpression(
  29. "new MyObject(1, 2, 3)",
  30. new ObjectCreateExpression {
  31. Type = new SimpleType("MyObject"),
  32. Arguments = {
  33. new PrimitiveExpression(1),
  34. new PrimitiveExpression(2),
  35. new PrimitiveExpression(3)
  36. }});
  37. }
  38. [Test]
  39. public void Nullable()
  40. {
  41. ParseUtilCSharp.AssertExpression(
  42. "new IntPtr?(1)",
  43. new ObjectCreateExpression {
  44. Type = new SimpleType("IntPtr").MakeNullableType(),
  45. Arguments = { new PrimitiveExpression(1) }
  46. });
  47. }
  48. [Test]
  49. public void ObjectInitializer()
  50. {
  51. ParseUtilCSharp.AssertExpression(
  52. "new Point() { X = 0, Y = 1 }",
  53. new ObjectCreateExpression {
  54. Type = new SimpleType("Point"),
  55. Initializer = new ArrayInitializerExpression {
  56. Elements = {
  57. new NamedExpression("X", new PrimitiveExpression(0)),
  58. new NamedExpression("Y", new PrimitiveExpression(1))
  59. }
  60. }});
  61. }
  62. [Test]
  63. public void ObjectInitializerWithoutParenthesis()
  64. {
  65. ParseUtilCSharp.AssertExpression(
  66. "new Point { X = 0, Y = 1 }",
  67. new ObjectCreateExpression {
  68. Type = new SimpleType("Point"),
  69. Initializer = new ArrayInitializerExpression {
  70. Elements = {
  71. new NamedExpression("X", new PrimitiveExpression(0)),
  72. new NamedExpression("Y", new PrimitiveExpression(1))
  73. }
  74. }});
  75. }
  76. [Test]
  77. public void ObjectInitializerWithTrailingComma()
  78. {
  79. ParseUtilCSharp.AssertExpression(
  80. "new Point() { X = 0, Y = 1, }",
  81. new ObjectCreateExpression {
  82. Type = new SimpleType("Point"),
  83. Initializer = new ArrayInitializerExpression {
  84. Elements = {
  85. new NamedExpression("X", new PrimitiveExpression(0)),
  86. new NamedExpression("Y", new PrimitiveExpression(1))
  87. }
  88. }});
  89. }
  90. [Test]
  91. public void NestedObjectInitializer()
  92. {
  93. ParseUtilCSharp.AssertExpression(
  94. "new Rectangle { P1 = new Point { X = 0, Y = 1 }, P2 = new Point { X = 2, Y = 3 } }",
  95. new ObjectCreateExpression {
  96. Type = new SimpleType("Rectangle"),
  97. Initializer = new ArrayInitializerExpression {
  98. Elements = {
  99. new NamedExpression(
  100. "P1",
  101. new ObjectCreateExpression {
  102. Type = new SimpleType("Point"),
  103. Initializer = new ArrayInitializerExpression {
  104. Elements = {
  105. new NamedExpression("X", new PrimitiveExpression(0)),
  106. new NamedExpression("Y", new PrimitiveExpression(1))
  107. }
  108. }}),
  109. new NamedExpression(
  110. "P2",
  111. new ObjectCreateExpression {
  112. Type = new SimpleType("Point"),
  113. Initializer = new ArrayInitializerExpression {
  114. Elements = {
  115. new NamedExpression("X", new PrimitiveExpression(2)),
  116. new NamedExpression("Y", new PrimitiveExpression(3))
  117. }
  118. }})
  119. }}});
  120. }
  121. [Test]
  122. public void NestedObjectInitializerForPreinitializedProperty()
  123. {
  124. ParseUtilCSharp.AssertExpression(
  125. "new Rectangle { P1 = { X = 0, Y = 1 }, P2 = { X = 2, Y = 3 } }",
  126. new ObjectCreateExpression {
  127. Type = new SimpleType("Rectangle"),
  128. Initializer = new ArrayInitializerExpression {
  129. Elements = {
  130. new NamedExpression(
  131. "P1",
  132. new ArrayInitializerExpression {
  133. Elements = {
  134. new NamedExpression("X", new PrimitiveExpression(0)),
  135. new NamedExpression("Y", new PrimitiveExpression(1))
  136. }
  137. }),
  138. new NamedExpression(
  139. "P2",
  140. new ArrayInitializerExpression {
  141. Elements = {
  142. new NamedExpression("X", new PrimitiveExpression(2)),
  143. new NamedExpression("Y", new PrimitiveExpression(3))
  144. }
  145. })
  146. }}});
  147. }
  148. [Test]
  149. public void CollectionInitializer()
  150. {
  151. ParseUtilCSharp.AssertExpression(
  152. "new List<int> { 0, 1, 2 }",
  153. new ObjectCreateExpression {
  154. Type = new SimpleType("List", new PrimitiveType("int")),
  155. Initializer = new ArrayInitializerExpression {
  156. Elements = {
  157. new ArrayInitializerExpression(new PrimitiveExpression(0)),
  158. new ArrayInitializerExpression(new PrimitiveExpression(1)),
  159. new ArrayInitializerExpression(new PrimitiveExpression(2))
  160. }}});
  161. }
  162. [Test]
  163. public void DictionaryInitializer()
  164. {
  165. ParseUtilCSharp.AssertExpression(
  166. "new Dictionary<string, int> { { \"a\", 0 }, { \"b\", 1 } }",
  167. new ObjectCreateExpression {
  168. Type = new SimpleType("Dictionary", new PrimitiveType("string"), new PrimitiveType("int")),
  169. Initializer = new ArrayInitializerExpression {
  170. Elements = {
  171. new ArrayInitializerExpression {
  172. Elements = { new PrimitiveExpression("a"), new PrimitiveExpression(0) }
  173. },
  174. new ArrayInitializerExpression {
  175. Elements = { new PrimitiveExpression("b"), new PrimitiveExpression(1) }
  176. }
  177. }}});
  178. }
  179. [Test]
  180. public void ComplexCollectionInitializer()
  181. {
  182. ParseUtilCSharp.AssertExpression(
  183. @"new List<Contact> {
  184. new Contact {
  185. Name = ""Chris"",
  186. PhoneNumbers = { ""206-555-0101"" }
  187. },
  188. new Contact(additionalParameter) {
  189. Name = ""Bob"",
  190. PhoneNumbers = { ""650-555-0199"", ""425-882-8080"" }
  191. }
  192. }",
  193. new ObjectCreateExpression {
  194. Type = new SimpleType("List", new SimpleType("Contact")),
  195. Initializer = new ArrayInitializerExpression(
  196. new ArrayInitializerExpression(
  197. new ObjectCreateExpression {
  198. Type = new SimpleType("Contact"),
  199. Initializer = new ArrayInitializerExpression {
  200. Elements = {
  201. new NamedExpression("Name", new PrimitiveExpression("Chris")),
  202. new NamedExpression("PhoneNumbers", new ArrayInitializerExpression () {
  203. Elements = { new ArrayInitializerExpression(new PrimitiveExpression("206-555-0101")) }
  204. })
  205. }}}),
  206. new ArrayInitializerExpression(
  207. new ObjectCreateExpression {
  208. Type = new SimpleType("Contact"),
  209. Arguments = { new IdentifierExpression("additionalParameter") },
  210. Initializer = new ArrayInitializerExpression {
  211. Elements = {
  212. new NamedExpression("Name", new PrimitiveExpression("Bob")),
  213. new NamedExpression("PhoneNumbers", new ArrayInitializerExpression () {
  214. Elements = {
  215. new ArrayInitializerExpression(new PrimitiveExpression("650-555-0199")),
  216. new ArrayInitializerExpression(new PrimitiveExpression("425-882-8080"))
  217. }
  218. })
  219. }}})
  220. )});
  221. }
  222. [Test]
  223. public void AssignmentInCollectionInitializer()
  224. {
  225. ParseUtilCSharp.AssertExpression(
  226. @"new List<int> { { a = 1 } }",
  227. new ObjectCreateExpression {
  228. Type = new SimpleType("List", new PrimitiveType("int")),
  229. Initializer = new ArrayInitializerExpression {
  230. Elements = {
  231. new ArrayInitializerExpression {
  232. Elements = {
  233. new AssignmentExpression(new IdentifierExpression("a"), new PrimitiveExpression(1))
  234. }
  235. }
  236. }}});
  237. }
  238. }
  239. }