Bug 15574. XML deserialization recursion: add array type to known_types?
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_Invoke.cs
1 //
2 // ExpressionTest_Invoke.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.Reflection;
31 using System.Linq;
32 using System.Linq.Expressions;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Linq.Expressions {
37
38         [TestFixture]
39         public class ExpressionTest_Invoke {
40
41                 static Expression CreateInvokable<T> ()
42                 {
43                         return Expression.Parameter (typeof (T), "invokable");
44                 }
45
46                 [Test]
47                 [ExpectedException (typeof (ArgumentNullException))]
48                 public void NullExpression ()
49                 {
50                         Expression.Invoke (null, new Expression [0]);
51                 }
52
53                 [Test]
54                 [ExpectedException (typeof (ArgumentNullException))]
55                 public void NullArgument ()
56                 {
57                         Expression.Invoke (CreateInvokable<Action<int>> (), new [] { null as Expression });
58                 }
59
60                 [Test]
61                 [ExpectedException (typeof (ArgumentException))]
62                 public void NonInvokableExpressionType ()
63                 {
64                         Expression.Invoke (CreateInvokable<int> (), null);
65                 }
66
67                 [Test]
68                 [ExpectedException (typeof (InvalidOperationException))]
69                 public void ArgumentCountDoesntMatchParametersLength ()
70                 {
71                         Expression.Invoke (CreateInvokable<Action<int>> (), 1.ToConstant (), 2.ToConstant ());
72                 }
73
74                 [Test]
75                 [ExpectedException (typeof (ArgumentException))]
76                 public void ArgumentNotAssignableToParameterType ()
77                 {
78                         Expression.Invoke (CreateInvokable<Action<int>> (), "".ToConstant ());
79                 }
80
81                 [Test]
82                 public void EmptyArguments ()
83                 {
84                         var invoke = Expression.Invoke (CreateInvokable<Action> (), null);
85                         Assert.AreEqual (typeof (void), invoke.Type);
86                         Assert.IsNotNull (invoke.Arguments);
87                         Assert.AreEqual (0, invoke.Arguments.Count);
88                         Assert.AreEqual ("Invoke(invokable)", invoke.ToString ());
89                 }
90
91                 [Test]
92                 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=352402
93                 public void InvokeFunc ()
94                 {
95                         var invoke = Expression.Invoke (CreateInvokable<Func<string, string, int>> (), "foo".ToConstant (), "bar".ToConstant ());
96                         Assert.AreEqual (typeof (int), invoke.Type);
97                         Assert.AreEqual (2, invoke.Arguments.Count);
98                         Assert.AreEqual ("Invoke(invokable, \"foo\", \"bar\")", invoke.ToString ());
99                 }
100
101                 [Test]
102                 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=352402
103                 public void InvokeLambda ()
104                 {
105                         var p = Expression.Parameter (typeof (int), "i");
106                         var lambda = Expression.Lambda<Func<int, int>> (p, p);
107
108                         var invoke = Expression.Invoke (lambda, 1.ToConstant ());
109                         Assert.AreEqual (typeof (int), invoke.Type);
110                         Assert.AreEqual (1, invoke.Arguments.Count);
111                         Assert.AreEqual ("Invoke(i => i, 1)", invoke.ToString ());
112                 }
113
114                 delegate string StringAction (string s);
115
116                 static string Identity (string s)
117                 {
118                         return s;
119                 }
120
121                 [Test]
122                 public void TestCompileInvokePrivateDelegate ()
123                 {
124                         var action = Expression.Parameter (typeof (StringAction), "action");
125                         var str = Expression.Parameter (typeof (string), "str");
126                         var invoker = Expression.Lambda<Func<StringAction, string, string>> (
127                                 Expression.Invoke (action, str), action, str).Compile ();
128
129                         Assert.AreEqual ("foo", invoker (Identity, "foo"));
130                 }
131
132                 [Test]
133                 public void InvokeWithExpressionLambdaAsArguments ()
134                 {
135                         var p = Expression.Parameter (typeof (string), "s");
136
137                         Func<string, Expression<Func<string, string>>, string> caller = (s, f) => f.Compile ().Invoke (s);
138
139                         var invoke = Expression.Lambda<Func<string>> (
140                                 Expression.Invoke (
141                                         Expression.Constant (caller),
142                                         Expression.Constant ("KABOOM!"),
143                                         Expression.Lambda<Func<string, string>> (
144                                                 Expression.Call (p, typeof (string).GetMethod ("ToLowerInvariant")), p)));
145
146                         Assert.AreEqual (ExpressionType.Quote,
147                                 (invoke.Body as InvocationExpression).Arguments [1].NodeType);
148
149                         Assert.AreEqual ("kaboom!", invoke.Compile ().DynamicInvoke ());
150                 }
151         }
152 }