ExpressionTest_New.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. public class ExpressionTest_New {
  36. [Test]
  37. [ExpectedException (typeof (ArgumentNullException))]
  38. public void NullType ()
  39. {
  40. Expression.New (null as Type);
  41. }
  42. [Test]
  43. [ExpectedException (typeof (ArgumentNullException))]
  44. public void NullConstructor ()
  45. {
  46. Expression.New (null as ConstructorInfo);
  47. }
  48. public class Foo {
  49. public Foo (string s)
  50. {
  51. }
  52. }
  53. public class Bar {
  54. public string Value { get; set; }
  55. public Bar ()
  56. {
  57. }
  58. }
  59. public struct Baz {
  60. }
  61. [Test]
  62. [ExpectedException (typeof (ArgumentException))]
  63. public void NoParameterlessConstructor ()
  64. {
  65. Expression.New (typeof (Foo));
  66. }
  67. [Test]
  68. [ExpectedException (typeof (ArgumentException))]
  69. public void ConstructorHasTooMuchParameters ()
  70. {
  71. Expression.New (typeof (Foo).GetConstructor (new [] { typeof (string) }));
  72. }
  73. [Test]
  74. [Category ("NotDotNet")]
  75. [ExpectedException (typeof (ArgumentException))]
  76. public void NewVoid ()
  77. {
  78. Expression.New (typeof (void));
  79. }
  80. [Test]
  81. [ExpectedException (typeof (ArgumentNullException))]
  82. public void HasNullArgument ()
  83. {
  84. Expression.New (typeof (Foo).GetConstructor (new [] { typeof (string) }), null as Expression);
  85. }
  86. [Test]
  87. [ExpectedException (typeof (ArgumentException))]
  88. public void HasWrongArgument ()
  89. {
  90. Expression.New (typeof (Foo).GetConstructor (new [] { typeof (string) }), Expression.Constant (12));
  91. }
  92. [Test]
  93. public void NewFoo ()
  94. {
  95. var n = Expression.New (typeof (Foo).GetConstructor (new [] { typeof (string) }), Expression.Constant ("foo"));
  96. Assert.AreEqual (ExpressionType.New, n.NodeType);
  97. Assert.AreEqual (typeof (Foo), n.Type);
  98. Assert.AreEqual (1, n.Arguments.Count);
  99. Assert.IsNull (n.Members);
  100. Assert.AreEqual ("new Foo(\"foo\")", n.ToString ());
  101. }
  102. [Test]
  103. public void NewBar ()
  104. {
  105. var n = Expression.New (typeof (Bar));
  106. Assert.IsNotNull (n.Constructor);
  107. Assert.IsNotNull (n.Arguments);
  108. Assert.IsNull (n.Members); // wrong doc
  109. Assert.AreEqual ("new Bar()", n.ToString ());
  110. n = Expression.New (typeof (Bar).GetConstructor (Type.EmptyTypes));
  111. Assert.AreEqual ("new Bar()", n.ToString ());
  112. }
  113. public class Gazonk {
  114. string value;
  115. public Gazonk (string s)
  116. {
  117. value = s;
  118. }
  119. public override bool Equals (object obj)
  120. {
  121. var o = obj as Gazonk;
  122. if (o == null)
  123. return false;
  124. return value == o.value;
  125. }
  126. public override int GetHashCode ()
  127. {
  128. return value.GetHashCode ();
  129. }
  130. }
  131. [Test]
  132. public void CompileNewClass ()
  133. {
  134. var p = Expression.Parameter (typeof (string), "p");
  135. var n = Expression.New (typeof (Gazonk).GetConstructor (new [] { typeof (string) }), p);
  136. var fgaz = Expression.Lambda<Func<string, Gazonk>> (n, p).Compile ();
  137. var g1 = new Gazonk ("foo");
  138. var g2 = new Gazonk ("bar");
  139. Assert.IsNotNull (g1);
  140. Assert.AreEqual (g1, fgaz ("foo"));
  141. Assert.IsNotNull (g2);
  142. Assert.AreEqual (g2, fgaz ("bar"));
  143. n = Expression.New (typeof (Bar));
  144. var lbar = Expression.Lambda<Func<Bar>> (n).Compile ();
  145. var bar = lbar ();
  146. Assert.IsNotNull (bar);
  147. Assert.IsNull (bar.Value);
  148. }
  149. public class FakeAnonymousType {
  150. public string Foo { get; set; }
  151. public string Bar { get; set; }
  152. public string Baz { get; set; }
  153. public int Gazonk { get; set; }
  154. public string Tzap { set {} }
  155. public FakeAnonymousType (string foo)
  156. {
  157. Foo = foo;
  158. }
  159. public FakeAnonymousType (string foo, string bar, string baz)
  160. {
  161. Foo = foo;
  162. Bar = bar;
  163. Baz = baz;
  164. }
  165. }
  166. [Test]
  167. public void NewFakeAnonymousType ()
  168. {
  169. var n = Expression.New (
  170. typeof (FakeAnonymousType).GetConstructor (new [] { typeof (string), typeof (string), typeof (string) } ),
  171. new [] { "Foo".ToConstant (), "Bar".ToConstant (), "Baz".ToConstant () },
  172. new [] { typeof (FakeAnonymousType).GetProperty ("Foo"), typeof (FakeAnonymousType).GetProperty ("Bar"), typeof (FakeAnonymousType).GetProperty ("Baz") });
  173. Assert.IsNotNull (n.Constructor);
  174. Assert.IsNotNull (n.Arguments);
  175. Assert.IsNotNull (n.Members);
  176. Assert.AreEqual ("new FakeAnonymousType(Foo = \"Foo\", Bar = \"Bar\", Baz = \"Baz\")", n.ToString ());
  177. }
  178. [Test]
  179. [ExpectedException (typeof (ArgumentNullException))]
  180. public void NullMember ()
  181. {
  182. Expression.New (
  183. typeof (FakeAnonymousType).GetConstructor (new [] { typeof (string) }),
  184. new [] { "Foo".ToConstant () },
  185. new MemberInfo [] { null });
  186. }
  187. [Test]
  188. [ExpectedException (typeof (ArgumentException))]
  189. public void MemberArgumentMiscount ()
  190. {
  191. Expression.New (
  192. typeof (FakeAnonymousType).GetConstructor (new [] { typeof (string) }),
  193. new [] { "Foo".ToConstant () },
  194. new [] { typeof (FakeAnonymousType).GetProperty ("Foo"), typeof (FakeAnonymousType).GetProperty ("Bar") });
  195. }
  196. [Test]
  197. [ExpectedException (typeof (ArgumentException))]
  198. public void MemberArgumentMismatch ()
  199. {
  200. Expression.New (
  201. typeof (FakeAnonymousType).GetConstructor (new [] { typeof (string) }),
  202. new [] { "Foo".ToConstant () },
  203. new [] { typeof (FakeAnonymousType).GetProperty ("Gazonk") });
  204. }
  205. [Test]
  206. [ExpectedException (typeof (ArgumentException))]
  207. public void MemberHasNoGetter ()
  208. {
  209. Expression.New (
  210. typeof (FakeAnonymousType).GetConstructor (new [] { typeof (string) }),
  211. new [] { "Foo".ToConstant () },
  212. new [] { typeof (FakeAnonymousType).GetProperty ("Tzap") });
  213. }
  214. public struct EineStrukt {
  215. public int left;
  216. public int right;
  217. public EineStrukt (int left, int right)
  218. {
  219. this.left = left;
  220. this.right = right;
  221. }
  222. }
  223. [Test]
  224. public void CompileNewStruct ()
  225. {
  226. var create = Expression.Lambda<Func<EineStrukt>> (
  227. Expression.New (typeof (EineStrukt))).Compile ();
  228. var s = create ();
  229. Assert.AreEqual (0, s.left);
  230. Assert.AreEqual (0, s.right);
  231. }
  232. [Test]
  233. public void CompileNewStructWithParameters ()
  234. {
  235. var pl = Expression.Parameter (typeof (int), "left");
  236. var pr = Expression.Parameter (typeof (int), "right");
  237. var create = Expression.Lambda<Func<int, int, EineStrukt>> (
  238. Expression.New (typeof (EineStrukt).GetConstructor (new [] { typeof (int), typeof (int) }), pl, pr), pl, pr).Compile ();
  239. var s = create (42, 12);
  240. Assert.AreEqual (42, s.left);
  241. Assert.AreEqual (12, s.right);
  242. }
  243. public class EineKlass {
  244. public string Left { get; set; }
  245. public string Right { get; set; }
  246. public EineKlass ()
  247. {
  248. }
  249. public EineKlass (string l, string r)
  250. {
  251. Left = l;
  252. Right = r;
  253. }
  254. }
  255. [Test]
  256. public void CompileNewClassEmptyConstructor ()
  257. {
  258. var create = Expression.Lambda<Func<EineKlass>> (
  259. Expression.New (typeof (EineKlass))).Compile ();
  260. var k = create ();
  261. Assert.IsNull (k.Left);
  262. Assert.IsNull (k.Right);
  263. }
  264. [Test]
  265. public void CompileNewClassWithParameters ()
  266. {
  267. var pl = Expression.Parameter (typeof (string), "left");
  268. var pr = Expression.Parameter (typeof (string), "right");
  269. var create = Expression.Lambda<Func<string, string, EineKlass>> (
  270. Expression.New (typeof (EineKlass).GetConstructor (new [] { typeof (string), typeof (string) }), pl, pr), pl, pr).Compile ();
  271. var k = create ("foo", "bar");
  272. Assert.AreEqual ("foo", k.Left);
  273. Assert.AreEqual ("bar", k.Right);
  274. }
  275. }
  276. }