Ver código fonte

new tests

svn path=/trunk/mcs/; revision=105233
Jb Evain 17 anos atrás
pai
commit
e0ddf817df

+ 113 - 0
mcs/class/System.Core/Test/System.Linq.Expressions/ExpressionTest_Coalesce.cs

@@ -18,6 +18,7 @@
 //
 // Authors:
 //		Federico Di Gregorio <[email protected]>
+//		Jb Evain <[email protected]>
 
 using System;
 using System.Reflection;
@@ -129,5 +130,117 @@ namespace MonoTests.System.Linq.Expressions
 			Assert.AreEqual (5, coalesce (5));
 			Assert.AreEqual (99, coalesce (null));
 		}
+
+		[Test]
+		[Category ("NotWorking")]
+		[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]
+		[Category ("NotWorking")]
+		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]
+		[Category ("NotWorking")]
+		[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]
+		[Category ("NotWorking")]
+		[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]
+		[Category ("NotWorking")]
+		[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]
+		[Category ("NotWorking")]
+		[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));
+		}
 	}
 }

+ 0 - 3
mcs/class/System.Core/Test/System.Linq.Expressions/ExpressionTest_Convert.cs

@@ -563,13 +563,10 @@ namespace MonoTests.System.Linq.Expressions {
 			var method = typeof (ImplicitToInt).GetMethod ("op_Implicit");
 
 			var node = Expression.Convert (i, typeof (int), method);
-
 			node = Expression.Convert (node, typeof (long?));
-
 			var conv = Expression.Lambda<Func<ImplicitToInt?, long?>> (node, i).Compile ();
 
 			Assert.AreEqual ((long?) 42, conv (new ImplicitToInt (42)));
-
 			Action convnull = () => Assert.AreEqual (null, conv (null));
 			convnull.AssertThrows (typeof (InvalidOperationException));
 		}