1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // Miguel de Icaza (miguel@novell.com)
24 using System.Reflection;
26 using System.Linq.Expressions;
27 using NUnit.Framework;
29 namespace MonoTests.System.Linq.Expressions
32 public class ExpressionTest_Equal
35 [ExpectedException (typeof (ArgumentNullException))]
36 public void Arg1Null ()
38 Expression.Equal (null, Expression.Constant (1));
42 [ExpectedException (typeof (ArgumentNullException))]
43 public void Arg2Null ()
45 Expression.Equal (Expression.Constant (1), null);
49 [ExpectedException (typeof (InvalidOperationException))]
50 public void ArgTypesDifferent ()
52 Expression.Equal (Expression.Constant (1), Expression.Constant (2.0));
56 public void ReferenceCompare ()
58 Expression.Equal (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
65 [ExpectedException (typeof (InvalidOperationException))]
66 public void NoOperatorClass ()
68 Expression.Equal (Expression.Constant (new D ()), Expression.Constant (new D ()));
72 public void Numeric ()
74 BinaryExpression expr = Expression.Equal (Expression.Constant (1), Expression.Constant (2));
75 Assert.AreEqual (ExpressionType.Equal, expr.NodeType);
76 Assert.AreEqual (typeof (bool), expr.Type);
77 Assert.IsNull (expr.Method);
78 Assert.AreEqual ("(1 = 2)", expr.ToString ());
82 public void Nullable_LiftToNull_SetToFalse ()
87 BinaryExpression expr = Expression.Equal (Expression.Constant (a, typeof(int?)),
88 Expression.Constant (b, typeof(int?)),
90 Assert.AreEqual (ExpressionType.Equal, expr.NodeType);
91 Assert.AreEqual (typeof (bool), expr.Type);
92 Assert.AreEqual (true, expr.IsLifted);
93 Assert.AreEqual (false, expr.IsLiftedToNull);
94 Assert.IsNull (expr.Method);
95 Assert.AreEqual ("(1 = 2)", expr.ToString ());
99 public void Nullable_LiftToNull_SetToTrue ()
104 BinaryExpression expr = Expression.Equal (Expression.Constant (a, typeof(int?)),
105 Expression.Constant (b, typeof(int?)),
107 Assert.AreEqual (ExpressionType.Equal, expr.NodeType);
108 Assert.AreEqual (typeof (bool?), expr.Type);
109 Assert.AreEqual (true, expr.IsLifted);
110 Assert.AreEqual (true, expr.IsLiftedToNull);
111 Assert.IsNull (expr.Method);
112 Assert.AreEqual ("(1 = 2)", expr.ToString ());
116 [ExpectedException(typeof (InvalidOperationException))]
117 public void Nullable_Mixed ()
122 BinaryExpression expr = Expression.Equal (Expression.Constant (a, typeof (int?)),
123 Expression.Constant (b, typeof (int)));
127 public void UserDefinedClass ()
129 // We can use the simplest version of GetMethod because we already know only one
130 // exists in the very simple class we're using for the tests.
131 MethodInfo mi = typeof (OpClass).GetMethod ("op_Equality");
133 BinaryExpression expr = Expression.Equal (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
134 Assert.AreEqual (ExpressionType.Equal, expr.NodeType);
135 Assert.AreEqual (typeof (bool), expr.Type);
136 Assert.AreEqual (mi, expr.Method);
137 Assert.AreEqual ("op_Equality", expr.Method.Name);
139 Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) = value(MonoTests.System.Linq.Expressions.OpClass))", expr.ToString ());
143 // Checks for the behavior when the return type for Equal is not
144 // bool, and its coping with nullable values.
147 public void UserDefinedEqual ()