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");
86 public void UserDefinedClass ()
88 // We can use the simplest version of GetMethod because we already know only one
89 // exists in the very simple class we're using for the tests.
90 MethodInfo mi = typeof (OpClass).GetMethod ("op_BitwiseOr");
92 BinaryExpression expr = Expression.OrElse (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
93 Assert.AreEqual (ExpressionType.OrElse, expr.NodeType, "OrElse#05");
94 Assert.AreEqual (typeof (OpClass), expr.Type, "OrElse#06");
95 Assert.AreEqual (mi, expr.Method, "OrElse#07");
96 Assert.AreEqual ("op_BitwiseOr", expr.Method.Name, "OrElse#08");
99 public class BrokenMethod {
100 public static int operator | (BrokenMethod a, BrokenMethod b)
106 public class BrokenMethod2 {
107 public static BrokenMethod2 operator | (BrokenMethod2 a, int b)
114 [ExpectedException(typeof(ArgumentException))]
115 public void MethodInfoReturnType ()
117 Expression.OrElse (Expression.Constant (new BrokenMethod ()),
118 Expression.Constant (new BrokenMethod ()));
122 [ExpectedException(typeof(ArgumentException))]
123 public void MethodInfoReturnType2 ()
125 Expression.OrElse (Expression.Constant (new BrokenMethod2 ()),
126 Expression.Constant (1));
130 public void OrElseNotLifted ()
132 var b = Expression.OrElse (
133 Expression.Constant (true, typeof (bool)),
134 Expression.Constant (true, typeof (bool)));
136 Assert.AreEqual (typeof (bool), b.Type);
137 Assert.IsFalse (b.IsLifted);
138 Assert.IsFalse (b.IsLiftedToNull);
142 public void OrElseTest ()
144 var a = Expression.Parameter (typeof (bool), "a");
145 var b = Expression.Parameter (typeof (bool), "b");
146 var l = Expression.Lambda<Func<bool, bool, bool>> (
147 Expression.OrElse (a, b), a, b);
149 var be = l.Body as BinaryExpression;
150 Assert.IsNotNull (be);
151 Assert.AreEqual (typeof (bool), be.Type);
152 Assert.IsFalse (be.IsLifted);
153 Assert.IsFalse (be.IsLiftedToNull);
155 var c = l.Compile ();
157 Assert.AreEqual (true, c (true, true), "o1");
158 Assert.AreEqual (true, c (true, false), "o2");
159 Assert.AreEqual (true, c (false, true), "o3");
160 Assert.AreEqual (false, c (false, false), "o4");
164 public void OrElseLifted ()
166 var b = Expression.OrElse (
167 Expression.Constant (null, typeof (bool?)),
168 Expression.Constant (null, typeof (bool?)));
170 Assert.AreEqual (typeof (bool?), b.Type);
171 Assert.IsTrue (b.IsLifted);
172 Assert.IsTrue (b.IsLiftedToNull);
176 [Category ("NotWorkingInterpreter")]
177 public void OrElseTestNullable ()
179 var a = Expression.Parameter (typeof (bool?), "a");
180 var b = Expression.Parameter (typeof (bool?), "b");
181 var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
182 Expression.OrElse (a, b), a, b);
184 var be = l.Body as BinaryExpression;
185 Assert.IsNotNull (be);
186 Assert.AreEqual (typeof (bool?), be.Type);
187 Assert.IsTrue (be.IsLifted);
188 Assert.IsTrue (be.IsLiftedToNull);
190 var c = l.Compile ();
192 Assert.AreEqual (true, c (true, true), "o1");
193 Assert.AreEqual (true, c (true, false), "o2");
194 Assert.AreEqual (true, c (false, true), "o3");
195 Assert.AreEqual (false, c (false, false), "o4");
197 Assert.AreEqual (true, c (true, null), "o5");
198 Assert.AreEqual (null, c (false, null), "o6");
199 Assert.AreEqual (null, c (null, false), "o7");
200 Assert.AreEqual (true, c (true, null), "o8");
201 Assert.AreEqual (null, c (null, null), "o9");
205 public void OrElseBoolItem ()
207 var i = Expression.Parameter (typeof (Item<bool>), "i");
208 var and = Expression.Lambda<Func<Item<bool>, bool>> (
210 Expression.Property (i, "Left"),
211 Expression.Property (i, "Right")), i).Compile ();
213 var item = new Item<bool> (true, false);
214 Assert.AreEqual (true, and (item));
215 Assert.IsTrue (item.LeftCalled);
216 Assert.IsFalse (item.RightCalled);
220 [Category ("NotWorkingInterpreter")]
221 public void OrElseNullableBoolItem ()
223 var i = Expression.Parameter (typeof (Item<bool?>), "i");
224 var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
226 Expression.Property (i, "Left"),
227 Expression.Property (i, "Right")), i).Compile ();
229 var item = new Item<bool?> (true, false);
230 Assert.AreEqual ((bool?) true, and (item));
231 Assert.IsTrue (item.LeftCalled);
232 Assert.IsFalse (item.RightCalled);
239 public Slot (int val)
244 public static Slot operator | (Slot a, Slot b)
246 return new Slot (a.Value | b.Value);
249 public static bool operator true (Slot a)
254 public static bool operator false (Slot a)
261 [Category ("NotWorkingInterpreter")]
262 public void UserDefinedOrElse ()
264 var l = Expression.Parameter (typeof (Slot), "l");
265 var r = Expression.Parameter (typeof (Slot), "r");
267 var method = typeof (Slot).GetMethod ("op_BitwiseOr");
269 var node = Expression.OrElse (l, r, method);
270 Assert.IsFalse (node.IsLifted);
271 Assert.IsFalse (node.IsLiftedToNull);
272 Assert.AreEqual (method, node.Method);
274 var orelse = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
276 Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
277 Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
280 [Category ("NotWorkingInterpreter")]
281 public void UserDefinedOrElseShortCircuit ()
283 var i = Expression.Parameter (typeof (Item<Slot>), "i");
284 var orelse = Expression.Lambda<Func<Item<Slot>, Slot>> (
286 Expression.Property (i, "Left"),
287 Expression.Property (i, "Right")), i).Compile ();
289 var item = new Item<Slot> (new Slot (1), new Slot (0));
290 Assert.AreEqual (new Slot (1), orelse (item));
291 Assert.IsTrue (item.LeftCalled);
292 Assert.IsFalse (item.RightCalled);
296 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350228
297 [Category ("NotWorkingInterpreter")]
298 public void UserDefinedLiftedOrElseShortCircuit ()
300 var i = Expression.Parameter (typeof (Item<Slot?>), "i");
301 var orelse = Expression.Lambda<Func<Item<Slot?>, Slot?>> (
303 Expression.Property (i, "Left"),
304 Expression.Property (i, "Right")), i).Compile ();
306 var item = new Item<Slot?> (new Slot (1), null);
307 Assert.AreEqual ((Slot?) new Slot (1), orelse (item));
308 Assert.IsTrue (item.LeftCalled);
309 Assert.IsFalse (item.RightCalled);
315 public Incomplete (int val)
320 public static Incomplete operator | (Incomplete a, Incomplete b)
322 return new Incomplete (a.Value | b.Value);
327 [ExpectedException (typeof (ArgumentException))]
328 public void IncompleteUserDefinedOrElse ()
330 var l = Expression.Parameter (typeof (Incomplete), "l");
331 var r = Expression.Parameter (typeof (Incomplete), "r");
333 var method = typeof (Incomplete).GetMethod ("op_BitwiseOr");
335 Expression.OrElse (l, r, method);