2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_TypeAs.cs
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:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
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.
28 //
29
30 using System;
31 using System.Reflection;
32 using System.Linq;
33 using System.Linq.Expressions;
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Linq.Expressions
37 {
38         [TestFixture]
39         public class ExpressionTest_TypeAs
40         {
41                 [Test]
42                 [ExpectedException (typeof (ArgumentNullException))]
43                 public void Arg1Null ()
44                 {
45                         Expression.TypeAs (null, typeof (int));
46                 }
47
48                 [Test]
49                 [ExpectedException (typeof (ArgumentNullException))]
50                 public void Arg2Null ()
51                 {
52                         Expression.TypeAs (Expression.Constant (1), null);
53                 }
54
55                 [Test]
56                 [ExpectedException (typeof (ArgumentException))]
57                 public void Arg2NotReferenceNorNullable ()
58                 {
59                         Expression.TypeAs (Expression.Constant (1), typeof (int));
60                 }
61
62                 [Test]
63                 public void NullableNumeric ()
64                 {
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");
69                 }
70
71                 [Test]
72                 public void String ()
73                 {
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");
78                 }
79
80                 [Test]
81                 public void UserDefinedClass ()
82                 {
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");
87                 }
88
89                 static Func<object, TType> CreateTypeAs<TType> ()
90                 {
91                         var obj = Expression.Parameter (typeof (object), "obj");
92
93                         return Expression.Lambda<Func<object, TType>> (
94                                 Expression.TypeAs (obj, typeof (TType)), obj).Compile ();
95                 }
96
97                 struct Foo {
98                 }
99
100                 class Bar {
101                 }
102
103                 class Baz : Bar {
104                 }
105
106                 [Test]
107                 public void CompiledTypeAs ()
108                 {
109                         var asbar = CreateTypeAs<Bar> ();
110                         var asbaz = CreateTypeAs<Baz> ();
111
112                         Assert.IsNotNull (asbar (new Bar ()));
113                         Assert.IsNull (asbar (new Foo ()));
114                         Assert.IsNotNull (asbar (new Baz ()));
115                         Assert.IsNull (asbaz (new Bar ()));
116                 }
117
118                 [Test]
119                 [ExpectedException (typeof (ArgumentException))]
120                 public void TypeAsVoid ()
121                 {
122                         Expression.TypeAs ("yoyo".ToConstant (), typeof (void));
123                 }
124         }
125 }