[bcl] Remove NET_4_0 defines from class libs
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest.cs
1 //
2 // ExpressionTest.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@novell.com)
6 //
7 // (C) 2008 Novell, Inc. (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Collections.Generic;
31 using System.Reflection;
32 using System.Runtime.CompilerServices;
33 using System.Linq;
34 using System.Linq.Expressions;
35
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Linq.Expressions {
39
40         [TestFixture]
41         public class ExpressionTest {
42
43                 [Test]
44                 [ExpectedException (typeof (ArgumentNullException))]
45                 public void GetFuncTypeArgNull ()
46                 {
47                         Expression.GetFuncType (null);
48                 }
49
50                 static Type [] GetTestTypeArray (int length)
51                 {
52                         return Enumerable.Range (0, length - 1)
53                                 .Select (i => typeof (int))
54                                 .ToArray ();
55                 }
56
57                 [Test]
58                 [ExpectedException (typeof (ArgumentException))]
59                 public void GetFuncTypeArgEmpty ()
60                 {
61                         Expression.GetFuncType (Type.EmptyTypes);
62                 }
63
64                 [Test]
65                 [ExpectedException (typeof (ArgumentException))]
66                 public void GetFuncTypeArgTooBig ()
67                 {
68                         Expression.GetFuncType (GetTestTypeArray (64));
69                 }
70
71                 [Test]
72                 public void GetFuncTypeTest ()
73                 {
74                         var func = Expression.GetFuncType (new [] {typeof (int)});
75                         Assert.AreEqual (typeof (Func<int>), func);
76
77                         func = Expression.GetFuncType (new [] {typeof (int), typeof (int)});
78                         Assert.AreEqual (typeof (Func<int, int>), func);
79
80                         func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int)});
81                         Assert.AreEqual (typeof (Func<int, int, int>), func);
82
83                         func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int), typeof (int)});
84                         Assert.AreEqual (typeof (Func<int, int, int, int>), func);
85
86                         func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int), typeof (int), typeof (int)});
87                         Assert.AreEqual (typeof (Func<int, int, int, int, int>), func);
88                 }
89
90                 [Test]
91                 [ExpectedException (typeof (ArgumentNullException))]
92                 public void GetActionTypeArgNull ()
93                 {
94                         Expression.GetActionType (null);
95                 }
96
97                 [Test]
98                 [ExpectedException (typeof (ArgumentException))]
99                 public void GetActionTypeArgTooBig ()
100                 {
101                         Expression.GetActionType (GetTestTypeArray (45));
102                 }
103
104                 [Test]
105                 public void GetActionTypeTest ()
106                 {
107                         var action = Expression.GetActionType (new Type [0]);
108                         Assert.AreEqual (typeof (Action), action);
109
110                         action = Expression.GetActionType (new [] {typeof (int)});
111                         Assert.AreEqual (typeof (Action<int>), action);
112
113                         action = Expression.GetActionType (new [] {typeof (int), typeof (int)});
114                         Assert.AreEqual (typeof (Action<int, int>), action);
115
116                         action = Expression.GetActionType (new [] {typeof (int), typeof (int), typeof (int)});
117                         Assert.AreEqual (typeof (Action<int, int, int>), action);
118
119                         action = Expression.GetActionType (new [] {typeof (int), typeof (int), typeof (int), typeof (int)});
120                         Assert.AreEqual (typeof (Action<int, int, int, int>), action);
121                 }
122
123                 [Test]
124                 [ExpectedException (typeof (ArgumentNullException))]
125                 public void ParameterNullType ()
126                 {
127                         Expression.Parameter (null, "foo");
128                 }
129
130                 [Test]
131                 public void ParameterNullName ()
132                 {
133                         var p = Expression.Parameter (typeof (string), null);
134                         Assert.AreEqual (null, p.Name);
135                         Assert.AreEqual (typeof (string), p.Type);
136                 }
137
138                 [Test]
139                 public void ParameterEmptyName ()
140                 {
141                         var p = Expression.Parameter (typeof (string), "");
142                         Assert.AreEqual ("", p.Name);
143                         Assert.AreEqual (typeof (string), p.Type);
144                 }
145
146                 [Test]
147                 public void Parameter ()
148                 {
149                         var p = Expression.Parameter (typeof (string), "foo");
150                         Assert.AreEqual ("foo", p.Name);
151                         Assert.AreEqual (typeof (string), p.Type);
152                         Assert.AreEqual ("foo", p.ToString ());
153                 }
154
155                 [Test]
156                 [Category ("NotDotNet")]
157                 [ExpectedException (typeof (ArgumentException))]
158                 public void VoidParameter ()
159                 {
160                         Expression.Parameter (typeof (void), "hello");
161                 }
162
163                 static int buffer;
164
165                 public static int Identity (int i)
166                 {
167                         buffer = i;
168                         return i;
169                 }
170
171                 [Test]
172                 public void CompileActionDiscardingRetValue ()
173                 {
174                         var p = Expression.Parameter (typeof (int), "i");
175                         var identity = GetType ().GetMethod ("Identity", BindingFlags.Static | BindingFlags.Public );
176                         Assert.IsNotNull (identity);
177
178                         var lambda = Expression.Lambda<Action<int>> (Expression.Call (identity, p), p);
179
180                         var method = lambda.Compile ();
181
182                         buffer = 0;
183
184                         method (42);
185                         Assert.AreEqual (42, buffer);
186                 }
187
188                 [Test]
189                 public void ExpressionDelegateTarget ()
190                 {
191                         var p = Expression.Parameter (typeof (string), "str");
192                         var identity = Expression.Lambda<Func<string, string>> (p, p).Compile ();
193
194                         Assert.AreEqual (typeof (Func<string, string>), identity.GetType ());
195                         Assert.IsNotNull (identity.Target);
196                 }
197
198                 class Foo {
199                         public string gazonk;
200                 }
201
202                 struct Bar {
203                         public int baz;
204
205                         public override string ToString ()
206                         {
207                                 return baz.ToString ();
208                         }
209                 }
210
211
212                 [Test]
213                 public void SimpleHoistedParameter ()
214                 {
215                         var p = Expression.Parameter (typeof (string), "s");
216
217                         var f = Expression.Lambda<Func<string, Func<string>>> (
218                                 Expression.Lambda<Func<string>> (
219                                         p,
220                                         new ParameterExpression [0]),
221                                 p).Compile ();
222
223                         var f2 = f ("x");
224
225                         Assert.AreEqual ("x", f2 ());
226                 }
227
228                 [Test]
229                 public void TwoHoistingLevels ()
230                 {
231                         var p1 = Expression.Parameter (typeof (string), "x");
232                         var p2 = Expression.Parameter (typeof (string), "y");
233
234                         Expression<Func<string, Func<string, Func<string>>>> e =
235                                 Expression.Lambda<Func<string, Func<string, Func<string>>>> (
236                                         Expression.Lambda<Func<string, Func<string>>> (
237                                                 Expression.Lambda<Func<string>> (
238                                                         Expression.Call (
239                                                                 typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }),
240                                                                 new [] { p1, p2 }),
241                                                         new ParameterExpression [0]),
242                                                 new [] { p2 }),
243                                         new [] { p1 });
244
245                         var f = e.Compile ();
246                         var f2 = f ("Hello ");
247                         var f3 = f2 ("World !");
248
249                         Assert.AreEqual ("Hello World !", f3 ());
250                 }
251
252                 [Test]
253                 public void HoistedParameter ()
254                 {
255                         var i = Expression.Parameter (typeof (int), "i");
256
257                         var l = Expression.Lambda<Func<int, string>> (
258                                 Expression.Invoke (
259                                         Expression.Lambda<Func<string>> (
260                                                 Expression.Call (i, typeof (int).GetMethod ("ToString", Type.EmptyTypes)))), i).Compile ();
261
262                         Assert.AreEqual ("42", l (42));
263                 }
264         }
265 }