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 // Federico Di Gregorio <fog@initd.org>
21 // Jb Evain <jbevain@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_OrElse
35 [ExpectedException (typeof (ArgumentNullException))]
36 public void Arg1Null ()
38 Expression.OrElse (null, Expression.Constant (1));
42 [ExpectedException (typeof (ArgumentNullException))]
43 public void Arg2Null ()
45 Expression.OrElse (Expression.Constant (1), null);
49 [ExpectedException (typeof (InvalidOperationException))]
50 public void NoOperatorClass ()
52 Expression.OrElse (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
56 [ExpectedException (typeof (InvalidOperationException))]
59 Expression.OrElse (Expression.Constant (1.0), Expression.Constant (2.0));
63 [ExpectedException (typeof (InvalidOperationException))]
64 public void Integer ()
66 Expression.OrElse (Expression.Constant (1), Expression.Constant (2));
70 [ExpectedException (typeof (InvalidOperationException))]
71 public void MismatchedTypes ()
73 Expression.OrElse (Expression.Constant (new OpClass ()), Expression.Constant (true));
77 public void Boolean ()
79 BinaryExpression expr = Expression.OrElse (Expression.Constant (true), Expression.Constant (false));
80 Assert.AreEqual (ExpressionType.OrElse, expr.NodeType, "OrElse#01");
81 Assert.AreEqual (typeof (bool), expr.Type, "OrElse#02");
82 Assert.IsNull (expr.Method, "OrElse#03");
83 Assert.AreEqual ("(True || False)", expr.ToString(), "OrElse#04");
87 public void UserDefinedClass ()
89 // We can use the simplest version of GetMethod because we already know only one
90 // exists in the very simple class we're using for the tests.
91 MethodInfo mi = typeof (OpClass).GetMethod ("op_BitwiseOr");
93 BinaryExpression expr = Expression.OrElse (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
94 Assert.AreEqual (ExpressionType.OrElse, expr.NodeType, "OrElse#05");
95 Assert.AreEqual (typeof (OpClass), expr.Type, "OrElse#06");
96 Assert.AreEqual (mi, expr.Method, "OrElse#07");
97 Assert.AreEqual ("op_BitwiseOr", expr.Method.Name, "OrElse#08");
98 Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) || value(MonoTests.System.Linq.Expressions.OpClass))",
99 expr.ToString(), "OrElse#09");
102 public class BrokenMethod {
103 public static int operator | (BrokenMethod a, BrokenMethod b)
109 public class BrokenMethod2 {
110 public static BrokenMethod2 operator | (BrokenMethod2 a, int b)
117 [ExpectedException(typeof(ArgumentException))]
118 public void MethodInfoReturnType ()
120 Expression.OrElse (Expression.Constant (new BrokenMethod ()),
121 Expression.Constant (new BrokenMethod ()));
125 [ExpectedException(typeof(ArgumentException))]
126 public void MethodInfoReturnType2 ()
128 Expression.OrElse (Expression.Constant (new BrokenMethod2 ()),
129 Expression.Constant (1));
133 public void OrElseNotLifted ()
135 var b = Expression.OrElse (
136 Expression.Constant (true, typeof (bool)),
137 Expression.Constant (true, typeof (bool)));
139 Assert.AreEqual (typeof (bool), b.Type);
140 Assert.IsFalse (b.IsLifted);
141 Assert.IsFalse (b.IsLiftedToNull);
145 public void OrElseTest ()
147 var a = Expression.Parameter (typeof (bool), "a");
148 var b = Expression.Parameter (typeof (bool), "b");
149 var l = Expression.Lambda<Func<bool, bool, bool>> (
150 Expression.OrElse (a, b), a, b);
152 var be = l.Body as BinaryExpression;
153 Assert.IsNotNull (be);
154 Assert.AreEqual (typeof (bool), be.Type);
155 Assert.IsFalse (be.IsLifted);
156 Assert.IsFalse (be.IsLiftedToNull);
158 var c = l.Compile ();
160 Assert.AreEqual (true, c (true, true), "o1");
161 Assert.AreEqual (true, c (true, false), "o2");
162 Assert.AreEqual (true, c (false, true), "o3");
163 Assert.AreEqual (false, c (false, false), "o4");
167 public void OrElseLifted ()
169 var b = Expression.OrElse (
170 Expression.Constant (null, typeof (bool?)),
171 Expression.Constant (null, typeof (bool?)));
173 Assert.AreEqual (typeof (bool?), b.Type);
174 Assert.IsTrue (b.IsLifted);
175 Assert.IsTrue (b.IsLiftedToNull);
179 public void OrElseTestNullable ()
181 var a = Expression.Parameter (typeof (bool?), "a");
182 var b = Expression.Parameter (typeof (bool?), "b");
183 var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
184 Expression.OrElse (a, b), a, b);
186 var be = l.Body as BinaryExpression;
187 Assert.IsNotNull (be);
188 Assert.AreEqual (typeof (bool?), be.Type);
189 Assert.IsTrue (be.IsLifted);
190 Assert.IsTrue (be.IsLiftedToNull);
192 var c = l.Compile ();
194 Assert.AreEqual (true, c (true, true), "o1");
195 Assert.AreEqual (true, c (true, false), "o2");
196 Assert.AreEqual (true, c (false, true), "o3");
197 Assert.AreEqual (false, c (false, false), "o4");
199 Assert.AreEqual (true, c (true, null), "o5");
200 Assert.AreEqual (null, c (false, null), "o6");
201 Assert.AreEqual (null, c (null, false), "o7");
202 Assert.AreEqual (true, c (true, null), "o8");
203 Assert.AreEqual (null, c (null, null), "o9");
207 public void OrElseBoolItem ()
209 var i = Expression.Parameter (typeof (Item<bool>), "i");
210 var and = Expression.Lambda<Func<Item<bool>, bool>> (
212 Expression.Property (i, "Left"),
213 Expression.Property (i, "Right")), i).Compile ();
215 var item = new Item<bool> (true, false);
216 Assert.AreEqual (true, and (item));
217 Assert.IsTrue (item.LeftCalled);
218 Assert.IsFalse (item.RightCalled);
222 public void OrElseNullableBoolItem ()
224 var i = Expression.Parameter (typeof (Item<bool?>), "i");
225 var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
227 Expression.Property (i, "Left"),
228 Expression.Property (i, "Right")), i).Compile ();
230 var item = new Item<bool?> (true, false);
231 Assert.AreEqual ((bool?) true, and (item));
232 Assert.IsTrue (item.LeftCalled);
233 Assert.IsFalse (item.RightCalled);
240 public Slot (int val)
245 public static Slot operator | (Slot a, Slot b)
247 return new Slot (a.Value | b.Value);
250 public static bool operator true (Slot a)
255 public static bool operator false (Slot a)
262 [Category ("NotWorking")]
263 public void UserDefinedOrElse ()
265 var l = Expression.Parameter (typeof (Slot), "l");
266 var r = Expression.Parameter (typeof (Slot), "r");
268 var method = typeof (Slot).GetMethod ("op_BitwiseOr");
270 var node = Expression.OrElse (l, r, method);
271 Assert.IsFalse (node.IsLifted);
272 Assert.IsFalse (node.IsLiftedToNull);
273 Assert.AreEqual (method, node.Method);
275 var orelse = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
277 Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
278 Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
284 public Incomplete (int val)
289 public static Incomplete operator | (Incomplete a, Incomplete b)
291 return new Incomplete (a.Value | b.Value);
296 [ExpectedException (typeof (ArgumentException))]
297 public void IncompleteUserDefinedOrElse ()
299 var l = Expression.Parameter (typeof (Incomplete), "l");
300 var r = Expression.Parameter (typeof (Incomplete), "r");
302 var method = typeof (Incomplete).GetMethod ("op_BitwiseOr");
304 Expression.OrElse (l, r, method);