ExpressionTest_Coalesce.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. using System;
  22. using System.Reflection;
  23. using System.Linq;
  24. using System.Linq.Expressions;
  25. using NUnit.Framework;
  26. namespace MonoTests.System.Linq.Expressions
  27. {
  28. [TestFixture]
  29. public class ExpressionTest_Coalesce
  30. {
  31. [Test]
  32. [ExpectedException (typeof (ArgumentNullException))]
  33. public void Arg1Null ()
  34. {
  35. Expression.Coalesce (null, Expression.Constant (1));
  36. }
  37. [Test]
  38. [ExpectedException (typeof (ArgumentNullException))]
  39. public void Arg2Null ()
  40. {
  41. Expression.Coalesce (Expression.Constant (1), null);
  42. }
  43. [Test]
  44. [ExpectedException (typeof (InvalidOperationException))]
  45. public void NonNullLeftParameter ()
  46. {
  47. // This throws because they are both doubles, which are never
  48. Expression.Coalesce (Expression.Constant (1.0), Expression.Constant (2.0));
  49. }
  50. [Test]
  51. [ExpectedException (typeof (ArgumentException))]
  52. public void Incompatible_Arguments ()
  53. {
  54. // The artuments are not compatible
  55. Expression.Coalesce (Expression.Parameter (typeof (int?), "a"),
  56. Expression.Parameter (typeof (bool), "b"));
  57. }
  58. [Test]
  59. public void IsCoalesceStringLifted ()
  60. {
  61. var coalesce = Expression.Coalesce (
  62. Expression.Parameter (typeof (string), "a"),
  63. Expression.Parameter (typeof (string), "b"));
  64. Assert.AreEqual ("(a ?? b)", coalesce.ToString ());
  65. Assert.IsFalse (coalesce.IsLifted);
  66. Assert.IsFalse (coalesce.IsLiftedToNull);
  67. }
  68. [Test]
  69. public void IsCoalesceNullableIntLifted ()
  70. {
  71. var coalesce = Expression.Coalesce (
  72. Expression.Parameter (typeof (int?), "a"),
  73. Expression.Parameter (typeof (int?), "b"));
  74. Assert.IsFalse (coalesce.IsLifted);
  75. Assert.IsFalse (coalesce.IsLiftedToNull);
  76. }
  77. [Test]
  78. public void CoalesceNullableInt ()
  79. {
  80. var a = Expression.Parameter (typeof (int?), "a");
  81. var b = Expression.Parameter (typeof (int?), "b");
  82. var coalesce = Expression.Lambda<Func<int?, int?, int?>> (
  83. Expression.Coalesce (a, b), a, b).Compile ();
  84. Assert.AreEqual ((int?) 1, coalesce (1, 2));
  85. Assert.AreEqual ((int?) null, coalesce (null, null));
  86. Assert.AreEqual ((int?) 2, coalesce (null, 2));
  87. Assert.AreEqual ((int?) 2, coalesce (2, null));
  88. }
  89. [Test]
  90. public void CoalesceString ()
  91. {
  92. var a = Expression.Parameter (typeof (string), "a");
  93. var b = Expression.Parameter (typeof (string), "b");
  94. var coalesce = Expression.Lambda<Func<string, string, string>> (
  95. Expression.Coalesce (a, b), a, b).Compile ();
  96. Assert.AreEqual ("foo", coalesce ("foo", "bar"));
  97. Assert.AreEqual (null, coalesce (null, null));
  98. Assert.AreEqual ("bar", coalesce (null, "bar"));
  99. Assert.AreEqual ("foo", coalesce ("foo", null));
  100. }
  101. [Test]
  102. public void CoalesceNullableToNonNullable ()
  103. {
  104. var a = Expression.Parameter (typeof (int?), "a");
  105. var node = Expression.Coalesce (a, Expression.Constant (99, typeof (int)));
  106. Assert.AreEqual (typeof (int), node.Type);
  107. Assert.IsFalse (node.IsLifted);
  108. Assert.IsFalse (node.IsLiftedToNull);
  109. var coalesce = Expression.Lambda<Func<int?, int>> (node, a).Compile ();
  110. Assert.AreEqual (5, coalesce (5));
  111. Assert.AreEqual (99, coalesce (null));
  112. }
  113. }
  114. }