TypeAs now ok.
authorFederico Di Gregorio <fog@initd.org>
Fri, 17 Aug 2007 00:23:38 +0000 (00:23 -0000)
committerFederico Di Gregorio <fog@initd.org>
Fri, 17 Aug 2007 00:23:38 +0000 (00:23 -0000)
svn path=/trunk/mcs/; revision=84252

mcs/class/System.Core/System.Core_test.dll.sources
mcs/class/System.Core/System.Linq.Expressions/ChangeLog
mcs/class/System.Core/System.Linq.Expressions/Expression.cs
mcs/class/System.Core/System.Linq.Expressions/UnaryExpression.cs
mcs/class/System.Core/Test/System.Linq.Expressions/ExpressionTest_TypeAs.cs [new file with mode: 0644]

index 7e117f851564eec316f8bbdea5228276c0f3fb8a..928fd1e13936c834f1fa5bbef82cddff13aaf63a 100644 (file)
@@ -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
index 15c2cd031565c8baa4202b85b3feab05540b0986..dbd1925aba920c29877e33eca5ee04e133f34f67 100644 (file)
@@ -1,5 +1,9 @@
 2007-08-17  Federico Di Gregorio <fog@initd.org>
 
+       * Expression.cs: checked TypeAs and added tests.
+
+       * UnaryExpression.cs: added BuildString() case for TypeAs.
+
        * Expression.cs: added tests for TypeIs.
 
        * TypeBinaryExpression.cs: implemented BuildString().
index 528263fc292f284eae7b1e14ad683aa1c2200a61..27437da261180c494b63d6df66b4ce20bc696439 100644 (file)
@@ -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
     }
index 796f567d4bb235b1763b4e7f905083f799aef21e..55559800463213279c2951f5c14fc7292f085b5d 100644 (file)
@@ -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
diff --git a/mcs/class/System.Core/Test/System.Linq.Expressions/ExpressionTest_TypeAs.cs b/mcs/class/System.Core/Test/System.Linq.Expressions/ExpressionTest_TypeAs.cs
new file mode 100644 (file)
index 0000000..93e908d
--- /dev/null
@@ -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 <fog@initd.org>
+
+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");
+        }
+
+    }
+}