2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_TypeAs.cs
blobc231e8cdae93281900e665e69bce48fecc74c2b6
1 //
2 // ExpressionTest_TypeAs.cs
3 //
4 // Author:
5 // Federico Di Gregorio <fog@initd.org>
6 // Jb Evain (jbevain@novell.com)
7 //
8 // (C) 2008 Novell, Inc. (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System;
31 using System.Reflection;
32 using System.Linq;
33 using System.Linq.Expressions;
34 using NUnit.Framework;
36 namespace MonoTests.System.Linq.Expressions
38 [TestFixture]
39 public class ExpressionTest_TypeAs
41 [Test]
42 [ExpectedException (typeof (ArgumentNullException))]
43 public void Arg1Null ()
45 Expression.TypeAs (null, typeof (int));
48 [Test]
49 [ExpectedException (typeof (ArgumentNullException))]
50 public void Arg2Null ()
52 Expression.TypeAs (Expression.Constant (1), null);
55 [Test]
56 [ExpectedException (typeof (ArgumentException))]
57 public void Arg2NotReferenceNorNullable ()
59 Expression.TypeAs (Expression.Constant (1), typeof (int));
62 [Test]
63 public void NullableNumeric ()
65 UnaryExpression expr = Expression.TypeAs (Expression.Constant (1), typeof (int?));
66 Assert.AreEqual (ExpressionType.TypeAs, expr.NodeType, "TypeAs#01");
67 Assert.AreEqual (typeof (int?), expr.Type, "TypeAs#02");
68 Assert.AreEqual ("(1 As Nullable`1)", expr.ToString(), "TypeAs#03");
71 [Test]
72 public void String ()
74 UnaryExpression expr = Expression.TypeAs (Expression.Constant (1), typeof (string));
75 Assert.AreEqual (ExpressionType.TypeAs, expr.NodeType, "TypeAs#04");
76 Assert.AreEqual (typeof (string), expr.Type, "TypeAs#05");
77 Assert.AreEqual ("(1 As String)", expr.ToString(), "TypeAs#06");
80 [Test]
81 public void UserDefinedClass ()
83 UnaryExpression expr = Expression.TypeAs (Expression.Constant (new OpClass()), typeof (OpClass));
84 Assert.AreEqual (ExpressionType.TypeAs, expr.NodeType, "TypeAs#07");
85 Assert.AreEqual (typeof (OpClass), expr.Type, "TypeAs#08");
86 Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) As OpClass)", expr.ToString(), "TypeAs#09");
89 static Func<object, TType> CreateTypeAs<TType> ()
91 var obj = Expression.Parameter (typeof (object), "obj");
93 return Expression.Lambda<Func<object, TType>> (
94 Expression.TypeAs (obj, typeof (TType)), obj).Compile ();
97 struct Foo {
100 class Bar {
103 class Baz : Bar {
106 [Test]
107 public void CompiledTypeAs ()
109 var asbar = CreateTypeAs<Bar> ();
110 var asbaz = CreateTypeAs<Baz> ();
112 Assert.IsNotNull (asbar (new Bar ()));
113 Assert.IsNull (asbar (new Foo ()));
114 Assert.IsNotNull (asbar (new Baz ()));
115 Assert.IsNull (asbaz (new Bar ()));
118 [Test]
119 [ExpectedException (typeof (ArgumentException))]
120 public void TypeAsVoid ()
122 Expression.TypeAs ("yoyo".ToConstant (), typeof (void));