07629aabce277804f22ce33d73f27074eedd6027
[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                 [Test]
51                 [ExpectedException (typeof (ArgumentException))]
52                 public void GetFuncTypeArgEmpty ()
53                 {
54                         Expression.GetFuncType (new Type [0]);
55                 }
56
57                 [Test]
58                 [ExpectedException (typeof (ArgumentException))]
59                 public void GetFuncTypeArgTooBig ()
60                 {
61                         Expression.GetFuncType (new Type [6]);
62                 }
63
64                 [Test]
65                 public void GetFuncTypeTest ()
66                 {
67                         var func = Expression.GetFuncType (new [] {typeof (int)});
68                         Assert.AreEqual (typeof (Func<int>), func);
69
70                         func = Expression.GetFuncType (new [] {typeof (int), typeof (int)});
71                         Assert.AreEqual (typeof (Func<int, int>), func);
72
73                         func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int)});
74                         Assert.AreEqual (typeof (Func<int, int, int>), func);
75
76                         func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int), typeof (int)});
77                         Assert.AreEqual (typeof (Func<int, int, int, int>), func);
78
79                         func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int), typeof (int), typeof (int)});
80                         Assert.AreEqual (typeof (Func<int, int, int, int, int>), func);
81                 }
82
83                 [Test]
84                 [ExpectedException (typeof (ArgumentNullException))]
85                 public void GetActionTypeArgNull ()
86                 {
87                         Expression.GetActionType (null);
88                 }
89
90                 [Test]
91                 [ExpectedException (typeof (ArgumentException))]
92                 public void GetActionTypeArgTooBig ()
93                 {
94                         Expression.GetActionType (new Type [5]);
95                 }
96
97                 [Test]
98                 public void GetActionTypeTest ()
99                 {
100                         var action = Expression.GetActionType (new Type [0]);
101                         Assert.AreEqual (typeof (Action), action);
102
103                         action = Expression.GetActionType (new [] {typeof (int)});
104                         Assert.AreEqual (typeof (Action<int>), action);
105
106                         action = Expression.GetActionType (new [] {typeof (int), typeof (int)});
107                         Assert.AreEqual (typeof (Action<int, int>), action);
108
109                         action = Expression.GetActionType (new [] {typeof (int), typeof (int), typeof (int)});
110                         Assert.AreEqual (typeof (Action<int, int, int>), action);
111
112                         action = Expression.GetActionType (new [] {typeof (int), typeof (int), typeof (int), typeof (int)});
113                         Assert.AreEqual (typeof (Action<int, int, int, int>), action);
114                 }
115
116                 [Test]
117                 [ExpectedException (typeof (ArgumentNullException))]
118                 public void ParameterNullType ()
119                 {
120                         Expression.Parameter (null, "foo");
121                 }
122
123                 [Test]
124                 public void ParameterNullName ()
125                 {
126                         var p = Expression.Parameter (typeof (string), null);
127                         Assert.AreEqual (null, p.Name);
128                         Assert.AreEqual (typeof (string), p.Type);
129                         Assert.AreEqual ("<param>", p.ToString ());
130                 }
131
132                 [Test]
133                 public void ParameterEmptyName ()
134                 {
135                         var p = Expression.Parameter (typeof (string), "");
136                         Assert.AreEqual ("", p.Name);
137                         Assert.AreEqual (typeof (string), p.Type);
138                         Assert.AreEqual ("", p.ToString ());
139                 }
140
141                 [Test]
142                 public void Parameter ()
143                 {
144                         var p = Expression.Parameter (typeof (string), "foo");
145                         Assert.AreEqual ("foo", p.Name);
146                         Assert.AreEqual (typeof (string), p.Type);
147                         Assert.AreEqual ("foo", p.ToString ());
148                 }
149
150                 [Test]
151                 [Category ("NotDotNet")]
152                 [ExpectedException (typeof (ArgumentException))]
153                 public void VoidParameter ()
154                 {
155                         Expression.Parameter (typeof (void), "hello");
156                 }
157
158                 static int buffer;
159
160                 public static int Identity (int i)
161                 {
162                         buffer = i;
163                         return i;
164                 }
165
166                 [Test]
167                 public void CompileActionDiscardingRetValue ()
168                 {
169                         var p = Expression.Parameter (typeof (int), "i");
170                         var identity = GetType ().GetMethod ("Identity", BindingFlags.Static | BindingFlags.Public );
171                         Assert.IsNotNull (identity);
172
173                         var lambda = Expression.Lambda<Action<int>> (Expression.Call (identity, p), p);
174
175                         var method = lambda.Compile ();
176
177                         buffer = 0;
178
179                         method (42);
180                         Assert.AreEqual (42, buffer);
181                 }
182
183                 [Test]
184                 [Category("TargetJvmNotSupported")]
185                 public void ExpressionDelegateTarget ()
186                 {
187                         var p = Expression.Parameter (typeof (string), "str");
188                         var identity = Expression.Lambda<Func<string, string>> (p, p).Compile ();
189
190                         Assert.AreEqual (typeof (Func<string, string>), identity.GetType ());
191                         Assert.IsNotNull (identity.Target);
192                         Assert.AreEqual (typeof (ExecutionScope), identity.Target.GetType ());
193                 }
194
195                 class Foo {
196                         public string gazonk;
197                 }
198
199                 struct Bar {
200                         public int baz;
201
202                         public override string ToString ()
203                         {
204                                 return baz.ToString ();
205                         }
206                 }
207
208                 [Test]
209                 [Category ("TargetJvmNotSupported")]
210                 public void GlobalsInScope ()
211                 {
212                         var foo = new Foo { gazonk = "gazonk" };
213                         var bar = new Bar { baz = 42 };
214
215                         var l = Expression.Lambda<Func<string>> (
216                                 Expression.Call (
217                                         typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }),
218                                         Expression.Field (Expression.Constant (foo), typeof (Foo).GetField ("gazonk")),
219                                         Expression.Call (Expression.Constant (bar), typeof (Bar).GetMethod ("ToString"))));
220
221                         var del = l.Compile ();
222
223                         var scope = del.Target as ExecutionScope;
224
225                         Assert.IsNotNull (scope);
226
227                         var globals = scope.Globals;
228
229                         Assert.IsNotNull (globals);
230
231                         Assert.AreEqual (2, globals.Length);
232                         Assert.AreEqual (typeof (StrongBox<Foo>), globals [0].GetType ());
233                         Assert.AreEqual (typeof (StrongBox<Bar>), globals [1].GetType ());
234
235                         Assert.AreEqual (foo, ((StrongBox<Foo>) globals [0]).Value);
236                         Assert.AreEqual (bar, ((StrongBox<Bar>) globals [1]).Value);
237
238                         Assert.AreEqual ("gazonk42", del ());
239                 }
240
241                 [Test]
242                 public void SimpleHoistedParameter ()
243                 {
244                         var p = Expression.Parameter (typeof (string), "s");
245
246                         var f = Expression.Lambda<Func<string, Func<string>>> (
247                                 Expression.Lambda<Func<string>> (
248                                         p,
249                                         new ParameterExpression [0]),
250                                 p).Compile ();
251
252                         var f2 = f ("x");
253
254                         Assert.AreEqual ("x", f2 ());
255                 }
256
257                 [Test]
258                 public void TwoHoistingLevels ()
259                 {
260                         var p1 = Expression.Parameter (typeof (string), "x");
261                         var p2 = Expression.Parameter (typeof (string), "y");
262
263                         Expression<Func<string, Func<string, Func<string>>>> e =
264                                 Expression.Lambda<Func<string, Func<string, Func<string>>>> (
265                                         Expression.Lambda<Func<string, Func<string>>> (
266                                                 Expression.Lambda<Func<string>> (
267                                                         Expression.Call (
268                                                                 typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }),
269                                                                 new [] { p1, p2 }),
270                                                         new ParameterExpression [0]),
271                                                 new [] { p2 }),
272                                         new [] { p1 });
273
274                         var f = e.Compile ();
275                         var f2 = f ("Hello ");
276                         var f3 = f2 ("World !");
277
278                         Assert.AreEqual ("Hello World !", f3 ());
279                 }
280
281                 [Test]
282                 public void HoistedParameter ()
283                 {
284                         var i = Expression.Parameter (typeof (int), "i");
285
286                         var l = Expression.Lambda<Func<int, string>> (
287                                 Expression.Invoke (
288                                         Expression.Lambda<Func<string>> (
289                                                 Expression.Call (i, typeof (int).GetMethod ("ToString", Type.EmptyTypes)))), i).Compile ();
290
291                         Assert.AreEqual ("42", l (42));
292                 }
293         }
294 }