ExpressionTest_Coalesce.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. //
  19. // Authors:
  20. // Federico Di Gregorio <[email protected]>
  21. // Jb Evain <[email protected]>
  22. using System;
  23. using System.Reflection;
  24. using System.Linq;
  25. using System.Linq.Expressions;
  26. using NUnit.Framework;
  27. namespace MonoTests.System.Linq.Expressions
  28. {
  29. [TestFixture]
  30. public class ExpressionTest_Coalesce
  31. {
  32. [Test]
  33. [ExpectedException (typeof (ArgumentNullException))]
  34. public void Arg1Null ()
  35. {
  36. Expression.Coalesce (null, Expression.Constant (1));
  37. }
  38. [Test]
  39. [ExpectedException (typeof (ArgumentNullException))]
  40. public void Arg2Null ()
  41. {
  42. Expression.Coalesce (Expression.Constant (1), null);
  43. }
  44. [Test]
  45. [ExpectedException (typeof (InvalidOperationException))]
  46. public void NonNullLeftParameter ()
  47. {
  48. // This throws because they are both doubles, which are never
  49. Expression.Coalesce (Expression.Constant (1.0), Expression.Constant (2.0));
  50. }
  51. [Test]
  52. [ExpectedException (typeof (ArgumentException))]
  53. public void Incompatible_Arguments ()
  54. {
  55. // The artuments are not compatible
  56. Expression.Coalesce (Expression.Parameter (typeof (int?), "a"),
  57. Expression.Parameter (typeof (bool), "b"));
  58. }
  59. [Test]
  60. public void IsCoalesceStringLifted ()
  61. {
  62. var coalesce = Expression.Coalesce (
  63. Expression.Parameter (typeof (string), "a"),
  64. Expression.Parameter (typeof (string), "b"));
  65. Assert.AreEqual ("(a ?? b)", coalesce.ToString ());
  66. Assert.IsFalse (coalesce.IsLifted);
  67. Assert.IsFalse (coalesce.IsLiftedToNull);
  68. }
  69. [Test]
  70. public void IsCoalesceNullableIntLifted ()
  71. {
  72. var coalesce = Expression.Coalesce (
  73. Expression.Parameter (typeof (int?), "a"),
  74. Expression.Parameter (typeof (int?), "b"));
  75. Assert.IsFalse (coalesce.IsLifted);
  76. Assert.IsFalse (coalesce.IsLiftedToNull);
  77. }
  78. [Test]
  79. [Category ("NotWorkingInterpreter")]
  80. public void CoalesceNullableInt ()
  81. {
  82. var a = Expression.Parameter (typeof (int?), "a");
  83. var b = Expression.Parameter (typeof (int?), "b");
  84. var coalesce = Expression.Lambda<Func<int?, int?, int?>> (
  85. Expression.Coalesce (a, b), a, b).Compile ();
  86. Assert.AreEqual ((int?) 1, coalesce (1, 2));
  87. Assert.AreEqual ((int?) null, coalesce (null, null));
  88. Assert.AreEqual ((int?) 2, coalesce (null, 2));
  89. Assert.AreEqual ((int?) 2, coalesce (2, null));
  90. }
  91. [Test]
  92. public void CoalesceString ()
  93. {
  94. var a = Expression.Parameter (typeof (string), "a");
  95. var b = Expression.Parameter (typeof (string), "b");
  96. var coalesce = Expression.Lambda<Func<string, string, string>> (
  97. Expression.Coalesce (a, b), a, b).Compile ();
  98. Assert.AreEqual ("foo", coalesce ("foo", "bar"));
  99. Assert.AreEqual (null, coalesce (null, null));
  100. Assert.AreEqual ("bar", coalesce (null, "bar"));
  101. Assert.AreEqual ("foo", coalesce ("foo", null));
  102. }
  103. [Test]
  104. [Category ("NotWorkingInterpreter")]
  105. public void CoalesceNullableToNonNullable ()
  106. {
  107. var a = Expression.Parameter (typeof (int?), "a");
  108. var node = Expression.Coalesce (a, Expression.Constant (99, typeof (int)));
  109. Assert.AreEqual (typeof (int), node.Type);
  110. Assert.IsFalse (node.IsLifted);
  111. Assert.IsFalse (node.IsLiftedToNull);
  112. var coalesce = Expression.Lambda<Func<int?, int>> (node, a).Compile ();
  113. Assert.AreEqual (5, coalesce (5));
  114. Assert.AreEqual (99, coalesce (null));
  115. }
  116. [Test]
  117. [Category ("NotWorkingInterpreter")]
  118. [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=349822
  119. public void CoalesceUserDefinedConversion ()
  120. {
  121. var s = Expression.Parameter (typeof (string), "s");
  122. var coalesce = Expression.Lambda<Func<string, int>> (
  123. Expression.Coalesce (
  124. s,
  125. Expression.Constant (42),
  126. Expression.Lambda<Func<string, int>> (
  127. Expression.Call (typeof (int).GetMethod ("Parse", new [] { typeof (string) }), s), s)), s).Compile ();
  128. Assert.AreEqual (12, coalesce ("12"));
  129. Assert.AreEqual (42, coalesce (null));
  130. }
  131. struct Slot {
  132. int Value;
  133. public Slot (int v)
  134. {
  135. Value = v;
  136. }
  137. public static implicit operator int (Slot s)
  138. {
  139. return s.Value;
  140. }
  141. }
  142. [Test]
  143. // #12987
  144. [Category ("MobileNotWorking")]
  145. [Category ("NotWorkingInterpreter")]
  146. public void CoalesceNullableSlotIntoInteger ()
  147. {
  148. var s = Expression.Parameter (typeof (Slot?), "s");
  149. var method = typeof (Slot).GetMethod ("op_Implicit");
  150. var coalesce = Expression.Lambda<Func<Slot?, int>> (
  151. Expression.Coalesce (
  152. s,
  153. Expression.Constant (-3),
  154. Expression.Lambda (
  155. Expression.Convert (s, typeof (int), method),
  156. s)), s).Compile ();
  157. Assert.AreEqual (-3, coalesce (null));
  158. Assert.AreEqual (42, coalesce (new Slot (42)));
  159. }
  160. [Test]
  161. [ExpectedException (typeof (ArgumentException))]
  162. public void WrongCoalesceConversionParameterCount ()
  163. {
  164. var s = Expression.Parameter (typeof (string), "s");
  165. var p = Expression.Parameter (typeof (string), "foo");
  166. Expression.Coalesce (
  167. s,
  168. 42.ToConstant (),
  169. Expression.Lambda<Func<string, string, int>> (
  170. Expression.Call (typeof (int).GetMethod ("Parse", new [] { typeof (string) }), s), s, p));
  171. }
  172. [Test]
  173. [ExpectedException (typeof (InvalidOperationException))]
  174. public void WrongCoalesceConversionParameterType ()
  175. {
  176. var s = Expression.Parameter (typeof (string), "s");
  177. var i = Expression.Parameter (typeof (int), "i");
  178. Expression.Coalesce (
  179. s,
  180. 42.ToConstant (),
  181. Expression.Lambda<Func<int, int>> (
  182. i, i));
  183. }
  184. [Test]
  185. [ExpectedException (typeof (InvalidOperationException))]
  186. public void WrongCoalesceConversionReturnType ()
  187. {
  188. var s = Expression.Parameter (typeof (string), "s");
  189. Expression.Coalesce (
  190. s,
  191. 42.ToConstant (),
  192. Expression.Lambda<Func<string, string>> (
  193. s, s));
  194. }
  195. [Test]
  196. [ExpectedException (typeof (ArgumentException))]
  197. public void CoalesceVoidUserDefinedConversion ()
  198. {
  199. var s = Expression.Parameter (typeof (string), "s");
  200. Expression.Coalesce (
  201. s,
  202. 42.ToConstant (),
  203. Expression.Lambda<Action<string>> (
  204. Expression.Call (typeof (int).GetMethod ("Parse", new [] { typeof (string) }), s), s));
  205. }
  206. }
  207. }