2 // ExpressionTest_Invoke.cs
5 // Jb Evain (jbevain@novell.com)
7 // (C) 2008 Novell, Inc. (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Reflection;
32 using System.Linq.Expressions;
34 using NUnit.Framework;
36 namespace MonoTests.System.Linq.Expressions {
39 public class ExpressionTest_Invoke {
41 static Expression CreateInvokable<T> ()
43 return Expression.Parameter (typeof (T), "invokable");
47 [ExpectedException (typeof (ArgumentNullException))]
48 public void NullExpression ()
50 Expression.Invoke (null, new Expression [0]);
54 [ExpectedException (typeof (ArgumentNullException))]
55 public void NullArgument ()
57 Expression.Invoke (CreateInvokable<Action<int>> (), new [] { null as Expression });
61 [ExpectedException (typeof (ArgumentException))]
62 public void NonInvokableExpressionType ()
64 Expression.Invoke (CreateInvokable<int> (), null);
68 [ExpectedException (typeof (InvalidOperationException))]
69 public void ArgumentCountDoesntMatchParametersLength ()
71 Expression.Invoke (CreateInvokable<Action<int>> (), 1.ToConstant (), 2.ToConstant ());
75 [ExpectedException (typeof (ArgumentException))]
76 public void ArgumentNotAssignableToParameterType ()
78 Expression.Invoke (CreateInvokable<Action<int>> (), "".ToConstant ());
82 public void EmptyArguments ()
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 ());
92 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=352402
93 public void InvokeFunc ()
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 ());
102 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=352402
103 public void InvokeLambda ()
105 var p = Expression.Parameter (typeof (int), "i");
106 var lambda = Expression.Lambda<Func<int, int>> (p, p);
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 ());
114 delegate string StringAction (string s);
116 static string Identity (string s)
122 public void TestCompileInvokePrivateDelegate ()
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 ();
129 Assert.AreEqual ("foo", invoker (Identity, "foo"));
133 [Category ("NotWorkingInterpreter")]
134 public void InvokeWithExpressionLambdaAsArguments ()
136 var p = Expression.Parameter (typeof (string), "s");
138 Func<string, Expression<Func<string, string>>, string> caller = (s, f) => f.Compile ().Invoke (s);
140 var invoke = Expression.Lambda<Func<string>> (
142 Expression.Constant (caller),
143 Expression.Constant ("KABOOM!"),
144 Expression.Lambda<Func<string, string>> (
145 Expression.Call (p, typeof (string).GetMethod ("ToLowerInvariant")), p)));
147 Assert.AreEqual (ExpressionType.Quote,
148 (invoke.Body as InvocationExpression).Arguments [1].NodeType);
150 Assert.AreEqual ("kaboom!", invoke.Compile ().DynamicInvoke ());