2 // ExpressionTest_TypeAs.cs
5 // Federico Di Gregorio <fog@initd.org>
6 // Jb Evain (jbevain@novell.com)
8 // (C) 2008 Novell, Inc. (http://www.novell.com)
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.
31 using System
.Reflection
;
33 using System
.Linq
.Expressions
;
34 using NUnit
.Framework
;
36 namespace MonoTests
.System
.Linq
.Expressions
39 public class ExpressionTest_TypeAs
42 [ExpectedException (typeof (ArgumentNullException
))]
43 public void Arg1Null ()
45 Expression
.TypeAs (null, typeof (int));
49 [ExpectedException (typeof (ArgumentNullException
))]
50 public void Arg2Null ()
52 Expression
.TypeAs (Expression
.Constant (1), null);
56 [ExpectedException (typeof (ArgumentException
))]
57 public void Arg2NotReferenceNorNullable ()
59 Expression
.TypeAs (Expression
.Constant (1), typeof (int));
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");
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");
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 ();
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 ()));
119 [ExpectedException (typeof (ArgumentException
))]
120 public void TypeAsVoid ()
122 Expression
.TypeAs ("yoyo".ToConstant (), typeof (void));