complete tests
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_Equal.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 //    Miguel de Icaza (miguel@novell.com)
21 //
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_Equal
33         {
34                 [Test]
35                 [ExpectedException (typeof (ArgumentNullException))]
36                 public void Arg1Null ()
37                 {
38                         Expression.Equal (null, Expression.Constant (1));
39                 }
40
41                 [Test]
42                 [ExpectedException (typeof (ArgumentNullException))]
43                 public void Arg2Null ()
44                 {
45                         Expression.Equal (Expression.Constant (1), null);
46                 }
47
48                 [Test]
49                 [ExpectedException (typeof (InvalidOperationException))]
50                 public void ArgTypesDifferent ()
51                 {
52                         Expression.Equal (Expression.Constant (1), Expression.Constant (2.0));
53                 }
54
55                 [Test]
56                 public void ReferenceCompare ()
57                 {
58                         Expression.Equal (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
59                 }
60
61                 public struct D {
62                 }
63
64                 [Test]
65                 [ExpectedException (typeof (InvalidOperationException))]
66                 public void NoOperatorClass ()
67                 {
68                         Expression.Equal (Expression.Constant (new D ()), Expression.Constant (new D ()));
69                 }
70
71                 [Test]
72                 public void Numeric ()
73                 {
74                         BinaryExpression expr = Expression.Equal (Expression.Constant (1), Expression.Constant (2));
75                         Assert.AreEqual (ExpressionType.Equal, expr.NodeType);
76                         Assert.AreEqual (typeof (bool), expr.Type);
77                         Assert.IsNull (expr.Method);
78                         Assert.AreEqual ("(1 = 2)", expr.ToString ());
79                 }
80
81                 [Test]
82                 public void Nullable_LiftToNull_SetToFalse ()
83                 {
84                         int? a = 1;
85                         int? b = 2;
86
87                         BinaryExpression expr = Expression.Equal (Expression.Constant (a, typeof(int?)),
88                                                                   Expression.Constant (b, typeof(int?)),
89                                                                   false, null);
90                         Assert.AreEqual (ExpressionType.Equal, expr.NodeType);
91                         Assert.AreEqual (typeof (bool), expr.Type);
92                         Assert.AreEqual (true, expr.IsLifted);
93                         Assert.AreEqual (false, expr.IsLiftedToNull);
94                         Assert.IsNull (expr.Method);
95                         Assert.AreEqual ("(1 = 2)", expr.ToString ());
96                 }
97
98                 [Test]
99                 public void Nullable_LiftToNull_SetToTrue ()
100                 {
101                         int? a = 1;
102                         int? b = 2;
103
104                         BinaryExpression expr = Expression.Equal (Expression.Constant (a, typeof(int?)),
105                                                                   Expression.Constant (b, typeof(int?)),
106                                                                   true, null);
107                         Assert.AreEqual (ExpressionType.Equal, expr.NodeType);
108                         Assert.AreEqual (typeof (bool?), expr.Type);
109                         Assert.AreEqual (true, expr.IsLifted);
110                         Assert.AreEqual (true, expr.IsLiftedToNull);
111                         Assert.IsNull (expr.Method);
112                         Assert.AreEqual ("(1 = 2)", expr.ToString ());
113                 }
114
115                 [Test]
116                 [ExpectedException(typeof (InvalidOperationException))]
117                 public void Nullable_Mixed ()
118                 {
119                         int? a = 1;
120                         int b = 2;
121
122                         BinaryExpression expr = Expression.Equal (Expression.Constant (a, typeof (int?)),
123                                                                   Expression.Constant (b, typeof (int)));
124                 }
125
126                 [Test]
127                 public void UserDefinedClass ()
128                 {
129                         // We can use the simplest version of GetMethod because we already know only one
130                         // exists in the very simple class we're using for the tests.
131                         MethodInfo mi = typeof (OpClass).GetMethod ("op_Equality");
132
133                         BinaryExpression expr = Expression.Equal (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
134                         Assert.AreEqual (ExpressionType.Equal, expr.NodeType);
135                         Assert.AreEqual (typeof (bool), expr.Type);
136                         Assert.AreEqual (mi, expr.Method);
137                         Assert.AreEqual ("op_Equality", expr.Method.Name);
138
139                         Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) = value(MonoTests.System.Linq.Expressions.OpClass))", expr.ToString ());
140                 }
141
142                 //
143                 // Checks for the behavior when the return type for Equal is not
144                 // bool, and its coping with nullable values.
145                 //
146                 [Test]
147                 public void UserDefinedEqual ()
148                 {
149
150                 }
151
152         }
153 }