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)
21 // Jb Evain (jbevain@novell.com)
25 using System.Reflection;
27 using System.Linq.Expressions;
28 using NUnit.Framework;
30 namespace MonoTests.System.Linq.Expressions
33 public class ExpressionTest_NotEqual
36 [ExpectedException (typeof (ArgumentNullException))]
37 public void Arg1Null ()
39 Expression.NotEqual (null, Expression.Constant (1));
43 [ExpectedException (typeof (ArgumentNullException))]
44 public void Arg2Null ()
46 Expression.NotEqual (Expression.Constant (1), null);
50 [ExpectedException (typeof (InvalidOperationException))]
51 public void ArgTypesDifferent ()
53 Expression.NotEqual (Expression.Constant (1), Expression.Constant (2.0));
57 public void ReferenceCompare ()
59 Expression.NotEqual (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
66 [ExpectedException (typeof (InvalidOperationException))]
67 public void NoOperatorClass ()
69 Expression.NotEqual (Expression.Constant (new D ()), Expression.Constant (new D ()));
73 public void Numeric ()
75 BinaryExpression expr = Expression.NotEqual (Expression.Constant (1), Expression.Constant (2));
76 Assert.AreEqual (ExpressionType.NotEqual, expr.NodeType);
77 Assert.AreEqual (typeof (bool), expr.Type);
78 Assert.IsNull (expr.Method);
79 Assert.AreEqual ("(1 != 2)", expr.ToString ());
83 public void Nullable_LiftToNull_SetToFalse ()
88 BinaryExpression expr = Expression.NotEqual (Expression.Constant (a, typeof(int?)),
89 Expression.Constant (b, typeof(int?)),
91 Assert.AreEqual (ExpressionType.NotEqual, expr.NodeType);
92 Assert.AreEqual (typeof (bool), expr.Type);
93 Assert.AreEqual (true, expr.IsLifted);
94 Assert.AreEqual (false, expr.IsLiftedToNull);
95 Assert.IsNull (expr.Method);
96 Assert.AreEqual ("(1 != 2)", expr.ToString ());
100 public void Nullable_LiftToNull_SetToTrue ()
105 BinaryExpression expr = Expression.NotEqual (Expression.Constant (a, typeof(int?)),
106 Expression.Constant (b, typeof(int?)),
108 Assert.AreEqual (ExpressionType.NotEqual, expr.NodeType);
109 Assert.AreEqual (typeof (bool?), expr.Type);
110 Assert.AreEqual (true, expr.IsLifted);
111 Assert.AreEqual (true, expr.IsLiftedToNull);
112 Assert.IsNull (expr.Method);
113 Assert.AreEqual ("(1 != 2)", expr.ToString ());
117 [ExpectedException(typeof (InvalidOperationException))]
118 public void Nullable_Mixed ()
123 Expression.NotEqual (Expression.Constant (a, typeof (int?)),
124 Expression.Constant (b, typeof (int)));
128 public void UserDefinedClass ()
130 // We can use the simplest version of GetMethod because we already know only one
131 // exists in the very simple class we're using for the tests.
132 MethodInfo mi = typeof (OpClass).GetMethod ("op_Inequality");
134 BinaryExpression expr = Expression.NotEqual (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
135 Assert.AreEqual (ExpressionType.NotEqual, expr.NodeType);
136 Assert.AreEqual (typeof (bool), expr.Type);
137 Assert.AreEqual (mi, expr.Method);
138 Assert.AreEqual ("op_Inequality", expr.Method.Name);
140 Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) != value(MonoTests.System.Linq.Expressions.OpClass))", expr.ToString ());
144 public void NullableInt32NotEqual ()
146 var l = Expression.Parameter (typeof (int?), "l");
147 var r = Expression.Parameter (typeof (int?), "r");
149 var neq = Expression.Lambda<Func<int?, int?, bool>> (
150 Expression.NotEqual (l, r), l, r).Compile ();
152 Assert.IsFalse (neq (null, null));
153 Assert.IsTrue (neq (null, 1));
154 Assert.IsTrue (neq (1, null));
155 Assert.IsTrue (neq (1, 2));
156 Assert.IsFalse (neq (1, 1));
157 Assert.IsTrue (neq (null, 0));
158 Assert.IsTrue (neq (0, null));
162 public void NullableInt32NotEqualLiftedToNull ()
164 var l = Expression.Parameter (typeof (int?), "l");
165 var r = Expression.Parameter (typeof (int?), "r");
167 var neq = Expression.Lambda<Func<int?, int?, bool?>> (
168 Expression.NotEqual (l, r, true, null), l, r).Compile ();
170 Assert.AreEqual ((bool?) null, neq (null, null));
171 Assert.AreEqual ((bool?) null, neq (null, 1));
172 Assert.AreEqual ((bool?) null, neq (1, null));
173 Assert.AreEqual ((bool?) true, neq (1, 2));
174 Assert.AreEqual ((bool?) false, neq (1, 1));
175 Assert.AreEqual ((bool?) null, neq (null, 0));
176 Assert.AreEqual ((bool?) null, neq (0, null));
186 public void EnumNotEqual ()
188 var l = Expression.Parameter (typeof (Foo), "l");
189 var r = Expression.Parameter (typeof (Foo), "r");
191 var node = Expression.NotEqual (l, r);
192 Assert.IsFalse (node.IsLifted);
193 Assert.IsFalse (node.IsLiftedToNull);
194 Assert.AreEqual (typeof (bool), node.Type);
195 Assert.IsNull (node.Method);
197 var neq = Expression.Lambda<Func<Foo, Foo, bool>> (node, l, r).Compile ();
199 Assert.AreEqual (false, neq (Foo.Bar, Foo.Bar));
200 Assert.AreEqual (true, neq (Foo.Bar, Foo.Baz));
204 public void LiftedEnumNotEqual ()
206 var l = Expression.Parameter (typeof (Foo?), "l");
207 var r = Expression.Parameter (typeof (Foo?), "r");
209 var node = Expression.NotEqual (l, r);
210 Assert.IsTrue (node.IsLifted);
211 Assert.IsFalse (node.IsLiftedToNull);
212 Assert.AreEqual (typeof (bool), node.Type);
213 Assert.IsNull (node.Method);
215 var neq = Expression.Lambda<Func<Foo?, Foo?, bool>> (node, l, r).Compile ();
217 Assert.AreEqual (false, neq (Foo.Bar, Foo.Bar));
218 Assert.AreEqual (true, neq (Foo.Bar, Foo.Baz));
219 Assert.AreEqual (true, neq (Foo.Bar, null));
220 Assert.AreEqual (false, neq (null, null));