mono-2.6.7-for-amiga /mcs/class/System.Core/Test/System.Linq.Expressions/ExpressionTest_TypeIs.cs

Language C# Lines 131
MD5 Hash fd8668763457bdef8ec3a5dde5f3c910 Estimated Cost $2,231 (why?)
Repository https://bitbucket.org/steenlund/mono-2.6.7-for-amiga View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// 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_TypeIs
	{
		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void Arg1Null ()
		{
			Expression.TypeIs (null, typeof (int));
		}

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void Arg2Null ()
		{
			Expression.TypeIs (Expression.Constant (1), null);
		}

		[Test]
		public void Numeric ()
		{
			TypeBinaryExpression expr = Expression.TypeIs (Expression.Constant (1), typeof (int));
			Assert.AreEqual (ExpressionType.TypeIs, expr.NodeType, "TypeIs#01");
			Assert.AreEqual (typeof (bool), expr.Type, "TypeIs#02");
			Assert.AreEqual ("(1 Is Int32)", expr.ToString(), "TypeIs#03");
		}

		[Test]
		public void String ()
		{
			TypeBinaryExpression expr = Expression.TypeIs (Expression.Constant (1), typeof (string));
			Assert.AreEqual (ExpressionType.TypeIs, expr.NodeType, "TypeIs#04");
			Assert.AreEqual (typeof (bool), expr.Type, "TypeIs#05");
			Assert.AreEqual ("(1 Is String)", expr.ToString(), "TypeIs#06");
		}

		[Test]
		public void UserDefinedClass ()
		{
			TypeBinaryExpression expr = Expression.TypeIs (Expression.Constant (new OpClass()), typeof (OpClass));
			Assert.AreEqual (ExpressionType.TypeIs, expr.NodeType, "TypeIs#07");
			Assert.AreEqual (typeof (bool), expr.Type, "TypeIs#08");
			Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) Is OpClass)", expr.ToString(), "TypeIs#09");
		}

		struct Foo {
		}

		class Bar {
		}

		class Baz : Bar {
		}

		static Func<TType, bool> CreateTypeIs<TType, TCandidate> ()
		{
			var p = Expression.Parameter (typeof (TType), "p");

			return Expression.Lambda<Func<TType, bool>> (
				Expression.TypeIs (p, typeof (TCandidate)), p).Compile ();
		}

		[Test]
		public void CompiledTypeIs ()
		{
			var foo_is_bar = CreateTypeIs<Foo, Bar> ();
			var foo_is_foo = CreateTypeIs<Foo, Foo> ();
			var bar_is_bar = CreateTypeIs<Bar, Bar> ();
			var bar_is_foo = CreateTypeIs<Bar, Foo> ();
			var baz_is_bar = CreateTypeIs<Baz, Bar> ();

			Assert.IsTrue (foo_is_foo (new Foo ()));
			Assert.IsFalse (foo_is_bar (new Foo ()));
			Assert.IsTrue (bar_is_bar (new Bar ()));
			Assert.IsFalse (bar_is_foo (new Bar ()));
			Assert.IsTrue (baz_is_bar (new Baz ()));
		}

		[Test]
		[Category ("NotDotNet")]
		[ExpectedException (typeof (ArgumentException))]
		public void TypeIsVoid ()
		{
			Expression.TypeIs ("yoyo".ToConstant (), typeof (void));
		}

		public static void TacTac ()
		{
		}

		[Test]
		public void VoidIsObject ()
		{
			var vio = Expression.Lambda<Func<bool>> (
				Expression.TypeIs (
					Expression.Call (GetType ().GetMethod ("TacTac")),
					typeof (object))).Compile ();

			Assert.IsFalse (vio ());
		}
	}
}
Back to Top