ExpressionTest_New.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //
  2. // ExpressionTest_New.cs
  3. //
  4. // Author:
  5. // Jb Evain ([email protected])
  6. //
  7. // (C) 2008 Novell, Inc. (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Reflection;
  30. using System.Linq;
  31. using System.Linq.Expressions;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Linq.Expressions {
  34. [TestFixture]
  35. [Category("SRE")]
  36. public class ExpressionTest_New {
  37. [Test]
  38. [ExpectedException (typeof (ArgumentNullException))]
  39. public void NullType ()
  40. {
  41. Expression.New (null as Type);
  42. }
  43. [Test]
  44. [ExpectedException (typeof (ArgumentNullException))]
  45. public void NullConstructor ()
  46. {
  47. Expression.New (null as ConstructorInfo);
  48. }
  49. public class Foo {
  50. public Foo (string s)
  51. {
  52. }
  53. }
  54. public class Bar {
  55. public string Value { get; set; }
  56. public Bar ()
  57. {
  58. }
  59. }
  60. public struct Baz {
  61. }
  62. [Test]
  63. [ExpectedException (typeof (ArgumentException))]
  64. public void NoParameterlessConstructor ()
  65. {
  66. Expression.New (typeof (Foo));
  67. }
  68. [Test]
  69. [ExpectedException (typeof (ArgumentException))]
  70. public void ConstructorHasTooMuchParameters ()
  71. {
  72. Expression.New (typeof (Foo).GetConstructor (new [] { typeof (string) }));
  73. }
  74. [Test]
  75. [Category ("NotDotNet")]
  76. [ExpectedException (typeof (ArgumentException))]
  77. public void NewVoid ()
  78. {
  79. Expression.New (typeof (void));
  80. }
  81. [Test]
  82. [ExpectedException (typeof (ArgumentNullException))]
  83. public void HasNullArgument ()
  84. {
  85. Expression.New (typeof (Foo).GetConstructor (new [] { typeof (string) }), null as Expression);
  86. }
  87. [Test]
  88. [ExpectedException (typeof (ArgumentException))]
  89. public void HasWrongArgument ()
  90. {
  91. Expression.New (typeof (Foo).GetConstructor (new [] { typeof (string) }), Expression.Constant (12));
  92. }
  93. [Test]
  94. public void NewFoo ()
  95. {
  96. var n = Expression.New (typeof (Foo).GetConstructor (new [] { typeof (string) }), Expression.Constant ("foo"));
  97. Assert.AreEqual (ExpressionType.New, n.NodeType);
  98. Assert.AreEqual (typeof (Foo), n.Type);
  99. Assert.AreEqual (1, n.Arguments.Count);
  100. Assert.IsNull (n.Members);
  101. Assert.AreEqual ("new Foo(\"foo\")", n.ToString ());
  102. }
  103. [Test]
  104. public void NewBar ()
  105. {
  106. var n = Expression.New (typeof (Bar));
  107. Assert.IsNotNull (n.Constructor);
  108. Assert.IsNotNull (n.Arguments);
  109. Assert.IsNull (n.Members); // wrong doc
  110. Assert.AreEqual ("new Bar()", n.ToString ());
  111. n = Expression.New (typeof (Bar).GetConstructor (Type.EmptyTypes));
  112. Assert.AreEqual ("new Bar()", n.ToString ());
  113. }
  114. public class Gazonk {
  115. string value;
  116. public Gazonk (string s)
  117. {
  118. value = s;
  119. }
  120. public override bool Equals (object obj)
  121. {
  122. var o = obj as Gazonk;
  123. if (o == null)
  124. return false;
  125. return value == o.value;
  126. }
  127. public override int GetHashCode ()
  128. {
  129. return value.GetHashCode ();
  130. }
  131. }
  132. [Test]
  133. public void CompileNewClass ()
  134. {
  135. var p = Expression.Parameter (typeof (string), "p");
  136. var n = Expression.New (typeof (Gazonk).GetConstructor (new [] { typeof (string) }), p);
  137. var fgaz = Expression.Lambda<Func<string, Gazonk>> (n, p).Compile ();
  138. var g1 = new Gazonk ("foo");
  139. var g2 = new Gazonk ("bar");
  140. Assert.IsNotNull (g1);
  141. Assert.AreEqual (g1, fgaz ("foo"));
  142. Assert.IsNotNull (g2);
  143. Assert.AreEqual (g2, fgaz ("bar"));
  144. n = Expression.New (typeof (Bar));
  145. var lbar = Expression.Lambda<Func<Bar>> (n).Compile ();
  146. var bar = lbar ();
  147. Assert.IsNotNull (bar);
  148. Assert.IsNull (bar.Value);
  149. }
  150. public class FakeAnonymousType {
  151. public string Foo { get; set; }
  152. public string Bar { get; set; }
  153. public string Baz { get; set; }
  154. public int Gazonk { get; set; }
  155. public string Tzap { set {} }
  156. public FakeAnonymousType (string foo)
  157. {
  158. Foo = foo;
  159. }
  160. public FakeAnonymousType (string foo, string bar, string baz)
  161. {
  162. Foo = foo;
  163. Bar = bar;
  164. Baz = baz;
  165. }
  166. }
  167. [Test]
  168. public void NewFakeAnonymousType ()
  169. {
  170. var n = Expression.New (
  171. typeof (FakeAnonymousType).GetConstructor (new [] { typeof (string), typeof (string), typeof (string) } ),
  172. new [] { "Foo".ToConstant (), "Bar".ToConstant (), "Baz".ToConstant () },
  173. new [] { typeof (FakeAnonymousType).GetProperty ("Foo"), typeof (FakeAnonymousType).GetProperty ("Bar"), typeof (FakeAnonymousType).GetProperty ("Baz") });
  174. Assert.IsNotNull (n.Constructor);
  175. Assert.IsNotNull (n.Arguments);
  176. Assert.IsNotNull (n.Members);
  177. Assert.AreEqual ("new FakeAnonymousType(Foo = \"Foo\", Bar = \"Bar\", Baz = \"Baz\")", n.ToString ());
  178. }
  179. [Test]
  180. [ExpectedException (typeof (ArgumentNullException))]
  181. public void NullMember ()
  182. {
  183. Expression.New (
  184. typeof (FakeAnonymousType).GetConstructor (new [] { typeof (string) }),
  185. new [] { "Foo".ToConstant () },
  186. new MemberInfo [] { null });
  187. }
  188. [Test]
  189. [ExpectedException (typeof (ArgumentException))]
  190. public void MemberArgumentMiscount ()
  191. {
  192. Expression.New (
  193. typeof (FakeAnonymousType).GetConstructor (new [] { typeof (string) }),
  194. new [] { "Foo".ToConstant () },
  195. new [] { typeof (FakeAnonymousType).GetProperty ("Foo"), typeof (FakeAnonymousType).GetProperty ("Bar") });
  196. }
  197. [Test]
  198. [ExpectedException (typeof (ArgumentException))]
  199. public void MemberArgumentMismatch ()
  200. {
  201. Expression.New (
  202. typeof (FakeAnonymousType).GetConstructor (new [] { typeof (string) }),
  203. new [] { "Foo".ToConstant () },
  204. new [] { typeof (FakeAnonymousType).GetProperty ("Gazonk") });
  205. }
  206. [Test]
  207. [ExpectedException (typeof (ArgumentException))]
  208. public void MemberHasNoGetter ()
  209. {
  210. Expression.New (
  211. typeof (FakeAnonymousType).GetConstructor (new [] { typeof (string) }),
  212. new [] { "Foo".ToConstant () },
  213. new [] { typeof (FakeAnonymousType).GetProperty ("Tzap") });
  214. }
  215. public struct EineStrukt {
  216. public int left;
  217. public int right;
  218. public EineStrukt (int left, int right)
  219. {
  220. this.left = left;
  221. this.right = right;
  222. }
  223. }
  224. [Test]
  225. public void CompileNewStruct ()
  226. {
  227. var create = Expression.Lambda<Func<EineStrukt>> (
  228. Expression.New (typeof (EineStrukt))).Compile ();
  229. var s = create ();
  230. Assert.AreEqual (0, s.left);
  231. Assert.AreEqual (0, s.right);
  232. }
  233. [Test]
  234. public void CompileNewStructWithParameters ()
  235. {
  236. var pl = Expression.Parameter (typeof (int), "left");
  237. var pr = Expression.Parameter (typeof (int), "right");
  238. var create = Expression.Lambda<Func<int, int, EineStrukt>> (
  239. Expression.New (typeof (EineStrukt).GetConstructor (new [] { typeof (int), typeof (int) }), pl, pr), pl, pr).Compile ();
  240. var s = create (42, 12);
  241. Assert.AreEqual (42, s.left);
  242. Assert.AreEqual (12, s.right);
  243. }
  244. public class EineKlass {
  245. public string Left { get; set; }
  246. public string Right { get; set; }
  247. public EineKlass ()
  248. {
  249. }
  250. public EineKlass (string l, string r)
  251. {
  252. Left = l;
  253. Right = r;
  254. }
  255. }
  256. [Test]
  257. public void CompileNewClassEmptyConstructor ()
  258. {
  259. var create = Expression.Lambda<Func<EineKlass>> (
  260. Expression.New (typeof (EineKlass))).Compile ();
  261. var k = create ();
  262. Assert.IsNull (k.Left);
  263. Assert.IsNull (k.Right);
  264. }
  265. [Test]
  266. public void CompileNewClassWithParameters ()
  267. {
  268. var pl = Expression.Parameter (typeof (string), "left");
  269. var pr = Expression.Parameter (typeof (string), "right");
  270. var create = Expression.Lambda<Func<string, string, EineKlass>> (
  271. Expression.New (typeof (EineKlass).GetConstructor (new [] { typeof (string), typeof (string) }), pl, pr), pl, pr).Compile ();
  272. var k = create ("foo", "bar");
  273. Assert.AreEqual ("foo", k.Left);
  274. Assert.AreEqual ("bar", k.Right);
  275. }
  276. }
  277. }