Exemplo n.º 1
0
		public void EqualityCheckBetweenIncompatibleTypesFails()
		{
			Derived1Class derived1Class = new Derived1Class();
			Derived2Class derived2Class = new Derived2Class();

			Expression<bool> expression = new Expression<bool>();
			expression.Parameters.Add("derived1Class", typeof(Derived1Class), derived1Class);
			expression.Parameters.Add("derived2Class", typeof(Derived2Class), derived2Class);

			try
			{
				expression.Text = "derived1Class = derived2Class";
				expression.Evaluate();
				Assert.Fail("Equality check between incompatible types should fail.");
			}
			catch (CompilationException ex)
			{
				Assert.AreEqual(1, ex.CompilationErrors.Count);
				Assert.AreEqual(ErrorId.CannotApplyBinaryOperator, ex.CompilationErrors[0].Id);
			}

			try
			{
				expression.Text = "derived1Class <> derived2Class";
				expression.Evaluate();
				Assert.Fail("Inequality check between incompatible types should fail.");
			}
			catch (CompilationException ex)
			{
				Assert.AreEqual(1, ex.CompilationErrors.Count);
				Assert.AreEqual(ErrorId.CannotApplyBinaryOperator, ex.CompilationErrors[0].Id);
			}
		}
Exemplo n.º 2
0
		public void EqualityCheckBetweenCompatibleTypes()
		{
			BaseClass baseClass = new BaseClass();
			Derived1Class derived1Class = new Derived1Class();
			bool result;

			Expression<bool> expression = new Expression<bool>();
			expression.Parameters.Add("baseClass", typeof(BaseClass), baseClass);
			expression.Parameters.Add("derived1Class", typeof(Derived1Class), derived1Class);
			expression.Parameters.Add("derived1ClassTypedAsBase", typeof(BaseClass), derived1Class);

			expression.Text = "baseClass = derived1Class";
			result = expression.Evaluate();
			Assert.AreEqual(false, result);

			expression.Text = "derived1Class = baseClass";
			result = expression.Evaluate();
			Assert.AreEqual(false, result);

			expression.Text = "derived1Class = derived1ClassTypedAsBase";
			result = expression.Evaluate();
			Assert.AreEqual(true, result);

			expression.Text = "derived1ClassTypedAsBase = derived1Class";
			result = expression.Evaluate();
			Assert.AreEqual(true, result);

			expression.Text = "baseClass <> derived1Class";
			result = expression.Evaluate();
			Assert.AreEqual(true, result);

			expression.Text = "derived1Class <> baseClass";
			result = expression.Evaluate();
			Assert.AreEqual(true, result);

			expression.Text = "derived1Class <> derived1ClassTypedAsBase";
			result = expression.Evaluate();
			Assert.AreEqual(false, result);

			expression.Text = "derived1ClassTypedAsBase <> derived1Class";
			result = expression.Evaluate();
			Assert.AreEqual(false, result);
		}
Exemplo n.º 3
0
		public void EqualityCheckBetweenArbitraryInterfaceTypes()
		{
			BaseClass baseClass = new BaseClass();
			Derived1Class derived1Class = new Derived1Class();
			Derived2Class derived2Class = new Derived2Class();
			bool result;

			Expression<bool> expression = new Expression<bool>();
			expression.Parameters.Add("baseClass", typeof(BaseClass), baseClass);
			expression.Parameters.Add("baseClassInterface0", typeof(Interface0), baseClass);
			expression.Parameters.Add("derived1ClassInterface0", typeof(Interface0), derived1Class);
			expression.Parameters.Add("derived1ClassInterface1", typeof(Interface1), derived1Class);
			expression.Parameters.Add("derived2ClassInterface0", typeof(Interface0), derived2Class);
			expression.Parameters.Add("derived2ClassInterface1", typeof(Interface1), derived2Class);
			expression.Parameters.Add("derived2ClassInterface2", typeof(Interface2), derived2Class);

			expression.Text = "baseClass = baseClassInterface0";
			result = expression.Evaluate();
			Assert.AreEqual(true, result);

			expression.Text = "baseClassInterface0 = baseClass";
			result = expression.Evaluate();
			Assert.AreEqual(true, result);

			expression.Text = "baseClass = derived1ClassInterface0";
			result = expression.Evaluate();
			Assert.AreEqual(false, result);

			expression.Text = "derived1ClassInterface0 = baseClass";
			result = expression.Evaluate();
			Assert.AreEqual(false, result);

			expression.Text = "baseClass = derived1ClassInterface1";
			result = expression.Evaluate();
			Assert.AreEqual(false, result);

			expression.Text = "derived1ClassInterface1 = baseClass";
			result = expression.Evaluate();
			Assert.AreEqual(false, result);

			expression.Text = "baseClass = derived2ClassInterface0";
			result = expression.Evaluate();
			Assert.AreEqual(false, result);

			expression.Text = "derived2ClassInterface0 = baseClass";
			result = expression.Evaluate();
			Assert.AreEqual(false, result);

			expression.Text = "baseClass = derived2ClassInterface1";
			result = expression.Evaluate();
			Assert.AreEqual(false, result);

			expression.Text = "derived2ClassInterface1 = baseClass";
			result = expression.Evaluate();
			Assert.AreEqual(false, result);

			expression.Text = "baseClass = derived2ClassInterface2";
			result = expression.Evaluate();
			Assert.AreEqual(false, result);

			expression.Text = "derived2ClassInterface2 = baseClass";
			result = expression.Evaluate();
			Assert.AreEqual(false, result);

			//-------------------------------
			// Now check with <> version.
			//-------------------------------

			expression.Text = "baseClass <> baseClassInterface0";
			result = expression.Evaluate();
			Assert.AreEqual(false, result);

			expression.Text = "baseClassInterface0 <> baseClass";
			result = expression.Evaluate();
			Assert.AreEqual(false, result);

			expression.Text = "baseClass <> derived1ClassInterface0";
			result = expression.Evaluate();
			Assert.AreEqual(true, result);

			expression.Text = "derived1ClassInterface0 <> baseClass";
			result = expression.Evaluate();
			Assert.AreEqual(true, result);

			expression.Text = "baseClass <> derived1ClassInterface1";
			result = expression.Evaluate();
			Assert.AreEqual(true, result);

			expression.Text = "derived1ClassInterface1 <> baseClass";
			result = expression.Evaluate();
			Assert.AreEqual(true, result);

			expression.Text = "baseClass <> derived2ClassInterface0";
			result = expression.Evaluate();
			Assert.AreEqual(true, result);

			expression.Text = "derived2ClassInterface0 <> baseClass";
			result = expression.Evaluate();
			Assert.AreEqual(true, result);

			expression.Text = "baseClass <> derived2ClassInterface1";
			result = expression.Evaluate();
			Assert.AreEqual(true, result);

			expression.Text = "derived2ClassInterface1 <> baseClass";
			result = expression.Evaluate();
			Assert.AreEqual(true, result);

			expression.Text = "baseClass <> derived2ClassInterface2";
			result = expression.Evaluate();
			Assert.AreEqual(true, result);

			expression.Text = "derived2ClassInterface2 <> baseClass";
			result = expression.Evaluate();
			Assert.AreEqual(true, result);
		}