Merge pull request #1909 from esdrubal/reflection
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_OrElse.cs
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.
11 //
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
18 //
19 // Authors:
20 //              Federico Di Gregorio <fog@initd.org>
21 //              Jb Evain <jbevain@novell.com>
22
23 using System;
24 using System.Reflection;
25 using System.Linq;
26 using System.Linq.Expressions;
27 using NUnit.Framework;
28
29 namespace MonoTests.System.Linq.Expressions
30 {
31         [TestFixture]
32         public class ExpressionTest_OrElse
33         {
34                 [Test]
35                 [ExpectedException (typeof (ArgumentNullException))]
36                 public void Arg1Null ()
37                 {
38                         Expression.OrElse (null, Expression.Constant (1));
39                 }
40
41                 [Test]
42                 [ExpectedException (typeof (ArgumentNullException))]
43                 public void Arg2Null ()
44                 {
45                         Expression.OrElse (Expression.Constant (1), null);
46                 }
47
48                 [Test]
49                 [ExpectedException (typeof (InvalidOperationException))]
50                 public void NoOperatorClass ()
51                 {
52                         Expression.OrElse (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
53                 }
54
55                 [Test]
56                 [ExpectedException (typeof (InvalidOperationException))]
57                 public void Double ()
58                 {
59                         Expression.OrElse (Expression.Constant (1.0), Expression.Constant (2.0));
60                 }
61
62                 [Test]
63                 [ExpectedException (typeof (InvalidOperationException))]
64                 public void Integer ()
65                 {
66                         Expression.OrElse (Expression.Constant (1), Expression.Constant (2));
67                 }
68
69                 [Test]
70                 [ExpectedException (typeof (InvalidOperationException))]
71                 public void MismatchedTypes ()
72                 {
73                         Expression.OrElse (Expression.Constant (new OpClass ()), Expression.Constant (true));
74                 }
75
76                 [Test]
77                 public void Boolean ()
78                 {
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                 }
84
85                 [Test]
86                 public void UserDefinedClass ()
87                 {
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");
91
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");
97                 }
98
99                 public class BrokenMethod {
100                         public static int operator | (BrokenMethod a, BrokenMethod b)
101                         {
102                                 return 1;
103                         }
104                 }
105
106                 public class BrokenMethod2 {
107                         public static BrokenMethod2 operator | (BrokenMethod2 a, int b)
108                         {
109                                 return null;
110                         }
111                 }
112
113                 [Test]
114                 [ExpectedException(typeof(ArgumentException))]
115                 public void MethodInfoReturnType ()
116                 {
117                         Expression.OrElse (Expression.Constant (new BrokenMethod ()),
118                                            Expression.Constant (new BrokenMethod ()));
119                 }
120
121                 [Test]
122                 [ExpectedException(typeof(ArgumentException))]
123                 public void MethodInfoReturnType2 ()
124                 {
125                         Expression.OrElse (Expression.Constant (new BrokenMethod2 ()),
126                                            Expression.Constant (1));
127                 }
128
129                 [Test]
130                 public void OrElseNotLifted ()
131                 {
132                         var b = Expression.OrElse (
133                                 Expression.Constant (true, typeof (bool)),
134                                 Expression.Constant (true, typeof (bool)));
135
136                         Assert.AreEqual (typeof (bool), b.Type);
137                         Assert.IsFalse (b.IsLifted);
138                         Assert.IsFalse (b.IsLiftedToNull);
139                 }
140
141                 [Test]
142                 public void OrElseTest ()
143                 {
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);
148
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);
154
155                         var c = l.Compile ();
156
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");
161                 }
162
163                 [Test]
164                 public void OrElseLifted ()
165                 {
166                         var b = Expression.OrElse (
167                                 Expression.Constant (null, typeof (bool?)),
168                                 Expression.Constant (null, typeof (bool?)));
169
170                         Assert.AreEqual (typeof (bool?), b.Type);
171                         Assert.IsTrue (b.IsLifted);
172                         Assert.IsTrue (b.IsLiftedToNull);
173                 }
174
175                 [Test]
176                 [Category ("NotWorkingInterpreter")]
177                 public void OrElseTestNullable ()
178                 {
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);
183
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);
189
190                         var c = l.Compile ();
191
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");
196
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");
202                 }
203
204                 [Test]
205                 public void OrElseBoolItem ()
206                 {
207                         var i = Expression.Parameter (typeof (Item<bool>), "i");
208                         var and = Expression.Lambda<Func<Item<bool>, bool>> (
209                                 Expression.OrElse (
210                                         Expression.Property (i, "Left"),
211                                         Expression.Property (i, "Right")), i).Compile ();
212
213                         var item = new Item<bool> (true, false);
214                         Assert.AreEqual (true, and (item));
215                         Assert.IsTrue (item.LeftCalled);
216                         Assert.IsFalse (item.RightCalled);
217                 }
218
219                 [Test]
220                 [Category ("NotWorkingInterpreter")]
221                 public void OrElseNullableBoolItem ()
222                 {
223                         var i = Expression.Parameter (typeof (Item<bool?>), "i");
224                         var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
225                                 Expression.OrElse (
226                                         Expression.Property (i, "Left"),
227                                         Expression.Property (i, "Right")), i).Compile ();
228
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);
233                 }
234
235                 struct Slot {
236
237                         public int Value;
238
239                         public Slot (int val)
240                         {
241                                 this.Value = val;
242                         }
243
244                         public static Slot operator | (Slot a, Slot b)
245                         {
246                                 return new Slot (a.Value | b.Value);
247                         }
248
249                         public static bool operator true (Slot a)
250                         {
251                                 return a.Value != 0;
252                         }
253
254                         public static bool operator false (Slot a)
255                         {
256                                 return a.Value == 0;
257                         }
258                 }
259
260                 [Test]
261                 [Category ("NotWorkingInterpreter")]
262                 public void UserDefinedOrElse ()
263                 {
264                         var l = Expression.Parameter (typeof (Slot), "l");
265                         var r = Expression.Parameter (typeof (Slot), "r");
266
267                         var method = typeof (Slot).GetMethod ("op_BitwiseOr");
268
269                         var node = Expression.OrElse (l, r, method);
270                         Assert.IsFalse (node.IsLifted);
271                         Assert.IsFalse (node.IsLiftedToNull);
272                         Assert.AreEqual (method, node.Method);
273
274                         var orelse = Expression.Lambda<Func<Slot, Slot, Slot>> (node, l, r).Compile ();
275
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)));
278                 }
279                 [Test]
280                 [Category ("NotWorkingInterpreter")]
281                 public void UserDefinedOrElseShortCircuit ()
282                 {
283                         var i = Expression.Parameter (typeof (Item<Slot>), "i");
284                         var orelse = Expression.Lambda<Func<Item<Slot>, Slot>> (
285                                 Expression.OrElse (
286                                         Expression.Property (i, "Left"),
287                                         Expression.Property (i, "Right")), i).Compile ();
288
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);
293                 }
294
295                 [Test]
296                 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=350228
297                 [Category ("NotWorkingInterpreter")]
298                 public void UserDefinedLiftedOrElseShortCircuit ()
299                 {
300                         var i = Expression.Parameter (typeof (Item<Slot?>), "i");
301                         var orelse = Expression.Lambda<Func<Item<Slot?>, Slot?>> (
302                                 Expression.OrElse (
303                                         Expression.Property (i, "Left"),
304                                         Expression.Property (i, "Right")), i).Compile ();
305
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);
310                 }
311
312                 struct Incomplete {
313                         public int Value;
314
315                         public Incomplete (int val)
316                         {
317                                 Value = val;
318                         }
319
320                         public static Incomplete operator | (Incomplete a, Incomplete b)
321                         {
322                                 return new Incomplete (a.Value | b.Value);
323                         }
324                 }
325
326                 [Test]
327                 [ExpectedException (typeof (ArgumentException))]
328                 public void IncompleteUserDefinedOrElse ()
329                 {
330                         var l = Expression.Parameter (typeof (Incomplete), "l");
331                         var r = Expression.Parameter (typeof (Incomplete), "r");
332
333                         var method = typeof (Incomplete).GetMethod ("op_BitwiseOr");
334
335                         Expression.OrElse (l, r, method);
336                 }
337         }
338 }