Merge pull request #4152 from BrzVlad/misc-gc-altstack
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_Coalesce.cs
index 03b86a49ae7720d752abd4c170dde0c6032bc7e7..2ab978d51876f4e468d375dc9b74b98bd0a4188e 100644 (file)
@@ -18,6 +18,7 @@
 //
 // Authors:
 //             Federico Di Gregorio <fog@initd.org>
+//             Jb Evain <jbevain@novell.com>
 
 using System;
 using System.Reflection;
@@ -129,5 +130,111 @@ namespace MonoTests.System.Linq.Expressions
                        Assert.AreEqual (5, coalesce (5));
                        Assert.AreEqual (99, coalesce (null));
                }
+
+               [Test]
+               [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=349822
+               public void CoalesceUserDefinedConversion ()
+               {
+                       var s = Expression.Parameter (typeof (string), "s");
+
+                       var coalesce = Expression.Lambda<Func<string, int>> (
+                               Expression.Coalesce (
+                                       s,
+                                       Expression.Constant (42),
+                                       Expression.Lambda<Func<string, int>> (
+                                               Expression.Call (typeof (int).GetMethod ("Parse", new [] { typeof (string) }), s), s)), s).Compile ();
+
+                       Assert.AreEqual (12, coalesce ("12"));
+                       Assert.AreEqual (42, coalesce (null));
+               }
+
+               struct Slot {
+                       int Value;
+
+                       public Slot (int v)
+                       {
+                               Value = v;
+                       }
+
+                       public static implicit operator int (Slot s)
+                       {
+                               return s.Value;
+                       }
+               }
+
+               [Test]
+               public void CoalesceNullableSlotIntoInteger ()
+               {
+                       var s = Expression.Parameter (typeof (Slot?), "s");
+
+                       var method = typeof (Slot).GetMethod ("op_Implicit");
+
+                       var coalesce = Expression.Lambda<Func<Slot?, int>> (
+                               Expression.Coalesce (
+                                       s,
+                                       Expression.Constant (-3),
+                                       Expression.Lambda (
+                                               Expression.Convert (s, typeof (int), method),
+                                               s)), s).Compile ();
+
+                       Assert.AreEqual (-3, coalesce (null));
+                       Assert.AreEqual (42, coalesce (new Slot (42)));
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void WrongCoalesceConversionParameterCount ()
+               {
+                       var s = Expression.Parameter (typeof (string), "s");
+                       var p = Expression.Parameter (typeof (string), "foo");
+
+                       Expression.Coalesce (
+                               s,
+                               42.ToConstant (),
+                               Expression.Lambda<Func<string, string, int>> (
+                                       Expression.Call (typeof (int).GetMethod ("Parse", new [] { typeof (string) }), s), s, p));
+
+               }
+
+               [Test]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void WrongCoalesceConversionParameterType ()
+               {
+                       var s = Expression.Parameter (typeof (string), "s");
+                       var i = Expression.Parameter (typeof (int), "i");
+
+                       Expression.Coalesce (
+                               s,
+                               42.ToConstant (),
+                               Expression.Lambda<Func<int, int>> (
+                                       i, i));
+
+               }
+
+               [Test]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void WrongCoalesceConversionReturnType ()
+               {
+                       var s = Expression.Parameter (typeof (string), "s");
+
+                       Expression.Coalesce (
+                               s,
+                               42.ToConstant (),
+                               Expression.Lambda<Func<string, string>> (
+                                       s, s));
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void CoalesceVoidUserDefinedConversion ()
+               {
+                       var s = Expression.Parameter (typeof (string), "s");
+
+                       Expression.Coalesce (
+                               s,
+                               42.ToConstant (),
+                               Expression.Lambda<Action<string>> (
+                                       Expression.Call (typeof (int).GetMethod ("Parse", new [] { typeof (string) }), s), s));
+               }
        }
 }