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");
84 Assert.AreEqual ("(True || False)", expr.ToString(), "OrElse#04");
89 public void UserDefinedClass ()
91 // We can use the simplest version of GetMethod because we already know only one
92 // exists in the very simple class we're using for the tests.
93 MethodInfo mi = typeof (OpClass).GetMethod ("op_BitwiseOr");
95 BinaryExpression expr = Expression.OrElse (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
96 Assert.AreEqual (ExpressionType.OrElse, expr.NodeType, "OrElse#05");
97 Assert.AreEqual (typeof (OpClass), expr.Type, "OrElse#06");
98 Assert.AreEqual (mi, expr.Method, "OrElse#07");
99 Assert.AreEqual ("op_BitwiseOr", expr.Method.Name, "OrElse#08");
101 Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) || value(MonoTests.System.Linq.Expressions.OpClass))",
102 expr.ToString(), "OrElse#09");
106 public class BrokenMethod {
107 public static int operator | (BrokenMethod a, BrokenMethod b)
113 public class BrokenMethod2 {
114 public static BrokenMethod2 operator | (BrokenMethod2 a, int b)
121 [ExpectedException(typeof(ArgumentException))]
122 public void MethodInfoReturnType ()
124 Expression.OrElse (Expression.Constant (new BrokenMethod ()),
125 Expression.Constant (new BrokenMethod ()));
129 [ExpectedException(typeof(ArgumentException))]
130 public void MethodInfoReturnType2 ()
132 Expression.OrElse (Expression.Constant (new BrokenMethod2 ()),
133 Expression.Constant (1));
137 public void OrElseNotLifted ()
139 var b = Expression.OrElse (
140 Expression.Constant (true, typeof (bool)),
141 Expression.Constant (true, typeof (bool)));
143 Assert.AreEqual (typeof (bool), b.Type);
144 Assert.IsFalse (b.IsLifted);
145 Assert.IsFalse (b.IsLiftedToNull);
149 public void OrElseTest ()
151 var a = Expression.Parameter (typeof (bool), "a");
152 var b = Expression.Parameter (typeof (bool), "b");
153 var l = Expression.Lambda<Func<bool, bool, bool>> (
154 Expression.OrElse (a, b), a, b);
156 var be = l.Body as BinaryExpression;
157 Assert.IsNotNull (be);
158 Assert.AreEqual (typeof (bool), be.Type);
159 Assert.IsFalse (be.IsLifted);
160 Assert.IsFalse (be.IsLiftedToNull);
162 var c = l.Compile ();
164 Assert.AreEqual (true, c (true, true), "o1");
165 Assert.AreEqual (true, c (true, false), "o2");
166 Assert.AreEqual (true, c (false, true), "o3");
167 Assert.AreEqual (false, c (false, false), "o4");
171 public void OrElseLifted ()
173 var b = Expression.OrElse (
174 Expression.Constant (null, typeof (bool?)),
175 Expression.Constant (null, typeof (bool?)));
177 Assert.AreEqual (typeof (bool?), b.Type);
178 Assert.IsTrue (b.IsLifted);
179 Assert.IsTrue (b.IsLiftedToNull);
183 [Category ("NotWorkingInterpreter")]
184 public void OrElseTestNullable ()
186 var a = Expression.Parameter (typeof (bool?), "a");
187 var b = Expression.Parameter (typeof (bool?), "b");
188 var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
189 Expression.OrElse (a, b), a, b);
191 var be = l.Body as BinaryExpression;
192 Assert.IsNotNull (be);
193 Assert.AreEqual (typeof (bool?), be.Type);
194 Assert.IsTrue (be.IsLifted);
195 Assert.IsTrue (be.IsLiftedToNull);
197 var c = l.Compile ();
199 Assert.AreEqual (true, c (true, true), "o1");
200 Assert.AreEqual (true, c (true, false), "o2");
201 Assert.AreEqual (true, c (false, true), "o3");
202 Assert.AreEqual (false, c (false, false), "o4");
204 Assert.AreEqual (true, c (true, null), "o5");
205 Assert.AreEqual (null, c (false, null), "o6");
206 Assert.AreEqual (null, c (null, false), "o7");
207 Assert.AreEqual (true, c (true, null), "o8");
208 Assert.AreEqual (null, c (null, null), "o9");
212 public void OrElseBoolItem ()
214 var i = Expression.Parameter (typeof (Item<bool>), "i");
215 var and = Expression.Lambda<Func<Item<bool>, bool>> (
217 Expression.Property (i, "Left"),
218 Expression.Property (i, "Right")), i).Compile ();
220 var item = new Item<bool> (true, false);
221 Assert.AreEqual (true, and (item));
222 Assert.IsTrue (item.LeftCalled);
223 Assert.IsFalse (item.RightCalled);
227 [Category ("NotWorkingInterpreter")]
228 public void OrElseNullableBoolItem ()
230 var i = Expression.Parameter (typeof (Item<bool?>), "i");
231 var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
233 Expression.Property (i, "Left"),
234 Expression.Property (i, "Right")), i).Compile ();
236 var item = new Item<bool?> (true, false);
237 Assert.AreEqual ((bool?) true, and (item));
238 Assert.IsTrue (item.LeftCalled);
239 Assert.IsFalse (item.RightCalled);
246 public Slot (int val)
251 public static Slot operator | (Slot a, Slot b)
253 return new Slot (a.Value | b.Value);
256 public static bool operator true (Slot a)
261 public static bool operator false (Slot a)
268 [Category ("NotWorkingInterpreter")]
269 public void UserDefinedOrElse ()
271 var l = Expression.Parameter (typeof (Slot), "l");
272 var r = Expression.Parameter (typeof (Slot), "r");
274 var method = typeof (Slot).GetMethod ("op_BitwiseOr");
276 var node = Expression.OrElse (l, r, method);
277 Assert.IsFalse (node.IsLifted);
278 Assert.IsFalse (node.IsLiftedToNull);
279 Assert.AreEqual (method, node.Method);
281 var orelse = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
283 Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
284 Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
286 #if !NET_4_0 // dlr bug 5867
288 public void UserDefinedOrElseLiftedToNull ()
290 var l = Expression.Parameter (typeof (Slot?), "l");
291 var r = Expression.Parameter (typeof (Slot?), "r");
293 var method = typeof (Slot).GetMethod ("op_BitwiseOr");
295 var node = Expression.OrElse (l, r, method);
296 Assert.IsTrue (node.IsLifted);
297 Assert.IsTrue (node.IsLiftedToNull);
298 Assert.AreEqual (method, node.Method);
300 var orelse = Expression.Lambda<Func<Slot?, Slot?, Slot?>> (node, l, r).Compile ();
302 Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
303 Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
304 Assert.AreEqual (new Slot (64), orelse (null, new Slot (64)));
305 Assert.AreEqual (new Slot (32), orelse (new Slot (32), null));
306 Assert.AreEqual (null, orelse (null, null));
310 [Category ("NotWorkingInterpreter")]
311 public void UserDefinedOrElseShortCircuit ()
313 var i = Expression.Parameter (typeof (Item<Slot>), "i");
314 var orelse = Expression.Lambda<Func<Item<Slot>, Slot>> (
316 Expression.Property (i, "Left"),
317 Expression.Property (i, "Right")), i).Compile ();
319 var item = new Item<Slot> (new Slot (1), new Slot (0));
320 Assert.AreEqual (new Slot (1), orelse (item));
321 Assert.IsTrue (item.LeftCalled);
322 Assert.IsFalse (item.RightCalled);
326 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350228
327 [Category ("NotWorkingInterpreter")]
328 public void UserDefinedLiftedOrElseShortCircuit ()
330 var i = Expression.Parameter (typeof (Item<Slot?>), "i");
331 var orelse = Expression.Lambda<Func<Item<Slot?>, Slot?>> (
333 Expression.Property (i, "Left"),
334 Expression.Property (i, "Right")), i).Compile ();
336 var item = new Item<Slot?> (new Slot (1), null);
337 Assert.AreEqual ((Slot?) new Slot (1), orelse (item));
338 Assert.IsTrue (item.LeftCalled);
339 Assert.IsFalse (item.RightCalled);
345 public Incomplete (int val)
350 public static Incomplete operator | (Incomplete a, Incomplete b)
352 return new Incomplete (a.Value | b.Value);
357 [ExpectedException (typeof (ArgumentException))]
358 public void IncompleteUserDefinedOrElse ()
360 var l = Expression.Parameter (typeof (Incomplete), "l");
361 var r = Expression.Parameter (typeof (Incomplete), "r");
363 var method = typeof (Incomplete).GetMethod ("op_BitwiseOr");
365 Expression.OrElse (l, r, method);