fix ET tests for 4.0
[mcs.git] / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_OrElse.cs
blob42c4459b0a4daf7aba5772f3956794629b478c9d
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:
8 //
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
19 // Authors:
20 // Federico Di Gregorio <fog@initd.org>
21 // Jb Evain <jbevain@novell.com>
23 using System;
24 using System.Reflection;
25 using System.Linq;
26 using System.Linq.Expressions;
27 using NUnit.Framework;
29 namespace MonoTests.System.Linq.Expressions
31 [TestFixture]
32 public class ExpressionTest_OrElse
34 [Test]
35 [ExpectedException (typeof (ArgumentNullException))]
36 public void Arg1Null ()
38 Expression.OrElse (null, Expression.Constant (1));
41 [Test]
42 [ExpectedException (typeof (ArgumentNullException))]
43 public void Arg2Null ()
45 Expression.OrElse (Expression.Constant (1), null);
48 [Test]
49 [ExpectedException (typeof (InvalidOperationException))]
50 public void NoOperatorClass ()
52 Expression.OrElse (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
55 [Test]
56 [ExpectedException (typeof (InvalidOperationException))]
57 public void Double ()
59 Expression.OrElse (Expression.Constant (1.0), Expression.Constant (2.0));
62 [Test]
63 [ExpectedException (typeof (InvalidOperationException))]
64 public void Integer ()
66 Expression.OrElse (Expression.Constant (1), Expression.Constant (2));
69 [Test]
70 [ExpectedException (typeof (InvalidOperationException))]
71 public void MismatchedTypes ()
73 Expression.OrElse (Expression.Constant (new OpClass ()), Expression.Constant (true));
76 [Test]
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 #if !NET_4_0
84 Assert.AreEqual ("(True || False)", expr.ToString(), "OrElse#04");
85 #endif
88 [Test]
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");
100 #if !NET_4_0
101 Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) || value(MonoTests.System.Linq.Expressions.OpClass))",
102 expr.ToString(), "OrElse#09");
103 #endif
106 public class BrokenMethod {
107 public static int operator | (BrokenMethod a, BrokenMethod b)
109 return 1;
113 public class BrokenMethod2 {
114 public static BrokenMethod2 operator | (BrokenMethod2 a, int b)
116 return null;
120 [Test]
121 [ExpectedException(typeof(ArgumentException))]
122 public void MethodInfoReturnType ()
124 Expression.OrElse (Expression.Constant (new BrokenMethod ()),
125 Expression.Constant (new BrokenMethod ()));
128 [Test]
129 [ExpectedException(typeof(ArgumentException))]
130 public void MethodInfoReturnType2 ()
132 Expression.OrElse (Expression.Constant (new BrokenMethod2 ()),
133 Expression.Constant (1));
136 [Test]
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);
148 [Test]
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");
170 [Test]
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);
182 [Test]
183 public void OrElseTestNullable ()
185 var a = Expression.Parameter (typeof (bool?), "a");
186 var b = Expression.Parameter (typeof (bool?), "b");
187 var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
188 Expression.OrElse (a, b), a, b);
190 var be = l.Body as BinaryExpression;
191 Assert.IsNotNull (be);
192 Assert.AreEqual (typeof (bool?), be.Type);
193 Assert.IsTrue (be.IsLifted);
194 Assert.IsTrue (be.IsLiftedToNull);
196 var c = l.Compile ();
198 Assert.AreEqual (true, c (true, true), "o1");
199 Assert.AreEqual (true, c (true, false), "o2");
200 Assert.AreEqual (true, c (false, true), "o3");
201 Assert.AreEqual (false, c (false, false), "o4");
203 Assert.AreEqual (true, c (true, null), "o5");
204 Assert.AreEqual (null, c (false, null), "o6");
205 Assert.AreEqual (null, c (null, false), "o7");
206 Assert.AreEqual (true, c (true, null), "o8");
207 Assert.AreEqual (null, c (null, null), "o9");
210 [Test]
211 public void OrElseBoolItem ()
213 var i = Expression.Parameter (typeof (Item<bool>), "i");
214 var and = Expression.Lambda<Func<Item<bool>, bool>> (
215 Expression.OrElse (
216 Expression.Property (i, "Left"),
217 Expression.Property (i, "Right")), i).Compile ();
219 var item = new Item<bool> (true, false);
220 Assert.AreEqual (true, and (item));
221 Assert.IsTrue (item.LeftCalled);
222 Assert.IsFalse (item.RightCalled);
225 [Test]
226 public void OrElseNullableBoolItem ()
228 var i = Expression.Parameter (typeof (Item<bool?>), "i");
229 var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
230 Expression.OrElse (
231 Expression.Property (i, "Left"),
232 Expression.Property (i, "Right")), i).Compile ();
234 var item = new Item<bool?> (true, false);
235 Assert.AreEqual ((bool?) true, and (item));
236 Assert.IsTrue (item.LeftCalled);
237 Assert.IsFalse (item.RightCalled);
240 struct Slot {
242 public int Value;
244 public Slot (int val)
246 this.Value = val;
249 public static Slot operator | (Slot a, Slot b)
251 return new Slot (a.Value | b.Value);
254 public static bool operator true (Slot a)
256 return a.Value != 0;
259 public static bool operator false (Slot a)
261 return a.Value == 0;
265 [Test]
266 public void UserDefinedOrElse ()
268 var l = Expression.Parameter (typeof (Slot), "l");
269 var r = Expression.Parameter (typeof (Slot), "r");
271 var method = typeof (Slot).GetMethod ("op_BitwiseOr");
273 var node = Expression.OrElse (l, r, method);
274 Assert.IsFalse (node.IsLifted);
275 Assert.IsFalse (node.IsLiftedToNull);
276 Assert.AreEqual (method, node.Method);
278 var orelse = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
280 Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
281 Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
284 [Test]
285 public void UserDefinedOrElseLiftedToNull ()
287 var l = Expression.Parameter (typeof (Slot?), "l");
288 var r = Expression.Parameter (typeof (Slot?), "r");
290 var method = typeof (Slot).GetMethod ("op_BitwiseOr");
292 var node = Expression.OrElse (l, r, method);
293 Assert.IsTrue (node.IsLifted);
294 Assert.IsTrue (node.IsLiftedToNull);
295 Assert.AreEqual (method, node.Method);
297 var orelse = Expression.Lambda<Func<Slot?, Slot?, Slot?>> (node, l, r).Compile ();
299 Assert.AreEqual (new Slot (64), orelse (new Slot (64), new Slot (64)));
300 Assert.AreEqual (new Slot (32), orelse (new Slot (32), new Slot (64)));
301 Assert.AreEqual (new Slot (64), orelse (null, new Slot (64)));
302 Assert.AreEqual (new Slot (32), orelse (new Slot (32), null));
303 Assert.AreEqual (null, orelse (null, null));
306 [Test]
307 public void UserDefinedOrElseShortCircuit ()
309 var i = Expression.Parameter (typeof (Item<Slot>), "i");
310 var orelse = Expression.Lambda<Func<Item<Slot>, Slot>> (
311 Expression.OrElse (
312 Expression.Property (i, "Left"),
313 Expression.Property (i, "Right")), i).Compile ();
315 var item = new Item<Slot> (new Slot (1), new Slot (0));
316 Assert.AreEqual (new Slot (1), orelse (item));
317 Assert.IsTrue (item.LeftCalled);
318 Assert.IsFalse (item.RightCalled);
321 [Test]
322 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350228
323 public void UserDefinedLiftedOrElseShortCircuit ()
325 var i = Expression.Parameter (typeof (Item<Slot?>), "i");
326 var orelse = Expression.Lambda<Func<Item<Slot?>, Slot?>> (
327 Expression.OrElse (
328 Expression.Property (i, "Left"),
329 Expression.Property (i, "Right")), i).Compile ();
331 var item = new Item<Slot?> (new Slot (1), null);
332 Assert.AreEqual ((Slot?) new Slot (1), orelse (item));
333 Assert.IsTrue (item.LeftCalled);
334 Assert.IsFalse (item.RightCalled);
337 struct Incomplete {
338 public int Value;
340 public Incomplete (int val)
342 Value = val;
345 public static Incomplete operator | (Incomplete a, Incomplete b)
347 return new Incomplete (a.Value | b.Value);
351 [Test]
352 [ExpectedException (typeof (ArgumentException))]
353 public void IncompleteUserDefinedOrElse ()
355 var l = Expression.Parameter (typeof (Incomplete), "l");
356 var r = Expression.Parameter (typeof (Incomplete), "r");
358 var method = typeof (Incomplete).GetMethod ("op_BitwiseOr");
360 Expression.OrElse (l, r, method);