ExpressionTest_Convert.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // ExpressionTest_Convert.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_Convert {
  36. [Test]
  37. [ExpectedException (typeof (ArgumentNullException))]
  38. public void NullExpression ()
  39. {
  40. Expression.Convert (null, typeof (int));
  41. }
  42. [Test]
  43. [ExpectedException (typeof (ArgumentNullException))]
  44. public void NullType ()
  45. {
  46. Expression.Convert (1.ToConstant (), null);
  47. }
  48. [Test]
  49. [Category ("NotWorking")]
  50. [ExpectedException (typeof (InvalidOperationException))]
  51. public void ConvertIntToString ()
  52. {
  53. Expression.Convert (1.ToConstant (), typeof (string));
  54. }
  55. interface IFoo { }
  56. class Foo : IFoo { }
  57. class Bar : Foo { }
  58. class Baz { }
  59. [Test]
  60. [Category ("NotWorking")]
  61. [ExpectedException (typeof (InvalidOperationException))]
  62. public void ConvertBazToFoo ()
  63. {
  64. Expression.Convert (Expression.Parameter (typeof (Baz), ""), typeof (Foo));
  65. }
  66. struct EineStrukt { }
  67. [Test]
  68. [Category ("NotWorking")]
  69. [ExpectedException (typeof (InvalidOperationException))]
  70. public void ConvertStructToFoo ()
  71. {
  72. Expression.Convert (Expression.Parameter (typeof (EineStrukt), ""), typeof (Foo));
  73. }
  74. [Test]
  75. [Category ("NotWorking")]
  76. [ExpectedException (typeof (InvalidOperationException))]
  77. public void ConvertInt32ToBool ()
  78. {
  79. Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (bool));
  80. }
  81. [Test]
  82. [Category ("NotWorking")]
  83. public void ConvertIFooToFoo ()
  84. {
  85. var c = Expression.Convert (Expression.Parameter (typeof (IFoo), ""), typeof (Foo));
  86. Assert.AreEqual (typeof (Foo), c.Type);
  87. Assert.IsFalse (c.IsLifted);
  88. Assert.IsFalse (c.IsLiftedToNull);
  89. Assert.IsNull (c.Method);
  90. }
  91. [Test]
  92. [Category ("NotWorking")]
  93. public void BoxInt32 ()
  94. {
  95. var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (object));
  96. Assert.AreEqual (typeof (object), c.Type);
  97. Assert.IsFalse (c.IsLifted);
  98. Assert.IsFalse (c.IsLiftedToNull);
  99. Assert.IsNull (c.Method);
  100. }
  101. [Test]
  102. [Category ("NotWorking")]
  103. public void UnBoxInt32 ()
  104. {
  105. var c = Expression.Convert (Expression.Parameter (typeof (object), ""), typeof (int));
  106. Assert.AreEqual (typeof (int), c.Type);
  107. Assert.IsFalse (c.IsLifted);
  108. Assert.IsFalse (c.IsLiftedToNull);
  109. Assert.IsNull (c.Method);
  110. }
  111. [Test]
  112. [Category ("NotWorking")]
  113. public void ConvertInt32ToInt64 ()
  114. {
  115. var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (long));
  116. Assert.AreEqual (typeof (long), c.Type);
  117. Assert.IsFalse (c.IsLifted);
  118. Assert.IsFalse (c.IsLiftedToNull);
  119. Assert.IsNull (c.Method);
  120. }
  121. [Test]
  122. [Category ("NotWorking")]
  123. public void ConvertInt64ToInt32 ()
  124. {
  125. var c = Expression.Convert (Expression.Parameter (typeof (long), ""), typeof (int));
  126. Assert.AreEqual (typeof (int), c.Type);
  127. Assert.IsFalse (c.IsLifted);
  128. Assert.IsFalse (c.IsLiftedToNull);
  129. Assert.IsNull (c.Method);
  130. }
  131. enum EineEnum { }
  132. [Test]
  133. [Category ("NotWorking")]
  134. public void ConvertEnumToInt32 ()
  135. {
  136. var c = Expression.Convert (Expression.Parameter (typeof (EineEnum), ""), typeof (int));
  137. Assert.AreEqual (typeof (int), c.Type);
  138. Assert.IsFalse (c.IsLifted);
  139. Assert.IsFalse (c.IsLiftedToNull);
  140. Assert.IsNull (c.Method);
  141. }
  142. [Test]
  143. [Category ("NotWorking")]
  144. public void ConvertNullableInt32ToInt32 ()
  145. {
  146. var c = Expression.Convert (Expression.Parameter (typeof (int?), ""), typeof (int));
  147. Assert.AreEqual (typeof (int), c.Type);
  148. Assert.IsTrue (c.IsLifted);
  149. Assert.IsFalse (c.IsLiftedToNull);
  150. Assert.IsNull (c.Method);
  151. }
  152. [Test]
  153. [Category ("NotWorking")]
  154. public void ConvertInt32ToNullableInt32 ()
  155. {
  156. var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (int?));
  157. Assert.AreEqual (typeof (int?), c.Type);
  158. Assert.IsTrue (c.IsLifted);
  159. Assert.IsTrue (c.IsLiftedToNull);
  160. Assert.IsNull (c.Method);
  161. }
  162. class Klang {
  163. int i;
  164. public Klang (int i)
  165. {
  166. this.i = i;
  167. }
  168. public static explicit operator int (Klang k)
  169. {
  170. return k.i;
  171. }
  172. }
  173. [Test]
  174. [Category ("NotWorking")]
  175. public void ConvertClassWithExplicitOp ()
  176. {
  177. var c = Expression.Convert (Expression.Parameter (typeof (Klang), ""), typeof (int));
  178. Assert.AreEqual (typeof (int), c.Type);
  179. Assert.IsFalse (c.IsLifted);
  180. Assert.IsFalse (c.IsLiftedToNull);
  181. Assert.IsNotNull (c.Method);
  182. }
  183. [Test]
  184. [Category ("NotWorking")]
  185. public void ConvertClassWithExplicitOpToNullableInt ()
  186. {
  187. var c = Expression.Convert (Expression.Parameter (typeof (Klang), ""), typeof (int?));
  188. Assert.AreEqual (typeof (int?), c.Type);
  189. Assert.IsTrue (c.IsLifted);
  190. Assert.IsTrue (c.IsLiftedToNull);
  191. Assert.IsNotNull (c.Method);
  192. }
  193. class Kling {
  194. int i;
  195. public Kling (int i)
  196. {
  197. this.i = i;
  198. }
  199. public static implicit operator int (Kling k)
  200. {
  201. return k.i;
  202. }
  203. }
  204. [Test]
  205. [Category ("NotWorking")]
  206. public void ConvertClassWithImplicitOp ()
  207. {
  208. var c = Expression.Convert (Expression.Parameter (typeof (Kling), ""), typeof (int));
  209. Assert.AreEqual (typeof (int), c.Type);
  210. Assert.IsFalse (c.IsLifted);
  211. Assert.IsFalse (c.IsLiftedToNull);
  212. Assert.IsNotNull (c.Method);
  213. }
  214. [Test]
  215. [Category ("NotWorking")]
  216. public void ConvertClassWithImplicitOpToNullableInt ()
  217. {
  218. var c = Expression.Convert (Expression.Parameter (typeof (Kling), ""), typeof (int?));
  219. Assert.AreEqual (typeof (int?), c.Type);
  220. Assert.IsTrue (c.IsLifted);
  221. Assert.IsTrue (c.IsLiftedToNull);
  222. Assert.IsNotNull (c.Method);
  223. }
  224. }
  225. }