ExpressionTest_New.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. }
  208. }