ExpressionTest_New.cs 8.3 KB

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