New tests.
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_AndAlso.cs
index 1ba683d58a2107e14f9470fdce97e99f1b3cc32e..27a7e2ec3e7bea0dd1fd727152f7e343c10fd104 100644 (file)
@@ -80,7 +80,9 @@ namespace MonoTests.System.Linq.Expressions
                        Assert.AreEqual (ExpressionType.AndAlso, expr.NodeType, "AndAlso#01");
                        Assert.AreEqual (typeof (bool), expr.Type, "AndAlso#02");
                        Assert.IsNull (expr.Method, "AndAlso#03");
+#if !NET_4_0
                        Assert.AreEqual ("(True && False)", expr.ToString(), "AndAlso#04");
+#endif
                }
 
                [Test]
@@ -95,8 +97,10 @@ namespace MonoTests.System.Linq.Expressions
                        Assert.AreEqual (typeof (OpClass), expr.Type, "AndAlso#06");
                        Assert.AreEqual (mi, expr.Method, "AndAlso#07");
                        Assert.AreEqual ("op_BitwiseAnd", expr.Method.Name, "AndAlso#08");
+#if !NET_4_0
                        Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) && value(MonoTests.System.Linq.Expressions.OpClass))",
                                expr.ToString(), "AndAlso#09");
+#endif
                }
 
                [Test]
@@ -254,7 +258,37 @@ namespace MonoTests.System.Linq.Expressions
                }
 
                [Test]
-               [Category ("NotWorking")]
+               public void UserDefinedAndAlsoShortCircuit ()
+               {
+                       var i = Expression.Parameter (typeof (Item<Slot>), "i");
+                       var and = Expression.Lambda<Func<Item<Slot>, Slot>> (
+                               Expression.AndAlso (
+                                       Expression.Property (i, "Left"),
+                                       Expression.Property (i, "Right")), i).Compile ();
+
+                       var item = new Item<Slot> (new Slot (0), new Slot (1));
+                       Assert.AreEqual (new Slot (0), and (item));
+                       Assert.IsTrue (item.LeftCalled);
+                       Assert.IsFalse (item.RightCalled);
+               }
+
+               [Test]
+               [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350228
+               public void UserDefinedLiftedAndAlsoShortCircuit ()
+               {
+                       var i = Expression.Parameter (typeof (Item<Slot?>), "i");
+                       var and = Expression.Lambda<Func<Item<Slot?>, Slot?>> (
+                               Expression.AndAlso (
+                                       Expression.Property (i, "Left"),
+                                       Expression.Property (i, "Right")), i).Compile ();
+
+                       var item = new Item<Slot?> (null, new Slot (1));
+                       Assert.AreEqual ((Slot?) null, and (item));
+                       Assert.IsTrue (item.LeftCalled);
+                       Assert.IsFalse (item.RightCalled);
+               }
+
+               [Test]
                public void UserDefinedAndAlsoLiftedToNull ()
                {
                        var l = Expression.Parameter (typeof (Slot?), "l");
@@ -302,5 +336,44 @@ namespace MonoTests.System.Linq.Expressions
 
                        Expression.AndAlso (l, r, method);
                }
+
+               class A {
+                       public static bool operator true (A x)
+                       {
+                               return true;
+                       }
+
+                       public static bool operator false (A x)
+                       {
+                               return false;
+                       }
+               }
+
+               class B : A {
+                       public static B operator & (B x, B y)
+                       {
+                               return new B ();
+                       }
+
+                       public static bool op_True<T> (B x)
+                       {
+                               return true;
+                       }
+
+                       public static bool op_False (B x)
+                       {
+                               return false;
+                       }
+               }
+
+               [Test] // from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350487
+               public void Connect350487 ()
+               {
+                       var p = Expression.Parameter (typeof (B), "b");
+                       var l = Expression.Lambda<Func<B, A>> (
+                               Expression.AndAlso (p, p), p).Compile ();
+
+                       Assert.IsNotNull (l (null));
+               }
        }
 }