Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_Lift.cs
1 //
2 // ExpressionTest_Lift: this contains tests for the various lifting settings in binary expressions
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 //
22 // Authors:
23 //    Miguel de Icaza (miguel@novell.com)
24 //
25
26 using System;
27 using System.Reflection;
28 using System.Linq;
29 using System.Linq.Expressions;
30 using NUnit.Framework;
31
32 namespace MonoTests.System.Linq.Expressions
33 {
34         [TestFixture]
35         public class ExpressionTest_Lifting
36         {
37                 [Test]
38                 public void TestLiftOnEqual ()
39                 {
40                         ConstantExpression a = Expression.Constant (1, typeof (int?));
41                         ConstantExpression b = Expression.Constant (1, typeof (int?));
42
43                         BinaryExpression cmp = Expression.Equal (a, b);
44
45                         Assert.AreEqual (true, cmp.IsLifted, "IsLifted");
46                         Assert.AreEqual (false, cmp.IsLiftedToNull, "IsLiftedToNull");
47                         Assert.AreEqual (typeof(bool), cmp.Type, "type");
48                 }
49
50                 [Test]
51                 public void TestLiftOnEqual_ForcedLifted ()
52                 {
53                         ConstantExpression a = Expression.Constant (1, typeof (int?));
54                         ConstantExpression b = Expression.Constant (1, typeof (int?));
55
56                         // Force the lift on equal
57                         BinaryExpression cmp = Expression.Equal (a, b, true, null);
58
59                         Assert.AreEqual (true, cmp.IsLifted, "IsLifted");
60                         Assert.AreEqual (true, cmp.IsLiftedToNull, "IsLiftedToNull");
61                         Assert.AreEqual (typeof(bool?), cmp.Type);
62                 }
63
64                 static MethodInfo GM(string n)
65                 {
66                         MethodInfo [] m = typeof(ExpressionTest_Lifting).GetMethods(BindingFlags.Static | BindingFlags.Public);
67
68                         foreach (MethodInfo mm in m)
69                                 if (mm.Name == n)
70                                         return mm;
71
72                         throw new Exception("No method found: " + n);
73                 }
74
75                 static public int MyCompare (OpStruct a, OpStruct b)
76                 {
77                         return 1;
78                 }
79
80                 [Test]
81                 public void TestLiftOnEqual_WithMethodInfo ()
82                 {
83                         ConstantExpression a = Expression.Constant (new OpStruct (), typeof (OpStruct?));
84                         ConstantExpression b = Expression.Constant (null, typeof (OpStruct?));
85
86                         // Force the lift on equal
87                         BinaryExpression cmp = Expression.Equal (a, b, true, GM("MyCompare"));
88
89                         Assert.AreEqual (true, cmp.IsLifted, "IsLifted");
90                         Assert.AreEqual (true, cmp.IsLiftedToNull, "IsLiftedToNull");
91
92                         BinaryExpression cmp2 = Expression.Equal (a, b, false, GM("MyCompare"));
93
94                         //
95                         // When we use a MethodInfo, that has a non-bool return type,
96                         // the result is always Nullable<returntype> regardless of the
97                         // setting of "liftToNull"
98                         //
99                         Assert.AreEqual (typeof(int?), cmp.Type);
100                         Assert.AreEqual (typeof(int?), cmp2.Type);
101
102                 }
103         }
104 }