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 public void OrElseTestNullable ()
178 var a = Expression.Parameter (typeof (bool?), "a");
179 var b = Expression.Parameter (typeof (bool?), "b");
180 var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
181 Expression.OrElse (a, b), a, b);
183 var be = l.Body as BinaryExpression;
184 Assert.IsNotNull (be);
185 Assert.AreEqual (typeof (bool?), be.Type);
186 Assert.IsTrue (be.IsLifted);
187 Assert.IsTrue (be.IsLiftedToNull);
189 var c = l.Compile ();
191 Assert.AreEqual (true, c (true, true), "o1");
192 Assert.AreEqual (true, c (true, false), "o2");
193 Assert.AreEqual (true, c (false, true), "o3");
194 Assert.AreEqual (false, c (false, false), "o4");
196 Assert.AreEqual (true, c (true, null), "o5");
197 Assert.AreEqual (null, c (false, null), "o6");
198 Assert.AreEqual (null, c (null, false), "o7");
199 Assert.AreEqual (true, c (true, null), "o8");
200 Assert.AreEqual (null, c (null, null), "o9");
204 public void OrElseBoolItem ()
206 var i = Expression.Parameter (typeof (Item<bool>), "i");
207 var and = Expression.Lambda<Func<Item<bool>, bool>> (
209 Expression.Property (i, "Left"),
210 Expression.Property (i, "Right")), i).Compile ();
212 var item = new Item<bool> (true, false);
213 Assert.AreEqual (true, and (item));
214 Assert.IsTrue (item.LeftCalled);
215 Assert.IsFalse (item.RightCalled);
219 public void OrElseNullableBoolItem ()
221 var i = Expression.Parameter (typeof (Item<bool?>), "i");
222 var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
224 Expression.Property (i, "Left"),
225 Expression.Property (i, "Right")), i).Compile ();
227 var item = new Item<bool?> (true, false);
228 Assert.AreEqual ((bool?) true, and (item));
229 Assert.IsTrue (item.LeftCalled);
230 Assert.IsFalse (item.RightCalled);
237 public Slot (int val)
242 public static Slot operator | (Slot a, Slot b)
244 return new Slot (a.Value | b.Value);
247 public static bool operator true (Slot a)
252 public static bool operator false (Slot a)
259 public void UserDefinedOrElse ()
261 var l = Expression.Parameter (typeof (Slot), "l");
262 var r = Expression.Parameter (typeof (Slot), "r");
264 var method = typeof (Slot).GetMethod ("op_BitwiseOr");
266 var node = Expression.OrElse (l, r, method);
267 Assert.IsFalse (node.IsLifted);
268 Assert.IsFalse (node.IsLiftedToNull);
269 Assert.AreEqual (method, node.Method);
271 var orelse = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
273 Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
274 Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
278 public void UserDefinedOrElseShortCircuit ()
280 var i = Expression.Parameter (typeof (Item<Slot>), "i");
281 var orelse = Expression.Lambda<Func<Item<Slot>, Slot>> (
283 Expression.Property (i, "Left"),
284 Expression.Property (i, "Right")), i).Compile ();
286 var item = new Item<Slot> (new Slot (1), new Slot (0));
287 Assert.AreEqual (new Slot (1), orelse (item));
288 Assert.IsTrue (item.LeftCalled);
289 Assert.IsFalse (item.RightCalled);
293 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350228
294 public void UserDefinedLiftedOrElseShortCircuit ()
296 var i = Expression.Parameter (typeof (Item<Slot?>), "i");
297 var orelse = Expression.Lambda<Func<Item<Slot?>, Slot?>> (
299 Expression.Property (i, "Left"),
300 Expression.Property (i, "Right")), i).Compile ();
302 var item = new Item<Slot?> (new Slot (1), null);
303 Assert.AreEqual ((Slot?) new Slot (1), orelse (item));
304 Assert.IsTrue (item.LeftCalled);
305 Assert.IsFalse (item.RightCalled);
311 public Incomplete (int val)
316 public static Incomplete operator | (Incomplete a, Incomplete b)
318 return new Incomplete (a.Value | b.Value);
323 [ExpectedException (typeof (ArgumentException))]
324 public void IncompleteUserDefinedOrElse ()
326 var l = Expression.Parameter (typeof (Incomplete), "l");
327 var r = Expression.Parameter (typeof (Incomplete), "r");
329 var method = typeof (Incomplete).GetMethod ("op_BitwiseOr");
331 Expression.OrElse (l, r, method);