new tests
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_Call.cs
1 //
2 // ExpressionTest_Call.cs
3 //
4 // Author:
5 //   Federico Di Gregorio <fog@initd.org>
6 //   Jb Evain (jbevain@novell.com)
7 //
8 // (C) 2008 Novell, Inc. (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Reflection;
32 using System.Collections.Generic;
33 using System.Linq;
34 using System.Linq.Expressions;
35 using NUnit.Framework;
36
37 namespace MonoTests.System.Linq.Expressions {
38
39         [TestFixture]
40         public class ExpressionTest_Call {
41
42                 [Test]
43                 [ExpectedException (typeof (ArgumentNullException))]
44                 public void Arg1Null ()
45                 {
46                         Expression.Call ((Type)null, "TestMethod", null, Expression.Constant (1));
47                 }
48
49                 [Test]
50                 [ExpectedException (typeof (ArgumentNullException))]
51                 public void Arg2Null ()
52                 {
53                         Expression.Call (typeof (MemberClass), null, null, Expression.Constant (1));
54                 }
55
56                 [Test]
57                 [ExpectedException (typeof (InvalidOperationException))]
58                 public void Arg4WrongType ()
59                 {
60                         Expression.Call (typeof (MemberClass), "StaticMethod", null, Expression.Constant (true));
61                 }
62
63                 [Test]
64                 [ExpectedException (typeof (InvalidOperationException))]
65                 public void InstanceMethod ()
66                 {
67                         Expression.Call (typeof (MemberClass), "TestMethod", null, Expression.Constant (1));
68                 }
69
70                 [Test]
71                 public void StaticMethod ()
72                 {
73                         Expression.Call (typeof (MemberClass), "StaticMethod", null, Expression.Constant (1));
74                 }
75
76                 [Test]
77                 public void StaticGenericMethod ()
78                 {
79                         Expression.Call (typeof (MemberClass), "StaticGenericMethod", new [] { typeof (int) }, Expression.Constant (1));
80                 }
81
82                 [Test]
83                 [ExpectedException (typeof (ArgumentNullException))]
84                 public void ArgMethodNull ()
85                 {
86                         Expression.Call (Expression.Constant (new object ()), null);
87                 }
88
89                 [Test]
90                 [ExpectedException (typeof (ArgumentNullException))]
91                 public void ArgInstanceNullForNonStaticMethod ()
92                 {
93                         Expression.Call (null, typeof (object).GetMethod ("ToString"));
94                 }
95
96                 [Test]
97                 [ExpectedException (typeof (ArgumentException))]
98                 public void InstanceTypeDoesntMatchMethodDeclaringType ()
99                 {
100                         Expression.Call (Expression.Constant (1), typeof (string).GetMethod ("Intern"));
101                 }
102
103                 [Test]
104                 [ExpectedException (typeof (ArgumentException))]
105                 public void MethodArgumentCountDoesnMatchParameterLength ()
106                 {
107                         Expression.Call (Expression.Constant (new object ()), typeof (object).GetMethod ("ToString"), Expression.Constant (new object ()));
108                 }
109
110                 public class Foo {
111                         public void Bar (string s)
112                         {
113                         }
114                 }
115
116                 [Test]
117                 [ExpectedException (typeof (ArgumentNullException))]
118                 public void MethodHasNullArgument ()
119                 {
120                         Expression.Call (Expression.New (typeof (Foo)), typeof (Foo).GetMethod ("Bar"), null as Expression);
121                 }
122
123                 [Test]
124                 [ExpectedException (typeof (ArgumentException))]
125                 public void MethodArgumentDoesntMatchParameterType ()
126                 {
127                         Expression.Call (Expression.New (typeof (Foo)), typeof (Foo).GetMethod ("Bar"), Expression.Constant (42));
128                 }
129
130                 [Test]
131                 public void CallToString ()
132                 {
133                         var call = Expression.Call (Expression.Constant (new object ()), typeof (object).GetMethod ("ToString"));
134                         Assert.AreEqual ("value(System.Object).ToString()", call.ToString ());
135                 }
136
137                 [Test]
138                 public void CallStringIsNullOrEmpty ()
139                 {
140                         var call = Expression.Call (null, typeof (string).GetMethod ("IsNullOrEmpty"), Expression.Constant (""));
141                         Assert.AreEqual ("IsNullOrEmpty(\"\")", call.ToString ());
142                 }
143
144                 public static object Identity (object o)
145                 {
146                         return o;
147                 }
148
149                 [Test]
150                 public void CompileSimpleStaticCall ()
151                 {
152                         var p = Expression.Parameter (typeof (object), "o");
153                         var lambda = Expression.Lambda<Func<object, object>> (Expression.Call (GetType ().GetMethod ("Identity"), p), p);
154
155                         var i = lambda.Compile ();
156
157                         Assert.AreEqual (2, i (2));
158                         Assert.AreEqual ("Foo", i ("Foo"));
159                 }
160
161                 [Test]
162                 public void CompileSimpleInstanceCall ()
163                 {
164                         var p = Expression.Parameter (typeof (string), "p");
165                         var lambda = Expression.Lambda<Func<string, string>> (
166                                 Expression.Call (
167                                         p, typeof (string).GetMethod ("ToString", Type.EmptyTypes)),
168                                 p);
169
170                         var ts = lambda.Compile ();
171
172                         Assert.AreEqual ("foo", ts ("foo"));
173                         Assert.AreEqual ("bar", ts ("bar"));
174                 }
175
176                 [Test]
177                 [ExpectedException (typeof (InvalidOperationException))]
178                 public void CheckTypeArgsIsNotUsedForParameterLookup ()
179                 {
180                         Expression.Call (GetType (), "EineMethod", new [] { typeof (string), typeof (int) }, "foo".ToConstant (), 2.ToConstant ());
181                 }
182
183                 public static void EineGenericMethod<X, Y> (string foo, int bar)
184                 {
185                 }
186
187                 [Test]
188                 public void CheckTypeArgsIsUsedForGenericArguments ()
189                 {
190                         var m = Expression.Call (GetType (), "EineGenericMethod", new [] { typeof (string), typeof (int) }, "foo".ToConstant (), 2.ToConstant ());
191                         Assert.IsNotNull (m.Method);
192                         Assert.AreEqual ("Void EineGenericMethod[String,Int32](System.String, Int32)", m.Method.ToString ());
193                 }
194
195                 public struct EineStrukt {
196
197                         public string Foo;
198
199                         public EineStrukt (string foo)
200                         {
201                                 Foo = foo;
202                         }
203
204                         public string GimmeFoo ()
205                         {
206                                 return Foo;
207                         }
208                 }
209
210                 [Test]
211                 public void CallMethodOnStruct ()
212                 {
213                         var param = Expression.Parameter (typeof (EineStrukt), "s");
214                         var foo = Expression.Lambda<Func<EineStrukt, string>> (
215                                 Expression.Call (param, typeof (EineStrukt).GetMethod ("GimmeFoo")), param).Compile ();
216
217                         var s = new EineStrukt ("foo");
218                         Assert.AreEqual ("foo", foo (s));
219                 }
220
221                 public static int OneStaticMethod ()
222                 {
223                         return 42;
224                 }
225
226                 [Test]
227                 public void CallStaticMethodOnNonSenseInstanceExpression ()
228                 {
229                         var call = Expression.Call (
230                                         Expression.Constant ("la la la"),
231                                         this.GetType ().GetMethod ("OneStaticMethod"));
232
233                         Assert.IsNotNull (call.Object);
234
235                         var callMethod = Expression.Lambda<Func<int>> (call).Compile ();
236
237                         Assert.AreEqual (42, callMethod ());
238                 }
239
240                 public static int DoSomethingWith (ref int a)
241                 {
242                         return a + 4;
243                 }
244
245                 [Test]
246                 public void CallStaticMethodWithRefParameter ()
247                 {
248                         var p = Expression.Parameter (typeof (int), "i");
249
250                         var c = Expression.Lambda<Func<int, int>> (
251                                 Expression.Call (GetType ().GetMethod ("DoSomethingWith"), p), p).Compile ();
252
253                         Assert.AreEqual (42, c (38));
254                 }
255
256                 public static int Bang (Expression i)
257                 {
258                         return (int) (i as ConstantExpression).Value;
259                 }
260
261                 [Test]
262                 [Category ("NotWorking")]
263                 public void CallMethodWithExpressionParameter ()
264                 {
265                         var call = Expression.Call (GetType ().GetMethod ("Bang"), Expression.Constant (42));
266                         Assert.AreEqual (ExpressionType.Quote, call.Arguments [0].NodeType);
267
268                         var l = Expression.Lambda<Func<int>> (call).Compile ();
269
270                         Assert.AreEqual (42, l ());
271                 }
272         }
273 }