瀏覽代碼

TypeAs now ok.

svn path=/trunk/mcs/; revision=84252
Federico Di Gregorio 18 年之前
父節點
當前提交
bcb310484b

+ 1 - 0
mcs/class/System.Core/System.Core_test.dll.sources

@@ -16,5 +16,6 @@ System.Linq.Expressions/ExpressionTest_Or.cs
 System.Linq.Expressions/ExpressionTest_OrElse.cs
 System.Linq.Expressions/ExpressionTest_Subtract.cs
 System.Linq.Expressions/ExpressionTest_SubtractChecked.cs
+System.Linq.Expressions/ExpressionTest_TypeAs.cs
 System.Linq.Expressions/ExpressionTest_TypeIs.cs
 System.Linq.Expressions/ExpressionTest_Utils.cs

+ 4 - 0
mcs/class/System.Core/System.Linq.Expressions/ChangeLog

@@ -1,5 +1,9 @@
 2007-08-17  Federico Di Gregorio <[email protected]>
 
+	* Expression.cs: checked TypeAs and added tests.
+
+	* UnaryExpression.cs: added BuildString() case for TypeAs.
+
 	* Expression.cs: added tests for TypeIs.
 
 	* TypeBinaryExpression.cs: implemented BuildString().

+ 10 - 10
mcs/class/System.Core/System.Linq.Expressions/Expression.cs

@@ -1034,27 +1034,27 @@ namespace System.Linq.Expressions
         }
         #endregion
 
-        #region TypeIs
-        public static TypeBinaryExpression TypeIs(Expression expression, Type type)
+        #region TypeAs
+        public static UnaryExpression TypeAs(Expression expression, Type type)
         {
             if (expression == null)
                 throw new ArgumentNullException("expression");
             if (type == null)
-                throw new ArgumentNullException("type"); 
-            
-            return new TypeBinaryExpression(ExpressionType.TypeIs, expression, type, typeof(bool));
+                throw new ArgumentNullException("type");
+
+            return new UnaryExpression(ExpressionType.TypeAs, expression, type);
         }
         #endregion
 
-        #region TypeAs
-        public static UnaryExpression TypeAs(Expression expression, Type type)
+        #region TypeIs
+        public static TypeBinaryExpression TypeIs(Expression expression, Type type)
         {
             if (expression == null)
                 throw new ArgumentNullException("expression");
             if (type == null)
-                throw new ArgumentNullException("type");
-
-            return new UnaryExpression(ExpressionType.TypeAs, expression, type);
+                throw new ArgumentNullException("type"); 
+            
+            return new TypeBinaryExpression(ExpressionType.TypeIs, expression, type, typeof(bool));
         }
         #endregion
     }

+ 10 - 2
mcs/class/System.Core/System.Linq.Expressions/UnaryExpression.cs

@@ -74,9 +74,17 @@ namespace System.Linq.Expressions
             {
             case ExpressionType.ArrayLength:
                 builder.Append ("ArrayLength(");
-                operand.BuildString(builder);
-                builder.Append(")");
+                operand.BuildString (builder);
+                builder.Append (")");
                 break;
+
+            case ExpressionType.TypeAs:
+                builder.Append ("(");
+                operand.BuildString (builder);
+                builder.Append (" As ").Append (Type.Name);
+                builder.Append (")");
+                break;
+
             }
          }
         #endregion

+ 75 - 0
mcs/class/System.Core/Test/System.Linq.Expressions/ExpressionTest_TypeAs.cs

@@ -0,0 +1,75 @@
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+//
+// Authors:
+//        Federico Di Gregorio <[email protected]>
+
+using System;
+using System.Reflection;
+using System.Linq;
+using System.Linq.Expressions;
+using NUnit.Framework;
+
+namespace MonoTests.System.Linq.Expressions
+{        
+    [TestFixture]
+    public class ExpressionTest_TypeAs
+    {
+        [Test]
+        [ExpectedException (typeof (ArgumentNullException))]
+        public void Arg1Null ()
+        {
+            Expression.TypeAs (null, typeof (int));
+        }
+
+        [Test]
+        [ExpectedException (typeof (ArgumentNullException))]
+        public void Arg2Null ()
+        {
+            Expression.TypeAs (Expression.Constant (1), null);
+        }
+
+        [Test]
+        public void Numeric ()
+        {
+            UnaryExpression expr = Expression.TypeAs (Expression.Constant (1), typeof (int));
+            Assert.AreEqual (ExpressionType.TypeAs, expr.NodeType, "TypeAs#01");
+            Assert.AreEqual (typeof (int), expr.Type, "TypeAs#02");
+            Assert.AreEqual ("(1 As Int32)", expr.ToString(), "TypeAs#03");
+        }
+
+        [Test]
+        public void String ()
+        {
+            UnaryExpression expr = Expression.TypeAs (Expression.Constant (1), typeof (string));
+            Assert.AreEqual (ExpressionType.TypeAs, expr.NodeType, "TypeAs#04");
+            Assert.AreEqual (typeof (string), expr.Type, "TypeAs#05");
+            Assert.AreEqual ("(1 As String)", expr.ToString(), "TypeAs#06");
+        }
+
+        [Test]
+        public void UserDefinedClass ()
+        {
+            UnaryExpression expr = Expression.TypeAs (Expression.Constant (new OpClass()), typeof (OpClass));
+            Assert.AreEqual (ExpressionType.TypeAs, expr.NodeType, "TypeAs#07");
+            Assert.AreEqual (typeof (OpClass), expr.Type, "TypeAs#08");
+            Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) As OpClass)", expr.ToString(), "TypeAs#09");
+        }
+
+    }
+}