ExpressionTest_Coalesce.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. [Category("SRE")]
  31. public class ExpressionTest_Coalesce
  32. {
  33. [Test]
  34. [ExpectedException (typeof (ArgumentNullException))]
  35. public void Arg1Null ()
  36. {
  37. Expression.Coalesce (null, Expression.Constant (1));
  38. }
  39. [Test]
  40. [ExpectedException (typeof (ArgumentNullException))]
  41. public void Arg2Null ()
  42. {
  43. Expression.Coalesce (Expression.Constant (1), null);
  44. }
  45. [Test]
  46. [ExpectedException (typeof (InvalidOperationException))]
  47. public void NonNullLeftParameter ()
  48. {
  49. // This throws because they are both doubles, which are never
  50. Expression.Coalesce (Expression.Constant (1.0), Expression.Constant (2.0));
  51. }
  52. [Test]
  53. [ExpectedException (typeof (ArgumentException))]
  54. public void Incompatible_Arguments ()
  55. {
  56. // The artuments are not compatible
  57. Expression.Coalesce (Expression.Parameter (typeof (int?), "a"),
  58. Expression.Parameter (typeof (bool), "b"));
  59. }
  60. [Test]
  61. public void IsCoalesceStringLifted ()
  62. {
  63. var coalesce = Expression.Coalesce (
  64. Expression.Parameter (typeof (string), "a"),
  65. Expression.Parameter (typeof (string), "b"));
  66. Assert.AreEqual ("(a ?? b)", coalesce.ToString ());
  67. Assert.IsFalse (coalesce.IsLifted);
  68. Assert.IsFalse (coalesce.IsLiftedToNull);
  69. }
  70. [Test]
  71. public void IsCoalesceNullableIntLifted ()
  72. {
  73. var coalesce = Expression.Coalesce (
  74. Expression.Parameter (typeof (int?), "a"),
  75. Expression.Parameter (typeof (int?), "b"));
  76. Assert.IsFalse (coalesce.IsLifted);
  77. Assert.IsFalse (coalesce.IsLiftedToNull);
  78. }
  79. [Test]
  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. public void CoalesceNullableToNonNullable ()
  105. {
  106. var a = Expression.Parameter (typeof (int?), "a");
  107. var node = Expression.Coalesce (a, Expression.Constant (99, typeof (int)));
  108. Assert.AreEqual (typeof (int), node.Type);
  109. Assert.IsFalse (node.IsLifted);
  110. Assert.IsFalse (node.IsLiftedToNull);
  111. var coalesce = Expression.Lambda<Func<int?, int>> (node, a).Compile ();
  112. Assert.AreEqual (5, coalesce (5));
  113. Assert.AreEqual (99, coalesce (null));
  114. }
  115. [Test]
  116. [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=349822
  117. public void CoalesceUserDefinedConversion ()
  118. {
  119. var s = Expression.Parameter (typeof (string), "s");
  120. var coalesce = Expression.Lambda<Func<string, int>> (
  121. Expression.Coalesce (
  122. s,
  123. Expression.Constant (42),
  124. Expression.Lambda<Func<string, int>> (
  125. Expression.Call (typeof (int).GetMethod ("Parse", new [] { typeof (string) }), s), s)), s).Compile ();
  126. Assert.AreEqual (12, coalesce ("12"));
  127. Assert.AreEqual (42, coalesce (null));
  128. }
  129. struct Slot {
  130. int Value;
  131. public Slot (int v)
  132. {
  133. Value = v;
  134. }
  135. public static implicit operator int (Slot s)
  136. {
  137. return s.Value;
  138. }
  139. }
  140. [Test]
  141. public void CoalesceNullableSlotIntoInteger ()
  142. {
  143. var s = Expression.Parameter (typeof (Slot?), "s");
  144. var method = typeof (Slot).GetMethod ("op_Implicit");
  145. var coalesce = Expression.Lambda<Func<Slot?, int>> (
  146. Expression.Coalesce (
  147. s,
  148. Expression.Constant (-3),
  149. Expression.Lambda (
  150. Expression.Convert (s, typeof (int), method),
  151. s)), s).Compile ();
  152. Assert.AreEqual (-3, coalesce (null));
  153. Assert.AreEqual (42, coalesce (new Slot (42)));
  154. }
  155. [Test]
  156. [ExpectedException (typeof (ArgumentException))]
  157. public void WrongCoalesceConversionParameterCount ()
  158. {
  159. var s = Expression.Parameter (typeof (string), "s");
  160. var p = Expression.Parameter (typeof (string), "foo");
  161. Expression.Coalesce (
  162. s,
  163. 42.ToConstant (),
  164. Expression.Lambda<Func<string, string, int>> (
  165. Expression.Call (typeof (int).GetMethod ("Parse", new [] { typeof (string) }), s), s, p));
  166. }
  167. [Test]
  168. [ExpectedException (typeof (InvalidOperationException))]
  169. public void WrongCoalesceConversionParameterType ()
  170. {
  171. var s = Expression.Parameter (typeof (string), "s");
  172. var i = Expression.Parameter (typeof (int), "i");
  173. Expression.Coalesce (
  174. s,
  175. 42.ToConstant (),
  176. Expression.Lambda<Func<int, int>> (
  177. i, i));
  178. }
  179. [Test]
  180. [ExpectedException (typeof (InvalidOperationException))]
  181. public void WrongCoalesceConversionReturnType ()
  182. {
  183. var s = Expression.Parameter (typeof (string), "s");
  184. Expression.Coalesce (
  185. s,
  186. 42.ToConstant (),
  187. Expression.Lambda<Func<string, string>> (
  188. s, s));
  189. }
  190. [Test]
  191. [ExpectedException (typeof (ArgumentException))]
  192. public void CoalesceVoidUserDefinedConversion ()
  193. {
  194. var s = Expression.Parameter (typeof (string), "s");
  195. Expression.Coalesce (
  196. s,
  197. 42.ToConstant (),
  198. Expression.Lambda<Action<string>> (
  199. Expression.Call (typeof (int).GetMethod ("Parse", new [] { typeof (string) }), s), s));
  200. }
  201. }
  202. }