ExpressionTest_Invoke.cs 4.9 KB

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