New tests.
[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 #if !NET_4_0
137                         Assert.AreEqual ("<param>", p.ToString ());
138 #endif
139                 }
140
141                 [Test]
142                 public void ParameterEmptyName ()
143                 {
144                         var p = Expression.Parameter (typeof (string), "");
145                         Assert.AreEqual ("", p.Name);
146                         Assert.AreEqual (typeof (string), p.Type);
147 #if !NET_4_0
148                         Assert.AreEqual ("", p.ToString ());
149 #endif
150                 }
151
152                 [Test]
153                 public void Parameter ()
154                 {
155                         var p = Expression.Parameter (typeof (string), "foo");
156                         Assert.AreEqual ("foo", p.Name);
157                         Assert.AreEqual (typeof (string), p.Type);
158                         Assert.AreEqual ("foo", p.ToString ());
159                 }
160
161                 [Test]
162                 [Category ("NotDotNet")]
163                 [ExpectedException (typeof (ArgumentException))]
164                 public void VoidParameter ()
165                 {
166                         Expression.Parameter (typeof (void), "hello");
167                 }
168
169                 static int buffer;
170
171                 public static int Identity (int i)
172                 {
173                         buffer = i;
174                         return i;
175                 }
176
177                 [Test]
178                 public void CompileActionDiscardingRetValue ()
179                 {
180                         var p = Expression.Parameter (typeof (int), "i");
181                         var identity = GetType ().GetMethod ("Identity", BindingFlags.Static | BindingFlags.Public );
182                         Assert.IsNotNull (identity);
183
184                         var lambda = Expression.Lambda<Action<int>> (Expression.Call (identity, p), p);
185
186                         var method = lambda.Compile ();
187
188                         buffer = 0;
189
190                         method (42);
191                         Assert.AreEqual (42, buffer);
192                 }
193
194                 [Test]
195                 [Category("TargetJvmNotSupported")]
196                 public void ExpressionDelegateTarget ()
197                 {
198                         var p = Expression.Parameter (typeof (string), "str");
199                         var identity = Expression.Lambda<Func<string, string>> (p, p).Compile ();
200
201                         Assert.AreEqual (typeof (Func<string, string>), identity.GetType ());
202                         Assert.IsNotNull (identity.Target);
203 #if !NET_4_0
204                         Assert.AreEqual (typeof (ExecutionScope), identity.Target.GetType ());
205 #endif
206                 }
207
208                 class Foo {
209                         public string gazonk;
210                 }
211
212                 struct Bar {
213                         public int baz;
214
215                         public override string ToString ()
216                         {
217                                 return baz.ToString ();
218                         }
219                 }
220
221 #if !NET_4_0
222                 [Test]
223                 [Category ("TargetJvmNotSupported")]
224                 public void GlobalsInScope ()
225                 {
226                         var foo = new Foo { gazonk = "gazonk" };
227                         var bar = new Bar { baz = 42 };
228
229                         var l = Expression.Lambda<Func<string>> (
230                                 Expression.Call (
231                                         typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }),
232                                         Expression.Field (Expression.Constant (foo), typeof (Foo).GetField ("gazonk")),
233                                         Expression.Call (Expression.Constant (bar), typeof (Bar).GetMethod ("ToString"))));
234
235                         var del = l.Compile ();
236
237                         var scope = del.Target as ExecutionScope;
238
239                         Assert.IsNotNull (scope);
240
241                         var globals = scope.Globals;
242
243                         Assert.IsNotNull (globals);
244
245                         Assert.AreEqual (2, globals.Length);
246                         Assert.AreEqual (typeof (StrongBox<Foo>), globals [0].GetType ());
247                         Assert.AreEqual (typeof (StrongBox<Bar>), globals [1].GetType ());
248
249                         Assert.AreEqual (foo, ((StrongBox<Foo>) globals [0]).Value);
250                         Assert.AreEqual (bar, ((StrongBox<Bar>) globals [1]).Value);
251
252                         Assert.AreEqual ("gazonk42", del ());
253                 }
254 #endif
255
256                 [Test]
257                 public void SimpleHoistedParameter ()
258                 {
259                         var p = Expression.Parameter (typeof (string), "s");
260
261                         var f = Expression.Lambda<Func<string, Func<string>>> (
262                                 Expression.Lambda<Func<string>> (
263                                         p,
264                                         new ParameterExpression [0]),
265                                 p).Compile ();
266
267                         var f2 = f ("x");
268
269                         Assert.AreEqual ("x", f2 ());
270                 }
271
272                 [Test]
273                 public void TwoHoistingLevels ()
274                 {
275                         var p1 = Expression.Parameter (typeof (string), "x");
276                         var p2 = Expression.Parameter (typeof (string), "y");
277
278                         Expression<Func<string, Func<string, Func<string>>>> e =
279                                 Expression.Lambda<Func<string, Func<string, Func<string>>>> (
280                                         Expression.Lambda<Func<string, Func<string>>> (
281                                                 Expression.Lambda<Func<string>> (
282                                                         Expression.Call (
283                                                                 typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }),
284                                                                 new [] { p1, p2 }),
285                                                         new ParameterExpression [0]),
286                                                 new [] { p2 }),
287                                         new [] { p1 });
288
289                         var f = e.Compile ();
290                         var f2 = f ("Hello ");
291                         var f3 = f2 ("World !");
292
293                         Assert.AreEqual ("Hello World !", f3 ());
294                 }
295
296                 [Test]
297                 public void HoistedParameter ()
298                 {
299                         var i = Expression.Parameter (typeof (int), "i");
300
301                         var l = Expression.Lambda<Func<int, string>> (
302                                 Expression.Invoke (
303                                         Expression.Lambda<Func<string>> (
304                                                 Expression.Call (i, typeof (int).GetMethod ("ToString", Type.EmptyTypes)))), i).Compile ();
305
306                         Assert.AreEqual ("42", l (42));
307                 }
308         }
309 }